Lab 4

Lab Description:

Lab 4 covers Polymorphism and the usage of Interfaces. Polymorphism with Interfaces standardizes uncommon classes together and gives ability to interact to each other. It works by providing list of tasks that it needs to complete to do what it’s suppose to do. Thus, the methods inside interface must be abstract. Then the ‘how-to’ part of the code is redefined in separate concrete class.

Interface class is provided with lab instructions with abstract methods. Using that, separate concrete classes are made for different types of vehicles. Since it’s a concrete class, this time the methods include tasks. Finally, MoveableApp.java will lead the user through the menu which enables user to change coordinates of the vehicles.

Source Code:

Moveable.java

package Lab4;

public interface Moveable {
	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

package Lab4;

public class Car implements Moveable{
	private int carx;
	private int cary;

	@Override
	public void moveForward() {
		carx = carx + 1;
	}

	@Override
	public void moveForward(int coorx) {
		carx = carx + coorx;
	}

	@Override
	public void moveBackward() {
		carx = carx - 1;
	}

	@Override
	public void moveBackward(int coorx) {
		carx = carx - coorx;
	}

	@Override
	public void moveLeft() {
		cary = cary - 1;
	}

	@Override
	public void moveLeft(int coory) {
		cary = cary - coory;
	}

	@Override
	public void moveRight() {
		cary = cary + 1;
	}

	@Override
	public void moveRight(int coory) {
		cary = coory + 1;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("X: " + carx + " Y: " + cary);
	}
}

Plane.java

package Lab4;

public class Plane implements Moveable {
	private int planex;
	private int planey;

	@Override
	public void moveForward() {
		planex = planex + 1;
	}

	@Override
	public void moveForward(int coorx) {
		planex = coorx + planex;
	}

	@Override
	public void moveBackward() {
		planex = planex - 1;
	}

	@Override
	public void moveBackward(int coorx) {
		planex = planex - coorx;
	}

	@Override
	public void moveLeft() {
		planey = planey - 1;
	}

	@Override
	public void moveLeft(int coory) {
		planey = planey - coory;
	}

	@Override
	public void moveRight() {
		planey = planey + 1;
	}

	@Override
	public void moveRight(int coory) {
		planey = coory + 1;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("X: " + planex + " Y: " + planey);
	}
}

Ship.java

package Lab4;

public class Ship implements Moveable{
	private int shipx;
	private int shipy;

	@Override
	public void moveForward() {
		shipx = shipx + 1;
	}

	@Override
	public void moveForward(int coorx) {
		shipx = coorx + shipx;
	}

	@Override
	public void moveBackward() {
		shipx = shipx - 1;
	}

	@Override
	public void moveBackward(int coorx) {
		shipx = shipx - coorx;
	}

	@Override
	public void moveLeft() {
		shipy = shipy - 1;
	}

	@Override
	public void moveLeft(int coory) {
		shipy = shipy - coory;
	}

	@Override
	public void moveRight() {
		shipy = shipy + 1;
	}

	@Override
	public void moveRight(int coory) {
		shipy = coory + 1;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("X: " + shipx + " Y: " + shipy);
	}
}

MoveableApp.java

package Lab4;

import java.util.Scanner;

public class MoveableApp {

	private static Car car = new Car();
	private static Plane plane = new Plane();
	private static Ship ship = new Ship();
	private static int vehicle = 0, coorx, coory;

	public static void main(String[] args) {

		while (true) {
			try {
				Scanner input = new Scanner(System.in);
				System.out.println("\nChoose: \n1)Car\n2)Plane"
						+ "\n3)Ship\n4)Exit");
				vehicle = input.nextInt();
				if (vehicle == 1) {
					System.out.println("\nCurrent Car Coordinate:");
					car.displayCoordinates();
					car();
				}
				if (vehicle == 2) {
					System.out.println("\nCurrent Plane Coordinate:");
					plane.displayCoordinates();
					plane();
				}
				if (vehicle == 3) {
					System.out.println("\nCurrent Ship Coordinate:");
					ship.displayCoordinates();
					ship();
				}
				/** EXIT **/
				if (vehicle == 4) {
					System.out.println("BYE");
					System.exit(0);
				}
				if (vehicle > 4) {
					System.out.println("Wrong Input! Try Again!");
					continue;
				}

			} catch (Exception e) {
				System.out.println("Bad Input! Try Again!");
			}
		}
	}

	public static void car() {
		while (true) {
			Scanner input = new Scanner(System.in);
			System.out.println("\n1)Move Forward\n2)Move Backward\n"
					+ "3)Move Left\n4)Move Right\n5)Change Vehicle");
			int movement = input.nextInt();
			if (movement == 1) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				car.moveForward(coorx);
				car.displayCoordinates();
			}
			if (movement == 2) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				car.moveBackward(coorx);
				car.displayCoordinates();
			}
			if (movement == 3) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				car.moveLeft(coory);
				car.displayCoordinates();
			}
			if (movement == 4) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				car.moveRight(coory);
				car.displayCoordinates();
			}
			if (movement == 5) {
				break;
			}
			if (movement > 5) {
				System.out.println("Wrong Input! Try Again!");
			}
		}
	}

	public static void plane() {
		while (true) {
			Scanner input = new Scanner(System.in);
			System.out.println("\n1)Move Forward\n2)Move Backward\n"
					+ "3)Move Left\n4)Move Right\n5)Change Vehicle");
			int movement = input.nextInt();
			if (movement == 1) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				plane.moveForward(coorx);
				plane.displayCoordinates();
			}
			if (movement == 2) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				plane.moveBackward(coorx);
				plane.displayCoordinates();
			}
			if (movement == 3) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				plane.moveLeft(coory);
				plane.displayCoordinates();
			}
			if (movement == 4) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				plane.moveRight(coory);
				plane.displayCoordinates();
			}
			if (movement == 5) {
				break;
			}
			if (movement > 5) {
				System.out.println("Wrong Input! Try Again!");
			}
		}
	}

	public static void ship() {
		while (true) {
			Scanner input = new Scanner(System.in);
			System.out.println("\n1)Move Forward\n2)Move Backward\n"
					+ "3)Move Left\n4)Move Right\n5)Change Vehicle");
			int movement = input.nextInt();
			if (movement == 1) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				ship.moveForward(coorx);
				ship.displayCoordinates();
			}
			if (movement == 2) {
				System.out.println("\nX: ");
				coorx = input.nextInt();
				ship.moveBackward(coorx);
				ship.displayCoordinates();
			}
			if (movement == 3) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				ship.moveLeft(coory);
				ship.displayCoordinates();
			}
			if (movement == 4) {
				System.out.println("\nY: ");
				coory = input.nextInt();
				ship.moveRight(coory);
				ship.displayCoordinates();
			}
			if (movement == 5) {
				break;
			}
			if (movement > 5) {
				System.out.println("Wrong Input! Try Again!");
			}
		}
	}
}

Screenshots:

Lab 4_Output

Leave a Reply

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