CS46A Lab

Loops

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

Part A. Counting Syllables

  1. Design a simple loop. 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.

    With your teammate, decide on a strategy.

    Scribe: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code. Turn it in to the lab instructor at the end of the lab.

    Pseudocode is not Java code. The pseudocode for countVowels might look like this:

    i = 0
    counter = 0
    while i is a valid position in text
       letter = i-th character of text
       if letter is a vowel
          ...
       ...
    return counter

    Driver: Make a sheet for tracing variables i, letter, and count.

  2. Trace your pseudocode. Follow these instructions. The scribe places a marker (such as a coin or paperclip) next to the currently executed line on the pseudocode sheet, starting with the first line of the countVowels method. The scribe reads off the next statement. If it involves updating a variable, the scribe tells the driver what to do, such as “add 1 to i” or “set letter to the character at position 0”. The driver 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".

    Assume that the isVowel method works correctly. Do not trace inside it.

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

    Driver: Paste the code for the working countVowels method into your lab report.

  4. Design a more complex loop. 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”.

    With your teammate, decide on a strategy.

    Scribe: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code. Turn it in to the lab instructor at the end of the lab.

    Driver: Make a sheet for tracing any variables used in your pseudocode.

  5. Trace your pseudocode. Follow the same instructions as in step 3.

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

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

B. Drawing a Spiral

  1. 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. With your teammate, 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 dropBottle method before every moveForward so that the bottles trace her path.

    Note that the length of the spiral arms follow a definite pattern. Find that pattern.

    You may want to review the API of the Robot class to remind yourselves what a robot can do.

    You will probably need a loop inside a loop. An outer loop for the spiral arms. An inner loop whenever Carol moves n positions, where n is the length of an arm.

    Scribe: Write up on a sheet of paper what you agreed upon, using pseudocode, i.e. a mixture of English and Java code. Turn it in to the lab instructor at the end of the lab.

    In the pseudocode, try to stay at a high level. For example, you can say

    Repeat armLength times
       Drop a bottle
       Move forward

    Driver: Make a sheet for tracing any variables used in your pseudocode.

  3. Execute your pseudocode. Follow these instructions. The scribe places a marker (such as a coin or paperclip) next to the currently executed statement on the pseudocode sheet. The scribe reads off the next statement. If it involves Carol, the scribe tells the driver what Carol should do, such as "move forward" or drop a bottle. The driver draws any bottles and Carol's new position on a sheet of graph paper. If any variables need to be updated, the driver updates any variables on the tracing sheet.

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

  4. Run your code. Unzip this file and open the contents as a Netbeans project. Now put your code into myFirstMethod inside the Scene class. (Remove what was there before.) Also, in the performCustomSetup method, comment out the two lines
            // makeRoom(8, 6, 3, 6);        
            // makeMaze();

    Run the code. Does it produce the spiral pattern? If not, what fixes did you make?

C. Drawing the spiral with Line2D.Double

  1. A more natural way of drawing the spiral is to use Java graphics. Here you have SpiralViewer and SpiralComponent classes to get you started. 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).

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

    Driver: Make a sheet for tracing any variables used in your pseudocode.

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

    The driver needs a sheet of graph paper.

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

    If you find any mistakes in the pseudocode, stop the simulation and 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.

  3. 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?