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

Please help with the following IN Java code the following

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.

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.

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 if you want.

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.

how it should look like

Please help with the following IN Java code the following Class Robot

A robot has a name and moves in a 2-dimensional plane (square

Console X Robot Tester (Java Application] C:\Program Files Javajrel.8.0_12 ------ Robot Walk What is the name of your robot? R2 C2 D2 What is the size of your grid? 5 Time for R2 C2 D2 to start walking!!!! At start R2 C2 D2 is facing E and at position (0,0) -=> Number of steps to take 4. Result: R2 C2 D2 is facing E and at position (4,0) ==> Number of steps to take 1. Result: R2 C2 D2 is facing E and at position (5,0) ==> Number of steps to take 3. New direction: N Result: R2 C2 D2 is facing N and at position (5,3) --> Number of steps to take 2. Result: R2 C2 D2 is facing N and at position (5,5) R2 C2 D2 reached its final destination in 4 moves. Console X RobotTester [Java Application] C:\Program Files Java\jre1.8.0_121\bin\j 0 = = = = = = = 0 Robot Walk 11 = = = = = = = = what is the name of your robot? Pink Elephant What is the size of your grid? 4 Time for Pink Elephant to start walking!!!! At start Pink Elephant is facing E and at position (0,0) ==> Number of steps to take 2. Result: Pink Elephant is facing E and at position (2,0) ==> Number of steps to take 1. Result: Pink Elephant is facing E and at position (3,0) ==> Number of steps to take 2. New direction: N Result: Pink Elephant is facing N and at position (4,1) ==> Number of steps to take 2. Result: Pink Elephant is facing N and at position (4,3) ==> Number of steps to take 4. New direction: W Result: Pink Elephant is facing W and at position (1,4) ==> Number of steps to take 3. New direction: S Result: Pink Elephant is facing 5 and at position (0,2) ==> Number of steps to take 2. Result: Pink Elephant is facing 5 and at position (0,0) ==> Number of steps to take 3. New direction: E Result: Pink Elephant is facing E and at position (3,0) ==> Number of steps to take 3. New direction: N Result: Pink Elephant is facing N and at position (4,2) ==> Number of steps to take 2. Result: Pink Elephant is facing N and at position (4,4) Pink Elephant reached its final destination in 10 moves

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!