Lab 4(Polymorphisim)

Lab Description:
In lab 6 we’re given an interface Movable Object. Therefore, based on this program we had to create three different classes with names of Car, Plane, Ship to implement it to the Interface Movable object. I then had to also create a MovableAPP and this will ask the end user to choose a vehicle, and ask which direction do you want to move. This can be looked as a graph in which there is x and y coordinates. This is a good lab because it gives you an understanding of how a game can be created like for example FlappyBird. FlappyBird uses a graph to pinpoint its location to (0,0). When the user clicks on the touch screen phone, it will go a certain distance up, while also if you don’t press up than the gravity will take it down a certain distance to the -x coordinates.
Code:

import java.util.Scanner;

public class MovableApp {
	public static void main(String[] args) {
		int vehicle;
		int direction;
		int number;

		Scanner reader = new Scanner(System.in);
		car car = new car(0, 0);
		planes plane = new planes(0, 0);
		ship ship = new ship(0, 0);
		int x = 0;
		int y = 0;
		do {
			System.out
			.println("Enter 1 for car, Enter 2 for planes, Enter 3 for ship, and Enter 0 for exit?");
			vehicle = reader.nextInt();
			if (vehicle == 1) {
				System.out.println("Which direction do you want to drive?");
				System.out
				.println("Enter 1 to move forward, Enter 2 to move back, Enter 3 to move left, and enter 4 to move right");
				direction = reader.nextInt();
				if (direction == 1) {
					System.out.println("Enter the number to move forward?");
					number = reader.nextInt();

					car.moveForward(x + number);
					car.displayCoordinates();

				} else if (direction == 2) {

					System.out.println("Enter the number to move backward?");
					number = reader.nextInt();
					car.moveBackward(x - number);
					car.displayCoordinates();
				} else if (direction == 3) {
					System.out.println("Enter the number to move left?");
					number = reader.nextInt();

					car.moveLeft(y - number);
					car.displayCoordinates();

				} else if (direction == 4) {
					System.out.println("Enter the number to move right?");
					number = reader.nextInt();

					car.moveLeft(y + number);
					car.displayCoordinates();

				}

			}

			else if (vehicle == 2) {
				System.out.println("Which direction do you want to drive?");
				System.out
				.println("Enter 1 to move forward, Enter 2 to move back, Enter 3 to move left, and enter 4 to move right");
				direction = reader.nextInt();
				if (direction == 1) {
					System.out.println("Enter the number to move forward?");
					number = reader.nextInt();

					plane.moveForward(x + number);
					plane.displayCoordinates();

				} else if (direction == 2) {

					System.out.println("Enter the number to move backward?");
					number = reader.nextInt();
					plane.moveBackward(x - number);
					plane.displayCoordinates();
				} else if (direction == 3) {
					System.out.println("Enter the number to move left?");
					number = reader.nextInt();

					plane.moveBackward(y - number);
					plane.displayCoordinates();
				}

				else if (direction == 4) {
					System.out.println("Enter the number to move right?");
					number = reader.nextInt();

					plane.moveForward(y + number);
					plane.displayCoordinates();

				}

			} else if (vehicle == 3) {
				System.out.println("Which direction do you want to drive?");
				System.out
				.println("Enter 1 to move forward, Enter 2 to move back, Enter 3 to move left, and enter 4 to move right");
				direction = reader.nextInt();
				if (direction == 1) {
					System.out.println("Enter the number to move forward?");
					number = reader.nextInt();

					ship.moveForward(x + number);
					ship.displayCoordinates();

				} else if (direction == 2) {

					System.out.println("Enter the number to move backward?");
					number = reader.nextInt();
					ship.moveBackward(x - number);
					ship.displayCoordinates();
				} else if (direction == 3) {
					System.out.println("Enter the number to move left?");
					number = reader.nextInt();

					ship.moveLeft(y - number);
					ship.displayCoordinates();

				} else if (direction == 4) {
					System.out.println("Enter the number to move right?");
					number = reader.nextInt();

					ship.moveLeft(y + number);
					ship.displayCoordinates();

				}
			}

			 
		} while (vehicle != 0);

	}
}
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();

}

public class planes implements Movable {
	private int x = 0, y = 0;
	private static final String classname = "planes";
	private static final String cleo = "Beginning planes";

	public planes(int x, int y) {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
	}

	public void moveForward() {

	}

	public void moveBackward() {

	}

	

	public void moveLeft() {

	}

	public void moveRight() {

	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println(cleo + "(" + x + "," + y + ")");
	}
}
public class ship implements Movable {
	private int x = 0, y = 0;
	private static final String classname = "ship";
	private static final String cleo = "Beginning Car";

	public ship(int x, int y) {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
	}

	public void moveForward() {

	}

	public void moveBackward() {

	}

	

	public void moveLeft() {

	}

	public void moveRight() {

	}

	@Override
	public void moveForward(int x) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveBackward(int x) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveLeft(int y) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void moveRight(int y) {
		// TODO Auto-generated method stub
		System.out.println(classname + "(" + x + "," + y + ")");
	}

	@Override
	public void displayCoordinates() {
		// TODO Auto-generated method stub
		System.out.println(cleo + "(" + x + "," + y + ")");
	}
}


Screenshots:
lab4

Leave a Reply