Lab 4

Lab Description

In this lab, I created an interface called Movable that is implemented by three classes: Car, Plane, and Ship. The interface is considered to be abstract because there is no implementation done in the class. However, it does have the method of what it wants to happen for the other three classes. In the classes plane, car, and ship, I created a object that will implement a result for each class. Next, I created a new class called Vehicles which will polymorphically process an array of Movable by calling each  of the interface methods.

Source Code

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

}

public class Plane implements Movable {

	@Override
	public void moveForward() {
		System.out.println("Plane fly 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 fly Left");

	}

	@Override
	public void moveRight() {
		System.out.println("Plane fly Right");

	}

}

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 drives Left");

	}

	@Override
	public void moveRight() {
		System.out.println("Car drives 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 navigates Left");

	}

	@Override
	public void moveRight() {
		System.out.println("Ship navigates Right");

	}

}

public class Vehicles {

	public static void main(String[] args) {

		Movable v[] = new Movable[3];
		v[0]= new Plane(); 
		v[1]= new Car();
		v[2]= new Ship();

		for(int x=0;x<3;++x) {
			v[x].moveForward();
			v[x].moveBackward();
			v[x].stop();
			v[x].moveLeft();
			v[x].moveRight();
		}
	}

}

ScreenShot

lab 4

Leave a Reply

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