COP 2800 (Java Programming) Project #3
Guessing Game

 

Due: by the start of class on the date shown on the syllabus

Description and Requirements:

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.

Other Requirements:

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!

Sample Game Log:

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

Hints:

JOptionPane input dialog boxes only return Strings.  You can use Integer.parseInt method to convert a String to an int.  On the other hand, confirm dialog boxes return a constant int, such as JOptionPane.YES_OPTION.  Check the Javadocs for more details and options with javax.swing.JOptionPane.

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 10 to i.  You also should have some comments at the top of every file with a brief description of the file and your name.

The design I provided (and am forcing you to use) is not a good design for this program.  A different set of methods would make this much better and would simplify the program!  But a proper design would not have given you as much practice working with multiple methods, passing data in and out of them, and deciding what to declare and where to do it.

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. 

To be turned in:

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.

Send projects to .  Please use a subject such as “Java Guessing Game Project” so I can tell which emails are submitted projects.
Send project questions to .  Please use a subject such as “Java Guessing Game Project Questions” so I can tell which emails are questions about the project.

Please review Submitting Assignments and Project Grading Criteria from your class syllabus for further details.