Description:
In this lab we basically, implemented Movable interface to car, plane, ship classes. We basically overloaded constructors from car, plane, ship classes with two coordinates X and Y, declared fields, and invoked abstract methods from Movable interface to our classes. In movable app, we basically used our classes to create object and used those object to invoke the methods, to let the user enter the value for methods and get the output as coordinates.
code:
movable app
package Lab4; import java.util.Scanner; public class MovableApp { public static void main(String [] args){ int n =0, numIn = 0, enter = 0, entVal = 0 ; Scanner input = new Scanner(System.in);//inputs System.out.println("Do you want to Exit? \n press 1 to enter\n press anyother number to exit"); entVal = input.nextInt(); if (entVal==1){ //creating objects Car carObj = new Car(); Ship ShipObj = new Ship(); Plane Planeobj = new Plane(); System.out.println("which vehicle would you like to choose?"); System.out.println("enter 1 for Car \n enter 2 for plane \n enter 3 for ship \n enter 4 to exit "); numIn = input.nextInt();//input to enter program switch (numIn){ case 1: do{ System.out.println("you have chosen to play with car"); System.out.println("what value would you like to enter for moveForward"); n = input.nextInt(); carObj.moveForward(n); carObj.displayCoordinates(); //displays coordinates System.out.println("what value would you like to enter for moveBackward"); n = input.nextInt(); carObj.moveBackward(n); carObj.displayCoordinates(); System.out.println("what value would you like to enter for moveLeft"); n = input.nextInt(); carObj.moveLeft(n); carObj.displayCoordinates(); System.out.println("what value would you like to enter for moveRight"); n = input.nextInt(); carObj.moveRight(n); carObj.displayCoordinates(); System.out.println("Would you like to move forward?"); System.out.println("enter 1 to continue \n enter 0 to exit"); enter = input.nextInt(); } while (enter != 0 || enter > 1 ); break; case 2: do{ System.out.println("you chose plane"); System.out.println("what value would you like to enter moveForward"); n = input.nextInt(); Planeobj.moveForward(n); Planeobj.displayCoordinates(); System.out.println("what value would you like to enter moveBackward"); n = input.nextInt(); Planeobj.moveBackward(n); Planeobj.displayCoordinates(); System.out.println("what value would you like to enter moveLeft"); n = input.nextInt(); Planeobj.moveLeft(n); Planeobj.displayCoordinates(); System.out.println("what value would you like to enter to moveRight"); n = input.nextInt(); Planeobj.moveRight(n); Planeobj.displayCoordinates(); System.out.println("Would you like to continue?"); System.out.println("enter 1 to continue \n enter 0 to exit"); enter = input.nextInt(); } while (enter != 0 || enter > 1 ); break; case 3: do{ System.out.println("you chose ship"); System.out.println("what value would you like to enter for moveForward"); n = input.nextInt(); ShipObj.moveForward(n); ShipObj.displayCoordinates(); System.out.println("what value would you like to enter moveBackward"); n = input.nextInt(); ShipObj.moveBackward(n); ShipObj.displayCoordinates(); System.out.println("what value would you like to enter moveLeft"); n = input.nextInt(); ShipObj.moveLeft(n); ShipObj.displayCoordinates(); System.out.println("what value would you like to enter moveRight"); n = input.nextInt(); ShipObj.moveRight(n); ShipObj.displayCoordinates(); System.out.println("Would you like to continue?"); System.out.println("enter 1 to continue \n enter 0 to exit"); enter = input.nextInt(); }while(enter !=0 || enter >1); break; case 4: return; } }else if (entVal != 1){ return ; } }}
car class:
package Lab4; public class Car implements Movable{ //implements Movable interface int x,y; public Car(){ //makes constructor this(0,0); } public Car(int x, int y) {//overloaded constructor with two ints this.x = x; //makes the cordinates int x and y equals fields x and y. this.y = y; } @Override public void moveForward() { this.x += 1; } @Override public void moveForward(int x) { this.x += x; //invokes x and adds the x to the user input of the previous value } @Override public void moveBackward() { this.x -= 1; } @Override public void moveBackward(int x) { this.x -=x; } @Override public void moveLeft() { this.y -=1; } @Override public void moveLeft(int y) { this.y -=y; } @Override public void moveRight() { this.y += 1; } @Override public void moveRight(int y) { this.y += y; } @Override public void displayCoordinates() { System.out.println(x +" "+ y); } }
plane class:
package Lab4; public class Plane implements Movable{//implements movable interface int x,y; //declares x and y as the fields public Plane(){ this(0,0); //makes coordinates to (0,0) } public Plane(int x, int y) { this.x = x; this.y = y; } @Override //calls methods of movable interface public void moveForward() { this.x += 1; } @Override public void moveForward(int x) { this.x += x; } @Override public void moveBackward() { this.x -=1; } @Override public void moveBackward(int x) { this.x -=x; } @Override public void moveLeft() { this.y -=1; } @Override public void moveLeft(int y) { this.y -=y; } @Override public void moveRight() { this.y -=1; } @Override public void moveRight(int y) { this.y -=y; } @Override public void displayCoordinates() {//displays coordinates System.out.println(x + " " + y); } }
ship class:
package Lab4; public class Ship implements Movable{//implements movable interface int x,y; public Ship(){ this(0,0); } public Ship(int x, int y) { this.x = x; this.y = y; } @Override public void moveForward() { this.x += 1; } @Override public void moveForward(int x) { this.x += x; } @Override public void moveBackward() { this.x -=1; } @Override public void moveBackward(int x) { this.x -= x; } @Override public void moveLeft() { this.y -=-1; } @Override public void moveLeft(int y) { this.y -= y; } @Override public void moveRight() { this.y += 1; } @Override public void moveRight(int y) { this.y += y; } @Override public void displayCoordinates() { System.out.println(x + " " + y); } }
screenshots: