Question

In: Computer Science

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 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

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Robot.java

package c14;

public class Robot {
   private int x;
   private int y;
   private String direction;
   public Robot() {
       this.x = 0;
       this.y = 0;
       this.direction = "east";
   }
   public Robot(int x, int y, String direction) {
       super();
       this.x = x;
       this.y = y;
       this.direction = direction;
   }
   public int getX() {
       return x;
   }
   public void setX(int x) {
       this.x = x;
   }
   public int getY() {
       return y;
   }
   public void setY(int y) {
       this.y = y;
   }
   public String getDirection() {
       return direction;
   }
   public void setDirection(String direction) {
       this.direction = direction;
   }
  
   void forward(int delta) {
       if(direction.equals("east")) {
           x+=delta;
       }else if(direction.equals("west")) {
           x-=delta;
       }else if(direction.equals("north")) {
           y-=delta;
       }else if(direction.equals("south")) {
           y+=delta;
       }
   }
  
   public static void main(String[] args) {
       Robot robot = new Robot();
       robot.forward(20);
       robot.setDirection("north");
       robot.forward(30);
       robot.setDirection("west");
       robot.forward(40);
       System.out.println("Final x is : "+robot.getX());
       System.out.println("Final y is : "+robot.getY());
       System.out.println("Final direction is : "+robot.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...
[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...
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