Copyright © Cay S. Horstmann 2009
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
A. Maze Escape
robot
directory. Run the project to make sure that it works. If it doesn't, ask
the instructor or lab assistant for help.
Read the problem description below and the javadoc of the Robot class.
Carol the robot was imprisoned by the sinister Cardinal Mazerin, and now she must escape. Fortunately, she remembers the “right hand rule” for getting out of a maze: Always keep your right hand on a wall, and eventually you will reach the exit. (This rule works, provided all parts of the wall are connected.)
In order to help Carol escape, her faithful servant, the hunchback Quaternion, will open the window when Carol stops in front of it.
public void step()
The program calls the step
method in the following
pattern:
while (carol.rightHasWall())
{
step(); // YOU are implementing this method
}
carol.say("Whoa! There isn't a wall to the right any more!");
Your step
method should enforce the "Right Hand on Wall"
strategy. At the end of the step
method, the robot should have
a wall or window to the right. That way, the robot will go from one "Right
Hand on Wall" position to the next "Right Hand on Wall" position, until it
reaches a window. Keep in mind that the robot should always have a wall or
window to the right when the step
method returns.
Oh, and it is important that, when the step
method returns,
the robot should either have a window or a wall to the right. Did I mention
that? Just checking, because the first time I gave this lab, most students
ignored this and had poor Carol run all over the place.
Discuss as a team how this problem can be solved. Be sure only to use the methods of the Robot class, not any general intuition that you may have. What moves and turns does the robot make in which situations? You will probably find it helpful to consider different cases such as the following:
Keep in mind that the robot should always have a wall or window
to the right when the step
method returns.
Scribe: Write up on a sheet of paper what you agreed upon, using
pseudocode, i.e. a mixture of English and Java code. Your step
method should have several "if" clauses but no loops.
Driver: Draw this pattern on a sheet of paper and obtain a token for the robot—something with a direction such as a paper clip.
step
method. The scribe reads off the next statement. If
it involves Carol, the scribe tells the simulator what Carol should do,
such as "move forward" or "is there a wall in front?" The driver moves the
robot token and answers any questions ("no wall in front").
After a statement has been executed, move on to the next. At the end of
the step
method, go back to the beginning, except if
the robot faces the window.
If you find any mistakes in the pseudocode, stop the simulation and as a tean discuss how the pseudocode needs to be improved.
step
method of
the Scene
class. Then change myFirstMethod
to
contain
while (carol.rightHasWall())
{
step();
}
carol.say("Whoa! There isn't a wall to the right any more!");
Finally, change the first two lines of the
performCustomSetup
method to
// makeRoom(8, 6, 3, 6); makeMaze();
B. Vowels
isVowel
method in this class that
should return true
if letter
consists of a single
letter that is a vowel. What is your method?main
method in WordTester.java
. Did all tests pass? If
not, what did you need to do to fix your isVowel
method.isVowel
method. Inexperienced programmers
commonly write code of the form
if (some condition) return true; else if (some other condition) return true; else return false;
when instead they could simply write
return some condition || some other condition;
Can you rewrite your solution using a single return
statement? If so, give your code. If not, explain.
isVowel
without any
if
statement. Hint: Look at the API documentation of the
String
class and the contains
method. What is
your implementation of isVowel
using this method? if
statements or the one with contains
? Why?C. Patterns
public boolean fill(int row, int column) { return your expression here; }
for each row and column between 0 and 9, filling in squares when the
method returns true
.
Your task is to implement the fill
method in various ways
in order to generate different patterns. For example, if your
fill
method is
public boolean fill(int row, int column) { return !(row == 0 && column == 0); }
then all squares are filled in except the one at (0,0).
In the following exercises, you will be asked to generate various patterns. First write the Boolean expression on a sheet of paper. Driver and scribe take turns for each of the patterns. Then discuss whether your expression is correct. Finally, the driver types in the expression in the program, compiles, and runs. The scribe types in the expression in the report.
Your goal should be to get the correct result in one try. If you find yourself making random changes on the computer, take your hands off the keyboard and start thinking again!
Some of the patterns depend on whether the row or column are even or
odd. Remember that n % 2 == 0
is true whenever the integer
n
is even.
What is the expression that makes this picture? (Driver)