AP Notes, Outlines, Study Guides, Vocabulary, Practice Exams and more!

Java Lesson 9 - Guessing Game pt. 1

Aug 08, 2009

Java Lesson 9 – Guessing Game pt.1

I think it’s about time we put all the basic stuff we’ve learned and put it practical use. I’ve decided to make our first project a guessing game. Before we jump in, let’s take a look at the game we’re making and the kind of functionality it will have. Generally, project overviews are referred to by a lot of people as design documents.

The object of our game is to have the player guess a number between 1 and 100, inclusively. The game will generate a random number before the game begins. After each guess, the game will compare the player’s guess to its random number and give the player a hint (i.e. guess higher, guess lower). The game will also keep track of how many times the player has guessed. When the player has guessed the right answer, the game will display a congratulatory message and report the number of guesses it took. For now, the game will last one round before exiting.
What we need:
A loop, so the player can keep guessing indefinitely. Can you figure out which one we need to use?
Variables, to keep track of game information. Can you figure out what kinds we need? Remember, we need to keep track of the number of guesses and whether or not the player has guessed right. We also need to remember our randomly generated number to compare to the player’s guess.
So, let’s first type the stuff that is any program. Name the file GuessGame.java.
import java.util.*;
public class GuessGame
{
public static void main(String[] args)
{
}
}
You may notice the very first line is a little different than what we’ve seen before. This is called an import statement. Because we need access to both random numbers and user input, we need to take these features from Java’s library because they aren’t normal, everyday features. So basically we are telling Java to get ready for some advanced classes in its Utility package. The astericks (*) at the end means everything. If you only need one thing, you can replace the astericks with the name of the class. In this case, both of the features we need are in the utility class, so it makes sense just to import everything as Java will only take the things we use.
The two classes we need are the Random class and the Scanner class. We declare these just like we would an int or a String, but bear in mind that these are a little different.

This lesson will continue in part 2.

Need Help?

We hope your visit has been a productive one. If you're having any problems, or would like to give some feedback, we'd love to hear from you.

For general help, questions, and suggestions, try our dedicated support forums.

If you need to contact the Course-Notes.Org web experience team, please use our contact form.

Need Notes?

While we strive to provide the most comprehensive notes for as many high school textbooks as possible, there are certainly going to be some that we miss. Drop us a note and let us know which textbooks you need. Be sure to include which edition of the textbook you are using! If we see enough demand, we'll do whatever we can to get those notes up on the site for you!