Problem 1 finished in Java