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

Lab08 : Insertion sort [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 getWeight(){
   return this.weight; 
  }
  public int getHeight(){
   return this.height; 
  }
  public int getAge(){
   return this.age; 
  }
  public String getName(){
   return this.name ;
  }
  public int getID(){
   return this.id; 
  }
  public void setWeight(int weight){
   this.weight=weight; 
  }
  public void setHeight(int height){
   this.height=height; 
  }
  public void setAge(int age){
   this.age=age; 
  }
  public void setName(String name){
   this.name=name ;
  }
  public void setID(int id){
   this.id=id; 
  }
  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 static void main(String[] args) { 

  Student[] std={new Student("Roger",1,18,55,165),
                 new Student("Gold",2,16,49,173),
                 new Student("D",3,17,80,163)};
    insertion_sort(std);
    display_student(std);

 }
  public static void display_student(Student[] std){
     int i=0;
   while (i<std.length){
    std[i].display();
     i+=1;
   } 
  }
  public static void insertion_sort(Student[] std){
    int j;
    for (int i=0;i<std.length;i++){
       j=i ;
         while (j>0 && std[j].getAge()<std[j-1].getAge()){
         swap_student(j,j-1,std);
         j-=1;
       }
    }
  }
  public static void swap_student(int i,int k,Student[] stdnt){
    int temp;
    temp=stdnt[i].getAge(); //1
    stdnt[i].setAge(stdnt[k].getAge());
    stdnt[k].setAge(temp) ;
    temp=stdnt[i].getWeight(); //2
    stdnt[i].setWeight(stdnt[k].getWeight());
    stdnt[k].setWeight(temp) ;
    temp=stdnt[i].getHeight() ;//3
    stdnt[i].setHeight(stdnt[k].getHeight());
    stdnt[k].setHeight(temp) ;
    temp=stdnt[i].getID() ;//4
    stdnt[i].setID(stdnt[k].getID());
    stdnt[k].setID(temp) ;
    String tempo=stdnt[i].getName(); //5
    stdnt[i].setName(stdnt[k].getName());
    stdnt[k].setName(tempo) ;
  }


}

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

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

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...