Description:
The main purpose of this lab was to create an interface with three separate classes. The three classes we had to create were car, plane, and ship. The main interface code was provided to us by the professor for the classes. We just had to implement each class using the keyword implements.
Code:
Movable
package lab6; public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
MovableApp
package lab6; public class MovableApp { public static void main(String[] args) { Movable[] myMovables = new Movable[3]; myMovables[0] = new Plane(); myMovables[1] = new Car(); myMovables[2] = new Ship(); for (Movable m: myMovables){ m.moveForward(); m.moveBackward(); m.stop(); m.moveLeft(); m.moveRight(); System.out.println("---------------------"); } } }
Car
package lab6; 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"); } }
Plane
package lab6; 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 flying right"); } }
Ship
package lab6; 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"); } }
Screen Shot: