LAB 4

In this lab our goal was to use movable interface to create 3 classes that implements car, plane, and ship. after we create these 3 classes we then had to write a program that allows a polymorphically to process an array of Movable by calling the interface methods. this lab was a bit challenging but with the help of my group members we figured it out.

package Lab4;
public interface Movable {

public void moveForward();
public void moveBackward();
public void stop();
public void moveLeft();
public void moveRight();

}

package Lab4;

public 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 turns left");
}

public void moveRight()
{
System.out.println("Car turns right");
}

}

package Lab4;

public class Plane implements Movable {

public void moveForward()
{
System.out.println("Plane flying forward");
}

public void moveBackward()
{
System.out.println("Plane taxiing 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");
}

}

package Lab4;

public 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");
}

}

package Lab4;

public class MovableTest {

public static void main(String[] args) {

Movable[] movableobject = new Movable[3];
movableobject[0] = new Plane();
movableobject[0].moveForward();
movableobject[0].moveBackward();
movableobject[0].stop();
movableobject[0].moveLeft();
movableobject[0].moveRight();

System.out.println("---------------------");

movableobject[1] = new Car();
movableobject[1].moveForward();
movableobject[1].moveBackward();
movableobject[1].stop();
movableobject[1].moveLeft();
movableobject[1].moveRight();

System.out.println("---------------------");

movableobject[2] = new Ship();
movableobject[2].moveForward();
movableobject[2].moveBackward();
movableobject[2].stop();
movableobject[2].moveLeft();
movableobject[2].moveRight();
}

}
Lab4

Leave a Reply

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