Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with the following: IN JAVA Class Robot A robot has a name and moves in a 2-dimensional plane (square grid) in a specified

Please help with the following: IN JAVA

Class Robot

A robot has a name and moves in a 2-dimensional plane (square grid) in a specified direction. It starts at location (0,0) facing East and moves a randomly generated number of steps each turn until it reaches the top right corner of the grid, whose coordinates will be N x N, where N is the size of the square grid.

The (x,y) coordinates are to be modified as follows (see figure 1): - If robot is facing E, it will move right, so the x coordinate is increased. - If the robot is facing W, it will move left, so the x coordinate is decreased. - If the robot is facing N, it will move up, so the y coordinate is increased. - If the robot is facing S, it will move down, so the y coordinate is decreased.

Figure 1: Grid robot must move in if a 5x5 grid

The robot cannot walk off the grid. So when it reaches the edge of the grid during any of its turns it needs to change directions and complete the remaining number of steps in the new direction.

Implement the class Robot based on the following specification. The class has

Four instance variables:

name that holds the name of the robot (which can be more than one word long) x and y which are the integer coordinates of the robots location (between 0 and N, where n is the size of the grid) direction a character indicating the direction the robot is facing (possible values E, W, N and S)

3 constructors: Default constructor: set name to noName, direction to E and both position coordinates to 0.

One parameter constructor: sets the name to the passed name, direction to E and both position coordinates to 0. Copy Constructor Accessor methods for each of the attributes getName() getDirection()

getX() getY()

Mutator method setName() a void method which sets the name of the object to the passed String no mutator methods for the x and y attributes or direction attribute. The only way these attributes can be changes is with the methods move() and changeDirection(), which are described below.

toString()toreturnarobotsinformationinthefollowingformat: name is facing direction and at position (x,y) where the words in italics and green are the values stored in the corresponding attributes of the object.

  • equals() to return true if the position and direction of two objects are the same, false otherwise.
  • changeDirection() a void method which changes the direction of the robot as follows (assigns a new value to the attribute direction): if it is facing E, it changes to N if it is facing W, it changes to S if it is facing N, it changes to W if it is facing S, it changes to E
  • move() which has 2 parameters: the first specifies the numbers of steps the robot must take, and the second specifies the size of the grid. This method moves the robot the specified number of steps. It must make sure that the robot does not move off the grid. Here are a few examples for a robot moving on a 5 x 5 grid to illustrate the general behaviour:
    • - if the robot is at position 0,0 and facing N and needs to move 4, after the call to the method move(), it will be at position 0,4 still facing N

won()is a boolean method which determines if a robot has reached the top left right corner of the grid once it has completed a turn. This method receives an an integer parameter which represents the size of the square grid. The method returns true if both the x and y coordinates of the robot are equal to the size of the grid, false otherwise.

These methods must be in implemented in your class, and work as described above. You can add other public or private methods

Driver to test Class Robot Write a java program using the Robot class from part a) of this question, to simulate the walking of a robot

from the bottom left hand size of a NxN grid to the top right hand side of this grid.

Your program must prompt the user for the name of the robot (which can be longer than 1 word) and the size of the grid. It then randomly generates the number of steps (between 1 and N) and moves the robot based on the behaviour described in the class until it reaches its final destination of (N, N).

Trace the behaviour of the robot for each turn by displaying the number of steps it needs to walk, the direction changes and the coordinates of its position.

Once the robot has reached its final destination, write a closing message specifying the number of turns it took the robot to reach its final destination. Be sure to include its name in the message. Even though they are robots, they are sensitive and gets offended if we dont address them with their name

Your driver must have 3 static methods:

a void method to display the header. a method which prompts the user for the size of the grid, reads it and returns the inputted value. This method requires the Scanner object as a parameter. A void method which displays the closing message. This method requires 2 parameters: the object and the number of turns.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started