Exercise: Extending Wordz

Let’s understand how to implement single-letter guesses against a target word using TDD.

Task and requirements

Previously, we implemented a basic word guessing system following the Test-Driven Development approach in Java. Now, this system will evaluate a single-letter guess in relation to a target word.

Here are the simplified requirements for this exercise:

  1. Create a Word class that represents a target word. The target word should be supplied as a constructor parameter when creating an instance of the Word class.

  2. Implement a guess method within the Word class that takes a single-letter guess as a parameter. The method should return true if the guess matches any letter in the target word; otherwise, it should return false.

  3. Implement a test case for the above functionality following TDD principles. The test case should include the following steps:

    1. Create an instance of the Word class with a target word.

    2. Use the guess method to guess a single letter.

    3. Assert that the result of the guess is true if the guessed letter is in the target word, or false if it is not.

  4. Ensure that the initial test case fails before writing the implementation code in the Word class.

  5. Implement the Word class to make the initial test case pass.

Note: Use assertions to compare the expected result with the actual result returned.

Try it yourself

Implement your solution in the widget below:

Get hands-on with 1200+ tech skills courses.