CS 161 Lab #3

September 17th

Goals:

This week's lab involves modifying and extending a "guessing game" class. It will give you a chance to practice writing if-else statements, output statements, methods with return values, and classes with multiple constructors.

Directions:

  1. Download the GuessingGame project and extract its contents, then start BlueJ and open the project. (You can use the BlueJ copy you installed on Alexandria last week, or the version that's already installed on the lab machines. The local copy should run more quickly.)
  2. Take a moment to familiarize yourself with the Java code in the Game class. A Game object stores the "right answer" in its answer field. Users can guess at the answer by invoking the makeGuess method, which prints a message if they guess correctly. The constructor for the class randomly picks a value between 1 and 100 (you don't need to understand how that part works). You can create a Game object and try it out for yourself.
  3. It's hard to tell if the code is working, since we don't know what the right answer is! Add an accessor method called getAnswer that returns an integer — the value in the answer field — when invoked. (Your method should return the value, not print it.) See the getDiameter method from class for inspiration if you wish. Compile your code and test it once you've added the new method: Create a Game object, invoke getAnswer to determine the right answer, then invoke makeGuess and see what happens when you guess correctly. Note: The "You got it!" message below will appear in the terminal window, not the codepad. I'm showing it all in one place here to keep the description simpler.
    > Game g = new Game();
    > g.getAnswer()
    37   (int)
    > g.makeGuess(37);
    You got it!
    
  4. Users may prefer to set their own right answer. (For example, you could set up a Game object with a specific right answer, then turn things over to a friend and watch them try to guess it.) Add a second constructor that takes an integer argument, and sets answer to the specified value. Compile your code. Note that when you right-click to create a new instance of the Game class, you now have a choice of two constructors. Test the one that takes an argument:
    > Game g = new Game(50);
    > g.makeGuess(50);
    You got it!
    
  5. When users guess incorrectly, nothing happens. It would be much better if they got feedback. Add an else to the if in the makeGuess method that prints out a message when the guess is wrong:
    > Game g = new Game(50);
    > g.makeGuess(23);
    Nope -- try again.
    > g.makeGuess(50);
    You got it!
    
  6. Next, add a counter to keep track of the number of guesses, and report the value once the user finally guesses correctly. You'll need to add a new field to keep the count. Use an assignment statement in makeGuess to increase the count value by one each time the user makes a guess. Then, add another output statement to display the count when the game is over. (Your output should appear in the terminal window — there's no way to make it appear in the codepad.)
    > Game g = new Game(50);
    > g.makeGuess(23);
    Nope -- try again.
    > g.makeGuess(53);
    Nope -- try again.
    > g.makeGuess(50);
    You got it!
    It took you 3 guesses.
    
  7. To enhance the game-playing experience, we should really insult the player if they take too many guesses. Add an if statement to makeGuess so that the player is insulted if they take five or more guesses. This message should appear instead of the normal rejection message:
    > Game g = new Game(50);
    > g.makeGuess(5);
    Nope -- try again.
    > g.makeGuess(15);
    Nope -- try again.
    > g.makeGuess(25);
    Nope -- try again.
    > g.makeGuess(35);
    Nope -- try again.
    > g.makeGuess(45);
    You're not very good at this...
    > g.makeGuess(55);
    You're not very good at this...
    > g.makeGuess(50);
    You got it!
    It took you 7 guesses.
    
If time permits, consider trying some of the following exercises:


Brad Richards, 2009