CET 3640 LAB 4

Lab Description

Base on the Movable interface given to use we are to create three classes: Car(), Plane(), and Ship() that implement the movable interface:
moveForward();

          moveBackward();

          stop();

          moveLeft();

          moveRight();

After that we will create a program that will process an array for Movable interface by calling each of the interface method, after creating an object for each of the classes the output should display:

Plane flying forward

Plane taxiing backward

Plane landed

Plane flying left

Plane flying right

———————

Car drives forward

Car drives backward

Car parked

Car turns left

Car turns right

———————

Ship navigates forward

Ship navigates backward

Ship docked

Ship navigates left

Ship navigates right

———————

 

Code

 
public interface Movable {
public void moveForward();
public void moveBackward();
public void stop();
public void moveLeft();
public void moveRight();
}

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 is parked");    
    }
    @Override
    public void moveLeft() {
        System.out.println("Car is moving left.");
    }    
    @Override
    public void moveRight() {
        System.out.println("Car drives forward")
    }

}    

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 landed");
    }
    @Override
    public void moveLeft() {
        System.out.println("Plane flying left");
    }    
    @Override
    public void moveRight() {
        System.out.println("Plane flying right");
    }
}    
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 navigate left");
    }    
    @Override
    public void moveRight() {
        System.out.println("Ship navigate right");
    }
}

public class MovableClient {

    public static void main(String[] args) {
        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("---------------------");
        }
    }
}

Screenshot from 2013-11-03 17:35:16

Leave a Reply

Your email address will not be published. Required fields are marked *