Lab Description:
Lab 6 is base on the Movable interface with three classes. The 3 classes are Car, Plane, and Ship. All 3 of the classes will implement from Movable. The Movable interface was given for this lab. After that the next step is to write a program that will polymorphically process an array of Movable. The new program will call each of the interface methods. Note: the implement of the classes must have the same output as the one that was given.
Code:
package lab_6; public class MovableArray { public static void main(String[] args) { Movable[] myMovable = new Movable[3]; myMovable[0] = new Plane(); myMovable[1] = new Car(); myMovable[2] = new Ship(); for (Movable m: myMovable){ m.moveForward(); m.movebackward(); m.stop(); m.moveleft(); m.moveright(); System.out.println("-------------------------"); } } }
package lab_6; public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); void moveright(); void moveleft(); void movebackward(); }
package lab_6; 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 turn right"); } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } }
package lab_6; 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 landen"); } @Override public void moveleft() { System.out.println("Plane flying left"); } @Override public void moveright() { System.out.println("Plane flying right"); } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } }
package lab_6; 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"); } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } }