In today's lab you will learn about
Class variables
Accessor and mutator methods
More practice with "for" loops
Multiple constructors
Printing to the console
A robot has been tipped off about some hidden treasure and the secret to finding it. The robot can find the treasure by walking forward until it finds a pile of rocks (Things). The number of rocks in the pile tells the robot which way to turn (more precisely, how many turns to make before moving forward again). If the robot keeps walking forward and turning the specified direction it will eventually find the treasure.
This program requires 2 class, PathFollower.java (your Robot), and TreasureMap.java, the driver.
Create a PathFollower robot that inherits from RobotSE
Instance Variable
Give your robot an instance variable, turnsMade, that records how many times the robot has made a turn. What should the access level of this variable be? Create an accessor and mutator for this variable.
follow Method:
Create a method, follow, that takes 1 parameter, "paces".
This tells the robot how many total moves (forward, not including
turns) it needs to make to find the treasure. The robot should move
forward, subtracting 1 from paces each time it moves.
Whenever
it comes to a Thing, the number of Things on the ground tell the
robot how many times it should turn right. For example, if there's 1
Thing on the ground, the robot should turn right once then keep
moving forward. If there's 2, it should turn right twice then keep
moving forward. There could be any number of things on the ground,
but the robot can get an accurate count and know the exact number by
picking them up one at a time. Once the robot knows the exact number
of turns to make, what kind of control flow structure should we use
to make it turn that number of times? Don't forget to update
turnsMade by adding 1 each time it comes to an intersection where it
must turn.
The robot knows it has reached the treasure (and can therefore stop) when it has moved the specified number of paces (remember, you should be checking and updating the number of paces each time you move!).
Now create a driver to test your PathFollower. The picture below shows a sample City (the number of Things on each intersection is labeled in the picture, but you're not required to put the labels):
You can download this example City here.
The robot can start on a Thing but is not required to. As an example, if you have the City shown above and you call follow(13), the robot should turn right (because it starts on 1 Thing), move forward to intersection (0,5), where it turns right once more, move forward to intersection (6,5), where it turns right 3 times, then move forward again, but come to a stop at intersection (6,7), because it has moved its 13 paces.
After calling follow print out a statement that describes what the robot did. Using the example above:
The robot found the treasure at intersection (6,7) and turned 2 times to get there.
Files need for this lab:
PathFollower.java - The robot
TreasureMap.java - The driver
As always, the source code must be indented properly and contain comments that describe important parts of the algorithm. The use of proper indentation is considered good programming style, and you are expected to use good programming style.