A City Tech OpenLab ePortfolio

Lab 4

Lab Description:
In this lab exercise, we are given an interface called Movable(). Then we are asked to crate 3 classes which are Car(), Plane(), and Ship() which everyone of them will implement the Movable interface. They all will need 2 int fields to keep track of the coordinates (x,y) with a default value of (0,0), but are able to modify them with an overloaded constructor. The default movement of the object will be 1 coordinate depending on which direction it’s moving. However, the overloaded methods will allow the user to change the default movement to any value they like. Lastly, we are asked to create a program named as MovableApp that ask the use to pick the object they want and allow the user to change the coordinates until the user decided to stop the program. Every time there’s a change of coordinates, the program need to display them for the user to see.

Code:
MovableApp

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


public class MovableApp {

	private static Scanner scan1;

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

		scan1 = new Scanner(System.in);
		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 = 4)
						valid1 = true;
					else if (rSelect  4 )
						throw new NumberFormatException();

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

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

			if (rSelect == 4)
			{
				System.out.println("Thank you for using this App. Hope you have fun with it. :)");
				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();

					if (cStarting = 2)
						valid11 = true;
					else if (cStarting  2 )
						throw new NumberFormatException();

					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;
					}

				}catch ( InputMismatchException nfe) {		//Number Check Exception
					System.out.println( "Invalid input. Please try again using only number." ); 
					valid11 = false;
					scan1.next();
				}
				catch (NumberFormatException nfe) {			//Range check exception
					System.out.println("Number is out of range. Please try again.");
					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 = 9)
							valid2 = false;
						else if (cSelect  9 )
							throw new NumberFormatException();

						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( "Invalid input. Please try again using only number." ); 
						valid2 = false;
						scan1.next();
					}
					catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of range. Please try again.");
						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 = 9)
							valid2 = false;
						else if (cSelect  9 )
							throw new NumberFormatException();

						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( "Invalid input. Please try again using only number." ); 
						valid2 = false;
						scan1.next();
					}
					catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of range. Please try again.");
						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 = 9)
							valid2 = false;
						else if (cSelect  9 )
							throw new NumberFormatException();

						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( "Invalid input. Please try again using only number." ); 
						valid2 = false;
						scan1.next();
					}
					catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of range. Please try again.");
						valid2 = false;
						scan1.nextLine();
					}
				}while (!valid2);

			}

		}while (!endValid);
	}
}


Car



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



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



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();
		
	}
	
	
}

Screenshot:
Lab 4

Leave a Reply

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