Add solution to Q 39 (Java).