Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab we will be creating a C++ class, and an object of that class. Classes can be used to represent anything: cars, gradebooks,

image text in transcribedimage text in transcribedimage text in transcribed

In this lab we will be creating a C++ class, and an object of that class. Classes can be used to represent anything: cars, gradebooks, students, professors, or in this case, robots. For this lab, you will create the class Robot. However, one robot by itself is not very interesting so you will also be expected to create a class called Robot Swarm that will manage a group of Robot objects, letting them move around within a grid without colliding with one another. As a final piece, you will also maintain a record of each Robot object's "history" - the locations it touched on the grid and the number of times it touched the points on the grid. You will write this history out to a file for each Robot. The learning objectives of this lab give you practice: 1. Using basic C++ classes; 2. Creating basic vectors of user-defined objects; 3. Creating constructors and overloading constructors; 4. Using and creating set and get member functions in C++ classes; 5. Using C++ string objects; 6. Using basic text file I/O objects. You will create the class Robot. Your class will have two private data members: Position and y Position. Your class will also have an additional data member, a two-dimensional array (your choice of C-style array or a C++ vector of vector of integers) for tracking each time the Robot touches a particular position. Your class will also have several public member functions for manipulating objects of the class: setXPosition (int) setyPosition (int) getXPosition() getyPosition() moveForward() moveLeft () moveRight() moveBackward() inputMove () displayPosition() printHistory () In addition, your class will have a constructor which initializes xPosition and y Position to zero. One robot working by itself is not very interesting though so you will be expected to also design a class called Robot Swarm. The constructor of the Robot Swarm will accept a single integer argument that represents the number of Robot objects that need to be created. The constructor will create that many Robot objects as a vector of Robot objects. When each Robot object is created, you must decide where to place each Robot object in a 32 by 32 grid, i.e. xPosition must be bound between 0 and 31, inclusive, and y Position must be bound between 0 and 31, inclusive. Provide these limits using either #define or global constant variables. (This will largely depend on your two-dimensional array implementation.) You will also want to make sure that each Robot object resides on its own position, i.e. no position is occupied by more than one Robot object at a time. The Robot Swarm class will have a public function moveSwarm that will have each Robot object randomly select to move one space in one of the four directions (move Forward(), moveLeft(), moveRight(), moveBackward(). Before permitting a Robot object to move in the selected direction, first make sure that no other Robot in the swarm occupies the intended space. If another Robot already occupies that space, have the current Robot object select another direction randomly. If the Robot object cannot select a position to move after 5 random selections, do not move that Robot object, and move on to the next one. After all Robot objects in the swarm have at least attempted to move, let the move Swarm function return. The Robot Swarm class will have another public function printHistory that will call the printHistory function of each of its Robot objects. In the main() function of the code, you should create one instantiation (object) of Robot Swarm, with object name mySwarm. The code should prompt the user asking for the number of times the swarm needs to move. After the swarm has moved the specified number of times, the main() function should call printHistory for my Swarm and then exit. DETAILED LAB PROGRAMMING REQUIREMENTS Your code must include the following: 1. A constructor for class Robot with no arguments. The constructor must initialize the values of Position and yPosition to zero. It must also initialize the two- dimensional integer array for tracking its positions. 2. setXPosition (int), setyPosition (int), getXPosition(), getyPosition (): Member functions of class Robot to set and get the values of xPosition and y Position. moveForward(), moveBack(), moveLeft(), moveRight(): Member functions of class Robot to move the robot forward, backward, left, or right by one integer unit, by calling setXPosition and setyPosition to update the robot's position. It should also increment the position in the Robot object's history array. NOTE: You do not need to worry about the orientation of the Robot in space, i.e. assume the Robot object only faces one direction the entire time. 4. inputMove (): A member function of class Robot to prompt the user for the next move of the robot, and call the move functions accordingly. 5. displayPosition (): A member function of class Robot to call getXPosition() and getyPosition() and display the x and y position of the robot on the screen. This function will be useful for debugging your code. 6. printHistory (): Prints the history of the positions for the Robot in the 32x32 grid of integers to a file. For simplicity, this can be a single file that each Robot object simply appends to. A constructor for class Robot Swarm. The constructor will take a single integer argument denoting the number of Robot objects that need to be created. It will also set the x and y positions of each Robot object randomly, making sure that no two Robot objects occupy the same spot. 8. moveSwarm: Moves each Robot object in a randomly selected direction according to the above instructions. For testing purposes, you can allow the random selections to use the default seed, but for the final submission, your code should seed the random number generator using the current time, i.e. srand(time (NULL)); 9. printHistory: Calls the printHistory function of each Robot object. 10. In the main() function of the code, you should create one instantiation (object) of Robot Swarm, with object name mySwarm. The code should prompt the user asking for the number of Robot objects to create and the number of times the swarm needs to move. After the swarm has moved the specified number of times, the main() function should call printHistory for my Swarm and then exit. Your code should be in at least 5 separate files. Robot.h should contain only the class definition for your Robot class. Robot.cc should contain the implementations for all of the methods defined in Robot.h. RobotSwarm.h should contain only the class definition for your Robot Swarm class. RobotSwarm.cc should contain the implementations for all of the methods defined in RobotSwarm.cc. main.cc should contain your main method. You may want an additional file for any constant or global information that Robot, RobotSwarm, and main may need to be aware of. In this lab we will be creating a C++ class, and an object of that class. Classes can be used to represent anything: cars, gradebooks, students, professors, or in this case, robots. For this lab, you will create the class Robot. However, one robot by itself is not very interesting so you will also be expected to create a class called Robot Swarm that will manage a group of Robot objects, letting them move around within a grid without colliding with one another. As a final piece, you will also maintain a record of each Robot object's "history" - the locations it touched on the grid and the number of times it touched the points on the grid. You will write this history out to a file for each Robot. The learning objectives of this lab give you practice: 1. Using basic C++ classes; 2. Creating basic vectors of user-defined objects; 3. Creating constructors and overloading constructors; 4. Using and creating set and get member functions in C++ classes; 5. Using C++ string objects; 6. Using basic text file I/O objects. You will create the class Robot. Your class will have two private data members: Position and y Position. Your class will also have an additional data member, a two-dimensional array (your choice of C-style array or a C++ vector of vector of integers) for tracking each time the Robot touches a particular position. Your class will also have several public member functions for manipulating objects of the class: setXPosition (int) setyPosition (int) getXPosition() getyPosition() moveForward() moveLeft () moveRight() moveBackward() inputMove () displayPosition() printHistory () In addition, your class will have a constructor which initializes xPosition and y Position to zero. One robot working by itself is not very interesting though so you will be expected to also design a class called Robot Swarm. The constructor of the Robot Swarm will accept a single integer argument that represents the number of Robot objects that need to be created. The constructor will create that many Robot objects as a vector of Robot objects. When each Robot object is created, you must decide where to place each Robot object in a 32 by 32 grid, i.e. xPosition must be bound between 0 and 31, inclusive, and y Position must be bound between 0 and 31, inclusive. Provide these limits using either #define or global constant variables. (This will largely depend on your two-dimensional array implementation.) You will also want to make sure that each Robot object resides on its own position, i.e. no position is occupied by more than one Robot object at a time. The Robot Swarm class will have a public function moveSwarm that will have each Robot object randomly select to move one space in one of the four directions (move Forward(), moveLeft(), moveRight(), moveBackward(). Before permitting a Robot object to move in the selected direction, first make sure that no other Robot in the swarm occupies the intended space. If another Robot already occupies that space, have the current Robot object select another direction randomly. If the Robot object cannot select a position to move after 5 random selections, do not move that Robot object, and move on to the next one. After all Robot objects in the swarm have at least attempted to move, let the move Swarm function return. The Robot Swarm class will have another public function printHistory that will call the printHistory function of each of its Robot objects. In the main() function of the code, you should create one instantiation (object) of Robot Swarm, with object name mySwarm. The code should prompt the user asking for the number of times the swarm needs to move. After the swarm has moved the specified number of times, the main() function should call printHistory for my Swarm and then exit. DETAILED LAB PROGRAMMING REQUIREMENTS Your code must include the following: 1. A constructor for class Robot with no arguments. The constructor must initialize the values of Position and yPosition to zero. It must also initialize the two- dimensional integer array for tracking its positions. 2. setXPosition (int), setyPosition (int), getXPosition(), getyPosition (): Member functions of class Robot to set and get the values of xPosition and y Position. moveForward(), moveBack(), moveLeft(), moveRight(): Member functions of class Robot to move the robot forward, backward, left, or right by one integer unit, by calling setXPosition and setyPosition to update the robot's position. It should also increment the position in the Robot object's history array. NOTE: You do not need to worry about the orientation of the Robot in space, i.e. assume the Robot object only faces one direction the entire time. 4. inputMove (): A member function of class Robot to prompt the user for the next move of the robot, and call the move functions accordingly. 5. displayPosition (): A member function of class Robot to call getXPosition() and getyPosition() and display the x and y position of the robot on the screen. This function will be useful for debugging your code. 6. printHistory (): Prints the history of the positions for the Robot in the 32x32 grid of integers to a file. For simplicity, this can be a single file that each Robot object simply appends to. A constructor for class Robot Swarm. The constructor will take a single integer argument denoting the number of Robot objects that need to be created. It will also set the x and y positions of each Robot object randomly, making sure that no two Robot objects occupy the same spot. 8. moveSwarm: Moves each Robot object in a randomly selected direction according to the above instructions. For testing purposes, you can allow the random selections to use the default seed, but for the final submission, your code should seed the random number generator using the current time, i.e. srand(time (NULL)); 9. printHistory: Calls the printHistory function of each Robot object. 10. In the main() function of the code, you should create one instantiation (object) of Robot Swarm, with object name mySwarm. The code should prompt the user asking for the number of Robot objects to create and the number of times the swarm needs to move. After the swarm has moved the specified number of times, the main() function should call printHistory for my Swarm and then exit. Your code should be in at least 5 separate files. Robot.h should contain only the class definition for your Robot class. Robot.cc should contain the implementations for all of the methods defined in Robot.h. RobotSwarm.h should contain only the class definition for your Robot Swarm class. RobotSwarm.cc should contain the implementations for all of the methods defined in RobotSwarm.cc. main.cc should contain your main method. You may want an additional file for any constant or global information that Robot, RobotSwarm, and main may need to be aware of

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Explain the causes of indiscipline.

Answered: 1 week ago