A programming kata is an exercise which helps a programmer hone his skills through practice and repetition.
This article is part of the series Java Tutorial Through Katas.
The article assumes that the reader already has experience with Java, that he is familiar with the basic usage of JUnit tests and that he knows how to run them from his favorite IDE (ours is IntelliJ IDEA).
Tests that prove that the solution is correct are displayed below. Recommended way to solve this kata is to use test-driven development approach (write the implementation for the first test, confirm that it passes and move to the next). Once all of the tests pass, the kata can be considered solved.
One possible solution is provided below the tests. Try to solve the kata by yourself first.
Implement a simple tennis game
- Scores from zero to three points are described as "love", "fifteen", "thirty", and "forty" respectively.
- If at least three points have been scored by each side and a player has one more point than his opponent, the score of the game is "advantage" for the player in the lead.
- If at least three points have been scored by each player, and the scores are equal, the score is "deuce".
- A game is won by the first player to have won at least four points in total and at least two points more than the opponent.
[ONE POSSIBLE SOLUTION]
What was your solution? Post it as a comment so that we can compare different ways to solve this kata.
Test-Driven Java Development
Test-Driven Java Development book wrote by Alex Garcia and me has been published by Packt Publishing. It was a long, demanding, but very rewarding journey that resulted in a very comprehensive hands-on material for all Java developers interested in learning or improving their TDD skills.
If you liked this article I am sure that you'll find this book very useful. It contains extensive tutorials, guidelines and exercises for all Java developers eager to learn how to successfully apply TDD practices.