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 7, 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.
Since many of the new features added to Java 8 were inspired by Scala and other functional programming languages, short comparison of Java 7 (or earlier), Java 8 and Scala solutions is provided at the end of the article.
One possible solution is provided below the tests. Try to solve the kata by yourself first.
Create a representation of the Berlin Clock for a given time (hh::mm:ss).
The Berlin Uhr (Clock) is a rather strange way to show the time.
On the top of the clock there is a yellow lamp that blinks on/off every two seconds.
The time is calculated by adding rectangular lamps.
The top two rows of lamps are red. These indicate the hours of a day. In the top row there are 4 red lamps.
Every lamp represents 5 hours. In the lower row of red lamps every lamp represents 1 hour.
So if two lamps of the first row and three of the second row are switched on that indicates 5+5+3=13h or 1 pm.
The two rows of lamps at the bottom count the minutes. The first of these rows has 11 lamps, the second 4.
In the first row every lamp represents 5 minutes.
In this first row the 3rd, 6th and 9th lamp are red and indicate the first quarter, half and last quarter of an hour.
The other lamps are yellow. In the last row with 4 lamps every lamp represents 1 minute.
The lamps are switched on from left to right.
Y = Yellow
R = Red
O = Off
Test code can be found in the GitHub BerlinClockTest.java.
[ONE POSSIBLE SOLUTION]
Java 8 solution code can be found in the BerlinClock.java solution.
Java 7 (old earlier) equivalent can be found in the BerlinClockSeven.java solution.
Scala solution can be found in the Scala Tutorial Through Katas: Berlin Clock (Easy) post.
Major difference between Java 8 and Java 7 solutions is in the usage of the Stream to create an array of integers.
Both Java 8 and Scala have the methods that can be used to transform a collection. While Java 8 requires a lot of boilerplate code, Scala provides a simple and clear solution.
Another difference between Java and Scala solutions that resulted in less code is the getOnOff method.
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.