Skip to content

Categories:

Lab 4

In this lab we were given a “Movable():” interface and we had to create three classes (“Car”,”Plane”, “Ship”) that will implement it. After we create these three classes we then had to create a “MoveableApp” that allows the user to change the coordinate. New coordinates will be displayed every time a change occurs. This lab uses the process we know as Polymorphism. Polymorphism in this lab is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters.

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();
}

MovableApp

import java.util.InputMismatchException;
import java.util.Scanner;

public class MovableApp {

	/**
	 * @param args command line arguments
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String vehicle;
		int x = 0;
		int y = 0;
		
		while(true) {
			System.out.println("Application Menu");
			System.out.println("----------------");
			System.out.println("c - choose car");
			System.out.println("p - choose plane");
			System.out.println("s - choose ship");
			System.out.println("q - quit the application");
			System.out.println("\nChoose any vehicle to move...");
			
			vehicle = input.nextLine().trim().toLowerCase();
			if(vehicle.equals("c") || vehicle.equals("p") || vehicle.equals("s")) {
				System.out.print("Do you want to initialize the position ? (y/n) : ");
				String ans = input.nextLine().trim().toLowerCase();
				if(ans.equals("y")) {
					while(true){
						System.out.println("Input the initial position : ");
						try{
							System.out.print("x position : ");
							x = input.nextInt();
							System.out.print("y position : ");
							y = input.nextInt();
							break;
						}catch(InputMismatchException e){
							System.out.println("Your input is wrong... Please enter integer values");
						}
					}
					if(vehicle.equals("c")) {
						doMove(new Car(x,y));
					}else if(vehicle.equals("p")){
						doMove(new Plane(x,y));
					}else {
						doMove(new Ship(x,y));
					}
				}else if(ans.equals("n")) {
					if(vehicle.equals("c")) {
						doMove(new Car());
					}else if(vehicle.equals("p")){
						doMove(new Plane());
					}else {
						doMove(new Ship());
					}
				}else {
					System.out.println("Wrong input: vehicle is not initialized... starts in default position(0,0)");
					if(vehicle.equals("c")) {
						doMove(new Car());
					}else if(vehicle.equals("p")){
						doMove(new Plane());
					}else {
						doMove(new Ship());
					}
				}
			}else if(vehicle.equals("q")) {
				System.out.println("exiting the program...");
				System.exit(0);
			}else {
				System.out.println("wrong input. Please select a correct menu...");
				continue;
			}
		}
	}
	
	private static void doMove(Movable vehicle) {
		Scanner input = new Scanner(System.in);
		String movement= null;
		
		System.out.println("Now we are going to move your " + vehicle);
		System.out.println("Application Menu");
		System.out.println("----------------");
		System.out.println("l - left");
		System.out.println("r - right");
		System.out.println("f - forward");
		System.out.println("b - backward");
		System.out.println("q - exit");
		System.out.print("Example of command :");
		System.out.println("if you type;\nl - moves one to left\nf - moves one to forward\nl5 - moves 5 to left\nf5 - moves 5 to forward");
		
		while(true) {
			System.out.print("input movement : ");
			movement = input.nextLine().trim().toLowerCase();
			if( movement!= null) {
				if(movement.startsWith("f")) {
					if(movement.length() > 1) {
						try{
							int moveNumber = Integer.parseInt(movement.substring(1));
							vehicle.moveForward(moveNumber);
						}catch(NumberFormatException e) {
							System.out.println("Wrong input number...");
						}
					}else {
						vehicle.moveForward();
					}
				}else if(movement.startsWith("b")) {
					if(movement.length() > 1) {
						try{
							int moveNumber = Integer.parseInt(movement.substring(1));
							vehicle.moveBackward(moveNumber);
						}catch(NumberFormatException e) {
							System.out.println("Wrong input number...");
						}
					}else {
						vehicle.moveBackward();
					}
				}else if(movement.startsWith("l")) {
					if(movement.length() > 1) {
						try{
							int moveNumber = Integer.parseInt(movement.substring(1));
							vehicle.moveLeft(moveNumber);
						}catch(NumberFormatException e) {
							System.out.println("Wrong input number...");
						}
					}else {
						vehicle.moveLeft();
					}
				}else if(movement.startsWith("r")) {
					if(movement.length() > 1) {
						try{
							int moveNumber = Integer.parseInt(movement.substring(1));
							vehicle.moveRight(moveNumber);
						}catch(NumberFormatException e) {
							System.out.println("Wrong input number...");
						}
					}else {
						vehicle.moveRight();
					}
				}else if(movement.startsWith("q")) {
					System.out.println("System is exiting...");
					System.exit(0);
				}else {
					System.out.println("wrong input argument");
				}	
			}else {
				System.out.println("Wrong input...");
			}
			vehicle.displayCoordinates();
		}
	}
}

Car


public class Car implements Movable{
	
	int x_position,y_position;
	
	public Car() {
		x_position = 0;
		y_position = 0;
	}
	
	public Car(int x_position,int y_position){
		this.x_position = x_position;
		this.y_position = y_position;
	}

	@Override
	public void displayCoordinates() {
		System.out.println("X Coordinate : " + x_position + "\nY Coordinate : " + y_position);
	}

	@Override
	public void moveBackward() {
		x_position -= 1;
	}

	@Override
	public void moveBackward(int x) {
		x_position -= x;
	}

	@Override
	public void moveForward() {
		x_position += 1;
	}

	@Override
	public void moveForward(int x) {
		x_position += x;
	}

	@Override
	public void moveLeft() {
		y_position -= 1;
	}

	@Override
	public void moveLeft(int y) {
		y_position -= y;
	}

	@Override
	public void moveRight() {
		y_position += 1;
	}

	@Override
	public void moveRight(int y) {
		y_position += y;
	}
	
	@Override
	public String toString() {
		return "car";
	}
}

Ship


public class Ship implements Movable {

	int x_position,y_position;

	public Ship() {
		x_position = 0;
		y_position = 0;
	}
	
	public Ship(int x_position,int y_position){
		this.x_position = x_position;
		this.y_position = y_position;
	}
	
	@Override
	public void displayCoordinates() {
		System.out.println("X Coordinate : " + x_position + "\nY Coordinate : " + y_position);
	}

	@Override
	public void moveBackward() {
		x_position -= 1;
	}

	@Override
	public void moveBackward(int x) {
		x_position -= x;
	}

	@Override
	public void moveForward() {
		x_position += 1;
	}

	@Override
	public void moveForward(int x) {
		x_position += x;
	}

	@Override
	public void moveLeft() {
		y_position -= 1;
	}

	@Override
	public void moveLeft(int y) {
		y_position -= y;
	}

	@Override
	public void moveRight() {
		y_position += 1;
	}

	@Override
	public void moveRight(int y) {
		y_position += y;
	}
	
	@Override
	public String toString() {
		return "ship";
	}
}

Plane


public class Plane implements Movable {

	int x_position,y_position;

	public Plane() {
		x_position = 0;
		y_position = 0;
	}
	
	public Plane(int x_position,int y_position){
		this.x_position = x_position;
		this.y_position = y_position;
	}
	
	@Override
	public void displayCoordinates() {
		System.out.println("X Coordinate : " + x_position + "\nY Coordinate : " + y_position);
	}

	@Override
	public void moveBackward() {
		x_position -= 1;
	}

	@Override
	public void moveBackward(int x) {
		x_position -= x;
	}

	@Override
	public void moveForward() {
		x_position += 1;
	}

	@Override
	public void moveForward(int x) {
		x_position += x;
	}

	@Override
	public void moveLeft() {
		y_position -= 1;
	}

	@Override
	public void moveLeft(int y) {
		y_position -= y;
	}

	@Override
	public void moveRight() {
		y_position += 1;
	}

	@Override
	public void moveRight(int y) {
		y_position += y;
	}
	
	@Override
	public String toString() {
		return "plane";
	}
}

Screenshots

lab4screenshot

lab4screenshot


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.