Instructions: You are given the following interface Movable(): public interface
ID: 3554447 • Letter: I
Question
Instructions:
You are 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();
}
Create three classes Car(), Plane(), and Ship() where each one will implement the Movable interface as follows:
After that, create a program MovableApp that 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.
Explanation / Answer
import java.util.Scanner;
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();
}
class Car implements Movable{
int X=0;
int Y=0;
public Car(int x, int y) {
this.X = x;
this.Y = y;
}
public Car() {
}
@Override
public void moveForward() {
X +=1;
}
@Override
public void moveForward(int x) {
X +=x;
}
@Override
public void moveBackward() {
X -=1;
}
@Override
public void moveBackward(int x) {
X -=x;
}
@Override
public void moveLeft() {
Y -=1;
}
@Override
public void moveLeft(int y) {
Y -=y;
}
@Override
public void moveRight() {
Y +=1;
}
@Override
public void moveRight(int y) {
Y +=y;
}
@Override
public void displayCoordinates() {
System.out.println(X + "," + Y);
}
}
class Plane implements Movable{
int X=0;
int Y=0;
public Plane(int x, int y) {
this.X = x;
this.Y = y;
}
public Plane() {
}
@Override
public void moveForward() {
X +=1;
}
@Override
public void moveForward(int x) {
X +=x;
}
@Override
public void moveBackward() {
X -=1;
}
@Override
public void moveBackward(int x) {
X -=x;
}
@Override
public void moveLeft() {
Y -=1;
}
@Override
public void moveLeft(int y) {
Y -=y;
}
@Override
public void moveRight() {
Y +=1;
}
@Override
public void moveRight(int y) {
Y +=y;
}
@Override
public void displayCoordinates() {
System.out.println(X + "," + Y);
}
}
class Ship implements Movable{
int X=0;
int Y=0;
public Ship(int x, int y) {
this.X = x;
this.Y = y;
}
public Ship() {
}
@Override
public void moveForward() {
X +=1;
}
@Override
public void moveForward(int x) {
X +=x;
}
@Override
public void moveBackward() {
X -=1;
}
@Override
public void moveBackward(int x) {
X -=x;
}
@Override
public void moveLeft() {
Y -=1;
}
@Override
public void moveLeft(int y) {
Y -=y;
}
@Override
public void moveRight() {
Y +=1;
}
@Override
public void moveRight(int y) {
Y +=y;
}
@Override
public void displayCoordinates() {
System.out.println(X + "," + Y);
}
}
public class MovableApp{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean done = false;
System.out.println("Select from Car, Plane or Ship");
String inp = keyboard.nextLine();
inp=inp.trim();
if(inp.equalsIgnoreCase("Car")){
Car car = new Car();
while (!done) {
System.out.println("Type a number for X coordinate:");
int entryX = keyboard.nextInt();
System.out.println("Type a number for Y coordinate:");
int entryY = keyboard.nextInt();
// code for point 3 of question
car.moveForward(entryX);
car.moveRight(entryY);
car.displayCoordinates();
System.out.print("More moves? ");
String ans = keyboard.nextLine();
if (!ans.equalsIgnoreCase("yes"))
done = true;
}
}
}
}
Your requirement of MovableApp is not clear.
Rest is done....
Feel free to ask in case of any doubt...
Result....
Select from Car, Plane or Ship
Car
Type a number for X coordinate:
5
Type a number for Y coordinate:
8
5,8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.