Rewrite of Program in Java