Lab Description:
In this lab I used the public interface which was given to design a program that is implemented by Plane, Car and Ship.
Code:
public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); } class Plane implements Movable { public void moveForward() { System.out.println("Plane flying forward");} public void moveBackward() { System.out.println("Plane taxing backward");} public void stop() { System.out.println("Plane landed");} public void moveLeft() { System.out.println("Plane flying left");} public void moveRight() { System.out.println("Plane flying right");} } class Car implements Movable { public void moveForward() { System.out.println("Car drives forward");} public void moveBackward() { System.out.println("Car drives backward");} public void stop() { System.out.println("Car parked");} public void moveLeft() { System.out.println("Car drives left");} public void moveRight() { System.out.println("Car drives right");} } class Ship implements Movable { public void moveForward() { System.out.println("Ship navigates forward");} public void moveBackward() { System.out.println("Ship navigates backward");} public void stop() { System.out.println("Ship docked");} public void moveLeft() { System.out.println("Ship navigates left");} public void moveRight() { System.out.println("Ship navigates right");} } public class Lab6{ 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 a: myMovable){ a.moveForward(); a.moveBackward(); a.stop(); a.moveLeft(); a.moveRight(); System.out.println("--------------------- "); } } }