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.
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!
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!
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!
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.
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.
Math.abs method to find the absolute value of a value. For example, Math.abs(-3) returns a value of 3.