Let's complete a class to implement a guessing game. When the class is created we'll save a secret number and set a maximum number of tries. Then we'll provide a method allowing someone to guess the secret number, but limit the number of guesses appropriately.
Create a public class GuessingGame
with a primary constructor accepting the secret value and the maximum number of
tries, in that order, both as Int
values.
If the maximum number of tries is negative or zero, throw an IllegalArgumentException
.
Provide a public method guess
accepting a guess (as an Int
) and returning a Boolean
.
If the guess matches the secret, return true
; otherwise return false
.
If the number of tries is exceeded, or if the secret has already been successfully guess, throw an
IllegalArgumentException
.
Your tries
property should be publicly readable but not publicly writable.
Do not expose any other object state publicly.
Stuck? You may find these lessons helpful: