Description
In this lab we need to create an interface call Movable and create three separate classes that will implement the interface of Movable and override them. The three classes which which we need to run this program and need to be consist in three classes which are plane, Car and ship. Then we need to create the interface and the classes that we needed,to write we need to create a program that will call each of the interfaces’ method that we created earlier polymorphically by processing in a array.
In the interface part, we didn’t have to do much, since the interface part code was given by the professor and by the three different classes which are Plane, Car and Ship which also we have to create each of the class implementing the interface using the keyword implements. And we implement the interface of Movable for each of the three classes that we need to create and then we override the method in each of the class with method override. Then we use system.out.println to print out the message that we need to print out for the lab. The main method MovableApp will have an array inside of the main body of the program.
CODE
PART 1
public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); void moveright(); void moveleft(); void movebackward(); }
PART 2
package lab6;
public class MovableApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Movable[] myMovables = new Movable[3];
myMovables[0] = new Plane();
myMovables[1] = new Car();
myMovables[2] = new Ship();
for (Movable m: myMovables){
m.moveForward();
m.movebackward();
m.stop();
m.moveleft();
m.moveright();
System.out.println(“—-“);
}
}
}
PART3
package lab6; 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"); } }
PART4
package lab6; 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"); } }
PART 5
package lab6;public class Ship implements Movable {
@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");
}}
PRINT SCREEN