Added solution to exercise 4.2 in java