question 4 done in java