Exercise: Extending Wordz
Let’s understand how to implement single-letter guesses against a target word using TDD.
We'll cover the following
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:
Create a
Word
class that represents a target word. The target word should be supplied as a constructor parameter when creating an instance of theWord
class.Implement a
guess
method within theWord
class that takes a single-letter guess as a parameter. The method should returntrue
if the guess matches any letter in the target word; otherwise, it should returnfalse
.Implement a test case for the above functionality following TDD principles. The test case should include the following steps:
Create an instance of the
Word
class with a target word.Use the
guess
method to guess a single letter.Assert that the result of the guess is
true
if the guessed letter is in the target word, orfalse
if it is not.
Ensure that the initial test case fails before writing the implementation code in the
Word
class.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.