The irresistable source.

Lab 4

Description: This is a Java application to demonstrate the use of interfaces. The classes: Car, Plane, and Ship each implement the Movable interface; the application reads input from the user and assigns the Movable reference to a Car, Plane or Ship at runtime. This allows the main function to use the common functionality of the objects without the need to know whether it is dealing with a Car, a Plane, or a Ship object.

Code:

package jwarren.lab4;

import java.util.Scanner;

/**
 * Demonstrates use of the Movable interface.
 * 
 * @author Jason Warren
 * 
 */
public class MovableApp {

	private enum MenuOption {
		CAR(1), PLANE(2), SHIP(3), EXIT(4);

		private int value;

		MenuOption(int v) {
			value = v;
		}

		public int getValue() {
			return value;
		}

		public static int valueOf(MenuOption m) {

			int v = m.ordinal() + 1;

			return v;
		}

		public static MenuOption fromValue(int v) {

			MenuOption choice;

			switch (v) {
			case 1:
				choice = CAR;
				break;
			case 2:
				choice = PLANE;
				break;
			case 3:
				choice = SHIP;
				break;
			case 4:
				choice = EXIT;
				break;
			default:
				choice = null;

			}

			return choice;
		}
	}

	public static void main(String args[]) {

		boolean done = false;
		boolean validInput = false;
		Movable vehicle = null;
		MenuOption inputOption = null;
		String inputString = null;
		int xDestination = 0;
		int yDestination = 0;
		Scanner in = new Scanner(System.in);

		do {
			System.out.println("How would you like to travel?");
			System.out.println(MenuOption.valueOf(MenuOption.CAR) + " - Car");
			System.out.println(MenuOption.valueOf(MenuOption.PLANE)
					+ " - Plane");
			System.out.println(MenuOption.valueOf(MenuOption.SHIP) + " - Ship");
			System.out.println(MenuOption.valueOf(MenuOption.EXIT)
					+ " - Exit Program");
			System.out.print(">");

			validInput = false;
			while (!validInput) {
				try {
					inputOption = MenuOption.fromValue(in.nextInt());
					if (inputOption != null) {
						validInput = true;
					}

					else {
						System.err.println("Selection must be between: "
								+ MenuOption.CAR.getValue() + "-"
								+ MenuOption.EXIT.getValue());
						validInput = false;
					}
				} catch (Exception e) {
					System.err.println("Please enter a valid integer.");
					in.next();
					validInput = false;
				}
			}

			switch (inputOption) {
			case CAR:
				vehicle = new Car();
				break;
			case PLANE:
				vehicle = new Plane();
				break;
			case SHIP:
				vehicle = new Ship();
				break;
			default:
				// Exit
				done = true;
			}

			if (!done) {
				vehicle.displayCoordinates();
				System.out.println();
				System.out.println("Enter position to travel to :");
				validInput = false;
				while (!validInput) {
					try {
						inputString = in.next();
						xDestination = Integer.parseInt(inputString.substring(
								0, inputString.indexOf(",")));
						yDestination = Integer.parseInt(inputString.substring(
								inputString.indexOf(",") + 1,
								inputString.length()));
						validInput = true;
					}

					catch (Exception e) {
						System.err
								.println("Please enter input in form of: \"x,y\"");
						validInput = false;
					}
				}
				for (int i = 0; i < Math.abs(xDestination); i++) {

					// Moving in negative x direction.
					if (xDestination < 0) {
						vehicle.moveBackward();
					}

					// Moving in positive x direction.
					else {
						vehicle.moveForward();
					}

					vehicle.displayCoordinates();
					System.out.println();
				}

				for (int i = 0; i < Math.abs(yDestination); i++) {

					// Moving in negative y direction.
					if (yDestination < 0) {
						vehicle.moveLeft();
					}

					// Moving in positive y direction.
					else {
						vehicle.moveRight();
					}

					vehicle.displayCoordinates();
					System.out.println();
				}
			}
			System.out.println();
		} while (!done);

		System.out.println("Goodbye.");

		in.close();
	}
}
package jwarren.lab4;

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 jwarren.lab4;

/**
 * Class to simulate a Car with a two dimensional position.
 * 
 * @author Jason Warren
 *
 */
public class Car implements Movable {

	public int x;
	public int y;

	/**
	 * Creates a new Car at coordinates (0,0).
	 */
	public Car(){

		setPosition(0,0);
	}

	/**
	 * Creates a new Car at the coordinates (x,y).
	 * 
	 * @param x		Position of this Car on the x axis.
	 * @param y		Position of this Car on the y axis.
	 */
	public Car(int x, int y){

		setPosition(x,y);
	}

	@Override
	public void moveForward() {

		x++;		
	}

	@Override
	public void moveForward(int x) {

		for(int i=0; i<this.x; i++){
			moveForward();
		}

	}

	@Override
	public void moveBackward() {

		x--;
	}

	@Override
	public void moveBackward(int x) {

		for(int i=0; i<x; i++){
			moveBackward();
		}
	}

	@Override
	public void moveLeft() {

		y--;		
	}

	@Override
	public void moveLeft(int y) {

		for(int i=0; i<y; i++){
			moveLeft();
		}
	}

	@Override
	public void moveRight() {

		y++;
	}

	@Override
	public void moveRight(int y) {

		for(int i=0; i<y; i++){
			moveRight();
		}
	}

	@Override
	public void displayCoordinates() {

		System.out.print("Car is at position: (" + x + "," + y + ")");
	}

	/**
	 * Sets this car's position to (x,y).
	 * 
	 * @param x		Position on the x axis to set.
	 * @param y		Position on the y axis to set.
	 */
	private void setPosition(int x, int y){

		this.x = x;
		this.y = y;
	}

}
package jwarren.lab4;

/**
 * Class to implement a simple plane.
 * 
 * @author Jason Warren
 *
 */
public class Plane implements Movable{

	int x;
	int y;

	public Plane(){

		setPosition(0,0);
	}

	public Plane(int x, int y){

		setPosition(x,y);
	}

	@Override
	public void moveForward() {

		x++;		
	}

	@Override
	public void moveForward(int x) {

		for(int i=0; i<this.x; i++){
			moveForward();
		}

	}

	@Override
	public void moveBackward() {

		x--;
	}

	@Override
	public void moveBackward(int x) {

		for(int i=0; i<x; i++){
			moveBackward();
		}
	}

	@Override
	public void moveLeft() {

		y--;		
	}

	@Override
	public void moveLeft(int y) {

		for(int i=0; i<y; i++){
			moveLeft();
		}
	}

	@Override
	public void moveRight() {

		y++;
	}

	@Override
	public void moveRight(int y) {

		for(int i=0; i<y; i++){
			moveRight();
		}
	}

	@Override
	public void displayCoordinates() {

		System.out.print("Plane is at position: (" + x + "," + y + ")");
	}

	/**
	 * Sets this Plane's position to (x,y).
	 * 
	 * @param x		Position on the x axis to set.
	 * @param y		Position on the y axis to set.
	 */
	private void setPosition(int x, int y){

		this.x = x;
		this.y = y;
	}

}
package jwarren.lab4;

/**
 * Class to implement a simple Ship.
 * 
 * @author Jason Warren
 *
 */
public class Ship implements Movable{

	int x;
	int y;

	public Ship(){

		setPosition(0,0);
	}

	public Ship(int x, int y){

		setPosition(x,y);
	}

	@Override
	public void moveForward() {

		x++;		
	}

	@Override
	public void moveForward(int x) {

		for(int i=0; i<this.x; i++){
			moveForward();
		}

	}

	@Override
	public void moveBackward() {

		x--;
	}

	@Override
	public void moveBackward(int x) {

		for(int i=0; i<x; i++){
			moveBackward();
		}
	}

	@Override
	public void moveLeft() {

		y--;		
	}

	@Override
	public void moveLeft(int y) {

		for(int i=0; i<y; i++){
			moveLeft();
		}
	}

	@Override
	public void moveRight() {

		y++;
	}

	@Override
	public void moveRight(int y) {

		for(int i=0; i<y; i++){
			moveRight();
		}
	}

	@Override
	public void displayCoordinates() {

		System.out.print("Ship is at position: (" + x + "," + y + ")");
	}

	/**
	 * Sets this Ship's position to (x,y).
	 * 
	 * @param x		Position on the x axis to set.
	 * @param y		Position on the y axis to set.
	 */
	private void setPosition(int x, int y){

		this.x = x;
		this.y = y;
	}

}

 

Screenshots:

screenshot

Leave a Reply

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