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

Display student records, sorted by age, use insertion sort.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

  public int get_weight(){
        return this.weight;
  }

  public int get_height(){
        return this.height;
  }
 
   public void set_name(String name){
      this.name = name;
   }
 
   public void set_ID(int ID){
      this.ID = ID;
   }
 
   public void set_age(int age){
      this.age = age;
   }
 
   public void set_weight(int weight){
      this.weight = weight;
   }
 
   public void set_height(int height){
      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);
      System.out.println();
  }

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};
    sort(data);
   
    a.display();
    b.display();
    c.display();
    d.display();
    e.display();
   
  }

   public static void sort(Student [] data){
      int i = 1;
      while(i < data.length){
      int value_a = data[i].get_age();
      String value_n = data[i].get_name();
      int value_I = data[i].get_ID();
      int value_w = data[i].get_weight();
      int value_h = data[i].get_height();
      int j = i;
         while(j > 0 && data[j-1].get_age() > value_a){
         data[j].set_age(data[j-1].get_age());
         data[j].set_name(data[j-1].get_name());
         data[j].set_ID(data[j-1].get_ID());
         data[j].set_weight(data[j-1].get_weight());
         data[j].set_height(data[j-1].get_height());
            j = j - 1;
      }
      data[j].set_age(value_a);
      data[j].set_name(value_n);
      data[j].set_ID(value_I);
      data[j].set_weight(value_w);
      data[j].set_height(value_h);
      i = i + 1;
   }
}
}

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

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

Link Video Presentation Resort Managemant System Project.

Video Presentation Resort Managemant System Project. จัดทำโดย พากษ์เสียง: คุณาสิน  ทองมณี  5801012620011 ลำดับภาพ: สุพิชชา  ศรีศิริ...