Question

In: Computer Science

[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...

  1. [20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east".

Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields.

Create a method named forward that moves the robot along the current direction for the given distance. For example, if the robot is currently facing east, forward(10) will move the robot along the positive x direction for 10 pixels.

In the main method, create a Robot object, move it to the east for 20 pixels, then set the direction to north and move 30 pixels, and finally set the direction to west and move 40 pixels. Find and display the final x and y values and the direction of robot by calling its methods. For example, for the sample testing above, your program should display x = -20, y = -30, and direction = west.

Solutions

Expert Solution

Program Code [JAVA]

// Robot class

class Robot {

    // Required data fields

    private int position_x;
    private int position_y;
    private String direction;

    // no-arg constructor

    public Robot() {
        this.position_x = 0;
        this.position_y = 0;
        direction = "east";
    }

    // Parameterized constructor

    public Robot(int position_x, int position_y, String direction) {
        this.position_x = position_x;
        this.position_y = position_y;
        this.direction = direction;
    }

    // Accessors & Mutators

    public int getPosition_x() {
        return position_x;
    }

    public void setPosition_x(int position_x) {
        this.position_x = position_x;
    }

    public int getPosition_y() {
        return position_y;
    }

    public void setPosition_y(int position_y) {
        this.position_y = position_y;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    // forward method

    public void forward(int pixels) {

        // Checking for each direction

        if (direction.equalsIgnoreCase("east"))
            position_x += pixels;
        else if(direction.equalsIgnoreCase("west"))
            position_x -= pixels;
        else if(direction.equalsIgnoreCase("south"))
            position_y += pixels;
        else if(direction.equalsIgnoreCase("north"))
            position_y -= pixels;
    }
}

public class TestRobot {

    // main method

    public static void main(String[] args) {

        // Creating a Robot object

        Robot robot = new Robot();

        // Moving as stated in statement

        robot.setDirection("east");
        robot.forward(20);
        robot.setDirection("north");
        robot.forward(30);
        robot.setDirection("west");
        robot.forward(40);

        // Now printing final values of x, y and direction

        System.out.println("x = " + robot.getPosition_x() + ", y = " + robot.getPosition_y() +
                ", and direction = " + robot.getDirection());
    }
}


Sample Output:-

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields:...
[20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method...
5. [20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data...
5. [20 marks] (TestRobot.java) Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction number (0 for east, 1 for south, 2 for west, and 3 for north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = 0. Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors...
Design a class named Robot. The Robot class has three private data fields: position x, position...
Design a class named Robot. The Robot class has three private data fields: position x, position y, and the direction (east, south, west, and north). Assume that positive x points to the east and positive y points to the south, and initially x = 0, y = 0, direction = "east". Create a no-arg constructor and another constructor which sets the three data fields. Create the accessors and mutators for the three data fields. Create a method named forward that...
In java, design a class named Motorcycle that has the following fields: • year – The...
In java, design a class named Motorcycle that has the following fields: • year – The year field is an int that holds the motorcycle’s year • make – The make field references a String object that holds the make of the motorcycle. • speed – The speed field is an int that holds the motorcycle’s current speed The class should have the following constructor and other methods: • Constructor – the constructor should accept the motorcycle’s year and make...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT