lab 4 polymorphism

Description:
This lab was basically about movement. First we created 3 different classes and each class with two int fields to keep track of the coordinates x and y. The default coordinates for x and y was 0 and 0, however the user can change the values because an overloaded method was implemented in the codes which allow the user to change them. The movement of the object changes the coordinate’s one step at a time depending on the direction the user chooses. For example ( moveForward() will add 1 to x, moveLeft() will subtract 1 to y). After that we created a program called MovableApp which basically gives the user the menu table of what to choose for the choices plane, car, or ship. The program also displays a new coordinates every time there is a change. Also it is the program will run.

Codes:

MovableApp:
package lab4;

import java.util.Scanner;

import lab4.movable.Movable;

/**
* @author osasere osayande
*
*/
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;
}
}

Car:
package lab4;

import lab4.movable.Movable;

/**
* @author osasere osayande
*
*/
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";
}
}

Plane:
package lab4;

import lab4.movable.Movable;

/**
* @author osasere osayande
*
*/
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";
}

}
Ship:
package lab4;

import lab4.movable.Movable;

/**
* @author osasere osayande
*
*/
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";
}

}
Movable:
package lab4;

public class movable {

/**
* @author osasere osayande
*
*/
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();
}

}

ScreenShots:
lab 4 pic 1
lab 4 pic 2
lab 4 pic 3
lab 4 pic 4

Leave a Reply

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