Java code written by Vilpesh Mistry.