CS46A Lab


Copyright Cay S. Horstmann 2009 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

A. Maze Escape

  1. Continue with the Robot project from the previous lab. If you don't already have it, download this project and unzip it somewhere. Be sure it is actually extracted. By default, Windows only gives you the illusion of extracting it. Start NetBeans. In NetBeans, select File->Open Project and select the extracted 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.

  2. General Discussion [15 minutes]: Your task is to design and implement a method
    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.

  3. Execute your pseudocode [15 minutes]. Follow these instructions. The driver places a marker (such as a coin or paperclip) next to the currently executed statement on the pseudocode sheet, starting with the first line of the 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.

  4. Write the actual code [5 minutes]. The driver translates the pseudocode instructions into Java and enters them into a step method of the Scene class. Then change myFirstMethod to contain
    while (carol.rightHasWall())
    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);        
  5. Execute the actual code [10 minutes]. The driver compiles the code. Fix any syntax errors as a group. Watch Carol. Does she escape? If not, why not?

B. Vowels

  1. In English, the letters a e i o u y denote vowels. Complete the isVowel method in this class that should return true if letter consists of a single letter that is a vowel. What is your method?
  2. Run the main method in WordTester.java. Did all tests pass? If not, what did you need to do to fix your isVowel method.
  3. Look inside your isVowel method. Inexperienced programmers commonly write code of the form
    if (some condition)
       return true;
    else if (some other condition)
       return true;
       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.

  4. In this situation, one can implement 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?
  5. Which implementation do you prefer? The one with the if statements or the one with contains? Why?

C. Patterns

  1. This class calls a method
    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)

  2. And for this? (Scribe)

  3. And for this? (Driver)

  4. And for this? (Scribe)

  5. And for this? (Driver)

  6. And for this? (Scribe)

  7. And for this? (Driver)

  8. And for this? (Scribe)

  9. Together come up with an interesting pattern of your own and attach a screen capture.