2 พฤศจิกายน 2558

Lab8 JAVA

public class Student {
  private String st_name;
  private int st_id;
  private int st_age;
  private float st_weight;
  private float st_height;
  public Student(String name ,int id ,int age ,int weight ,int height){
    this.st_name = name;
    this.st_id = id;
    this.st_age = age ;
    this.st_weight = weight;
    this.st_height = height;
  }
  public void display(){
    System.out.println("Name : "+this.st_name);
    System.out.println("ID : "+this.st_id);
    System.out.println("Age : "+this.st_age);
    System.out.println("Weight : "+this.st_weight);
    System.out.println("Height : "+this.st_height);
  }
  public float getWeight(){
    return this.st_weight;
  }
  public float getHeight(){
    return this.st_height;
  }
  public int getAge(){
    return this.st_age;
  }
  public int getID(){
    return this.st_id;
  }
  public static void main (String[] agrs) {
    Student[] st_record  = {new Student("Tar",10016,21,69,179),
                            new Student("Tape",10032,21,90,174),
                            new Student("Bas",10032,19,69,175),
                            new Student("Top",10091,21,92,178),
                            new Student("Karn",10130,18,65,184)};
    displayStData(st_record);
    showBmi(st_record);
    showAvgAge(st_record);
    sortRecordByAge(st_record);
    findMinWeight(st_record);
   
  }
  public static void displayStData(Student[] std){
    int i = 0;
    while (i<std.length) {
      std[i].display();
      System.out.println("----------------------------------");
      i+=1;
    }
  }
  public static void showBmi(Student[] std){
    int i = 0;
    int count = 1;
    while (i<std.length) {
      float bmi = std[i].getWeight() / ((std[i].getHeight()/100)*(std[i].getHeight()/100));
      String bmi_f = String.format("%.2f",bmi);
        if (bmi > 25){
          System.out.println("No : "+count);
          std[i].display();
          System.out.printf("BMI : "+bmi_f);
          System.out.println();
          System.out.println("----------------------------------");
          count+=1;
      }
      i+=1;
    }
  }
  public static void showAvgAge(Student[] std){
    int i = 0;
    int count = 0;
    int sum_age = 0;
    while (i < std.length) {
      sum_age += std[i].getAge();
      if (std[i].getAge() < 30){
        count+=1;
      }
      i+=1;
    }
    float avg_age = sum_age/std.length;
    System.out.println("Average age of students : "+avg_age);
    System.out.println("----------------------------------");
  }
  public static void sortRecordByAge(Student[] std){
    int i = 0;
    while (i < std.length) {
      if (i!=std.length-1 && std[i].getAge() > std[i+1].getAge()) {
        int j  = i;
        while (j >= 0) {
          if (std[j].getAge() > std[j+1].getAge()) {
            Student data_copy = std[j];
            std[j] = std[j+1];
            std[j+1] = data_copy;
            j-=1;
          }
          else {
            break;
          }
        }
      }
      i+=1;
    }
    System.out.println("Sort record by age");
    System.out.println("////////////////////////////////");
    displayStData(std);
  }
  public static void findMinWeight(Student[] std){
    int i = 0;
    int count = 1;
    float min_w = std[0].getWeight();
    System.out.println("Student weight < 70");
    System.out.println("////////////////////////////////");
    while (i<std.length){
      if (std[i].getWeight() < min_w){
        min_w = std[i].getWeight();
      }
      if (std[i].getWeight() < 70){
        System.out.println("No : "+count);
        std[i].display();
        System.out.println("----------------------------------");
        count+=1;
      }
      i+=1;
    }
    System.out.println("Minimum weight of students : "+min_w);
    System.out.println("----------------------------------");
  }
}

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

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