(1)针对实验4编写的Staff类,分别编写NameComparator,AgeComparator和ComplexComparator比较器。其中ComplexComparator为复合比较器,至少选用两个关键字来排序,关键字选择、次序及升降序均由同学们自行设计。 (2)编写一个测试类,在测试类中添加若干个Staff, Teacher, SecurityGuard, Dean实例(个数及内容自定),并在测试类中定义并测试如下方法:①编写一个方法private static void printName(Staff[] staffs)打印出每个人的名字;②编写一个方法private static void printSalary(Staff[] staffs)打印出Staff类或者其子类对象的薪水(注意:Staff的薪水只有salary,Teacher的薪水为salary+postAllowance,SecurityGuard的薪水为salary+dangerousAllowance,而Dean的薪水则为salary+postAllowance+adminAward);③编写一方法private static void sortBySalary(Staff[] staffs),支持对Staff类及其子类按照各自的薪水降序排序;④编写一方法private static void sortByAge(Staff[] staffs),对Staff对象按照年龄升序排序,再编写一个方法按name升序进行排序;⑤编写一方法sortByDateHired,支持对Staff类及其子类按照各自的dateHired升序排序,可以使用java.util.Date类的getTime方法。
时间: 2024-02-12 09:09:19 浏览: 71
以下是代码实现:
Staff.java
```java
import java.util.Date;
public class Staff {
private String name;
private int age;
private Date dateHired;
private double salary;
public Staff(String name, int age, Date dateHired, double salary) {
this.name = name;
this.age = age;
this.dateHired = dateHired;
this.salary = salary;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Date getDateHired() {
return dateHired;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Staff{" +
"name='" + name + '\'' +
", age=" + age +
", dateHired=" + dateHired +
", salary=" + salary +
'}';
}
}
```
Teacher.java
```java
import java.util.Date;
public class Teacher extends Staff {
private double postAllowance;
public Teacher(String name, int age, Date dateHired, double salary, double postAllowance) {
super(name, age, dateHired, salary);
this.postAllowance = postAllowance;
}
public double getPostAllowance() {
return postAllowance;
}
public void setPostAllowance(double postAllowance) {
this.postAllowance = postAllowance;
}
@Override
public double getSalary() {
return super.getSalary() + postAllowance;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", dateHired=" + getDateHired() +
", salary=" + getSalary() +
'}';
}
}
```
SecurityGuard.java
```java
import java.util.Date;
public class SecurityGuard extends Staff {
private double dangerousAllowance;
public SecurityGuard(String name, int age, Date dateHired, double salary, double dangerousAllowance) {
super(name, age, dateHired, salary);
this.dangerousAllowance = dangerousAllowance;
}
public double getDangerousAllowance() {
return dangerousAllowance;
}
public void setDangerousAllowance(double dangerousAllowance) {
this.dangerousAllowance = dangerousAllowance;
}
@Override
public double getSalary() {
return super.getSalary() + dangerousAllowance;
}
@Override
public String toString() {
return "SecurityGuard{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", dateHired=" + getDateHired() +
", salary=" + getSalary() +
'}';
}
}
```
Dean.java
```java
import java.util.Date;
public class Dean extends Teacher {
private double adminAward;
public Dean(String name, int age, Date dateHired, double salary, double postAllowance, double adminAward) {
super(name, age, dateHired, salary, postAllowance);
this.adminAward = adminAward;
}
public double getAdminAward() {
return adminAward;
}
public void setAdminAward(double adminAward) {
this.adminAward = adminAward;
}
@Override
public double getSalary() {
return super.getSalary() + adminAward;
}
@Override
public String toString() {
return "Dean{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", dateHired=" + getDateHired() +
", salary=" + getSalary() +
'}';
}
}
```
NameComparator.java
```java
import java.util.Comparator;
public class NameComparator implements Comparator<Staff> {
@Override
public int compare(Staff o1, Staff o2) {
return o1.getName().compareTo(o2.getName());
}
}
```
AgeComparator.java
```java
import java.util.Comparator;
public class AgeComparator implements Comparator<Staff> {
@Override
public int compare(Staff o1, Staff o2) {
return Integer.compare(o1.getAge(), o2.getAge());
}
}
```
ComplexComparator.java
```java
import java.util.Comparator;
public class ComplexComparator implements Comparator<Staff> {
@Override
public int compare(Staff o1, Staff o2) {
if (o1.getSalary() == o2.getSalary()) {
return o1.getDateHired().compareTo(o2.getDateHired());
} else {
return Double.compare(o2.getSalary(), o1.getSalary());
}
}
}
```
TestStaff.java
```java
import java.util.Arrays;
import java.util.Date;
public class TestStaff {
public static void main(String[] args) {
Staff[] staffs = {
new Staff("Jack", 25, new Date(), 5000),
new Teacher("Tom", 35, new Date(), 8000, 2000),
new SecurityGuard("Jerry", 40, new Date(), 6000, 1000),
new Dean("Rose", 45, new Date(), 10000, 3000, 4000)
};
// 打印每个人的名字
printName(staffs);
// 打印出Staff类或者其子类对象的薪水
printSalary(staffs);
// 按照各自的薪水降序排序
sortBySalary(staffs);
System.out.println("按照各自的薪水降序排序:");
for (Staff staff : staffs) {
System.out.println(staff);
}
// 对Staff对象按照年龄升序排序,再按照name升序进行排序
sortByAge(staffs);
System.out.println("对Staff对象按照年龄升序排序,再按照name升序进行排序:");
for (Staff staff : staffs) {
System.out.println(staff);
}
// 对Staff类及其子类按照各自的dateHired升序排序
sortByDateHired(staffs);
System.out.println("对Staff类及其子类按照各自的dateHired升序排序:");
for (Staff staff : staffs) {
System.out.println(staff);
}
}
private static void printName(Staff[] staffs) {
System.out.println("每个人的名字:");
for (Staff staff : staffs) {
System.out.println(staff.getName());
}
System.out.println();
}
private static void printSalary(Staff[] staffs) {
System.out.println("Staff类或者其子类对象的薪水:");
for (Staff staff : staffs) {
System.out.println(staff.getSalary());
}
System.out.println();
}
private static void sortBySalary(Staff[] staffs) {
Arrays.sort(staffs, new Comparator<Staff>() {
@Override
public int compare(Staff o1, Staff o2) {
return Double.compare(o2.getSalary(), o1.getSalary());
}
});
}
private static void sortByAge(Staff[] staffs) {
Arrays.sort(staffs, new AgeComparator().thenComparing(new NameComparator()));
}
private static void sortByDateHired(Staff[] staffs) {
Arrays.sort(staffs, new Comparator<Staff>() {
@Override
public int compare(Staff o1, Staff o2) {
return o1.getDateHired().compareTo(o2.getDateHired());
}
});
}
}
```
阅读全文