Lab Description:
This lab is to create a interface class. The code “interface Moveable” is given, base on this code we need to implement three classes “Car”,”Plane” and “Ship”. To do that, we use keyword implements for these classes, then we implementing the codes for each of the interface method. After that, create a program that will polymorphically process an array of Movable by calling each of the interface methods.
Code:
package lab6; public class MoveableApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Moveable[] myMoveable = new Moveable[3]; myMoveable[0] = new Plane(); myMoveable[1] = new Car(); myMoveable[2] = new Ship(); for (Moveable m : myMoveable) { m.moveForward(); m.moveBackward(); m.stop(); m.moveLeft(); m.moveRight(); System.out.println("--------------------------"); } } } ____________________________________________________________________ package lab6; public interface Moveable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); } _____________________________________________________________________ package lab6; public class Car implements Moveable { @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 turn right"); } } _____________________________________________________________________ package lab6; public class Plane implements Moveable { @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 landen"); } @Override public void moveLeft() { System.out.println("Plane flying left"); } @Override public void moveRight() { System.out.println("Plane flying right"); } } _____________________________________________________________________ package lab6; public class Ship implements Moveable { @Override public void moveForward() { System.out.println("Ship navigate forward"); } @Override public void moveBackward() { System.out.println("Ship navigate backward"); } @Override public void stop() { System.out.println("Ship docked"); } @Override public void moveLeft() { System.out.println("Ship navigate left"); } @Override public void moveRight() { System.out.println("Ship navigate right"); } }