CS46A Lab

Loops

Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014 Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Part A. Counting Syllables

  1. You are going to design a simple loop to complete the countVowels method in this class that counts the number of vowels in a word. Use the isVowel method provided 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 counter.

  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. Translate the pseudocode into Java and add it to the Word class countVowels method, using BlueJ. To test your code, Run this WordTester1 class. Scribe: Did it pass the tests? If not, find and fix the problem.

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

  4. Now you will 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 2.

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

  6. In BlueJ, translate the pseudocode into Java and add it to the Word class countVowelGroups method. Run the WordTester2 class to test your code. Did it pass the tests? If not, discuss what went wrong and find a fix for your error.

    Driver: paste your code into your report.

Part B. Drawing a Spiral using the Line class

  1. Everyone: On a sheet of paper (preferably graph paper), draw this spiral. Start in the center of the spiral.

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

  2. We can draw a spiral using Dr. Horstmann's graphics package . Create a BlueJ project and import the graphics package. Here you have SpiralViewer class to get you started. Look at the code that is provided. Develop an algorithm for drawing the spiral.
    1. there is no obvious way of changing directions (think about using positive numbers when you need to go right or down and negative numbers when you need to go left or up).
    2. you need to compute the start and end points of each segment. Actually, you only need to compute the end point because the start point is always the end point of the previous line.
  3. You will need to develop the math for locating the x, y coordinates of a point that is above, below, to the left, or to 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?

    You should draw 40 segments of the spiral. There are two strategies. You might write a loop that is executed 40 times, creating a line segment and then calling line.draw() 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.

  4. Execute your pseudocode. 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.

  5. Put your code into the main method of the SpiralViewer class. Run the code. Does it produce the spiral pattern? If not, what fixes did you make?

    Driver: paste the code into the report

C. Drawing the spiral with Carol

  1. 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. The shadow of her old positions should trace out a spiral.

    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.  Show it in to the lab instructor before you leave the lab.

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

    Repeat armLength times
       Move forward

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

  2. 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 "turn left". The driver draws 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 positions simply fill the entire surface. Look at the picture above and notice how Carol takes two steps for every square in the graph paper.)

  3. Make a project called Lab8C. Import the graphics package in the usual way. Translate the pseudocode into Java and add it to SpiralViewer.

    You need to construct a Robot using the the constructor that takes the x, y coordinates and the name of the image file. Use x= 10 and y = 10 for the coordinates and "roomba.png" for the image.

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

    Driver: paste your code into the report.

Part D. Homework07, Par14, and Quiz15

  1. Discuss with your partner about Homework07, Par14, and Quiz15
  2. Ask the Lab instructor if you need any help