CS46A Lab

Loops

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 problems in parts A through C. 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.

Part A. Counting Syllables

  1. Assign group roles [1 minute]. The first person in the group assignment list is the driver, the second the simulator, the third the director, and the fourth the facilitator.

    Driver: Make a page with the title "Lab 8 name1 (Facilitator), name2 (Simulator), name3 (Director), your name (Driver). Use first and last names.

  2. Design a simple loop [5 minutes] As a warm-up exercise, complete the countVowels method in this class that counts the number of vowels in a word. Use the isVowel method to test whether a character is a vowel.

    As a group, decide on a strategy.

    Simulator: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code, then give it to the director.

    Driver: Make a sheet for tracing variables i, letter, and count, and give it to the simulator.

  3. Trace your pseudocode [5 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 countVowels method. The director reads off the next statement. If it involves updating a variable, the director tells the simulator what to do, such as “add 1 to i” or “set ch to the character with index 0”. If the director needs to know a variable's value, ask the simulator! (Remember, no peeking by the director. Everyone else should peek.) The simulator reveals and updates any variables.

    After a statement has been executed, move on to the next, until the loop ends.

    For this simulation, assume that the text instance variable has been initialized with the string "beauty".

    In this particular simulation, we will assume that the isVowel method works correctly. You need not trace inside it.

  4. Run your code [5 minutes] Driver: Translate the pseudocode into Java and add it to the Word class, using BlueJ. Run this WordTester1 class. Did it pass the tests?

    Simulator: While the driver is entering code, enter the pseudocode into the lab report.

    If not, discuss as a group what went wrong.

    Driver: Paste the code for the working countVowels method into the Google group.

  5. Design a more complex loop [10 minutes]. Everyone: Consider this problem. We want to count how many syllables a word has. For that, we need the number of vowel groups. Each vowel group is a sequence of adjacent vowels, delimited by non-vowels or the beginning/end of the word. For example, the word “beauty” has two vowel groups: “b eau t y”.

    As a group, decide on a strategy.

    Simulator: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code, then give it to the director.

    Everyone: Review the pseudocode to see that it reflects your group's intent.

    Driver: Make a sheet for tracing any variables used in your pseudocode and give it to the simulator.

  6. Trace your pseudocode [5 minutes]. Follow the same instructions as in step 3.

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

    Facilitator: Be prepared to report whether your group succeeded, what strategy you used in your algorithm, and what you learned from this exercise.

  7. Run your code [5 minutes]. Follow the same instructions as in Step 4, but use the WordTester2 class. Did it pass the tests? If not, discuss as a group what went wrong and find a fix for your error.

    Simulator: While the driver is entering code, enter the pseudocode into the lab report.

    Driver: Give the code for the working countVowels method to the simulator for inclusion in the lab report.

B. Drawing a Spiral

  1. [3 minutes] Everyone: On a sheet of paper (preferably graph paper), draw this spiral.

    I really mean everyone should draw the spiral. I want your brain and your fingers to connect...

  2. Develop an algorithm [10 minutes]. As a group, discuss how you can program the drawing of the spiral using Carol the Robot. Have Carol move and turn in a spiral pattern Use the dropBeeper method before every moveForward so that the beepers trace her path.

    Note that the length of the spiral arms follow a definite pattern. Find that pattern, then develop a strategy as a group. You will want to review the API of the Robot class to remind yourselves what a robot can do.

    Simulator: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code, then give it to the director.

    Everyone: Review the pseudocode to see that it reflects your group's intent.

    Driver: Make a sheet for tracing any variables used in your pseudocode and give it to the simulator.

  3. Execute your pseudocode [10 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. 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 drop a beeper. The simulator draws any beepers and Alice's new position on a sheet of graph paper. If any variables need to be updated, the director tells the simulator what to do, such as “add 1 to i”. If the director needs to know a variable's value, ask the simulator! (Remember, no peeking by the director. Everyone else should peek.) The simulator reveals and updates any variables.

    If you find any mistakes in the pseudocode, stop the simulation and as a group discuss how the pseudocode needs to be improved. (Note: A common mistake is to make the spiral too tight, so that the beepers simply fill the entire surface. Look at the picture above and notice how Carol drops two beepers for every square in the graph paper.)

    Facilitator: Be prepared to report whether your group succeeded, what strategy you used in your algorithm, and what you learned from this exercise.

  4. Run your code [10 minutes]. Driver: Unzip this file and open the contents as a Netbeans project. Now put your code into the run method of the MyScene class. Run the code. Does it produce the spiral pattern? If not, what fixes did you make?

    Simulator: While the driver is entering code, enter the pseudocode into the lab report.

    Driver: Give your code to the simulator for pasting into the lab report.

C. Drawing the spiral with Line2D.Double

  1. [15 minutes] A more natural way of drawing the spiral is to use Java graphics. Here you have SpiralViewer and SpiralComponent classes to get you started. As a group, look at the code that is provided. Develop an algorithm for drawing the spiral. This is not as easy as with the robot because (a) there is no obvious way of "turning" a point or line and (b) you need to compute the start and end points yourself. Actually, you only need to compute the end point because the start point is always the end point of the previous line.

    You will need to develop the math for locating a point that is above, below, to the left, or the right of a given point. That is a good way to get started. Given the point (100, 100), how do you find the point above, below, to the left, to the right, at distance 10? At an arbitrary distance? What if it is an arbitrary point p? (Hint: p.getX(), p.getY().)

    You should draw 40 segments of the spiral. There are two strategies. You might write a loop that is executed 40 times, calling g2.draw(new Line2D.Double(start, end)) once per iteration. Or you might execute a loop ten times, drawing four segments per iteration.

    Discuss the advantages and disadvantages of each approach and make a choice.

    Keep in mind that the segments need to be long enough to be visible. Use a grid size of 10 pixels. That is, the first segment should be 10 pixels long. Start at (100, 100).

    Simulator: Write up the pseudocode on a sheet of paper.

    Everyone: Review the pseudocode to see that it reflects your group's intent.

    Driver: Make a sheet for tracing any variables used in your pseudocode and give it to the simulator.

  2. Execute your pseudocode [10 minutes]. Follow these instructions.

    The simulator needs a sheet of graph paper.

    The director places a marker (such as a coin or paperclip) next to the currently executed statement on the pseudocode sheet. The director reads off the next statement. The director tells the simulator to draw line segments, for example "Draw a line from (100, 100) to (100, 90)". If any variables need to be updated, the director tells the simulator what to do, such as “add 1 to n”. If the director needs to know a variable's value, ask the simulator! (Remember, no peeking by the director. Everyone else should peek.) The simulator reveals and updates any variables.

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

    You don't need to simulate all 40 segments. Stop when you are confident that your algorithm is right.

    Facilitator: Be prepared to report whether your group succeeded, what strategy you used in your algorithm, and what you learned from this exercise.

  3. Run your code [10 minutes]. Put your code into the paintComponent method of the SpiralComponent class. Run the code. Does it produce the spiral pattern? If not, what fixes did you make?

    Simulator: While the driver is entering code, enter the pseudocode into the lab report.

    Driver: Paste your code into the lab report.