Lab 4

Description:

For this java lab, I had to use the programming technique Polymorphism. The type of polymorphism used is interface which presents a capacity that allows unrelated classes to implement a set of common methods needed. Three different classes are implemented by the MoveableMach interface. Also the three classes are overridden to let the output show when eclipse compile the code and execute it. Arrays are used in the test class MoveableMachApp to serve as a container to hold constant number of values of the same type. It will help to allow the user to see the end results of the program when it is called on to run.

package cet3640.Fall13.Lab4;
/**
 * 
 * @AntonScott
 *
 */
public interface MoveableMach {
	public void moveForward();
	public void moveBackward();
	public void stop();
	public void moveLeft();
	public void moveRight();

}
package cet3640.Fall13.Lab4;

public class Plane implements MoveableMach {

	@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 cet3640.Fall13.Lab4;

public class Car implements MoveableMach {

	@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 cet3640.Fall13.Lab4;

public class Ship implements MoveableMach {

	@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 cet3640.Fall13.Lab4;

public class MoveableMachApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MoveableMach[] myDestinationMovementTime = new MoveableMach[3];
		myDestinationMovementTime[0] = new Plane();
		myDestinationMovementTime[1] = new Car();
		myDestinationMovementTime[2] = new Ship();

		for (MoveableMach m: myDestinationMovementTime){
			m.moveForward();
			m.moveBackward();
			m.stop();
			m.moveLeft();
			m.moveRight();
			System.out.println("---------------------");
		}
	}


	}

Lab 4 part 1

Leave a Reply

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