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. If they lost this dialog box must show them what the correct answer was.
All user input and output should be done using
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
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 message dialog for
game over dialog.
The user is told if the guess is too low or too high, or if they
got the right answer.
If the user doesn't guess correctly with four tries,
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:
public static void main ( String  args )
Play again?" logic, including the
you won/you lostdialog. This means a loop that runs at least once, and continues until the player quits.
static boolean playGame ( ??? )
what is your guess?input dialog. Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low. The method should return
trueif the user won. If they don't guess correctly after four tries, the user has lost and the method should return
static int compareTo ( ???, ??? )
0(zero) if the guess is correct.
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
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
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:
JOptionPane input dialog boxes only return
You can use
Integer.parseInt method to convert a
String to an
On the other hand, confirm dialog boxes return a constant
Check the Javadocs for more details and options with
Comments can be difficult for some students. The trick is to know your audience: another Java programmer. You therefore don't want to include comments like the following:
int i; // declaring i as an int i += 10; // adding 10 to i
A Java programmer already understands the code.
Instead you should have comments that explain why
you are declaring
i, and why are you adding
You also should have some comments at the top of every file with
a brief description of the file and your name.
A good way to start is to first enter your design as a skeleton
class with nothing but comments in the methods.
Later you fill in the code below each comment.
If you have no idea how to start, see How to Develop Your First Java Program for some (possibly) helpful advice.
Email to me your Java source file, by copy-and-paste (no attachments please!), and the answer to the following question:
What changes would you suggest to support this feature: the input dialog box should include a "cancel" button so users can quit playing before they have used all four guesses?
You can put the answer in your
.java file as a
comment it that is easier for you.
Note you don't have to write any code to answer this question,
just discuss what changes you would consider.
Java Guessing Game Projectso I can tell which emails are submitted projects.
Java Guessing Game Project Questionsso I can tell which emails are questions about the project.