Given the following interface Movable():
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();
}
We have to create three three called Car(), Plane(), and Ship() each of these classes will implement the movable interface.
Each class must have its own private field so that the default coordinates would be set, x= 0, and y=0. (0,0). each class must also define its own direction. The movefoward() method would add 1 to the x field while the move backward() method would subtract one from the x field. The user must be allowed to change the coordination to any number at any time and the final coordination should be displayed every time the user change the coordination.
source code:
package Interfaces; //interfaces for the movement public interface Movable { public void moveForward(); public void moveForward(int y); public void moveBackward(); public void moveBackward(int y); public void moveLeft(); public void moveLeft(int x); public void moveRight(); public void moveRight(int x); public void displayCoordinates(); }
package Interfaces; public class Car implements Movable{ int x=0; int y=0; //setting the coordinate t oh default value of (0,0) public Car(int x, int y) { this.x=x; this.y=y; } //getter method for x public int getx(int p) { return x; } //setter method for x public void setx(int p) { x=p; } //getter method for y public int gety(int s) { return y; } //setter method for y public void sety(int s) { y=s; } public Car() { // TODO Auto-generated constructor stub } @Override public void moveForward(int y){ System.out.println("moving forward"); y=y+y; } @Override public void moveRight(int x){ System.out.println("moving right"); x=x+x; } @Override public void moveLeft(int x){ System.out.println("moving left"); x=x+x; } @Override public void moveBackward(int y){ System.out.println("moving Backwards"); y=y+y; } public void moveForward() { // TODO Auto-generated method stub } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } @Override public void displayCoordinates() { System.out.println("The coordinate now is :" + "(" + x + "," + y + ")"); } }
package Interfaces; public class Plane implements Movable { int x=0; int y=0; public Plane(int x, int y) { this.x=x; this.y=y; } public int getx(int p) { return x; } public void setx(int p) { x=p; } public int gety(int s) { return y; } public void sety(int s) { y=s; } public Plane() { // TODO Auto-generated constructor stub } @Override public void moveForward(int y){ System.out.println("moving forward"); } @Override public void moveRight(int x){ System.out.println("moving right"); } @Override public void moveLeft(int x){ System.out.println("moving left"); } @Override public void moveBackward(int y){ System.out.println("moving backwards"); } @Override public void moveForward() { // TODO Auto-generated method stub } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } @Override public void displayCoordinates() { System.out.println("The coordinate now is :" + "(" + x + "," + y + ")"); } }
package Interfaces; public class Ship implements Movable{ int x=0; int y=0; //setting the coordinate t oh default value of (0,0) public Ship (int x, int y) { this.x=x; this.y=y; } //getter method for x public int getx(int p) { return x; } //setter method for x public void setx(int p) { x=p; } //getter method for y public int gety(int s) { return y; } //setter method for y public void sety(int s) { y=s; } public Ship() { // TODO Auto-generated constructor stub } @Override public void moveForward(int y){ System.out.println("moving forward"); y=y+y; } @Override public void moveRight(int x){ System.out.println("moving right"); x=x+x; } @Override public void moveLeft(int x){ System.out.println("moving left"); x=x+x; } @Override public void moveBackward(int y){ System.out.println("moving Backwards"); y=y+y; } public void moveForward() { // TODO Auto-generated method stub } @Override public void moveBackward() { // TODO Auto-generated method stub } @Override public void moveLeft() { // TODO Auto-generated method stub } @Override public void moveRight() { // TODO Auto-generated method stub } @Override public void displayCoordinates() { System.out.println("The coordinate now is :" + "(" + x + "," + y + ")"); } }
package Interfaces; import java.util.Scanner; import java.util.Scanner; public class MovableApp { //main program public static void main(String[] args) { System.out.println("What do you want to control?"); System.out.println("Press 1 for car"); System.out.println("Press 2 for Plane"); System.out.println("Press 3 for Ship"); Scanner input= new Scanner(System.in); int choice= input.nextInt(); Movable[] move= new Movable[1]; // adding a new object from the mobavle interface called move Car run = new Car();//making a new object from the class car Plane run2 = new Plane();//making a new object from the class plane Ship run3 = new Ship();//making a new object from the class plane while(true) { if (choice== 1)//start the car { System.out.println("You have chosen a car."); System.out.println("Now type the coordinations"); int p=input.nextInt();//p varaible for the forward motion int s=input.nextInt();//s variable for the backward motion if (s >0){ run.moveForward(s); // call the moveforward method } if (s >0){ run.moveRight(p); // call the moveright method } if (p> 0){ run2.moveForward(s); // call the moveforward method } if (s >0){ run2.moveRight(p);// call the moveright method } if (p <0){ run3.moveForward(s); // call the moveforward method } if (s> 0){ run3.moveRight(p);// call the moveright method } if (p <0){ run3.moveLeft(p);// call the moveleft method } p=run3.x+p; s=run3.y+s; move[0]= new Ship(p,s);//add the coordinates to the move object of the Movable interface run3.x=p; run3.y=s; } for (Movable moving : move){ moving.displayCoordinates(); //involve the displayCoordinate method from either the car class, the plane or the ship class } System.out.println("Press 1 to change coordinate."); System.out.println("Press 2 to exit."); int time=input.nextInt(); if (time==2){ System.exit(0);//exit the program if 2 is pressed. } } } }