Lab 4

“POLYMORPHISM”

LAB DESCRIPTION:

This lab will show students how to use the interface using the polymorphism. Student will have three class Car(), Plane() and Ship(); they will be implement from the interface Movable(). In Java, one class can only inherit from one super class, but it can implement as many interface as it needs. The interface will only content the abstract method, the concrete class will implement the specific details for that method.

The application will use to control Car, Plane  and Ship. When the users choose one of them, they can control the vehicle moving forward, backward or turn left, right. After each movement, the (x,y) position of that vehicle will be shown on the screen. The application allow users to control the vehicle until they decide to stop.

CODE:
“Movable.java”

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.java”

public class Car implements Movable
{
	private int x;
	private int y;
	public Car()
	{
		x=0;
		y=0;
	}
	public Car(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	public void moveForward()
	{
		x+=1;
	}
	public void moveForward(int x)
	{
		this.x+=x;
	}
	public void moveBackward()
	{
		x-=1;
	}
	public void moveBackward(int x)
	{
		this.x-=x;
	}
	public void moveLeft()
	{
		y-=1;
	}
	public void moveLeft(int y)
	{
		this.y-=y;
	}
	public void moveRight()
	{
		y+=1;
	}
	public void moveRight(int y)
	{
		this.y+=y;
	}
        //Display the coordinate
	@Override
	public void displayCoordinates()
	{
		System.out.println("The coodinate of Car is:");
		System.out.println("( "+x+" ; "+y+")");
	}
}

“Plane.java”

public class Plane implements Movable
{
	private int x;
	private int y;
	public Plane()
	{
		x=0;
		y=0;
	}
	public Plane(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	public void moveForward()
	{
		x+=1;
	}
	public void moveForward(int x)
	{
		this.x+=x;
	}
	public void moveBackward()
	{
		x-=1;
	}
	public void moveBackward(int x)
	{
		this.x-=x;
	}
	public void moveLeft()
	{
		y-=1;
	}
	public void moveLeft(int y)
	{
		this.y-=y;
	}
	public void moveRight()
	{
		y+=1;
	}
	public void moveRight(int y)
	{
		this.y+=y;
	}
        //Display the coordinate
	@Override
	public void displayCoordinates()
	{
		System.out.println("The coodinate of Plane is:");
		System.out.println("( "+x+" ; "+y+")");
	}
}

“Ship.java”

public class Ship implements Movable
{
	private int x;
	private int y;
	public Ship()
	{
		x=0;
		y=0;
	}
	public Ship(int x,int y)
	{
		this.x=x;
		this.y=y;
	}
	public void moveForward()
	{
		x+=1;
	}
	public void moveForward(int x)
	{
		this.x+=x;
	}
	public void moveBackward()
	{
		x-=1;
	}
	public void moveBackward(int x)
	{
		this.x-=x;
	}
	public void moveLeft()
	{
		y-=1;
	}
	public void moveLeft(int y)
	{
		this.y-=y;
	}
	public void moveRight()
	{
		y+=1;
	}
	public void moveRight(int y)
	{
		this.y+=y;
	}
        //Display the coordinate
	@Override
	public void displayCoordinates()
	{
		System.out.println("The coodinate of Ship is:");
		System.out.println("( "+x+" ; "+y+" )");
	}
}

“MoveApp.java”

import java.util.*;
public class MoveApp {

	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		//Asking user for choosing vehicle
		System.out.println("What vehicle do you want to control?");
		System.out.println("Car(1), Plane(2), Ship(3): ");
		int choice=keyboard.nextInt();
		int coordinate;
		//Choose Car
		if(choice==1)
		{
			Car newCar = new Car();
			//Asking for direction
			System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
			choice=keyboard.nextInt();
			do
			{
				//Asking for coordinate
				System.out.println("Coordinate:");
				coordinate=keyboard.nextInt();
				//Moving based on coordinate
				if(choice==1)
				{	
					if(coordinate==0)
					newCar.moveForward();
					else
					newCar.moveForward(coordinate);
				}
				else if(choice==2)
				{
					if(coordinate==0)
					newCar.moveBackward();
					else
					newCar.moveBackward(coordinate);
				}
				else if(choice==3)
				{
					if(coordinate==0)
					newCar.moveLeft();
					else
					newCar.moveLeft(coordinate);
				}
				else if(choice==4)
				{
					if(coordinate==0)
					newCar.moveRight();
					else
					newCar.moveRight(coordinate);
				}
				//Display the coordinate
				newCar.displayCoordinates();
				//Asking to move again or stop
				System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
				choice=keyboard.nextInt();
			}while(choice!=5);

		}
		//Choose Plane
		else if(choice==2)
		{
			Plane newPlane = new Plane();
			System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
			choice=keyboard.nextInt();
			do
			{
				//Asking for coordinate
				System.out.println("Coordinate:");
				coordinate=keyboard.nextInt();
				//Moving based on coordinate
				if(choice==1)
				{
					if(coordinate==0)
					newPlane.moveForward();
					else
					newPlane.moveForward(coordinate);
				}
				else if(choice==2)
				{
					if(coordinate==0)
					newPlane.moveBackward();
					else
					newPlane.moveBackward(coordinate);
				}
				else if(choice==3)
				{
					if(coordinate==0)
					newPlane.moveLeft();
					else
					newPlane.moveLeft(coordinate);
				}
				else if(choice==4)
				{
					if(coordinate==0)
					newPlane.moveRight();
					else
					newPlane.moveRight(coordinate);
				}
				//Display the coordinate
				newPlane.displayCoordinates();
				//Asking to move again or stop
				System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
				choice=keyboard.nextInt();
			}while(choice!=5);

		}
		//Choose Ship
		else if(choice==3)
		{
			Ship newShip = new Ship();
			System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
			choice=keyboard.nextInt();
			//Moving based on coordinate
			do
			{
				//Asking for coordinate
				System.out.println("Coordinate:");
				coordinate=keyboard.nextInt();
				if(choice==1)
				{
					if(coordinate==0)
					newShip.moveForward();
					else
					newShip.moveForward(coordinate);
				}
				else if(choice==2)
				{
					if(coordinate==0)
					newShip.moveBackward();
					else
					newShip.moveBackward(coordinate);
				}
				else if(choice==3)
				{
					if(coordinate==0)
					newShip.moveLeft();
					else
					newShip.moveLeft(coordinate);
				}
				else if(choice==4)
				{
					if(coordinate==0)
					newShip.moveRight();
					else
					newShip.moveRight(coordinate);
				}
				//Display the coordinate
				newShip.displayCoordinates();
				//Asking to move again or stop
				System.out.println("MoveForward(1), MoveBackward(2), TurnLeft(3), TurnRight(4),Stop(5) ");
				choice=keyboard.nextInt();
			}while(choice!=5);

		}

	}

}

SCREENSHOT:
Lab2-2

lab4a