challenge3 implemented in Java