วันเสาร์ที่ 14 พฤศจิกายน พ.ศ. 2558

Lab8_Avg_age


public class Student {
 private String name;
 private int idNO;
 private int age;
 private int weight;
 private int height;

 public Student(String name,int idNO,int age,int weight,int height){
 this.name = name;
 this.idNO = idNO;
 this.age = age;
 this.height = height;
 this.weight = weight;
}
public int get_age(){
return this.age;
}

public void display(){
 System.out.println("Name :"+this.name+" ID NO :"+this.idNO+" Age : "+this.age+" Weight : "+this.weight+" Height : "+this.height);
}

public static void main(String[] arg){
  Student[] a =  {new Student("James",58001,13,177,90),
        new Student("Jane",58002,15,140,60),
        new Student("John",58003,17,156,54),
        new Student("Jonas",58004,20,188,70)};
 int i =0;
    while(i<a.length){
     a[i].display();
     i=i+1;
    }
    System.out.print("the avg. age is "+cal_avg_age(a));
 }
public static float cal_avg_age(Student[] a){
int i = 0;
int sums = 0 ;
float avg = 0 ;
while(i<a.length){
int age=a[i].get_age();
   sums = sums+age;
   avg = sums/a.length;
   i=i+1;  
}
return avg;
}
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น