Description:
This lab is about polymorphism. I first created an interface class called Movalbe.java, and then I created three subclasses which inherited from Movable.java. And finally, I created a class called MovableApp.java to implement the three subclasses. In MovableApp.java, the user will be asked to create an object from one of the three subclasses and set up initial position if the user would like to. Note that the object is declared as superclass, but constructed as subclass. Next, the user is able to move the object around by choosing the direction and entering the distance.
Code:
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(); }
package Lab4; public class Car implements Movable { private int x; private int y; public Car(){ this.x = 0; this.y = 0; } public Car(int x, int y ){ this.x = x; this.y = y; } @Override public void moveForward() { this.moveForward(1); } @Override public void moveForward(int x) { this.x += x; } @Override public void moveBackward() { this.moveBackward(1); } @Override public void moveBackward(int x) { this.x -= x; } @Override public void moveLeft() { this.moveLeft(1); } @Override public void moveLeft(int y) { this.y -= y; } @Override public void moveRight() { this.moveRight(1); } @Override public void moveRight(int y) { this.y += y; } @Override public void displayCoordinates() { System.out.println("the X is "+ x + "\nthe Y is "+ y); } }
package Lab4; public class Plane implements Movable{ private int x; private int y; public Plane(){ this.x = 0; this.y = 0; } public Plane(int x, int y ){ this.x = x; this.y = y; } public void moveForward() { this.moveForward(1); } public void moveForward(int x) { this.x += x; } public void moveBackward() { this.moveBackward(1); } public void moveBackward(int x) { this.x -= x; } public void moveLeft() { this.moveLeft(1); } public void moveLeft(int y) { this.y -= y; } public void moveRight() { this.moveRight(1); } public void moveRight(int y) { this.y += y; } public void displayCoordinates() { System.out.println("the X is "+ x + "\nthe Y is "+ y); } }
package Lab4; public class Ship implements Movable{ private int x; private int y; public Ship(){ this.x = 0; this.y = 0; } public Ship(int x, int y ){ this.x = x; this.y = y; } public void moveForward() { this.moveForward(1); } public void moveForward(int x) { this.x += x; } public void moveBackward() { this.moveBackward(1); } public void moveBackward(int x) { this.x -= x; } public void moveLeft() { this.moveLeft(1); } public void moveLeft(int y) { this.y -= y; } public void moveRight() { this.moveRight(1); } public void moveRight(int y) { this.y += y; } public void displayCoordinates() { System.out.println("the X is "+ x + "\nthe Y is "+ y); } }
package Lab4; import java.util.Scanner; public class MovableApp { static Scanner userInput = new Scanner(System.in); static String transpotationType; static Movable movable; public static void main(String[] args) { selectTranspotation(); switch (transpotationType) { case "1": movable = new Car(); break; case "2": movable = new Plane(); break; case "3": movable = new Ship(); break; } askSetPosition(); getTranspotation(); movable.displayCoordinates(); moveAround(); } private static void moveAround() { int i = 0; do { System.out.println("Choose direction:" + "\n1. Forward" + "\n2. Backward" + "\n3. Right" + "\n4. Left" + "\n5. Exit"); String direction = userInput.nextLine(); try { Integer.parseInt(direction); } catch (java.lang.NumberFormatException e) { System.out.println("Invalid Input"); moveAround(); } ; if (Integer.parseInt(direction) > 5) { System.out.println("Invalid Input"); moveAround(); } else if (Integer.parseInt(direction) == 5) { System.out.println("Exiting...."); return; } System.out.println("Enter distance:"); String distance = userInput.nextLine(); try { Integer.parseInt(distance); } catch (java.lang.NumberFormatException e) { System.out.println("Invalid Input"); moveAround(); } ; switch (direction) { case "1": movable.moveForward(Integer.parseInt(distance)); break; case "2": movable.moveBackward(Integer.parseInt(distance)); break; case "3": movable.moveRight(Integer.parseInt(distance)); break; case "4": movable.moveLeft(Integer.parseInt(distance)); break; } movable.displayCoordinates(); System.out.println(); } while (i == 0); } private static void askSetPosition() { System.out.println("Do you want to set default position?(y/n)"); String askSetDefult = userInput.nextLine(); switch (askSetDefult) { case "y": case "Y": setPosition(); break; case "n": case "N": break; default: System.out.println("Invalid input."); askSetPosition(); break; } } private static void setPosition() { int x, y; System.out.println("enter x value:"); x = userInput.nextInt(); System.out.println("enter y value:"); y = userInput.nextInt(); switch (transpotationType) { case "1": movable = new Car(x, y); break; case "2": movable = new Plane(x, y); break; case "3": movable = new Ship(x, y); break; } } public static void selectTranspotation() { System.out.println("Choose type of transpotation:" + "\n1.Car" + "\n2.Plane" + "\n3.Ship"); transpotationType = userInput.nextLine(); switch (transpotationType) { case "1": case "2": case "3": break; default: System.out.println("Invalid input."); selectTranspotation(); break; } } public static void getTranspotation() { switch (transpotationType) { case "1": System.out.println("Car"); break; case "2": System.out.println("Plane"); break; case "3": System.out.println("Ship"); break; } } }
Screenshot: