Copyright © Cay S. Horstmann 2009, 2012
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
Part A. Counting Syllables
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.
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, using BlueJ. Run this WordTester1
class. Did it
pass the tests?
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.
WordTester2
class. Did it pass the tests? If not, discuss what went wrong and find
a fix for your error. B. Drawing a Spiral
I really mean everyone should draw the spiral. I want your brain and your fingers to connect...
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.
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.)
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
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.
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.
paintComponent
method of the SpiralComponent
class. Run the code. Does it
produce the spiral pattern? If not, what fixes did you make?