Lab Description:
In this lab we are to use interface to write the following program. Than make 3 program Plane, Car and Ship that implements the program.
CODE:
package lab6; public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
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"); } }
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 drives left"); } @Override public void moveRight() { System.out.println("Car drives right"); } }
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"); } }
package lab6; public class MovableStep { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Movable[] myMovables = new Movable[3]; myMovables[0] = new Plane(); myMovables[1] = new Car(); myMovables[2] = new Ship(); for (Movable v: myMovables){ v.moveForward(); v.moveBackward(); v.stop(); v.moveLeft(); v.moveRight(); System.out.println("------------------"); } } }
Screen shot: