Lab description:
In this lab, I will have to create four java files. There are three classes, Car(), Plane(), and Ship() that will implement the abstract methods in the given interface, Movable(); Finally, create a client/program, that uses an array to created three new objects, to run all methods implemented in the three classes and given the output as same as the instruction sheet.
Code:
//Name: Guan Hong Chen //Date: Nov. 04 2013 //CET 3640 Fall 2013 //Lab 4 (Movable.java) public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
//Name: Guan Hong Chen //Date: Nov. 04 2013 //CET 3640 Fall 2013 //Lab 4 (Car.java) public class Car implements Movable { @Override public void moveForward() { System.out.println("Car drives forward"); } @Override public void moveBackward() { System.out.println("Car drives backward"); } @Override public void stop() { System.out.println("Car parked"); } @Override public void moveLeft() { System.out.println("Car turns left"); } @Override public void moveRight() { System.out.println("Car turns right"); } }
//Name: Guan Hong Chen //Date: Nov. 04 2013 //CET 3640 Fall 2013 //Lab 4 (Plane.java) public class Plane implements Movable { @Override public void moveForward() { System.out.println("Plane flying forward"); } @Override public void moveBackward() { System.out.println("Plane taxiing backward"); } @Override public void stop() { System.out.println("Plane Landed"); } @Override public void moveLeft() { System.out.println("Plane flying left"); } @Override public void moveRight() { System.out.println("Plane fly right"); } }
//Name: Guan Hong Chen //Date: Nov. 04 2013 //CET 3640 Fall 2013 //Lab 4 (Ship.java) public class Ship implements Movable { @Override public void moveForward() { System.out.println("Ship navigates forward"); } @Override public void moveBackward() { System.out.println("Ship navigates backward"); } @Override public void stop() { System.out.println("Ship docked"); } @Override public void moveLeft() { System.out.println("Ship navigates left"); } @Override public void moveRight() { System.out.println("Ship navigates right"); } }
//Name: Guan Hong Chen //Date: Nov. 04 2013 //CET 3640 Fall 2013 //Lab 4 (App.java) public class App { public static void main(String[] args) { Movable[] movableObjects = new Movable[3]; movableObjects[0] = new Plane(); movableObjects[1] = new Car(); movableObjects[2] = new Ship(); for(Movable currentMovable : movableObjects) { currentMovable.moveForward(); currentMovable.moveBackward(); currentMovable.stop(); currentMovable.moveLeft(); currentMovable.moveRight(); // Just so it looks exactly like shown in the lab instruction; System.out.println("---------------------"); } //Bonus comment: Should I comment my code when I am writing? } }
Screenshot: