(java) Solved problem 8