Java code for problem 22.