IBM interview question

Rewrite the above in Java.