For this project you are to implement a stand-alone Java program to play a guessing game. Your program should pick a random number between one and ten (inclusive) and prompt the user for their guess. For each guess made, your program should tell the user if the guess was too high, too low, or correct. The user should only have four (4) tries to get it right before they lose the game.
When a game is over, a dialog should announce if they won or lost and ask the user if they want to play again. Your dialog should have yes and no buttons. (A cancel button is allowed in addition.) If they lost this dialog box must show them what the correct answer was.
All user input and output should be done using javax.swing.JOptionPane class, which display dialog boxes. Other than this, the program is non-GUI.
Your project must be a stand-alone program, not an applet.
The program should use the class java.util.Random to generate the numbers (which is easier than using java.lang.Math random number functions), and use the swing JOptionPane input dialog to read in the user's guess, and a confirm dialog for the “game over” dialog. When reading the user's guess, they are told if the guess is too low or too high. If the user doesn't guess correctly with four tries, they lose.
As this is your first programming assignment where I don't provide sample code to use, I provide the design of the program for you. Your code must have the following methods at least:
No other methods are needed, but if you can make a good case for it you may have additional methods. (You still need these three methods.) You must decide what arguments, if any, to pass to these methods. Please note you must use the design implied by the methods I have required. (Even if you would have designed the game program differently!)
You must meet all the requirements from the description above. If you include any creative extras, be sure your program still performs the guessing game as described above. Creative extras are “extras” and you are not free to modify the project requirements.
A non-working project can score quite well (so don't be afraid to turn one in). Also a fully working project may not score 100%.
You must work alone on your project, however you can ask your instructor for help anytime. Do not wait until “the last minute” to begin work on your projects!
Below is the captured output of a sample run of this game. Your game should work the same way. To make this easier to follow, the model solution has had the GUI input and output replaced with non-GUI I/O, but is otherwise identical. User input is shown using boldface.
C:\Temp>java GuessingGameNonGUI (4 Guesses left) What is your guess (1-10)? 5 (3 Guesses left) Too high! What is your guess (1-10)? 3 (Game Over) Correct! Well done! Play again (y/n)? y (4 Guesses left) What is your guess (1-10)? 10 (3 Guesses left) Too high! What is your guess (1-10)? 9 (2 Guesses left) Too high! What is your guess (1-10)? 8 (1 Guesses left) Too high! What is your guess (1-10)? 7 (Game Over) Sorry, you lose! (number was 2) Play again (y/n)? n C:\Temp>
You should download the model solution GuessingGame.class and run it a few times, using:
C:\Temp>java GuessingGame
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
23 | 24 | 25 | 26 | 27 | 28 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 1 | 2 | 3 | 4 | 5 |
Get Free Quote!
300 Experts Online