(java) Solved problem 31