updating Java and Scala solutions for problem 8