Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- In a comment, give a general explanation of what your program does. As the programming questions get more complex, the explanations will get lengthier.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

- In a comment, give a general explanation of what your program does. As the programming questions get more complex, the explanations will get lengthier. - Include comments in your program describing the main steps in your program. - Display a welcome message. - Display clear prompts for users when you are expecting the user to enter data from the keyboard. - All output should be displayed with clear messages and in an easy to read format. - End your program with a closing message so that the user knows that the program has terminate

Question 1- Class Robot & Driver to test it Part a) 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. 54 y-axis (0,4) 3 N -* 2 W E 1 S x-axis 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 robot's 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 String getName() .getDirection () getX() getY() Mutator method setName() a void method which sets the name of the object to the passed 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() to return a robot's information in the following format: 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 (figure 2). 0, 2 Figure 2: Move from (0,0) to (0,4) if the robot is at position (5,4) facing N (figure 3) and needs to move 3 steps, it can move 1 step North without falling of the grid, meaning that it is at location (5,5) (figure 4). It now needs to change directions to complete the remaining 2 steps. When facing N, it changes directions to W, meaning it will move left (decrease x position). So the final position is (3,5) and the robot is facing Wat the end of this turn (figure 5). y-axis (5,4) 2 1 x-axis Figure 3: Starting position, facing North y-axis (5,5) 2 1 X-axis 2 Figure 4: Moves 1 of 3 steps North, still has 2 steps to make. Changes directions. -axis (3,5) 1 x-axis Figure 5: Final position and facing West 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. Note: It is possible that you don't use all of the methods you have implemented in Robot in this assignment Part b) 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 I 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 don't 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. Here are a couple of sample outputs to illustrate the expected behaviour of your code (Figures 6 and 7). Console Number of steps to take 1. Result: R2 C202 is facing and at position (5,0) --> Number of steps to take 3. New direction: N Result: R2 C2D2 is facing and at position (5,3) -=> Number of steps to take 2. Result: R2 C2D2 is facing Nand at position (5,5) R2 C2 D2 reached its final destination in 4 moves. Figure 6-Sample output for a 5x5 grid Console 3 Robot Tester (Java Application] C:\Program Files Java\jrel 8.0_121\binj Robot Walk @-@-@-@ 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 (@,) --> 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 18 moves. Figure 7- Sample output for a 4x4 grid Question 2- Robot race Using the class Robot from Question 1 of this assignment, write a java program to simulate a robot race. Your program should ask the user the size of the grid, and the number of robots that will race, as well as their names. The robot objects must be stored in an array. The winner is the larobot to reach the top right corner of the grid. Your program must work for any size grid and any number of robots. Make sure the number of robots is at least 1 and that the grid size is at least 2x2. Make use of the static methods you wrote for the driver in Question 1. (Write a static method to read and validate the number of robots.) Here are a couple of screen shots to illustrate the expected behaviour of the race (Figures 8 & 9). Question 1- Class Robot & Driver to test it Part a) 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. 54 y-axis (0,4) 3 N -* 2 W E 1 S x-axis 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 robot's 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 String getName() .getDirection () getX() getY() Mutator method setName() a void method which sets the name of the object to the passed 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() to return a robot's information in the following format: 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 (figure 2). 0, 2 Figure 2: Move from (0,0) to (0,4) if the robot is at position (5,4) facing N (figure 3) and needs to move 3 steps, it can move 1 step North without falling of the grid, meaning that it is at location (5,5) (figure 4). It now needs to change directions to complete the remaining 2 steps. When facing N, it changes directions to W, meaning it will move left (decrease x position). So the final position is (3,5) and the robot is facing Wat the end of this turn (figure 5). y-axis (5,4) 2 1 x-axis Figure 3: Starting position, facing North y-axis (5,5) 2 1 X-axis 2 Figure 4: Moves 1 of 3 steps North, still has 2 steps to make. Changes directions. -axis (3,5) 1 x-axis Figure 5: Final position and facing West 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. Note: It is possible that you don't use all of the methods you have implemented in Robot in this assignment Part b) 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 I 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 don't 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. Here are a couple of sample outputs to illustrate the expected behaviour of your code (Figures 6 and 7). Console Number of steps to take 1. Result: R2 C202 is facing and at position (5,0) --> Number of steps to take 3. New direction: N Result: R2 C2D2 is facing and at position (5,3) -=> Number of steps to take 2. Result: R2 C2D2 is facing Nand at position (5,5) R2 C2 D2 reached its final destination in 4 moves. Figure 6-Sample output for a 5x5 grid Console 3 Robot Tester (Java Application] C:\Program Files Java\jrel 8.0_121\binj Robot Walk @-@-@-@ 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 (@,) --> 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 18 moves. Figure 7- Sample output for a 4x4 grid Question 2- Robot race Using the class Robot from Question 1 of this assignment, write a java program to simulate a robot race. Your program should ask the user the size of the grid, and the number of robots that will race, as well as their names. The robot objects must be stored in an array. The winner is the larobot to reach the top right corner of the grid. Your program must work for any size grid and any number of robots. Make sure the number of robots is at least 1 and that the grid size is at least 2x2. Make use of the static methods you wrote for the driver in Question 1. (Write a static method to read and validate the number of robots.) Here are a couple of screen shots to illustrate the expected behaviour of the race (Figures 8 & 9)

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

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

Explain the role of a support bond in a CMO structure. AppendixLO1

Answered: 1 week ago