Lab 4

We are given the following interface “Movable”
Based on the Movable interface, we create three classes: Car, Plane, and Ship that implement it. After that, we create a program that will similarly process an array of movable. we implement data structures to make the three classes to either move forward/backward or left/right.

package lab4;

public class 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();

}

}

package lab4;
import java.util.Scanner;
import lab4.movable.Movable;

public class MovableApp {
 static Movable a;
 private static Scanner input;
 private static Scanner input2;
 private static Scanner input3;
 public static void main(String[] args) {
  System.out.println("Please select an option:");
  System.out.println("1. Car");
  System.out.println("2. Plane");
  System.out.println("3. Ship");
  System.out.println("4. Exit");
  int option = getMenuInput();
  System.out.println("Option selected: "+ option);
  a = createObjects(option);  
  a.displayCoordinates();
  do{
   System.out.println("Please select");
   System.out.println("1. moveForward");
   System.out.println("2. moveForward(n)");
   System.out.println("3. moveBackward");
   System.out.println("4. moveBackward(n)");
   System.out.println("5. moveLeft");
   System.out.println("6. moveLeft(n)");
   System.out.println("7. moveRight");
   System.out.println("8. moveRight(n)");
   System.out.println("9. exit");
   int option1 = getMoveInput();
   performAction(option1);
   a.displayCoordinates();
   System.out.println("\n");
  }while(true);
 }
 static Movable createObjects(int option){
  Movable a = null ;
  switch (option) {
  case 1:
   a = new car();
   break;
  case 2:
   a = new plane();
   break;
  case 3:
   a = new ship();
   break;
  }
  return a;
 }
 static void performAction(int option){
  int n =0;
  switch (option) {
  case 1:
   a.moveForward();
   break;
  case 2:
   n = getN();
   a.moveForward(n);
   break;
  case 3:
   a.moveBackward();
   break;
  case 4:
   n = getN();
   a.moveBackward(n);
   break;
  case 5:
   a.moveLeft();
   break;
  case 6:
   n = getN();
   a.moveLeft(n);
   break;
  case 7:
   a.moveRight();
   break;
  case 8:
   n = getN();
   a.moveRight(n);
   break;
  case 9:
   System.exit(0);
  } 
 }
 static int  getN(){
  System.out.println("Please n number of steps to move: ");
  input = new Scanner(System.in);
  return Integer.parseInt(input.nextLine());
 }
 static int getMenuInput() {
  int option = 0;
  boolean inputError = true;
  input2 = new Scanner(System.in);
  do {
   try {
    option = Integer.parseInt(input2.nextLine());
    if (option 4) 
     throw new Exception();
    else
     inputError = false;
   } catch (Exception e) {
    System.out.println("Error! Please enter valid number between 1 and 4");
    input2.reset();
   }
  } while (inputError);
  // Exit system if user choose exit
  if ((option == 4)) System.exit(0);
  return option;
 }

 static int getMoveInput() {
  int option = 0;
  boolean inputError = true;
  input3 = new Scanner(System.in);
  do {
   try {
    option = Integer.parseInt(input3.nextLine());
    if (option 9) 
     throw new Exception();
    else
     inputError = false;
   } catch (Exception e) {
    System.out.println("Error! Please enter valid number between 1 and 9");
    input3.reset();
   }
  } while (inputError);
  // Exit system if user choose exit
  if ((option == 9)) System.exit(0);
  return option;
 }
}

package lab4;

import lab4.movable.Movable;

public class car implements Movable {
int x;
int y;

public car() {
x =0;
y=0;

}
public car(int x, int y) {
this.x = x;
this.y = y;

}
@Override
public void moveForward() {
x++;
System.out.println("Moving "+toString()+" forward by X = 1");

}
@Override
public void moveForward(int c) {
x= x+c;
System.out.println("Moving "+toString()+" forward by X = "+c);

}
@Override
public void moveBackward() {
x--;
System.out.println("Moving "+toString()+" backward by X = -1");

}
@Override
public void moveBackward(int c) {
x=x-c;
System.out.println("Moving "+toString()+" backward by X = -"+c);

}
@Override
public void moveLeft() {
y--;
System.out.println("Moving "+toString()+" backward by Y = -1");

}
@Override
public void moveLeft(int c) {
y=y-c;
System.out.println("Moving "+toString()+" backward by Y = -"+c);

}
@Override
public void moveRight() {
y++;
System.out.println("Moving "+toString()+" backward by Y = 1");

}
@Override
public void moveRight(int c) {
y=y+c;
System.out.println("Moving "+toString()+" backward by Y = "+c);

}
@Override
public void displayCoordinates() {
System.out.println(toString()+"'s position X:"+x+ " Y:" +y);

}
@Override
public String toString(){
return "Car";

}
}

package lab4;

import lab4.movable.Movable;

public class plane implements Movable {
int x;
int y;

public plane() {
x =0;
y=0;

}
public plane(int x, int y) {
this.x = x;
this.y = y;

}
@Override
public void moveForward() {
x++;
System.out.println("Moving "+toString()+" forward by X = 1");

}
@Override
public void moveForward(int c) {
x= x+c;
System.out.println("Moving "+toString()+" forward by X = "+c);

}
@Override
public void moveBackward() {
x--;
System.out.println("Moving "+toString()+" backward by X = -1");

}
@Override
public void moveBackward(int c) {
x=x-c;
System.out.println("Moving "+toString()+" backward by X = -"+c);

}
@Override
public void moveLeft() {
y--;
System.out.println("Moving "+toString()+" backward by Y = -1");

}
@Override
public void moveLeft(int c) {
y=y-c;
System.out.println("Moving "+toString()+" backward by Y = -"+c);

}
@Override
public void moveRight() {
y++;
System.out.println("Moving "+toString()+" backward by Y = 1");

}
@Override
public void moveRight(int c) {
y=y+c;
System.out.println("Moving "+toString()+" backward by Y = "+c);

}
@Override
public void displayCoordinates() {
System.out.println(toString()+"'s position X:"+x+ " Y:" +y);

}
@Override
public String toString(){
return "Plane";

}
}

package lab4;

import lab4.movable.Movable;

public class ship implements Movable {
int x;
int y;

public ship() {
x=0;
y=0;

}
public ship(int x, int y) {
this.x = x;
this.y = y;

}

@Override
public void moveForward() {
x++;
System.out.println("Moving "+toString()+" forward by X = 1");

}
@Override
public void moveForward(int c) {
x= x+c;
System.out.println("Moving "+toString()+" forward by X = "+c);

}
@Override
public void moveBackward() {
x--;
System.out.println("Moving "+toString()+" backward by X = -1");

}
@Override
public void moveBackward(int c) {
x=x-c;
System.out.println("Moving "+toString()+" backward by X = -"+c);

}
@Override
public void moveLeft() {
y--;
System.out.println("Moving "+toString()+" backward by Y = -1");

}
@Override
public void moveLeft(int c) {
y=y-c;
System.out.println("Moving "+toString()+" backward by Y = -"+c);

}
@Override
public void moveRight() {
y++;
System.out.println("Moving "+toString()+" backward by Y = 1");

}
@Override
public void moveRight(int c) {
y=y+c;
System.out.println("Moving "+toString()+" backward by Y = "+c);

}
@Override
public void displayCoordinates() {
System.out.println(toString()+"'s position X:"+x+ " Y:" +y);

}
@Override
public String toString(){
return "Ship";

}
}

1

2

3

plane1

plane2

ship1

ship2

Leave a Reply

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