(java) Solve problem 6