(java) Solved problem 3