Added Problem 4 in Java