CS46A Lab

Decisions

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.

Work together as a group of four to solve the programming problem in part A. Your group should have the following roles:

  1. The facilitator makes sure that everyone plays their roles and keeps the discussion moving, staying within the time for each step. The facilitator also reports the group results back to the instructor during debriefing.
  2. The driver enters code into the computer and executes it.
  3. The simulator simulates relevant parts of a program (such as a robot, a drawing, or the state of objects or variables), by moving a token on a board, making a drawing, or updating the values of variables. The simulator should only simulate the steps that the director (see below) specifically requests. The simulator is also the scribe for the group.
  4. The director directs the simulator, calling out the next action to be simulated (such as "turn left" or "change n to 10"). The director can also ask questions such as "is there a wall to the right?" or "what is the value of n"? The director should not look at the board or work sheet that the simulator keeps.

You will not always play these roles. During general discussion, everyone participates.

A. Maze Escape

  1. [1 Minute]. Assign group roles. The first person in the group assignment list is the facilitator, the second the driver, the third the simulator, and the fourth the director.
  2. [10 minutes] Simulator: Start a report page and add the names and roles of your group, like this: "Name1 (Facilitator), Name2 (Driver), Name3 (Director), your name (Simulator). Use first and last names.

    Driver: 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 robot1 directory. Run the project to make sure that it works. If it doesn't, ask the instructor or lab assistant for help.

    Everyone: 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, dropped a beeper just outside the entrance.

  3. 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.isOverBeeper()){
       step(); // YOU are implementing this method
       if (!carol.rightHasWall()) 
       {
          carol.say("Whoa! There isn't a wall to the right any more!");
       }
    }

    Here, ! is the “not” operator: While Carol is not over a beeper, call the step method.

    Your step method should enforce the "Right Hand on Wall" strategy. At the end of the step method, the robot should have a wall to the right or be over a beeper. That way, the robot will go from one "Right Hand on Wall" position to the next "Right Hand on Wall" position, until it reaches the beeper. Keep in mind that the robot should always have a wall to the right or be over a beeper when the step method returns.

    Oh, and it is important that, when the step method returns, the robot should either be on top of a beeper or have 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 group 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 to the right or a beeper below when the step method returns.

    Simulator: 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.

    Director: Draw this pattern on a sheet of paper and obtain a token for the robot—something with a direction such as a paper clip.

    Everybody: Review your group's pseudocode and check that it reflects your understanding of the discussion.

  4. Execute your pseudocode [15 minutes]. Follow these instructions. The director 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 director reads off the next statement. If it involves Carol, the director tells the simulator what Carol should do, such as "move forward" or "is there a wall in front?" (Remember, no peeking by the director.) The simulator 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 is on a beeper.

    If you find any mistakes in the pseudocode, stop the simulation and as a group discuss how the pseudocode needs to be improved.

    As the simulation progresses, the driver translates the pseudocode instructions into Java and enters them into the step method of the MyScene class, under the line marked // Your work here

  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.
  6. Debriefing [15 minutes]. The facilitator reports (a) the key steps of the algorithm (how to turn in each of the various cases), as originally conceived (b) what changes needed to be made during development and (c) whether the final algorithm enabled Carol to escape

Disband your group of four and work together as a group of two to solve the programming problem in parts B, C and D.

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;
    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.

  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?

D. 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.

D. The Genetic Code

  1. The genetic code is the encoding of DNA or RNA into proteins. Each codon (sequence of three compounds with abbreviations A C G U) encodes one of twenty amino acids (with abbreviations Phe, Leu, etc.), according to the following scheme:

    Your task is to complete the methods isLeu and isIle in this class. As in the preceding assignment, your methods should compute and return a single Boolean expression, like this:

    public boolean isPhe()
    {
       return bases.startsWith("UU") && "UC".contains(bases.substring(2));
    }

    What is the code for your methods?

  2. Run this test class. Did you pass the tests on first try? If not, what modifications did you have to make to your code?