need identify names of youngest/ oldest of following three, using "if" statements: can list students running code, need identify youngest , oldest of three.
public class app { public static void main(string args[]) { student st1 = new student(); st1.setfirstname("fred"); st1.setlastname("fonz"); st1.setage(44); student st2 = new student(); st2.setfirstname("john"); st2.setlastname("smith"); st2.setage(20); student st3 = new student(); st3.setfirstname("zack"); st3.setlastname("mills"); st3.setage(21); system.out.println("student 1 info = "+ st1.getfirstname()+ " "+ st1.getlastname()+ " "+ st1.getage()); system.out.println("student 2 info = "+ st2.getfirstname()+ " "+ st2.getlastname()+ " "+ st2.getage()); system.out.println("student 3 info = "+ st3.getfirstname()+ " "+ st3.getlastname()+ " "+ st3.getage()); } }
i think need :
student[] arr = new student[3]; arr[0] = st1; arr[1] = st2; arr[2] = st3; int min_age = integer.max_value, max_age = integer.min_value, min_age_index = 0, max_age_index= 0; for(int =0; <arr.length ; i++){ int age = arr[i].getage(); if(min_age>age) { min_age = age; min_age_index = i; } if(max_age<age) { max_age = age; max_age_index = i; } } system.out.println("youngest student : "+arr[min_age_index].getfirstname()+" "+arr[min_age_index].getlastname()); system.out.println("oldest student : "+arr[max_age_index].getfirstname()+" "+arr[max_age_index].getlastname());
Comments
Post a Comment