Lab 4

Lab 4: Polymorphism

Description:

We were given an interface, Movable. From that, we had to create 3 classes (objects):  Car, Plane, Ship. These 3 classes were to implement the interface through a menu, and as a result, coordinates would display the position of the object, based on the x and y axis values. The user would be allowed to choose the number of spaces that the vehicle choice would move, as well as the vehicle choice, and the direction that it would go. Depending on the direction, the vehicle choice would increment or decrement its coordinate values as such.

Not only that, but the user was also supposed to be allowed to repeatedly choose a vehicle, direction, and number of spaces, until they desired to exit the program. This was done using a switch, where each case was configured accordingly.

Code:

Movable interface

 package lab4;
public interface Movable {

public void moveForward();
public void moveForward(int x);
public void moveBackward();
public void moveBackward(int x);
public void moveLeft();
public void moveLeft(int y);
public void moveRight();
public void moveRight(int y);
public void displayCoordinates();

}

Car

package lab4;

public class Car implements Movable { 
	private int x=0, y=0, n=0; 
	private static final String classname = "Car"; 

public Car() {} 

public Car(int x, int y) { 
this.x = x; 
this.y= y; 
} 

	// methods for each direction when Car is chosen, override methods, with int n allows for user to change coordinates
	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x += 1; 
		displayCoordinates(); 
	}
	// user controls number of spaces forward (y axis), increment. Same applies for other classes.
	@Override
	public void moveForward(int n) {
		// TODO Auto-generated method stub
		x += n; 
		displayCoordinates(); 
	}
	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x -= 1; 
		displayCoordinates(); 
	}

	//user controls number of spaces moving backwards (y axis), decrement. Applies to other classes.
	@Override
	public void moveBackward(int n) {
		// TODO Auto-generated method stub
		x -= n; 
		displayCoordinates(); 
		}
	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y -= 1; 
		displayCoordinates(); 
		}
	// user controls number of spaces to the left, decrement. Applies to other classes.
	@Override
	public void moveLeft(int n) {
		// TODO Auto-generated method stub
		y -= n; 
		displayCoordinates(); 
	}
	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		y += 1; 
		displayCoordinates(); 
	}

	// user gets to control number of spaces to the right, increment. Applies to other classes.
	@Override
	public void moveRight(int n) {
		// TODO Auto-generated method stub
		y += n; 
		displayCoordinates(); 
	}
	@Override

	//displays coordinates for choice Car, will change when user enters new value for number of spaces (n). Same for other classes.
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")"); 
		}
}

Plane

package lab4;

public class Plane implements Movable{
	private int x=0, y=0, n=0; 
	private static final String classname = "Plane"; 

public Plane() {} 

public Plane(int x, int y) { 
this.x = x; 
this.y= y; 
} 

//methods for each direction when Plane is chosen, override methods, with int n allows for user to change coordinates
	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x += 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveForward(int n) {
		// TODO Auto-generated method stub
		x += n; 
		displayCoordinates(); 
	}
	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x -= 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveBackward(int n) {
		// TODO Auto-generated method stub
		x -= n; 
		displayCoordinates(); 
		}
	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y -= 1; 
		displayCoordinates(); 
		}
	@Override
	public void moveLeft(int n) {
		// TODO Auto-generated method stub
		y -= n; 
		displayCoordinates(); 
	}
	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		y += 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveRight(int n) {
		// TODO Auto-generated method stub
		y += n; 
		displayCoordinates(); 
	}
	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")"); 

	}

}

Ship

package lab4;

public class Ship implements Movable {
	private int x=0, y=0, n=0; 
	private static final String classname = "Ship"; 

public Ship() {} 

public Ship(int x, int y) { 
this.x = x; 
this.y= y; 
} 
//methods for each direction when Ship is chosen, override methods, with int n allows for user to change coordinates
	@Override
	public void moveForward() {
		// TODO Auto-generated method stub
		x += 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveForward(int n) {
		// TODO Auto-generated method stub
		x += n; 
		displayCoordinates(); 
	}
	@Override
	public void moveBackward() {
		// TODO Auto-generated method stub
		x -= 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveBackward(int n) {
		// TODO Auto-generated method stub
		x -= n; 
		displayCoordinates(); 
		}
	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		y -= 1; 
		displayCoordinates(); 
		}
	@Override
	public void moveLeft(int n) {
		// TODO Auto-generated method stub
		y -= n; 
		displayCoordinates(); 
	}
	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		y += 1; 
		displayCoordinates(); 
	}
	@Override
	public void moveRight(int n) {
		// TODO Auto-generated method stub
		y += n; 
		displayCoordinates(); 
	}
	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")"); 	
	}
}

MovableApp

package lab4;
import java.util.Scanner;

public class MovableApp 
{
	static int n = -1;
	static Scanner sc = new Scanner(System.in);

	//method for user to choose number of spaces, returns value of n so that it can be used
	public static int getNoOfSpaces()
	{
		System.out.println("Choose the number of spaces to move: ");
		n = sc.nextInt();
		System.out.println();
		return n;
	}

	//method for user to choose direction, n is reused.
	public static void movementOption(Movable obj)
	{		
		while(true)
		{
			System.out.println("Choose a direction: ");
			System.out.println(" 0. Exit \n 1. Forward \n 2. Backward \n 3. Left \n 4. Right \n ");
			System.out.print("Direction: ");
			n = sc.nextInt();
			System.out.println();

			//switch cases, for each choice given for direction
			switch (n) {
			case 0:
			{
				return;
			}
			case 1:
			{
				//Take the input from user. whether he/she wants to move the car forward/backward/left/right
				int noOfSteps=getNoOfSpaces();
				//Move forward by that number of spaces
				obj.moveForward(noOfSteps);

				break;
			}
			case 2:
			{
				int noOfSteps=getNoOfSpaces();
				obj.moveBackward(noOfSteps);
				break;
			}
			case 3:
			{
				int noOfSteps=getNoOfSpaces();
				obj.moveLeft(noOfSteps);
				break;
			}
			case 4:
			{
				int noOfSteps=getNoOfSpaces();
				obj.moveRight(noOfSteps);
				break;
			}

		default:
			break;
		}
		}

	}

	public static void main(String[] args) {

		//objects created referencing Movable interface, to access the 3 classes created.
		Movable[] Movement = new Movable[3];
		Movement[0] = new Plane();
		Movement[1] = new Car();
		Movement[2] = new Ship();

		while(true)
		{
			System.out.println("Choose a vehicle: ");
			System.out.println(" 0. Exit \n 1. Car \n 2. Plane \n 3. Ship \n");
			System.out.print("Selection: ");
			n = sc.nextInt();
			System.out.println(); 

			//switch conditions for choices of vehicles
			switch (n) {
			case 0:
				{
					return;
				}
			case 1:
			{
				//Take the Movement[1] object and typecast (relate it) to Car class. 
				//Same applies for other cases, but with their corresponding objects.
				Car c=(Car)Movement[1];
				movementOption(c);
				break;
			}
			case 2:
			{
				Plane p=(Plane)Movement[0];
				movementOption(p);
				break;
			}
			case 3:
			{
				Ship s=(Ship)Movement[2];
				movementOption(s);
				break;
			}

			default:
				break;
			}

		}
	}
}

Screenshot:LAB 4 CET3640

 

Leave a Reply

Your email address will not be published. Required fields are marked *