Kotlinlearncs.online LogoJava
Return to List

Test Writing: Guessing Game With Tries

Created By: Geoffrey Challen
/ Version: 2022.7.0

Let's complete a class to implement a guessing game. When the class is created we'll set a secret number and 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 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. Provide a getter for the number of tries used, but do not expose any other object state publicly.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestGuessingGame with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Java's assert method