Solve problem 8 using Java.