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

Lab 8 - (Java) Find minimum weight of students

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_weight(){
    return this.weight;
  }
  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("The number of students that have weight less than 50 kg. is "+count_weight(std));
   System.out.println( "The minimum weight is  " +find_minimum_weight( std)+"  kg. ");
    }
   public static int count_weight(Student[] std){
    int index = 0 ;
    int count = 0 ;
      while(index<std.length){
      if(std[index].get_weight()<50){
          count +=1 ;
      }
          index = index + 1 ;
      }
        return count    ;
      }
  public static int find_minimum_weight(Student[] std){
    int  index = 0;
    int weight = 100;
    while(index<std.length){
      if(std[index].get_weight()<weight){
        weight = std[index].get_weight();
      }
      index = index + 1;
    }
    return weight;
  }
    }

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

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