Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Workshop: Array of Prizes, Indeed

Watch the syndicated game show Wheel of Fortune for any length of time and you'll be surprised at how predictable the contestants are.

In the years that this show has been one of the world's most successful television programs, fortune seekers have worked the puzzle-solving process into an exact science.

Wheel contestants typically guess the same letters when they are starting out on a puzzle: R, S, T, L, and N. In the final round, when these letters and the vowel E are given to the players right away, they usually choose four other letters: C, D, M, and the vowel O.

The reason for this predictability is that these are the letters that appear most often in English words. The contestants are stifling their desire for spontaneity in order to better their chances to win a trip to Bermuda, cash, and a Yamaha Waverunner.

Your Java workshop during this hour will test the most-common-letter theory by looking at as many different phrases and expressions as you care to type. An array will be used to count the number of times that each letter appears. When you're done, the program will present each letter and the number of times it appears in the phrases you entered. It also will present some clues about which letters to avoid entirely (unless you suspect that a puzzle's answer is the Aztec priest-ruler Quetzalcoatl or the fire god Xiuhtecuhtle).

Open up a new file in your word processor and call it Wheel.java. Enter Listing 9.2 and save the file when you're done.

Example 9.3. The Full Source Code of Wheel.java

 1: class Wheel { 
 2:    public static void main(String[] arguments) { 
 3:        String phrase[] = { 
 4:            "A STITCH IN TIME SAVES NINE", 
 5:            "DON'T EAT YELLOW SNOW", 
 6:            "JUST DO IT", 
 7:            "EVERY GOOD BOY DOES FINE", 
 8:            "I WANT MY MTV", 
 9:            "HOW 'BOUT THEM COWBOYS", 
10:            "PLAY IT AGAIN, SAM", 
11:            "FROSTY THE SNOWMAN", 
12:            "ONE MORE FOR THE ROAD", 
13:            "HOME FIELD ADVANTAGE", 
14:            "VALENTINE'S DAY MASSACRE", 
15:            "GROVER CLEVELAND OHIO", 
16:            "WONDERFUL WORLD OF DISNEY", 
17:            "COAL MINER'S DAUGHTER", 
18:            "WILL IT PLAY IN PEORIA" 
19:        }; 
20:        int[] letterCount = new int[26]; 
21:        for (int count = 0; count < phrase.length; count++) { 
22:            String current = phrase[count]; 
23:            char[] letters = current.toCharArray(); 
24:            for (int count2 = 0;  count2 < letters.length; count2++) { 
25:                char lett = letters[count2];  
26:                if ( (lett >= 'A') & (lett <= 'Z') ) { 
27:                    letterCount[lett - 'A']++; 
28:                } 
29:            } 
30:         } 
31:        for (char count = 'A'; count <= 'Z'; count++) { 
32:            System.out.print(count + ": " + 
33:                    letterCount[count - 'A'] + 
34:                    "\t"); 
35:        } 
36:        System.out.println(); 
37:    } 
38: } 

After you compile the file and run it, the output should resemble Listing 9.3.

Example 9.4. Output of the Wheel Program

A: 22   B: 3    C: 5    D: 13   E: 28   F: 6    G: 5    H: 8    I: 18 
J: 1    K: 0    L: 13   M: 10   N: 19   O: 27   P: 3    Q: 0    R: 13 
S: 15   T: 19   U: 4    V: 7    W: 9    X: 0    Y: 10   Z: 0 

The following things are taking place in the Wheel program:

This workshop project shows how two nested for loops can be used to cycle through a group of phrases one letter at a time. Java attaches a numeric value to each character; this value is easier to use than the character inside arrays.

Using the length variable makes it possible for you to add as many phrases as desired within the { and } marks. The letters in each of the new phrases you add will be analyzed, and you can build up a better idea of what it takes to make a small fortune in 30 minutes on television.

Share ThisShare This

Informit Network