Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Part A. Counting Syllables
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.
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.
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.
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.
If you find any mistakes in the pseudocode, stop the simulation and discuss how the pseudocode needs to be improved.
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. Part B. Drawing a Spiral using the Line class
I really mean everyone should draw the spiral. I want your brain and your fingers to connect...
SpiralViewer
class to get you started. Look at the code that is provided. Develop an algorithm for drawing the spiral.
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.
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.
main
method of the SpiralViewer
class. Run the code. Does it produce the spiral pattern? If not, what fixes did you make? C. Drawing the spiral with Carol
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.
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.)
SpiralViewer
. 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