Lab Description:
We are given a movable interface. We are then to create 3 classes based on the given interface. The three classes are to be Car, Plane, and Ship. The three classes will implement the interface. Afterwards, a program will be created, which will process an array of Movable by calling each of the interface methods. There will be a separate object for each of the 3 classes.
Code:
Movable.java
package Lab4; public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
Car.java
package Lab4; public class Car implements Movable { private String forward; private String backward; private String stop; private String left; private String right; public Car() { forward = "Car drives forward"; backward = "Car drives backward"; stop = "Car parked"; left = "Car turns left"; right = "Car turns right"; } @Override public void moveForward() { System.out.println(forward); } @Override public void moveBackward() { System.out.println(backward); } @Override public void stop() { System.out.println(stop); } @Override public void moveLeft() { System.out.println(left); } @Override public void moveRight() { System.out.println(right); } }
Plane.java
package Lab4; public class Plane implements Movable { private String forward; private String backward; private String stop; private String left; private String right; public Plane() { forward = "Plane flying forward"; backward = "Plane taxiing backward"; stop = "Plane landed"; left = "Plance flying left"; right = "Plane flying right"; } @Override public void moveForward() { System.out.println(forward); } @Override public void moveBackward() { System.out.println(backward); } @Override public void stop() { System.out.println(stop); } @Override public void moveLeft() { System.out.println(left); } @Override public void moveRight() { System.out.println(right); } }
Ship.java
package Lab4; public class Ship implements Movable { private String forward; private String backward; private String stop; private String left; private String right; public Ship() { forward = "Ship navigates forward"; backward = "Ship navigates backward"; stop = "Ship docked"; left = "Ship navigates left"; right = "Ship navigates right"; } @Override public void moveForward() { System.out.println(forward); } @Override public void moveBackward() { System.out.println(backward); } @Override public void stop() { System.out.println(stop); } @Override public void moveLeft() { System.out.println(left); } @Override public void moveRight() { System.out.println(right); } }
MovableInterfaceTest.java
package Lab4; public class MovableInterfaceTest { public static void main(String[] args) { Movable[] movableObjects = new Movable[3]; movableObjects[0] = new Car(); movableObjects[1] = new Plane(); movableObjects[2] = new Ship(); System.out.println("-------------------------"); for (Movable currentMovable : movableObjects) { currentMovable.moveForward(); currentMovable.moveBackward(); currentMovable.stop(); currentMovable.moveLeft(); currentMovable.moveRight(); System.out.println("-------------------------"); } } }