Problem 1 solution in Java added