A City Tech OpenLab ePortfolio

LAB 4 – POLYMORPHISM

LAB 4 – Polymorphism

Lab Description:

In this lab, we will be creating an application called MovableApp with the implementation of an Interface named Movable(). The project will contain three classes by the name of Car(), Plane (), and Ship() with overloaded constructors. They will contain two int fields that will keep track of the movement coordinates (x.y). They will contain the default coordinates of (0,0) and provide the user with a way to change the starting coordinates if desired. As they will be implementing the interface Movable(), all methods from the interface will need to be used on each class. They will determine the movement by either increasing it by one or by the desired value input by the user with the use of overloaded methods. The MovableApp itself will then allow for the user to choose from a Car, Plane or Ship and allow for the user to change its coordinates until it wants to stop as well as display the current coordinate after each change.

Code:

Movable() Interface

public interface Moveable {

	public void moveFoward();
	public void moveFoward(int x);
	public void moveBackward();
	public void moveBackward(int x);
	public void moveLeft();
	public void moveLeft(int y);
	public void moveRight();
	public void moveRight(int y);
	public void displayCoordinates();
}

Car() class

public class Car implements Moveable{

	private int x = 0;
	private int y = 0;

	public Car(){

	}
	public Car(int n){
		x = n;
	}
	public Car(int n, int f){
		x = n;
		y = f;
	}

	@Override
	public void moveFoward() {
		x++;
	}

	@Override
	public void moveFoward(int n) {
		x = n + x;
	}

	@Override
	public void moveBackward() {
		x--;
	}

	@Override
	public void moveBackward(int n) {
		x = x - n;
	}

	@Override
	public void moveLeft() {
		y--;
	}

	@Override
	public void moveLeft(int n) {
		y = y - n;

	}

	@Override
	public void moveRight() {
		y++;

	}

	@Override
	public void moveRight(int n) {
		y = y + n;
	}

	@Override
	public void displayCoordinates() {
	System.out.println("("+ x + "," + y + ")");
	System.out.println();

	}	
}

Plane() class

public class Plane implements Moveable{

	private int x = 0;
	private int y = 0;

	public Plane(){

	}
	public Plane(int n){
		x = n;
	}
	public Plane(int n, int f){
		x = n;
		y = f;
	}
	@Override
	public void moveFoward() {
		x++;
	}

	@Override
	public void moveFoward(int n) {
		x = n + x;
	}

	@Override
	public void moveBackward() {
		x--;
	}

	@Override
	public void moveBackward(int n) {
		x = x - n;
	}

	@Override
	public void moveLeft() {
		y--;
	}

	@Override
	public void moveLeft(int n) {
		y = y - n;

	}

	@Override
	public void moveRight() {
		y++;

	}

	@Override
	public void moveRight(int n) {
		y = y + n;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("("+ x + "," + y + ")");
		System.out.println();

	}

}

Ship() class

public class Ship implements Moveable{

	private int x = 0;
	private int y = 0;

	public Ship(){

	}
	public Ship(int n){
		x = n;
	}
	public Ship(int n, int f){
		x = n;
		y = f;
	}

	@Override
	public void moveFoward() {
		x++;
	}

	@Override
	public void moveFoward(int n) {
		x = n + x;
	}

	@Override
	public void moveBackward() {
		x--;
	}

	@Override
	public void moveBackward(int n) {
		x = x - n;
	}

	@Override
	public void moveLeft() {
		y--;
	}

	@Override
	public void moveLeft(int n) {
		y = y - n;

	}

	@Override
	public void moveRight() {
		y++;

	}

	@Override
	public void moveRight(int n) {
		y = y + n;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("("+ x + "," + y + ")");
		System.out.println();

	}

}

MovableApp

import java.util.InputMismatchException;
import java.util.Scanner;

public class MovableApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scan1 = new Scanner(System.in); //Creates new scanner object needed for input from user
		int rSelect = 0;

		boolean endValid = false;
		do{
			boolean valid1;						//Boolean used followed do...while loop used for try/catch Exceptions
			do {
				try {
					valid1 = true;
					System.out.println("Choose a Ride");
					System.out.println("1.Car 2.Plane 3.Ship 4.Exit");
					rSelect = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (rSelect >= 1 && rSelect <= 4)
						valid1 = true;

					else if (rSelect < 1 || rSelect > 4 )
						throw new NumberFormatException("Integer is out of range.");

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Input is not a number." ); 
						valid1 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid1 = false;
						scan1.nextLine();
					}

		}while (!valid1);	//End of do...while loop used for try/catch exception

			if( rSelect == 4)
				break;

			Moveable[] sRide = new Moveable[3];
			boolean valid11;						//Boolean used followed do...while loop used for try/catch Exceptions
			int rx = 0;
			int ry = 0;
			do {
				try {
					valid11 = true;
					System.out.println("Enter starting position 1.Yes, 2.No");
					int cStarting = scan1.nextInt();
					switch (cStarting){
					case 1:	
							System.out.println("Enter x:");
							rx = scan1.nextInt();
							System.out.println("Enter y:");
							ry = scan1.nextInt();
							sRide[0] = new Car(rx,ry);
							sRide[1] = new Plane(rx,ry);
							sRide[2] = new Ship(rx,ry);
							break;
					case 2: sRide[0] = new Car();
							sRide[1] = new Plane();
							sRide[2] = new Ship();
							break;
					}

					if (cStarting < 1 || cStarting > 2 )
						throw new NumberFormatException("Integer is out of range.");

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Invalid Input" ); 
						valid11 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid11 = false;
						scan1.nextLine();
					}

		}while (!valid11);	//End of do...while loop used for try/catch exception

			if( rSelect == 1){

				boolean valid2 = false;
				do{

					try{
					System.out.println("Where would you like to move?");
					System.out.println("1. Foward");
					System.out.println("2. Foward by ?");
					System.out.println("3. Backwards");
					System.out.println("4. Backward by ?");
					System.out.println("5. Left");
					System.out.println("6. Left by ?");
					System.out.println("7. Right");
					System.out.println("8. Right by ?");
					System.out.println("9. Go Back");
					int cSelect = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (cSelect >= 1 && cSelect <= 9)
						valid2 = true;

					else if (cSelect < 1 || cSelect > 9 )
						throw new NumberFormatException("Integer is out of range.");

					int nNumber;

					if (cSelect == 1){
						sRide[0].moveFoward();
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 2){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[0].moveFoward(nNumber);
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 3){
						sRide[0].moveBackward();
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 4){	
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[0].moveBackward(nNumber);
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 5){
						sRide[0].moveLeft();
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 6){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[0].moveLeft(nNumber);
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 7){
						sRide[0].moveRight();
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 8){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[0].moveRight(nNumber);
						sRide[0].displayCoordinates();
					}
					else if (cSelect == 9)	
						valid2 = true;

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Input is not a number." ); 
						valid2 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid2 = false;
						scan1.nextLine();
					}

			}while (!valid2);

		}

			if( rSelect == 2){

				boolean valid2 = false;
				do{
					try{
					System.out.println("Where would you like to move?");
					System.out.println("1. Foward");
					System.out.println("2. Foward by ?");
					System.out.println("3. Backwards");
					System.out.println("4. Backward by ?");
					System.out.println("5. Left");
					System.out.println("6. Left by ?");
					System.out.println("7. Right");
					System.out.println("8. Right by ?");
					System.out.println("9. Go Back");
					int cSelect = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (cSelect >= 1 && cSelect <= 9)
						valid2 = true;

					else if (cSelect < 1 || cSelect > 9 )
						throw new NumberFormatException("Integer is out of range.");

					int nNumber;

					if (cSelect == 1){
						sRide[1].moveFoward();
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 2){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[1].moveFoward(nNumber);
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 3){
						sRide[1].moveBackward();
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 4){	
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[1].moveBackward(nNumber);
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 5){
						sRide[1].moveLeft();
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 6){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[1].moveLeft(nNumber);
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 7){
						sRide[1].moveRight();
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 8){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[1].moveRight(nNumber);
						sRide[1].displayCoordinates();
					}
					else if (cSelect == 9)	
						valid2 = true;	

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Input is not a number." ); 
						valid2 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid2 = false;
						scan1.nextLine();
					}

			}while (!valid2);

		}

			if( rSelect == 3){

				boolean valid2 = false;
				do{
					try{
					System.out.println("Where would you like to move?");
					System.out.println("1. Foward");
					System.out.println("2. Foward by ?");
					System.out.println("3. Backwards");
					System.out.println("4. Backward by ?");
					System.out.println("5. Left");
					System.out.println("6. Left by ?");
					System.out.println("7. Right");
					System.out.println("8. Right by ?");
					System.out.println("9. Go Back");
					int cSelect = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (cSelect >= 1 && cSelect <= 9)
						valid2 = true;

					else if (cSelect < 1 || cSelect > 9 )
						throw new NumberFormatException("Integer is out of range.");

					int nNumber;

					if (cSelect == 1){
						sRide[2].moveFoward();
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 2){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[2].moveFoward(nNumber);
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 3){
						sRide[2].moveBackward();
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 4){	
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[2].moveBackward(nNumber);
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 5){
						sRide[2].moveLeft();
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 6){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[2].moveLeft(nNumber);
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 7){
						sRide[2].moveRight();
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 8){
						System.out.println("Enter number");
						nNumber = scan1.nextInt();
						sRide[2].moveRight(nNumber);
						sRide[2].displayCoordinates();
					}
					else if (cSelect == 9)	
						valid2 = true;	

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Input is not a number." ); 
						valid2 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid2 = false;
						scan1.nextLine();
					}
			}while (!valid2);

		}

	}while (!endValid);

		System.out.println("Thank you!");
}
}

Screenshot:

Lab4Screen

Leave a Reply

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