Objective:
In this laboratory experiment, we had to create a program that will polymorphically process an array of Movable by calling each of the interface methods. in this program , there will be three classes; Planes, cars, And ships. These classes will implement the main movable interferance.
SOURCE 1 (Movable)
public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); }
SOURCEĀ 2 (car)
public class car implements Movable { public static void main(String[] args) { } 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 turns left"); } public void moveRight() { System.out.println("Car turns right"); } }
SOURCEĀ 3Ā Ā (ships)
public class ships implements Movable { public static void main(String[] args) { } public void moveForward() { System.out.println("ships navegates Foward"); } public void moveBackward() { System.out.println("ships navegates backwards"); } public void stop() { System.out.println("ships Stop"); } public void moveLeft() { System.out.println("ships turn left"); } public void moveRight() { System.out.println("ships turn right"); } }
SOURCE 4Ā (planes)
public class planes implements Movable { public static void main(String[] args) { } public void moveForward() { System.out.println("Planes Fly Foward"); } public void moveBackward() { System.out.println("Planes Fly backwards"); } public void stop() { System.out.println("Planes parked"); } public void moveLeft() { System.out.println("Planes turn left"); } public void moveRight() { System.out.println("Planes turn right"); } }
SOURCEĀ 5Ā (Mobile app)
public class MovableApp { public static void main(String[] args) { Movable[] myMovables = new Movable[3]; myMovables[0] = new car(); myMovables[1] = new planes(); myMovables[2] = new ships(); for(Movable m: myMovables){ m.moveForward(); m.moveBackward(); m.stop(); m.moveLeft(); m.moveRight(); System.out.println("_____________________"); } } }
ScreenShot