Lab 4

Description

Implement the given Movable() interface, and provide a track of the objects’ coordinates. User should be able to set the initial coordinate, and allows to move forward, backward, left, and right. Display new coordinate when there is a change to the coordinates.

Codes

Movable.java

package Lab4;

/**
 * abstract codes for Movable interface. Later will be implemented by car, plane, and ship.
 */
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();
}

Car.java

package Lab4;

/**
 * Implement the Movable interface for Car
 */
public class Car implements Movable {
    private int xAxis, yAxis;

    /**
     * initialize this class when crated by passing 2 params
     *
     * @param x int value for x-axis
     * @param y int value for y-axis
     */
    public Car(int x, int y) {
        xAxis = x;
        yAxis = y;
    }

    /**
     * Move car forward by increasing x-axis by 1
     */
    public void moveForward() {
        xAxis++;
    }

    /**
     * Move car forward by increasing x-axis's value by x
     *
     * @param x
     */
    public void moveForward(int x) {
        xAxis += x;
    }

    /**
     * Move car backward by decreasing x-axis by 1
     */
    public void moveBackward() {
        xAxis--;
    }

    /**
     * Move car backward by decreasing x-axis by x
     *
     * @param x int negative value
     */
    public void moveBackward(int x) {
        xAxis += x;
    }

    /**
     * Move car to the left by 1 unit
     * to do so, decrease y-axis by 1
     */
    public void moveLeft() {
        yAxis--;
    }

    /**
     * Move car to the left by y units
     * to do so, add this negative value to the y-axis
     *
     * @param y int negative value
     */
    public void moveLeft(int y) {
        yAxis += y;
    }

    /**
     * Move car to the right by 1 unit
     * add 1 to the y-axis
     */
    public void moveRight() {
        yAxis++;
    }

    /**
     * Move car to the right by y units
     * add y to the y-axis
     *
     * @param y int a number of units
     */
    public void moveRight(int y) {
        yAxis += y;
    }

    /**
     * Display current car's most recent coordinate
     */
    public void displayCoordinates() {
        System.out.printf("Car's Current Coordinate(%d,%d) \n", xAxis, yAxis);
    }

}

Plane.java

package Lab4;

/**
 * Implement the Movable interface for Plane
 */
public class Plane {
    private int xAxis, yAxis;


    /**
     * Default Constructor. Set all class fields to zero
     */
    public Plane() {
        xAxis = 0;
        yAxis = 0;
    }

    /**
     * Constructor with 2 params, set all class fields to user defines
     *
     * @param x int for x-axis
     * @param y int for y-axis
     */
    public Plane(int x, int y) {
        xAxis = x;
        yAxis = y;
    }

    /**
     * Move forward by 1 unit
     */
    public void moveForward() {
        xAxis++;
    }

    /**
     * Move forward by some units
     *
     * @param x int for x-axis
     */
    public void moveForward(int x) {
        xAxis += x;
    }

    /**
     * Move backward by 1 unit
     */
    public void moveBackward() {
        xAxis--;
    }

    /**
     * Move backward by some units
     *
     * @param x int (negative) for x-axis
     */
    public void moveBackward(int x) {
        xAxis += x;
    }

    /**
     * Move left by one unit
     */
    public void moveLeft() {
        yAxis--;
    }

    /**
     * Move left by some units
     *
     * @param y int (negative) for y-axis
     */
    public void moveLeft(int y) {
        yAxis += y;
    }

    /**
     * Move right by one unit
     */
    public void moveRight() {
        yAxis++;
    }

    /**
     * Move right by some units
     *
     * @param y int for y-axis
     */
    public void moveRight(int y) {
        yAxis += y;
    }

    /**
     * Display current coordinate
     */
    public void displayCoordinates() {
        System.out.printf("Plane's Current Coordinate(%d,%d) \n", xAxis, yAxis);
    }
}

Ship.java

package Lab4;

/**
 * Implement the Movable interface for Ship
 */
public class Ship {
    private int xAxis, yAxis;


    /**
     * Default constructor, set all private variables to zero
     */
    public Ship() {
        xAxis = 0;
        yAxis = 0;
    }

    /**
     * Constructor, set user defined values to the private field
     *
     * @param x int for x-axis
     * @param y int for y-axis
     */
    public Ship(int x, int y) {
        xAxis = x;
        yAxis = y;
    }

    /**
     * Move forward by one unit
     */
    public void moveForward() {
        xAxis++;
    }

    /**
     * Move forward by some units
     *
     * @param x int for x-axis
     */
    public void moveForward(int x) {
        xAxis += x;
    }

    /**
     * Move backward by one unit
     */
    public void moveBackward() {
        xAxis--;
    }

    /**
     * Move backward by some units
     *
     * @param x int (negative) for x-axis
     */
    public void moveBackward(int x) {
        xAxis += x;
    }

    /**
     * Move left by one unit
     */
    public void moveLeft() {
        yAxis--;
    }

    /**
     * Move left by some units
     *
     * @param y int (negative) for y-axis
     */
    public void moveLeft(int y) {
        yAxis += y;
    }

    /**
     * Move right by one unit
     */
    public void moveRight() {
        yAxis++;
    }

    /**
     * Move right by some units
     *
     * @param y int for y-axis
     */
    public void moveRight(int y) {
        yAxis += y;
    }

    /**
     * Display current coordinate
     */
    public void displayCoordinates() {
        System.out.printf("Ship's Current Coordinate(%d,%d) \n", xAxis, yAxis);
    }
}

MovableApp.java

package Lab4;

import java.util.Scanner;

/**
 * Lab 4
 * Implement the Movable interface to control the movement of 3 different transportation tools
 * car, plane, and ship. Extends the abstract methods to control the unit forward and backward
 * by the user. User should have the choice to enter how many number of moves before entering the coordinates.
 */
public class MovableApp {

    public static void main(String[] args) {
        Car car1;
        Plane plane1;
        Ship ship1;
        Scanner input = new Scanner(System.in);
        int transportTool, move, x, y;

        do {
            // init the objects for 3 modes of transportation
            car1 = new Car(0, 0);
            plane1 = new Plane(0, 0);
            ship1 = new Ship(0, 0);

            // ask user to choose the mode
            System.out.println("\nTransportation Tools:\n1) Car\n2) Plane\n3) Ship\n0) Quit");
            System.out.print("Choose your transportation tool: ");
            transportTool = input.nextInt();
            // quit program if user choose EXIT or QUIT
            if (transportTool == 0)
                return;
            // enter number of moves
            System.out.print("How many moves: ");
            move = input.nextInt();

            System.out.println("Enter your move");

            do {
                // enter x and y axis
                System.out.print("Enter your next coordinate, x and y:");
                x = input.nextInt();
                y = input.nextInt();

                switch (transportTool) {
                    case 1: // case 1 is car
                        if (x > 1) {
                            // move forward by x
                            car1.moveForward(x);
                        } else if (x == 1)
                            // move forward by 1
                            car1.moveForward();
                        else if (x == -1)
                            // move backward by -1
                            car1.moveBackward();
                        else if (x  0)
                            // move right by y
                            car1.moveRight(y);
                        else if (y == 1)
                            // move right by 1
                            car1.moveRight();
                        else if (y == -1)
                            // move left by 1
                            car1.moveLeft();
                        else if (y  1)
                            // move forward by x units
                            plane1.moveForward(x);
                        else if (x == 1)
                            // move forward by one unit
                            plane1.moveForward();
                        else if (x == -1)
                            // move backward by one unit
                            plane1.moveBackward();
                        else if (x  0)
                            // move right by y units
                            plane1.moveRight(y);
                        else if (y == 1)
                            // move right by one unit
                            plane1.moveRight();
                        else if (y == -1)
                            // move left by one unit
                            plane1.moveLeft();
                        else if (y  1)
                            // move forward by x units
                            ship1.moveForward(x);
                        else if (x == 1)
                            // move forward by one unit
                            ship1.moveForward();
                        else if (x == -1)
                            // move backward by one unit
                            ship1.moveBackward();
                        else if (x  0)
                            // move right by y units
                            ship1.moveRight(y);
                        else if (y == 1)
                            // move right by one unit
                            ship1.moveRight();
                        else if (y == -1)
                            // move left by one unit
                            ship1.moveLeft();
                        else if (y < -1)
                            // move left by y units
                            ship1.moveLeft(y);
                        // display coordinates
                        ship1.displayCoordinates();
                }
                // subtract move by 1
                move--;
            } while (move != 0); // end loop when move = 0

        } while (true);// program use if to kill the loop
    }
}

Screenshots

Leave a Reply

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