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

//Robot class definition
class Robot{
    //private members variables
    private int posX; //posX for position x
    private int posY; //posY for position y
    private String direction; //direction for directions("north","south","east","west")

    //Default constructor
    Robot(){
        posX = 0; //initially posX = 0
        posY = 0; //initially posY = 0
        direction = "east"; //initially direction ="east"
    }

    //Parameterized constructor
    Robot(int posX, int posY, String direction){
        this.posX = posX;
        this.posY = posY;
        this.direction = direction;
    }

    //mutators for setting postion x
    public void setPosX(int posX){
        this.posX = posX;
    }

    //mutators for setting postion y
    public void setPosY(int posY){
        this.posY = posY;
    }

    //mutators for setting direction
    public void setDirection(String direction){
        this.direction = direction;
    }

    //accessors for getting position x
    public int getPosX(){
        return this.posX;
    }

    //accessors for getting position y
    public int getPosY(){
        return this.posY;
    }

    //accessors for getting direction
    public String getDirection(){
        return this.direction;
    }

    //forward method for moving positions
    public void forward(int pos){
        if(this.direction=="east"){
            posX+=pos; //if direction=="east" , move in positive x-direction
        }
        else if(this.direction=="west"){
            posX-=pos; //if direction=="west" , move in negative x-direction
        }
        else if(this.direction=="south"){
            posY+=pos; //if direction=="south" , move in positive y-direction
        }
        else if(this.direction=="north"){
            posY-=pos; //if direction=="north" , move in negative y-direction
        }

    }

}

//TestRobot class definition
class TestRobot{
    //program execution starts from here
    public static void main(String [] args) {
        //Robot class's object robot1 is declared
        Robot robot1 = new Robot(); //initially no arguments are passed , thus default constructor will be called

        //intial postion x = 0, y = 0,  direction = "east"
        //moving robot1 in positive x-direction 
        robot1.forward(20); //postion x = 20, y = 0,  direction = "east"

        robot1.setDirection("north"); //postion x = 20, y = 0,  direction = "north"

        //moving robot1 in negative y-direction 
        robot1.forward(30); //postion x = 20, y = -30,  direction = "north"

        robot1.setDirection("west"); //postion x = 20, y = -30,  direction = "west"

        //moving robot1 in negative x-direction 
        robot1.forward(40); //postion x = -20, y = -30,  direction = "west"

        //printing the required output
        System.out.println("x = "+robot1.getPosX()+", y = "+robot1.getPosY()+", and direction = "+robot1.getDirection()+".");
    }
}

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...
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...
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...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - 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....
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT