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

lab 8 find count age that less than 30 years old and average age by java

public class Student {

  private String name;

  private int weight;
 
  private int high;
 
  private int age;

  private int ID;

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

  public void display() {

    System.out.println( "NAME: "+this.name);
    System.out.println( "WEIGHT:"+this.weight+"  kg.");
    System.out.println( "HIGH:"+this.high+"  cm.");
    System.out.println( "AGE :"+this.age+"  years old");
    System.out.println( "ID :"+this.ID);

 }
  public static void main(String[] args) {
    Student[] std = {new Student("Park",40,160,18,1),
                    new Student("Nani",50,180,19,2),
                     new Student("Pepe",80,185,20,3),
                     new Student("Giggs",70,160,18,4),
                     new Student("Laws",85,155,18,5)};
    int index = 0;
    while(index<std.length){
      std[index].display();
      index = index + 1;
    }
    System.out.println("Average age is  "+average_age(std)+"  years old");
    System.out.println("The number of students that have age less than 30 years old is  "+count_age(std));
  }
  public static float average_age(Student[] std){
    int index = 0;
   float sum_age = 0;
   float age = 0;
    while(index<std.length){
      sum_age += std[index].get_age();
      index = index + 1;
    }
    age = (sum_age)/5 ;
     return age;
  }
  public static int count_age(Student[] std){
   int index  = 0 ;
    int count  = 0;
    while(index<std.length){
      if(std[index].get_age()<30){
        count +=1;
      }
      index  = index + 1;
    }
    return count ;
  }

}

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

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