Copyright © Cay S. Horstmann, Kathleen O’Brien 2009-2014
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
The purpose of this lab is to practice designing algorithms. You will gain experience in
getVowels
that returns all
vowels of a string. For example, new
Word("monopoly").getVowels()
should return the string
"oooy"
. You will add your method to this Word class.
It is useful to think about “building blocks:" snippets of code that may help you attack this problem. These are things you already know how to do and you may be able to put various ideas together to solve the current problem. Here are a few things you know how to do:
i
th character of a string (or, more
accurately, a string containing the i
th character)
String ch = str.substring(i, i + 1);
for (int i = 0; i < str.length(); i++) { ... }
result = result + ch;
if (str1.equals(str2)) ...
if (isVowel(ch)) ...
The isVowel method was given to you in the previous lab, and you will also want to use it in this lab.
if (!isVowel(ch)) ...
Discuss with your lab partner which of these could be helpful for solving your task. Scribe: Record the consensus--just list the building blocks that you think you'll be using (a, b, etc.), not your rationale. No code to be written in this step.
One way of developing ideas is to simulate the process with physical objects such as Scrabble tiles and paper clips (to indicate a current position).
Try this with your partner. The lab instructor has a supply of Scrabble tiles and paper clips. Get some tiles that spell a word with at least two vowels. Use a paper clip to indicate the current loop index.
Put the paper clip at the first letter, then move it to the second letter, etc. Each time, decide what to do with the letter. (Is it a vowel? Do you want to add it to your vowel string?) If you decide to add it to another string, just move it.
If anyone gets confused about what is happening, start over. Just indicate in the report that you did step 2)
Caution: In the Word class, the string in
question is stored in an instance variable text
. It will make your life easier to also
use text
when you write down your
pseudocode.
Scribe: Once the group agrees on the pseudocode, type a copy into the lab report. (no code yet, just pseudocode)
text
string to the result
string. In this regard, the simulation is
not totally accurate (it doesn't reflect what goes on inside the computer).
What is the difference between the physical process and the process inside
the computer? Discuss. Scribe: Put the answer in your report.getVowelsFirst
that returns all vowels of a string, followed
by all other letters. For example, new
Word("monopoly").getVowelsFirst()
should return the string
"oooymnpl"
. You will want to adapt the algorithm that you
discovered in the preceding steps. As a group, discuss how you can do that.
Play out your adaptation with the tiles and paper clip.
Scribe: In your adaptation, does the paperclip traverse the text
string once or twice? (There are two ways of solving this, both
valid. I want to know which one you came up
with.)
text
string once or twice. If your answer was "twice", how can
you improve your algorithm so that the string is only traversed once? Programmers often use "stepwise refinement" in which they first increment a smaller problem that gets them closer to the solution.
getVowelsFirst
method.
In part A, you developed a method getVowels
.
One way of solving a problem is to reduce it to a simpler problem, like this:
public String getVowelsFirst() { String result = getVowels(); // get the vowels in the text // now what? return result; }
What needs to be added to result
to complete the method?
First consider the concrete case in which text
is
"monopoly"
.
getVowels()
return? getVowelsFirst()
return?result
to complete the solution?
Scribe: record your thoughts.result
to complete the
method? getVowels
. Just tell how the two differ.getVowelsFirst
method using these two
methods?outrageous
has three vowel groups. Here is one algorithm for this purpose:
count = 0 i = 0 while i < text.length() letter = the ith letter in text i++ if letter is a vowel count++ while i < text.length() and the ith letter is a vowel i++
Make a word with a couple of vowel groups and simulate this algorithm,
using the Scrabble tiles and a paper clip to mark the position of i
.
Note how the inner while
loop skips over the vowel
group.
In our example, that would be eou
.
First off, we need to assemble the vowel groups, not just count them. You know from part A how to assemble a sequence of letters in a string. Start with the empty string, then add letters as you encounter them.
Look at the pseudocode above.
while there are more inputs newValue = next input if newValue > largestValueSoFar largestValueSoFar = newValue
In that case, we remember the largest value. But in our situation, we want to remember the longest string.
So, let's make a variable longestVowelGroupSoFar. In step 3, look where you printed the assembled vowel group. Replace the print statement with the code required to update the longest group. Scribe: What is your pseudocode for that replacement? (No code yet)
Scribe: What is the final pseudocode for the longestVowelGroup method? (No code yet)
beauteous
Scribe: Which of them is reported as the longest one? The first or the second?
Discuss Homework08, Par16, and Quiz17 with your partner and ask your lab instructor for help when needed.