วันอาทิตย์ที่ 1 พฤศจิกายน พ.ศ. 2558

Lab 8 - Student(BMI) [JAVA]

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

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

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

  public float get_weight(){
    return this.weight;
  }
   
  public float get_height(){
    return this.height;
  }
  public static void main(String[] args){
    Student[] std = {new Student("ant",58001,39,55,165),
           new Student("bird",58010,25,75,170),
           new Student("cat",58011,19,44,155),
           new Student("dog",58100,30,72,169),
           new Student("eagle",58101,22,70,178),
           new Student("frog",58110,18,100,175)};
System.out.println("Number of students with BMI > 25 ="+find_numBMI_greaterthan25(std));
    int i = 0;
    while(i < std.length){
      if(calBMI(std[i]) > 25){
        System.out.println();
        std[i].display();
        System.out.println("BMI : "+String.format("%.2f",calBMI(std[i])));
      }
      i += 1;
    }
  }
 
  public static float calBMI(Student student){
    float highM = student.get_height()/100;
    float bmi = student.get_weight()/(highM*highM);
    return bmi;
  }
  public static int find_numBMI_greaterthan25(Student[] student){
    int i = 0;
    int count = 0;
    while(i < student.length){
      if(calBMI(student[i]) > 25){
        count += 1;
      }
      i += 1;
    }
    return count;
  }
}

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

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