Solved Problem7 in java