Skip to content

Categories:

Lab 4

Description:
This lab uses a new keyword which is referred to as interface. A class that is labeled interface contain
methods without a body and therefore contains no returns. The other subclasses which uses key word
implements, uses the methods from the interface class, however, these methods must now contain a body
with no returns (void). These methods must also be the same as the interface class, because the subclass
is implementing the methods from the interface as said before. The main class does not have any access
to the interface class but can pull the information of the subclasses from the interface class by using
polymorphism.

The only issue I had with the lab was getting used to how the objects were created to be able to access
the methods from the subclasses. It was much easier to do when I realized that the object could be made
into an array, and therefore I was able to create objects for the subclasses by using just one variable.
Other than that, it was easy enough to update the coordinates after the car/ship/plane moved.

Code:
Main Class:
import java.util.InputMismatchException;//libraries
import java.util.Scanner;
public class MoveableApp {

    public static void main(String[] args) { //main method
        int trans, dir, dist, x = 0, loop = 1, loop1 = 1, a = 1; //variables
        Scanner input = new Scanner(System.in); //Scanner object
        Movable moveObject[] = new Movable[3]; //Array object for Movable class
        moveObject[0] = new Car(); //Object to grab Car methods
        moveObject[1] = new Plane(); //Object to grab Plane methods
        moveObject[2] = new Ship(); //Object to grab Ship methods
        while (loop == 1) { //Main loop
            loop1 = 1; //Loop reset for directions
            System.out.println("Please choose your transportation:"); //Print statements
            System.out.println("(1) Car\n(2) Plane\n(3) Ship\n(4) Exit");
            try { //Catch character inputs
                trans = input.nextInt(); //Transportation input choice
                while (trans <= 0 || trans >= 5) { //Limit choices to 1-4
                    System.out.println("Please enter a valid transportation");
                    trans = input.nextInt();
                }
            }catch(InputMismatchException e) {
                System.out.println("Invalid input!");
                input.nextLine();
                continue;
            }
            switch (trans) { //Switch statement for transportation choice
            case 1: 
                x = 0;
                break;
            case 2:
                x = 1;
                break;
            case 3:
                x = 2;
                break;
            default://Completely end loops, end program
                loop = 0;
                loop1 = 0;
                break;
            }
            while (loop1 == 1) { //Loop for direction
                if (a == 1) {
                    moveObject[x].displayCoordinates();
                    a++;
                }
                System.out.println("Please choose a direction:"); //Print statement
                System.out.println("(1) Move Forward\n(2) Move Forward by 'x'\n(3) Move Backward\n(4) Move Backward by 'x'\n(5) Move Left\n(6) Move Left by 'x'\n(7) Move Right\n(8) Move Right by 'x'\n(9) Stop");
                try {
                    dir = input.nextInt(); //Input direction
                    while (dir <= 0 || dir >= 10) {
                        System.out.println("Please enter a valid direction");
                        dir = input.nextInt();
                    }
                }catch(InputMismatchException e) {
                    System.out.println("Invalid input!");
                    input.nextLine();
                    continue;
                }
                switch (dir) { //Direction switch statement
                case 1:
                    moveObject[x].moveForward(); //Move increase x by 1
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 2:
                    System.out.println("By how much?");
                    try {
                        dist = input.nextInt();
                    }catch(InputMismatchException e) {
                        System.out.println("Invalid input!");
                        input.nextLine();
                        continue;
                    }
                    moveObject[x].moveForward(dist); //Increase x by user amount
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 3:
                    moveObject[x].moveBackward(); //Decrease x by 1
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 4:
                    System.out.println("By how much?");
                    try{
                        dist = input.nextInt();
                    }catch(InputMismatchException e) {
                        System.out.println("Invalid input!");
                        input.nextLine();
                        continue;
                    }
                    moveObject[x].moveBackward(dist); //Decrease x by user amount
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 5:
                    moveObject[x].moveLeft(); //Decrease y by 1
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 6:
                    System.out.println("By how much?");
                    try {
                        dist = input.nextInt();
                    }catch(InputMismatchException e) {
                        System.out.println("Invalid input!");
                        input.nextLine();
                        continue;
                    }
                    moveObject[x].moveLeft(dist); //Decrease y by user amount
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 7:
                    moveObject[x].moveRight(); //Increase y by 1
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                case 8:
                    System.out.println("By how much?");
                    try {
                        dist = input.nextInt();
                    }catch(InputMismatchException e) {
                        System.out.println("Invalid input!");
                        input.nextLine();
                        continue;
                    }
                    moveObject[x].moveRight(dist); //Increase y by user amount
                    moveObject[x].displayCoordinates(); //Display current coordinates
                    break;
                default:
                    a = 1;
                    loop1 = 0; //Exit direction loop
                    moveObject[x].Reset();
                    break;
                }
            }
        }
    }
}

Movable Class:
public interface Movable { //Interface class
	public void moveForward(); //Move forward method
	public void moveForward(int x); //Move forward method with one parameter
	public void moveBackward(); //Move backward method
	public void moveBackward(int x); //Move backward method with one parameter
	public void moveLeft(); //Move left method
	public void moveLeft(int y); //Move left method with one parameter
	public void moveRight(); //Move right method
	public void moveRight(int y); //Move right method with one parameter
	public void displayCoordinates(); //Display Coordinates (x,y)
	public void Reset(); //Reset values
}

Car Class:
public class Car implements Movable { //Car class implementing Movable
	private int a = 0, b = 0; //Private variable
	public void moveForward() {
		a++; //Increase 'a' by 1
	}
	public void moveForward(int x) {
		a = a + x; //Increase 'a' with user input 'x'
	}
	public void moveBackward() {
		a--; //Decrease 'a' by 1
	};
	public void moveBackward(int x) {
		a = a - x; //Decrease 'a' with user input 'x'
	};
	public void moveLeft() {
		b--; //Decrease 'b' by 1
	};
	public void moveLeft(int y) {
		b = b - y; //Decrease 'b' with user input 'y'
	};
	public void moveRight() {
		b++; //Increase 'b' by 1
	};
	public void moveRight(int y) {
		b = b + y; //Increase 'b' by user input 'y'
	};
	public void displayCoordinates() {
		System.out.println("Current Coordinates: (" + a + "," + b + ")\n"); //Print (a,b)
	};
	public void Reset() {
		a = 0;
		b = 0;
	}
}

Plane Class:
public class Plane implements Movable { //Plane class implementing Movable
	private int a = 0, b = 0; //Private variable
	public void moveForward() {
		a++; //Increase 'a' by 1
	}
	public void moveForward(int x) {
		a = a + x; //Increase 'a' with user input 'x'
	}
	public void moveBackward() {
		a--; //Decrease 'a' by 1
	};
	public void moveBackward(int x) {
		a = a - x; //Decrease 'a' with user input 'x'
	};
	public void moveLeft() {
		b--; //Decrease 'b' by 1
	};
	public void moveLeft(int y) {
		b = b - y; //Decrease 'b' with user input 'y'
	};
	public void moveRight() {
		b++; //Increase 'b' by 1
	};
	public void moveRight(int y) {
		b = b + y; //Increase 'b' by user input 'y'
	};
	public void displayCoordinates() {
		System.out.println("Current Coordinates: (" + a + "," + b + ")\n"); //Print (a,b)
	};
	public void Reset() {
		a = 0;
		b = 0;
	}
}

Plane Class:
public class Ship implements Movable { //Ship class implementing Movable
	private int a = 0, b = 0; //Private variable
	public void moveForward() {
		a++; //Increase 'a' by 1
	}
	public void moveForward(int x) {
		a = a + x; //Increase 'a' with user input 'x'
	}
	public void moveBackward() {
		a--; //Decrease 'a' by 1
	};
	public void moveBackward(int x) {
		a = a - x; //Decrease 'a' with user input 'x'
	};
	public void moveLeft() {
		b--; //Decrease 'b' by 1
	};
	public void moveLeft(int y) {
		b = b - y; //Decrease 'b' with user input 'y'
	};
	public void moveRight() {
		b++; //Increase 'b' by 1
	};
	public void moveRight(int y) {
		b = b + y; //Increase 'b' by user input 'y'
	};
	public void displayCoordinates() {
		System.out.println("Current Coordinates: (" + a + "," + b + ")\n"); //Print (a,b)
	};
	public void Reset() {
		a = 0;
		b = 0;
	}
}
img1

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.