ECE 2036                                                                                                                   Spring 2015

Lab 1: Robot Motion

Assigned: January 12, 2015                                                   Section B: Due January 22, 2015

Section A: Due January 23, 2015

 

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.

You will help create the class Robot.  Your class will have two private data members: xPosition and yPosition.  Your class will also have several public member functions for manipulating objects of the class: setXPosition(int), setYPosition(int), getXPosition(), getYPosition(), moveForward(int), moveLeft(int), moveRight(int), moveBackward(int), inputMove() and displayPosition().  In addition, your class will have a constructor which initializes xPosition and yPosition to zero. 

In the main() function of the code, you should create one instantiation (object) of Robot, with object name myRobot.  The code should prompt the user to input the next move of the robot, using F, B, L, and R to represent forward, backward, left, and right respectively, and an integer to represent the distance moved.  For example, entering ‘F 3’ would cause the robot to move forward 3 units.  This should appear on the screen as:

Please enter the direction the robot should move (F, B, L, or R), followed by the integer distance: F 3

Robot is moving forward 3 units

The robot never turns, so F, B, L, and R are fixed directions at all times.  Assume forward and backward refer to moves on the y-axis, while right and left refer to moves on the x-axis.  After each move, your code must update the position of the robot and print it to the screen.  It must output this result to the screen as:

Robot is located at x = 0, y = 3

the code then loops back to prompt the next move.

 

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 xPosition and yPosition to zero.

2.  setXPosition(int), setYPosition(int), getXPosition(), getYPosition(): Member functions of class Robot to set and get the values of xPosition and yPosition.

3.  moveForward(int), moveBackward(int), moveLeft(int), moveRight(int):  Member functions of class Robot to move the robot forward, backward, left, or right by an integer number of units, by calling setXPosition and setYPosition to update the robot’s position.

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.

6.  In the main() function,  the code must instantiate an object of class Robot, and display the starting position on the screen.  The main() function must then start an infinite loop.  Inside the loop, main() must first call a member function of Robot in order to prompt the user to input the next move of the robot and execute that move.  The main() function must then call a member functions of Robot in order to display the robot’s new position, and then go back to the start of the loop.

You must use the skeleton code given to you in class as the basis for your code.   As part of this lab, it is your responsibility to manually type in this skeleton code.  Do not copy this from another source!  There are various places in the code where the comments indicate an intentionally placed syntax error or missing code.  You should correct the errors so indicated, and insert more code where indicated in order to ‘flesh out’ the skeleton and obtain the functionality delineated above. Modify and complete the skeleton code only as indicated in the skeleton code comments.

As shown in the skeleton code, you will include implementations of the member functions outside the class definition.  However, your entire code can be contained in a single file.  (In the future, implementations will generally be contained in separate files.)

Sample output from completed code

Robot is located at x = 0, y = 0

 

Please enter the direction the robot should move (F, B, L, or R),

followed by the integer distance: F 3

Robot is moving forward 3 units

Robot is located at x = 0, y = 3

 

Please enter the direction the robot should move (F, B, L, or R),

followed by the integer distance: R 10

Robot is moving right 10 units

Robot is located at x = 10, y = 3

 

Please enter the direction the robot should move (F, B, L, or R),

followed by the integer distance: L 6

Robot is moving left 6 units

Robot is located at x = 4, y = 3

 

Please enter the direction the robot should move (F, B, L, or R),

followed by the integer distance: B 1

Robot is moving backward 1 units

Robot is located at x = 4, y = 2