วันพุธที่ 4 พฤศจิกายน พ.ศ. 2558

Lab 8 - Student(Weight) [JAVA]

public class Student {
  private String name;
  private int ID;
  private int age;
  private int weight;
  private int 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 int get_weight(){
 return this.weight;
  }

  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+"\n");
  }
  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 weight < 50 = "+find_numWeight_lessthan50(std)+"\n");
    for(int i = 0; i < std.length; i++){
        if(std[i].get_weight() < 50){
            std[i].display();
        }
    }
  }
 
  public static int find_numWeight_lessthan50(Student[] student){
    int count = 0;
    for(int i = 0; i < student.length; i++){
        if(student[i].get_weight() < 50){
            count += 1;
        }
    }
    return count;
  }
}

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

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