建立一个员工类Employee,有属性:姓名name,性别sex,工龄workAge,薪水salary和奖金award,java
建立一个排序类SortUtil,有静态方法sort(Employee[] s);方法能够对员工按年薪排升序(必须用插入排序)数组
建立一个Test类,生成5个员工的数组,排序后打印全部员工信息(包括年薪)。this
建立一个员工类Employee:
spa
public class Employee { private String name; private String sex; private int workAge; private int salary; private int award; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getWorkAge() { return workAge; } public void setWorkAge(int workAge) { this.workAge = workAge; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public int getAward() { return award; } public void setAward(int award) { this.award = award; } public Employee() { super(); } public Employee(String name, String sex, int workAge, int salary, int award) { super(); this.name = name; this.sex = sex; this.workAge = workAge; this.salary = salary; this.award = award; } public void displayEm() { // TODO Auto-generated method stub System.out.println(getName() + "\t" + getSex() + "\t" + getWorkAge()+"\t" + getSalary()+ "\t" + getAward()); } }
建立一个排序类SortUtil,有静态方法sort(Employee[] s)code
public class SortUtil { private Employee[] s = new Employee[5]; private int length = 0; //插入 public void insert(String name,String sex,int workAge,int salary,int award) { s[length++]=new Employee(name, sex, workAge,salary,award); } //显示 public void display() { for(int j=0;j<length;j++) { s[j].displayEm(); } System.out.println(); } //排序 public static void sort(Employee[] s) { for (int i = 0; i < s.length; i++) { for (int j = i; j >0; j--) { if(s[j].getSalary() < s[j-1].getSalary()) { Employee temp = s[j]; s[j] = s[j-1]; s[j-1] = temp; } } } for (int i = 0; i < s.length; i++) { System.out.println(s[i].getName()+"\t" + s[i].getSex() +"\t" +s[i].getWorkAge() +"\t" + s[i].getSalary() +"\t" +s[i].getAward()); } } }
建立一个Test类:排序
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Employee[] s = new Employee[5]; Employee em = new Employee(); SortUtil sortutil = new SortUtil(); sortutil.insert("员工一","女",5,4000,500); sortutil.insert("员工二","女",5,3000,1000); sortutil.insert("员工三","男",5,4200,400); sortutil.insert("员工四","男",5,6500,1000); sortutil.insert("员工五","男",5,4500,500); sortutil.display(); sortutil.sort(s); sortutil.display(); } }