This lab has been adapted from a lab authored by Kent Beck at SDSU.
Carol the robot moves along the lines of a grid. Here, we denote her position with an arrow whose tip points to the direction in which she moves. (This is just for diagramming purposes. You'll see later how she actually looks.)
She can carry out the following five operations:
However, if you try to move Carol into a wall, she “crashes” (turns black and ceases to function). Don't do this.
true
if
there is a wall or directly in front of the robot, false
otherwise. (Her front sensor is a touch sensor that can't tell the
difference between a wall or window)true
if there is a
wall directly to the right of the robot, false
otherwise. (Her
side sensor is an optical sensor that can't tell the difference between a
window and empty space.) Carol has been placed into a rectangular room with a single window. The width and height of the room, the position of the window, and Carol's position can be arbitrary. The following program has been designed to position Carol next to the window.
The program is presented in flowchart notation.
1) Main program
Here, the hexagons denote method calls. The flowcharts for the methods are below:
2) “Go to wall” method:
3) “Find window” method:
The scribe calls out the steps from the flowchart. The driver moves the robot on a sheet of paper, following the instructions given by the scribe.
Question: In step 1, you should have successfully completed the program. (That is, Karel should have found the window and stopped beside it.) However, there can be many different starting situations—that is, different positions for the window and different starting locations for Caroll. There is at least one starting situation for which the program does not work correctly. Can you find it? Is there more than one such problem situation? (Try different locations for the window and different starting positions for Karel. Use pencil and paper for sketches!)
robot
directory. Run the project to make sure that it works. If it doesn't, ask
the instructor or lab assistant for help.
What does the program do?
performCustomSetup
method
calls makeRoom
. Modify the call to makeRoom
so
that the window is on the left wall. How did you do that? (Note: The x-axis
points towards you, and the y-axis points to your right.)makeRoom
so that the window is on one of
the problem situations you identified in part A. What is the call that you
used?findWindow
method. Apply the fix
that you identified in part A. What is your fix in Java code?