Solve problem 23 using Java.