(java) Solved problem 2