(java) Solved problem 7