solved Problem4 with Java