java prob2 and 3 added with loop reduced