In this lab you will write programs using arrays.
- Set up a new eclipse project for each lab (detailed instructions)
- Copy the files Inequality.java and WordMorph2.java into your project folder.
Add code to the Inequality.java program to display all
numbers above the average.
Paste your program
and the output from the
Console into your solutions
document.
Modify your program from above to also count the number of
values above the average and display that as well.
Copy your program
and the output from the
Console into your solutions document.
One of the flaws with our earlier word morph program was that it could not check if words were valid. Here is a new starting version of WordMorph. At the start of this version I have added a big array of 3 letter words that we will use as a dictionary. (I also took out our earlier input validation just to keep it a little simpler for these new exercises.)
String[] dictionary = {
"abc","ace","act","add","ado","aft","age","ago","aid","ail","aim",
"air","ala","alb","ale","all","alp","ami","amp","amy","ana","and",
"ani","ant","any","ape","apt","arc","are","ark","arm","art","ash",
"ask","ass","ate","auk","awe","awl","awn","axe","aye","bad","bag",
"bah","bam","ban","bar","bat","bay","bed","bee","beg","bel","bet",
"bey","bib","bid","big","bin","bit","biz","boa","bob","bog","bon",
"boo","bop","bow","box","boy","bub","bud","bug","bum","bun","bus",
...
"yaw","yea","yen","yet","yin","yip","yon","you","yow","yuh","zag",
"zap","zig","zip","zoo"};
The drawback of a big array initializer like this is that it takes up a lot of space in the program code. A better option would be too store the dictionary in a file and then read the words from the file. We will learn about files in Chapter 7.
Replace the old code for getting
the starting word with code to get a random word from
the dictionary array.
Random rand = new Random();
String word = __________________________________________
Copy just the code that you added into your solutions document.
For Exercise 4 each time a new
word is created, you are going
to verify that it is in the dictionary. Loop through the
entire dictionary array and compare the new word to
every dictionary word. If you find a match, set the
boolean variable foundIt to true. After
the loop has finished, if foundIt still has not
been set, then the word is not in the dictionary.
Remember that you need to use string1.equals(string2)
to compare strings.
boolean foundIt = false;
for (____________________________) {
if (____________________________) {
foundIt = true;
}
}
if (!foundIt) {
System.out.println("\"" + word + "\" is not a valid word. Exiting.");
System.exit(1);
}
Test
it out and make sure it works. If
you have trouble, try using the debugger and step though
your program to watch what happens. When debugging
programs like this with keyboard input, sometimes the
debugger is waiting for your next debugger command, like
step over, but sometimes the program is waiting for you to
type something into the console window. When the debugger
is waiting, it will always show the next line to be
executed in green, but when the program is waiting for
console input, no line will be highlighted in green.
Once
you find a match, there is really no point in searching the rest
of the array. If you want to make it faster, you
could use a break statement to break out of the loop after
you find a match., If you continue in Computer Science, in
COS 161 you will learn a very clever searching algorithm
called Binary Search that can search a sorted
array while looking at very few words, and in COS 285 you
will learn about fancier data structures than arrays, one
of which is a hash table that can usually find a
match as the very first word that is checked!
In your solutions document include just the code that you added
along with an example execution where you make a few
valid words before making a mistake.
Another flaw in our original word morph program is that the user could repeat words: yip, yap, yip, yap ...
To solve this
problem you are going to add a history array to keep
track of all the previous words. history[0] will
hold the initial word, history[1] the first changed
word, ect.
Declare the
array and store the starting word at index 0,
__________________________________________
// Exercise 5b: store the starting word at index 0 in the history array
______________________
If
you want to do a good job of incremental development, you
can use the debugger to check the code that you just
wrote. In the variables panel you can expand (>)
the history array and you should see that it has 11 slots
numbered 0-10 and that slot 0 contains the starting word.
Copy just the code that you added into your solutions document.
Add the new word to the history array.
____________________
I
again suggest that you verify that it is working using the
debugger.
Copy just the code that you added into your solutions document.
Now search the history array to
check if the new word is a repeat. Note you only need to
search the filled in part of the history array.
for (_____________________) {
if (_____________________) {
System.out.println("You used that word already. Exiting.");
System.exit(1);
}
}
Test
it.
In
your solutions document include just the code that you added
along with an example execution where you make a few
valid words before making a repeat.
Save your solutions document and submit it.