Write a program that implements two different algorithms for guessing the user's first name. Use JOptionPane methods for input/output. Follow the instructions carefully. You will be graded down severely if you do not implement methods as directed or if you fail to organize the guessing algorithms correctly.
- Your program should have an outer loop that gives the user the option to quit or to use either algorithm #1 or algorithm #2 for guessing his/her name. Use JOptionPane.showOptionDialog() to take input. One iteration of this loop represents the guessing of one name. Each time a name is guessed, control returns to the outer loop, repeating until the user chooses to quit.
- Use a middle loop that continues until the user indicates that all letters of his/her name have been guessed. One iteration of this loop represents one letter. You may want to use JOptionPane.showConfirmDialog() to ask if there are any letters remaining in the name, and have the loop break on either "No" or "Cancel" responses. You could also use showOptionDialog().
- Implement each guessing algorithm with a separate method. In each case, your program should make guesses until the current letter has guessed correctly. This will require a loop in which each iteration represents one guess and whose whose termination condition is that the current guess is correct. This is the innermost of three nested loops which are located in at least two different methods.
- Guessing algorithm #1 will make each guess at random. Each time a guess is generated, check to see if it duplicates any past wrong guesses. Do this by keeping an ArrayList of wrong guesses and searching it sequentially (checking each value in the list one at a time, from the first to the last) each time you make a new guess. If the current guess has not been made before, ask the user whether it is correct. Since you might make a series of guesses that have already been seen, use a loop for this. Make sure the ArrayList starts empty for each new letter in the name; don't accidentally keep the list of wrong guesses made for the previous letter.
- Guessing algorithm #2 will, when possible, guess the middle letter in each sequence of possible letters. The first time, this is m or n. Whenever a guess in made, ask the user whether a) the guess is correct, b) the correct answer is earlier in alphabetical order than the guess, or c) the correct letter is later in alphabetical order than the guess. Adjust subsequent guesses accordingly, always guessing the middle letter in the remaining set of possible letters. When the number of possibilities is even, it does not matter whether you guess the first or the second "middle" letter. When there is only one possibility, that one is the middle choice in a list of one value. Note that this algorithm does not require you to keep track of wrong guesses.
This pseudocode may be helpful:
until user chooses to quit // outer loop
until name has been guessed // middle loop
until current letter has been guessed // inner loop
guess a letter using user's choice of algorithms
end of inner loop // done guessing current letter
end of middle loop // done guessing name
end of outer loop // done with game
Hints and References:
- Consult Eclipse's context-sensitive help or Oracle's online documentation for help understanding the parameters to use with the JOptionPane methods and how to use Math.Random or java.util.Random to generate random numbers to use in guessing letters.
- Use all caps to make the guessing easier. The Unicode codes for capital letters range from 65(A) to 90(Z). All of your guesses should be within this range.
- You can cast from an integer x to the corresponding Unicode character this way: char myChar = (char) x;
- The opposite cast looks like this: int x = (int) myChar;
- Once the name has been guessed correctly in either algorithm, show a thank you message that includes the entire name, eg: "Thanks for playing, JOHN!" Get the name from your list of correct guesses, not by asking the user for his/her full name.
- If you have a long first name, you may borrow mine for purposes of testing :)
Extra Credit (answer in comments in your code)
a) (This one should be easy) In the worst case, how many guesses would it take to correctly guess a four-letter name with algorithm #1?
b) in the worst case, how many guesses would it take to guess a four-letter name letter with algorithm #2?
c) (This one is hard, but you will see it again in CS 312.) Try a few four-letter names and see how long it takes your program to guess correctly for several different names. Why is the average case so close to the worst case?
|Due By (Pacific Time)
||01/12/2015 12:00 am