solved Problem2 with Java