problem 10 java + scala solution