LAB 4 – Polymorphism

Lab Report 4 – Description

We were given an interface called Movable(); with five abstract methods.  We created three classes based on the interface we were given.  These classes are Car(), Plane(), and Ship() that implement the interface Movable.  We then created a program that will polymorphically process an array of the interface methods in Movable by calling on them.  Before calling on them we will create objects for each one of the classes we created.  Underneath will be my code and a screenshot of the displayed results in the console.

Code:

package LAB_4;

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

package LAB_4;

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 turns right");

	}

}

package LAB_4;

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

	}

}

package LAB_4;

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

	}

}

package LAB_4;

public class Interface {
	String Introduction;
	public Interface (String Program)
	{
		Introduction = Program;
	}

	public String getWELCOME()
	{
		return Introduction;
	}

	public void MSGDISPLAY()
	{
		System.out.printf("Welcome to Washington Sarmiento's Fourth Program\n%s!\n\n", getWELCOME());
	}

	public static void main(String[] args) {
		Interface prgMessage = new Interface("CET3640f2013 LAB 4: Polymorphism");
		prgMessage.MSGDISPLAY();
		Movable[] move = new Movable[3];
		move[0] = new Plane();
		move[1] = new Car();
		move[2] = new Ship();

		for (int i = 0; i < move.length; i++) {
			move[i].moveForward();
			move[i].moveBackward();
			move[i].stop();
			move[i].moveLeft();
			move[i].moveRight();
			System.out.println(""+dottedLine());
		}

	}

	private static String dottedLine() {
		String motors = "-------------";
		return motors;
	}

}

Screenshots:

Screen Shot 2013-11-11 at 12.33.14 AM

Leave a Reply

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