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

Java Lesson 10 - Guessing Game pt. 2

Aug 08, 2009

Java Lesson 10 – Guessing Game pt.2
Scanner keyboard = new Scanner(System.in);
We first have the type of variable we are making followed by our name for it, same as usual. But, because this is a class we need to instantiate it. Think of it this way. A class is a lot like an architectural blueprint. Once you have the blueprints that contain the instructions you can make a copy of the building to use. Scanner is the blueprint in this case and ‘keyboard’ is the name we are giving this particular copy of the blueprint. By saying ‘new Scanner(System.in)’ we are specifying to make a new copy of the Scanner class.
Now, when creating objects (the instances of our class), a lot of the time you will need to give the class (blueprint) specific information to help make the object a little more useful for this particular task. In the case of Scanner, we need to let Scanner know where exactly we need to take input from. There are several places Scanner could read from including text files and keyboards, so it makes sense that it requires us to make it clear what we want to use this object for. ‘System.in’ means ‘from the keyboard’ and because that is what our game needs this is what we tell Scanner. Scanner is a Java file just like this one, so don’t think of it as a giant foreign database library.
After we make our object we can use the variable name we gave it (in this case ‘keyboard’) to refer to our Scanner object. We can use the object’s methods (things it knows how to do, specified in the file it was written in) by simply saying the name, followed by the method. So we would use the following line to get input from the user.
Int input;
input = keyboard.readInt();
We make an int variable so we can later store the player’s guessed number. We can then, at any point in time, get the input from the player and set it equal to ‘input.’ The variable input now holds the player’s typed guess.
Now, we are going to need another object for our game; Random. Unlike Scanner, Random does not need to be given any information. So, we simply say:
Random randomGenerator = new Random();
When we call the method that generates our random number, we need to give the method some information, much like we did with Scanner. (I will teach you about methods and classes in a later lesson). We need to tell the method between 0 and what number do we want our number to be generated from. In this case, we want it to be generated from between 0 and 100.
Int numToGuess;
numToGuess = randomGenerator.nextInt(100);
This will continue in part 3 next week.

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!