Lab_4 – Polymorphism

Lab Description

In today’s lab we are given the following interface Moveable and with this set of interface we are to create a java program that has three classes named: Car, Plane and Ship. This program will polymorphically create an array of Moveable by calling each of the interface method. By doing this lab we are gaining experience on how to program using java and also learn how to program using different kinds of methods.

………………………………………………………………………………………………………………………………………………….

Movable.java :

package Lab4;

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

………………………………………………………………………………………………………………………………………..

Plane.java:

 

package Lab4;

public class Plane implements Movable
{
	private String plane = "Plane";
	
	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 +" has landed");
	}
	
	public void moveLeft()
	{
		System.out.println(plane +" flying left");
	}
	
	public void moveRight()
	{
		System.out.println(plane +" flying right");
		System.out.println("----------------------");
	}

}

…………………………………………………………………………………………………………………………………………………

Car.java:

 

package Lab4;
import java.lang.String;

public class Car implements Movable
{
	private String car = "Car";
	
	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");
		System.out.println("---------------------");
	}
}

……………………………………………………………………………………………………………………………………………..

Ship.java:

 

package Lab4;

public class Ship implements Movable
{
	private String ship = "Ship";
	
	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");
	}

}

…………………………………………………………………………………………………………………………………………………………………………………………

Lab4.java:

package Lab4;

public class Lab4
{
	public static void main(String args[])
	{
		Movable go[] = new Movable[3];
		go[0] = new Plane();
		go[1] = new Car();
		go[2] = new Ship();

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

……………………………………………………………………………………………………………………………………….

OUTPUT :

lab4_1

Leave a Reply

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