Lab 4

Description:

In this lab we were given an Interface called Movable.java. Next we had to create three classes Car(), Plane(), and Ship() where each one will implement the Movable interface as follows:

  • Each class will have two int fields to keep track of the coordinates (x, y).
  • The default coordinates will be (0, 0), and an overloaded constructor will allow the user to initialize them to other values.
  • The movement of the objects will change the coordinates (x, y) one step at a time by default depending on the direction (i.e. moveForward() will add 1 to x, moveLeft() will subtract 1 to y). Overloaded methods will allow the user to change the coordinates a n number of steps at a time (i.e. moveForward( 7 ) will add 7 to x).

After that,  a program MovableApp will ask the user to choose from Car, Plane or Ship, and allow the user to change the coordinates until the user decides to stop and will display the new coordinates every time there is a change.

Code:

Movable.java

package Lab_4;

public interface Movable {
	public void moveForward();
	public void moveForward(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.java

//Created by Ryan Lawrence
//Car.java will implement the interface Movable.java
package Lab_4;

public class Car implements Movable{

	private int xPosition,yPosition;

	public Car(){
		xPosition=0;
		yPosition=0;
	}

	public Car(int a, int b){
		xPosition = a;
		yPosition = b;
	}

	//All methods in the interface have to be overridden 
	@Override
	public void moveForward() {
		xPosition++;

	}

	@Override
	public void moveForward(int x) {
		xPosition+=x;

	}

	@Override
	public void moveBackward() {
		xPosition--;

	}

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

	}

	@Override
	public void moveLeft() {
		yPosition--;

	}

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

	}

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

	}

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

	}

	@Override
	public void displayCoordinates() {
		System.out.println("Position of Car: "+xPosition +", " +yPosition);

	}

}

 Ship.java

//Created by Ryan Lawrence
//Ship.java will implement the interface Movable.java
package Lab_4;

public class Ship implements Movable{

	private int xPosition,yPosition;

	public Ship(){
		xPosition=0;
		yPosition=0;
	}

	public Ship(int a, int b){
		xPosition = a;
		yPosition = b;
	}

	//All methods in the interface have to be overridden
	@Override
	public void moveForward() {
		xPosition++;

	}

	@Override
	public void moveForward(int x) {
		xPosition+=x;

	}

	@Override
	public void moveBackward() {
		xPosition--;

	}

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

	}

	@Override
	public void moveLeft() {
		yPosition--;

	}

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

	}

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

	}

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

	}

	@Override
	public void displayCoordinates() {
		System.out.println("Position of Ship: "+xPosition +", " +yPosition);

	}

}

 

Plane.java

//Created by Ryan Lawrence
//Plane.java will implement the interface Movable.java
package Lab_4;

public class Plane implements Movable{

	private int xPosition,yPosition;

	public Plane(){
		xPosition=0;
		yPosition=0;
	}

	public Plane(int a, int b){
		xPosition = a;
		yPosition = b;
	}

	//All methods in the interface have to be overridden
	@Override
	public void moveForward() {
		xPosition++;

	}

	@Override
	public void moveForward(int x) {
		xPosition+=x;

	}

	@Override
	public void moveBackward() {
		xPosition--;

	}

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

	}

	@Override
	public void moveLeft() {
		yPosition--;

	}

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

	}

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

	}

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

	}

	@Override
	public void displayCoordinates() {
		System.out.println("Position of Plane: "+xPosition +", " +yPosition);

	}

}

 

MoveableApp.java

//Created by Ryan Lawrence
//MovableApp.java will implement the specific classes that were created.
package Lab_4;

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

public class MovableApp {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		Scanner i = new Scanner(System.in);
		int sMove;	//Specific Move Fields
		int move;	//Move Option Field
		int choice;	//Used to determine which object to create.
		String specificLocationChoice = "n";	//Field used to determine if overloaded constructor will be used
		int xCord=0,yCord=0;	//Used for overloaded constructors
		while(true){

			//Objects will be created when they are needed.
			//try/catch blocks used for error detection
			try{			
				System.out.print("1. Car\n2. Plane\n3. Ship\n4. Exit\nWhat would you like to move: ");
				choice = input.nextInt();
			}
			catch(InputMismatchException inputmismatch){
				System.out.println("Invalid Choice. Enter a number from 1 to 4");
				input.nextLine();
				System.out.println("Restarting program\n");
				continue;
			}
			if(choice 4){
				System.out.println("Invalid Choice");
				continue;
			}
			if(choice == 4){
				System.out.println("Bye");
				break;
			}
			//Used to specify a Specific Start Location
			System.out.print("Would you like to enter a specific Start position? [y/n]: ");
			specificLocationChoice = i.nextLine();
			if(specificLocationChoice.equals("y")){
				System.out.print("Enter the x co-ordinate: ");
				xCord = input.nextInt();
				System.out.print("\nEnter the y co-ordinate: ");
				yCord = input.nextInt();
			}

			switch(choice){
			case 1:
				Car honda = new Car();

				//Reinitialized object with overloaded constructor
				if(specificLocationChoice.equals("y")){
					honda = new Car(xCord, yCord);
				}

				while(true){
					try{
						moveMenu();
						move = input.nextInt();
					}
					catch (Exception e){
						System.out.println("Invalid Choice, Enter a number from 1 to 9");
						input.nextLine();
						continue;
					}
					if (move==9){
						break;
					}
					if (move<1||move>9){
						System.out.println("Invalid Choice");
						continue;
					}
					switch(move){
					case 1:
						honda.moveForward();
						honda.displayCoordinates();
						break;
					case 2:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						honda.moveForward(sMove);
						honda.displayCoordinates();
						break;
					case 3:
						honda.moveBackward();
						honda.displayCoordinates();
						break;
					case 4:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						honda.moveBackward(sMove);
						honda.displayCoordinates();
						break;
					case 5:
						honda.moveLeft();
						honda.displayCoordinates();
						break;
					case 6:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						honda.moveLeft(sMove);
						honda.displayCoordinates();
						break;
					case 7:
						honda.moveRight();
						honda.displayCoordinates();
						break;
					case 8:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						honda.moveRight(sMove);
						honda.displayCoordinates();
						break;
					}
				}
				break;
			case 2:
				Plane boeing = new Plane();
				if(specificLocationChoice.equals("y")){
					boeing = new Plane(xCord, yCord);
				}

				while(true){
					try{
						moveMenu();
						move = input.nextInt();
					}
					catch (Exception e){
						System.out.println("Invalid Choice, Enter a number from 1 to 9");
						input.nextLine();
						continue;
					}
					if (move==9){
						break;
					}
					if (move<1||move>9){
						System.out.println("Invalid Choice");
						continue;
					}
					switch(move){
					case 1:
						boeing.moveForward();
						boeing.displayCoordinates();
						break;
					case 2:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						boeing.moveForward(sMove);
						boeing.displayCoordinates();
						break;
					case 3:
						boeing.moveBackward();
						boeing.displayCoordinates();
						break;
					case 4:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						boeing.moveBackward(sMove);
						boeing.displayCoordinates();
						break;
					case 5:
						boeing.moveLeft();
						boeing.displayCoordinates();
						break;
					case 6:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						boeing.moveLeft(sMove);
						boeing.displayCoordinates();
						break;
					case 7:
						boeing.moveRight();
						boeing.displayCoordinates();
						break;
					case 8:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						boeing.moveRight(sMove);
						boeing.displayCoordinates();
						break;
					}
				}
				break;
			case 3:
				Ship titanic = new Ship();
				if(specificLocationChoice.equals("y")){
					titanic = new Ship(xCord, yCord);
				}

				while(true){
					try{
						moveMenu();
						move = input.nextInt();
					}
					catch (Exception e){
						System.out.println("Invalid Choice, Enter a number from 1 to 9");
						input.nextLine();
						continue;
					}
					if (move==9){
						break;
					}
					if (move<1||move>9){
						System.out.println("Invalid Choice");
						continue;
					}
					switch(move){
					case 1:
						titanic.moveForward();
						titanic.displayCoordinates();
						break;
					case 2:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						titanic.moveForward(sMove);
						titanic.displayCoordinates();
						break;
					case 3:
						titanic.moveBackward();
						titanic.displayCoordinates();
						break;
					case 4:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						titanic.moveBackward(sMove);
						titanic.displayCoordinates();
						break;
					case 5:
						titanic.moveLeft();
						titanic.displayCoordinates();
						break;
					case 6:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						titanic.moveLeft(sMove);
						titanic.displayCoordinates();
						break;
					case 7:
						titanic.moveRight();
						titanic.displayCoordinates();
						break;
					case 8:
						System.out.print("Enter amount you would like to move: ");
						sMove=input.nextInt();
						titanic.moveRight(sMove);
						titanic.displayCoordinates();
						break;
					}
				}

				break;

			}

		}

	}

	//Move menu method created to reduce total size of code.
	public static void moveMenu(){

		System.out.println("1.Forward by 1\n2.Forward by Specific Amount");
		System.out.println("3.Backward by 1\n4.Backwards by a Specific Amount");
		System.out.println("5.Left by 1\n6.Left by a Specific Amount");
		System.out.println("7.Right by 1\n8.Right by a Specific Amount");
		System.out.println("9.Stop");
		System.out.print("\nHow would you like to move the Vehicle?: ");
	}
}

 Screenshot:

lab4_3640

Leave a Reply

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