Lab Description
In this lab we had to create three classes, Car(), Plane(), and Ship(), in which each one will implement the Movable interface:
• 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.
After that,we then created a program MovableApp that 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. The program should display the new coordinates every time there is a change.
Code
Movable public interface Movable { public void moveForward(); public void moveForward(int y); public void moveBackward(); public void moveBackward(int x); public void moveLeft(); public void moveLeft(int n); public void moveRight(); public void moveRight(int y); public void displayCoordinates(int i); void displayCoordinates(); }
MovableApp import java.util.Scanner; public class MovableApp { private static Scanner input; public static void main(String[] args) { //array that initializes the interface Movable that implements Car Ship Plane Movable movableObject[] = new Movable[3]; movableObject[0] = new Car(); movableObject[1] = new Plane(); movableObject[2] = new Ship(); input = new Scanner(System.in); int carChoice, moveChoice=1, x, y, startChoice; System.out.print("Enter 0 to initialize your position (If not default is (0) "); startChoice = input.nextInt(); //if startChoice = 1 allows user to defines its own coordinates instead of default 0,0 if (startChoice==1) { System.out.print("As of now, what is your longitude (x coordinate)? "); x = input.nextInt(); System.out.print("As of now, what is your latitude (y courdinate)? "); y = input.nextInt(); movableObject[0] = new Car(x,y); movableObject[1] = new Plane(x,y); movableObject[2] = new Ship(x,y); } //Allows user to choose Movable Object System.out.print("Choose the Movable object you desire (0) Car (1) Plane (2) Ship\nWhat is your choice? "); carChoice = input.nextInt(); //if moveChoice = 1 allow user to continuously move its Movable Object while(moveChoice==1) { //Ask user if he wants to move 1 will run this loop else it exits System.out.print("Choose (1) Get Moving (2) Exit "); moveChoice = input.nextInt(); //if it does not equal to run skip this step if (moveChoice==1) { //Input the amount of steps you want to take in x and y System.out.print("How much Foward or Back to move (+# is Foward, # is Back): "); x = input.nextInt(); System.out.print("How much Left or Right to move (+# is Right, -# is Left): "); y = input.nextInt(); //if x is positive move forward if (x>=0) { movableObject[carChoice].moveForward(x); movableObject[carChoice].moveForward(); } //else if y is negative move backward else if (x<0) { movableObject[carChoice].moveBackward(x); movableObject[carChoice].moveBackward(); } //if y is positive move right if (y>=0) { movableObject[carChoice].moveRight(y); movableObject[carChoice].moveRight(); } //else if y is negative move left else if (y<0) { movableObject[carChoice].moveLeft(y); movableObject[carChoice].moveLeft(); } //display coordinate of movable object movableObject[carChoice].displayCoordinates(0); } } } }
Ship public class Ship implements Movable { //integers used for only this class private int x, y, moveFoward, moveBackward, moveLeft, moveRight; //Default Constructor for Ship Class public Ship() { this.x=1; this.y=2; } //Overwritten Constructor for Ship Class public Ship(int x,int y) { this.x = y; this.y = x; } //Methods Being Implements by the Movable interface!! //Prints out the steps of move Forward @Override public void moveForward() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveFoward;i++) { int goFoward=(this.x-this.moveFoward)+i; System.out.println("Ship start from "+"("+(goFoward-1)+","+this.y+")"+" moved to "+"("+goFoward+","+this.y+")" ); } } //Determines forward position of x @Override public void moveForward(int x) { // TODO Auto-generated method stub this.moveFoward = x; } //Prints out the steps of move backwards @Override public void moveBackward() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveBackward;i++) { int goBackward=(this.x+this.moveBackward)-i; System.out.println("Ship start from "+"("+(goBackward+1)+","+this.y+")"+" moved to "+"("+goBackward+","+this.y+")" ); } } //Determines backward position of x @Override public void moveBackward(int x) { // TODO Auto-generated method stub this.x +=x; this.moveBackward = x*(1); } //Prints out the steps of move left @Override public void moveLeft() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveLeft;i++) { int goLeft=(this.y+this.moveLeft)-i; System.out.println("Ship start from "+"("+this.x+","+(goLeft+1)+")"+" moved to "+"("+this.x+","+goLeft+")" ); } } //Determines left position of y @Override public void moveLeft(int y) { // TODO Auto-generated method stub this.y +=y; this.moveLeft = y*(-1); } //Prints out the steps of move right @Override public void moveRight() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveRight;i++) { int goRight=(this.y-this.moveRight)+i; System.out.println("Ship start from "+"("+this.x+","+(goRight-1)+")"+" moved to "+"("+this.x+","+goRight+")" ); } } //Determines right position of y @Override public void moveRight(int y) { // TODO Auto-generated method stub this.y +=y; this.moveRight = y; } //Prints out the coordinates @Override public void displayCoordinates() { // TODO Auto-generated method stub System.out.println("Ship is at " + "("+this.x+","+this.y+")" ); } @Override public void displayCoordinates(int i) { // TODO Auto-generated method stub } }
Plane public class Plane implements Movable { //integers used for only this class private int x, y, moveFoward, moveBackward, moveLeft, moveRight; //Default Constructor for Plane Class public Plane() { this.x=01; this.y=02; } //Overwritten Constructor for Plane Class public Plane(int x,int y) { this.x = y; this.y = x; } //Methods Being Implements by the Movable interface!! //Prints out the steps of move Forward @Override public void moveForward() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveFoward;i++) { int goFoward=(this.x-this.moveFoward)+i; System.out.println("Plane start from "+"("+(goFoward-1)+","+this.y+")"+" moved to "+"("+goFoward+","+this.y+")" ); } } //Determines forward position of x @Override public void moveForward(int x) { // TODO Auto-generated method stub this.x +=x; this.moveFoward = x; } //Prints out the steps of move backwards @Override public void moveBackward() { // TODO Auto-generated method stub for(int i = 1;i<this.moveBackward;i++) { int goBackward=(this.x+this.moveBackward)-i; System.out.println("Plane start from "+"("+(goBackward+1)+","+this.y+")"+" moved to "+"("+goBackward+","+this.y+")" ); } } //Determines backward position of x @Override public void moveBackward(int x) { // TODO Auto-generated method stub this.x +=x; this.moveBackward = x+(-1); } //Prints out the steps of move left @Override public void moveLeft() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveLeft;i++) { int goLeft=(this.y+this.moveLeft)-i; System.out.println("Plane start from "+"("+this.x+","+(goLeft+1)+")"+" moved to "+"("+this.x+","+goLeft+")" ); } } //Determines left position of y @Override public void moveLeft(int y) { // TODO Auto-generated method stub this.y +=y; this.moveLeft = (y -1); } //Prints out the steps of move right @Override public void moveRight() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveRight;i++) { int goRight=(this.y-this.moveRight)+i; System.out.println("Plane start from "+"("+this.x+","+(goRight-1)+")"+" moved to "+"("+this.x+","+goRight+")" ); } } //Determines right position of y @Override public void moveRight(int y) { // TODO Auto-generated method stub this.y +=y; this.moveRight = y; } //Prints out the coordinates @Override public void displayCoordinates() { // TODO Auto-generated method stub System.out.println("Plane is at " + "("+this.x+","+this.y+")" ); } @Override public void displayCoordinates(int i) { // TODO Auto-generated method stub } }
Car public class Car implements Movable { //integers used for only this class private int x, y, moveFoward, moveBackward, moveLeft, moveRight; //Default Constructor for Car Class public Car() { this.x=0; this.y=0; } //Overwritten Constructor for Car Class public Car(int x,int y) { this.x = x; this.y = y; } //Methods Being Implements by the Movable interface!! //Prints out the steps of move Forward @Override public void moveForward() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveFoward;i++) { int goFoward=(this.x-this.moveFoward)+i; System.out.println("Car start from "+"("+(goFoward-1)+","+this.y+")"+" moved to "+"("+goFoward+","+this.y+")" ); } } //Determines forward position of x @Override public void moveForward(int x) { // TODO Auto-generated method stub this.x +=x; this.moveFoward = x; } //Prints out the steps of move backwards @Override public void moveBackward() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveBackward;i++) { int goBackward=(this.x+this.moveBackward); System.out.println("Car start from "+"("+(goBackward+1)+","+this.y+")"+" moved to "+"("+goBackward+","+this.y+")" ); } } //Determines backward position of x @Override public void moveBackward(int x) { // TODO Auto-generated method stub this.x +=x; this.moveBackward = x*(1); } //Prints out the steps of move left @Override public void moveLeft() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveLeft;i++) { int goLeft=(this.y+this.moveLeft)-i; System.out.println("Car start from "+"("+this.x+","+(goLeft+1)+")"+" moved to "+"("+this.x+","+goLeft+")" ); } } //Determines left position of y @Override public void moveLeft(int y) { // TODO Auto-generated method stub this.y +=y; this.moveLeft = y*(1); } //Prints out the steps of move right @Override public void moveRight() { // TODO Auto-generated method stub for(int i = 1;i<=this.moveRight;i++) { int goRight=(this.y-this.moveRight)+i; System.out.println("Car start from "+"("+this.x+","+(goRight)+")"+" moved to "+"("+this.x+","+goRight+")" ); } } //Determines right position of y @Override public void moveRight(int y) { // TODO Auto-generated method stub this.y +=y; this.moveRight = y; } //Prints out the coordinates @Override public void displayCoordinates() { // TODO Auto-generated method stub System.out.println("Car is at " + "("+this.x+","+this.y+")" ); } @Override public void displayCoordinates(int i) { // TODO Auto-generated method stub } }
package lab4; import java.util.Scanner; public class MovableApp { static int n = -1; static Scanner sc = new Scanner(System.in); public static int getNoOfSpaces() { System.out.println("Choose the number of spaces to move: "); n = sc.nextInt(); System.out.println(); return n; } public static void movementOption(Movable obj) { while(true) { System.out.println("Choose a direction: "); System.out.println(" 0. Exit \n 1. Forward \n 2. Backward \n 3. Left \n 4. Right \n "); System.out.print("Direction: "); n = sc.nextInt(); System.out.println(); switch (n) { case 0: { return; } case 1: { int noOfSteps=getNoOfSpaces(); obj.moveForward(noOfSteps); break; } case 2: { int noOfSteps=getNoOfSpaces(); obj.moveBackward(noOfSteps); break; } case 3: { int noOfSteps=getNoOfSpaces(); obj.moveLeft(noOfSteps); break; } case 4: { int noOfSteps=getNoOfSpaces(); obj.moveRight(noOfSteps); break; } default: break; } } } public static void main(String[] args) { Movable[] Movement = new Movable[3]; Movement[0] = new Plane(); Movement[1] = new Car(); Movement[2] = new Ship(); while(true) { System.out.println("Choose a vehicle: "); System.out.println(" 0. Exit \n 1. Car \n 2. Plane \n 3. Ship \n"); System.out.print("Selection: "); n = sc.nextInt(); System.out.println(); switch (n) { case 0: { return; } case 1: { Car c=(Car)Movement[1]; movementOption(c); break; } case 2: { Plane p=(Plane)Movement[0]; movementOption(p); break; } case 3: { Ship s=(Ship)Movement[2]; movementOption(s); break; } default: break; } } } }