(java) Solved problem 18