5-Comparator

实验目的:掌握接口的定义与实现;熟悉内部类机制;能够使用Comparable、Comparator接口;会使用简单的Lambda表达式。

  1. Shape与Comparable接口
    改写实验4中的抽象类Shape,让其实现Comparable接口,实现对面积的排序;在测试类中创建Shape类型的数组或动态数组,里面有若干个Circle、Rectangle和Triangle对象,然后利用Arrays.sort或Collections.sort对该Shape数组或动态数组进行排序;输出排序后Shapes数组。
  2. Staff与Comparator接口
      1. 针对实验4编写的Staff类,分别编写NameComparator,AgeComparator和ComplexComparator比较器。其中ComplexComparator为复合比较器,至少选用两个关键字来排序,关键字选择、次序及升降序均由同学们自行设计。
      1. 编写一个测试类,在测试类中添加若干个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或java.time.LocalDateTime类型的compareTo方法。
      
  3. Lambda表达式
    使用Lambda表达式简化上述“Staff与Comparator接口”题目中的NameComparator,AgeComparator和ComplexComparator比较器。测试方法与上述相同。

实验报告

一、目的

掌握接口的定义与实现;熟悉内部类机制;能够使用Comparable、Comparator接口;会使用简单的Lambda表达式。

二、实验内容与设计思想

1.设计思路

  1. Shape与Comparable接口

  2. Staff与Comparator接口

  3. Lambda表达式

主要数据结构

问题1
abstract public class Shape {
public final static double PI = 3.1415926;
public abstract double getPerimeter();
public abstract double getArea();}
形状类: 存放PI求周长和求面积的方法
public class Rectangle extends Shape {
private double }
长方形类: 存放长方形的长和宽
public class Circle extends Shape{
private double r;}
圆类: 存放圆的半径
public class Triangle extends Shape {
private double a,b,c;}
三角形类: 存放三角形的三条边的周长
问题二
public class Staff {
protected String name, address, sex;
protected int age;
protected double salary;
protected LocalDate dataHired;}
职工类: 存放职工的姓名,地址,性别,年龄,工资,入职日期信息。
public class Teacher extends Staff{
protected String department;
protected String speciality;
protected double postAllowance;}
教师类: 存放教师除了职工信息外的系,专业,岗位津贴信息。
public class SecurityGuard extends Staff {
private String skills;
private double dangerousAllowance;}
保安类: 存放保安除了职工信息外的专技和高危津贴信息。
public class Dean extends Teacher{
private double adminAward;}
院长类: 存放院长除了教师信息外的行政补贴
问题三
public class Staff {
    protected String name, address, sex;
    protected int age;
    protected double salary;
    protected LocalDate dataHired;}
职工类: 存放职工的姓名,地址,性别,年龄,工资,入职日期信息。
public class Teacher extends Staff{
    protected String department;
    protected String speciality;
    protected double postAllowance;}
教师类: 存放教师除了职工信息外的系,专业,岗位津贴信息。
public class SecurityGuard extends Staff {
    private String skills;
    private double dangerousAllowance;}
保安类: 存放保安除了职工信息外的专技和高危津贴信息。
public class Dean extends Teacher{
    private double adminAward;}
院长类: 存放院长除了教师信息外的行政补贴

主要代码结构


三、实验使用环境

软件:java version "18.0.2"

EclipseIDE 2022-06

平台:win10

四、实验步骤和调试过程

exp1:Shape与Comparable接口

需求

改写实验4中的抽象类Shape,让其实现Comparable接口,实现对面积的排序;

在测试类中创建Shape类型的数组或动态数组,里面有若干个Circle、Rectangle和Triangle对象,然后利用Arrays.sort或Collections.sort对该Shape数组或动态数组进行排序;输出排序后Shapes数组。

实验步骤

Shape类

package exp1;

import java.util.Arrays;
import java.util.Comparator;

abstract public class Shape implements Comparable{
    public final static double PI = 3.1415926;
    public abstract double  getPerimeter();
    public abstract double getArea();

    public int compareTo(Shape o)
    {
        return Double.compare(this.getArea(), o.getArea());
    }
}

测试类

package exp1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import static java.lang.Math.random;

public class Test
{
    public static void main(String[] args)
    {
        System.out.println("测试内容:2个三角形,2个圆形,2个长方形的面积排序");

        Shape[] shapes = new Shape[6];
        shapes[0] = new Rectangle(random() * 100, random() * 100);
        shapes[1] = new Rectangle(random() * 100, random() * 100);
        shapes[2] = new Triangle(random() * 100, random() * 100, random() * 100);
        shapes[3] = new Triangle(random() * 100, random() * 100, random() * 100);
        shapes[4] = new Circle(random() * 100);
        shapes[5] = new Circle(random() * 100);

        System.out.println("测试面积排序-------------------------------------------- ");
        Arrays.sort(shapes);
        for (Shape i : shapes)
            System.out.println(i.getArea());
    }
}

Circle类(与实验四同,无修改)

package exp1;

import java.util.Objects;

import static java.lang.Math.random;

public class Circle extends Shape
{
    private double r;

    public Circle()
    {
        r = random();
    }

    public Circle(double r)
    {
        this.r = r;
    }

    public double getPerimeter()
    {
        return 2 * Math.PI * r;
    }

    public double getArea()
    {
        return Math.PI * r * r;
    }

    public double getR()
    {
        return r;
    }

    public void setR(double r)
    {
        this.r = r;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Circle circle = (Circle) o;
        return Double.compare(circle.r, r) == 0;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(r);
    }

    @Override
    public String toString()
    {
        return "Circle{" +
                "r=" + r +
                '}';
    }
}

Triangle类(与实验四同,无修改)

package exp1;

import java.util.Arrays;
import java.util.Objects;

import static java.lang.Math.random;
import static java.lang.Math.sqrt;

public class Triangle extends Shape
{
    private double a, b, c;

    public Triangle()
    {
        a = random();
        b = random();
        c = random();
    }

    public Triangle(double a, double b, double c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getPerimeter()
    {
        return a + b + c;
    }

    public double getArea()
    {
        double p = (a + b + c) / 2;

        double[] length = {a, b, c};
        Arrays.sort(length);
        if (length[0] + length[1] <= length[2])
            return 0;
        return sqrt(p * (p - a) * (p - b) * (p - c));
    }

    public double getA()
    {
        return a;
    }

    public double getB()
    {
        return b;
    }

    public double getC()
    {
        return c;
    }

    public void setA(double a)
    {
        this.a = a;
    }

    public void setB(double b)
    {
        this.b = b;
    }

    public void setC(double c)
    {
        this.c = c;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Triangle triangle = (Triangle) o;
        return Double.compare(triangle.a, a) == 0 && Double.compare(triangle.b, b) == 0 && Double.compare(triangle.c, c) == 0;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(a, b, c);
    }

    @Override
    public String toString()
    {
        return "Triangle{" +
                "a=" + a +
                ", b=" + b +
                ", c=" + c +
                '}';
    }
}

Rectangle类(与实验四同,无修改)

package exp1;

import java.util.Objects;

import static java.lang.Math.random;

public class Rectangle extends Shape
{
    private double width, height;

    public Rectangle()
    {
        width = random();
        height = random();
    }

    public Rectangle(double a, double b)
    {
        width = a;
        height = b;
    }

    public double getPerimeter()
    {
        return 2 * (width + height);
    }

    public double getArea()
    {
        return width * height;
    }


    public double getWidth()
    {
        return width;
    }

    public double getHeight()
    {
        return height;
    }

    public void setWidth(double width)
    {
        this.width = width;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Rectangle rectangle = (Rectangle) o;
        return Double.compare(rectangle.width, width) == 0 && Double.compare(rectangle.height, height) == 0;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(width, height);
    }

    @Override
    public String toString()
    {
        return "Rectangle{" +
                "width=" + width +
                ", height=" + height +
                '}';
    }
}

测试数据设计

2个三角形,2个长方形,2个圆形

测试内容:2个三角形,2个圆形,2个长方形的面积排序
测试面积排序-------------------------------------------- 
13.29407932008806
355.9673605592515
1408.771828943242
2766.254050847555
4067.2443482681883
13242.47508864874

exp2:Staff与Comparator接口

需求

  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类或者其子类对象的薪水
③编写一方法private static void sortBySalary(Staff[] staffs),支持对Staff类及其子类按照各自的薪水降序排序;
④编写一方法private static void sortByAge(Staff[] staffs),对Staff对象按照年龄升序排序,再编写一个方法按name升序进行排序;
⑤编写一方法sortByDateHired,支持对Staff类及其子类按照各自的dateHired升序排序,可以使用java.util.Date或java.time.LocalDateTime类型的compareTo方法。

实验步骤

package exp2;


import javax.security.auth.callback.NameCallback;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;


public class Staff
{
    protected String name, address, sex;
    protected int age;
    protected double salary;
    protected LocalDate dataHired;

    public Staff(String name, String address, String sex, int age, double salary, LocalDate dataHired)
    {
        this.name = name;
        this.address = address;
        this.sex = sex;
        this.age = age;
        this.salary = salary;
        this.dataHired = dataHired;
    }


    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getSex()
    {
        return sex;
    }

    public void setSex(String sex)
    {
        this.sex = sex;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public double getSalary()
    {
        return salary;
    }

    public void setSalary(double salary)
    {
        this.salary = salary;
    }

    public LocalDate getDataHired()
    {
        return dataHired;
    }

    public void setDataHired(LocalDate dataHired)
    {
        this.dataHired = dataHired;
    }

    public double getSumSalary()
    {
        return salary;
    }
    @Override
    public String toString()
    {
        return "Staff{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", salary=" + getSumSalary() +
                ", dataHired=" + dataHired +
                '}';
    }
}

Test类

package exp2;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import static java.lang.Math.random;

public class Test
{
    static private int personNum = 4 * 3;
    private static int id;
    static String[] departments;
    static HashMap<String, String[]> specialists;
    static String[] skills;


    static void init()
    {
        id = 0;
        departments = new String[]{"中文系", "美术系", "音乐系", "计算机系"};
        specialists = new HashMap<>();
        specialists.put("中文系", new String[]{"汉语言文学", "汉语言", "秘书学"});
        specialists.put("美术系", new String[]{"美术教育", "书法", "雕塑", "美术"});
        specialists.put("音乐系", new String[]{"作曲系", "声乐系", "民声系", "钢琴系", "电子琴系"});
        specialists.put("计算机系", new String[]{"软件工程", "网络工程", "计算机与科学", "智能科学与技术"});
        skills = new String[]{"抓小偷", "使用防暴叉", "看门", "查看健康码", "巡逻校园", "保卫校园"};
    }


    static String getName()
    {
        return "张三" + (++id);
    }

    static String getAddress()
    {
        return "翻斗大街翻斗花园二号楼10" + id;
    }

    static String getSex()
    {
        return random() <= 0.5 ? "男" : "女";
    }

    static int getAge()
    {
        return (int) (random() * 100);
    }

    static double getSalary()
    {
        return random() * 10000;
    }

    static LocalDate getData()
    {
        int year = 2000 + (int) (random() * 22);
        int month = 1 + (int) (random() * 11);
        int day = 1 + (int) (random() * 27);
        return LocalDate.of(year, month, day);
    }

    static double getPostAllowance()
    {
        return random() * 1000;
    }

    static String getDepartment()
    {
        return departments[(int) (random() * departments.length)];
    }

    static String getSpeciality(String department)
    {
        String[] speciality = specialists.get(department);
        return speciality[(int) (random() * speciality.length)];
    }

    static String getSkill()
    {
        return skills[(int) (random() * skills.length)];
    }

    static double getDangerousAllowance()
    {
        return random() * 1000;
    }

    static double getAdminAward()
    {
        return random() * 1000;
    }


    public static void main(String[] args)
    {
        init();
        Staff[] staffs = new Staff[personNum];

        for (int i = 0; i < personNum; i += 4)
        {
            Staff staff = new Staff(getName(), getAddress(), getSex(), getAge(), getSalary(), getData());
            String department = getDepartment();
            Teacher teacher = new Teacher(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), department, getSpeciality(department), getPostAllowance());
            SecurityGuard securityGuard = new SecurityGuard(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), getSkill(), getDangerousAllowance());
            Dean dean = new Dean(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), department, getSpeciality(department), getPostAllowance(), getAdminAward());

            staffs[i] = staff;
            staffs[i + 1] = teacher;
            staffs[i + 2] = securityGuard;
            staffs[i + 3] = dean;
        }

        System.out.println("打印名字-------------------------------------------- ");
        printName(staffs);
        System.out.println("打印薪水-------------------------------------------- ");
        printSalary(staffs);


        System.out.println("按照工资降序排序-------------------------------------------- ");
        sortBySalary(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getSalary());
        System.out.println("按照年龄升序排序-------------------------------------------- ");
        sortByAge(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getAge());
        System.out.println("按照名字升序排序-------------------------------------------- ");
        sortByName(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getName());
        System.out.println("按照日期升序排序-------------------------------------------- ");
        sortByDateHired(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getDataHired());
    }

    public static void printName(Staff[] persons)
    {
        for (Staff staff : persons)
            System.out.println(staff.getName());
    }

    public static void printSalary(Staff[] staffs)
    {
        for (Staff staff : staffs)
            System.out.println(staff.getSumSalary());
    }

    //薪水降序
    public static void sortBySalary(Staff[] staffs)
    {
        Arrays.sort(staffs, new Comparator<Staff>()
        {
            @Override
            public int compare(Staff o1, Staff o2)
            {
                return Double.compare(o2.getSumSalary(),o1.getSumSalary());
            }
        });
    }

    public static void sortByAge(Staff[] staffs)
    {
        Arrays.sort(staffs, new Comparator<Staff>()
        {
            @Override
            public int compare(Staff o1, Staff o2)
            {
                return o1.getAge()-o2.getAge();
            }
        });
    }

    public static void sortByName(Staff[] staffs)
    {
        Arrays.sort(staffs, new Comparator<Staff>()
        {
            @Override
            public int compare(Staff o1, Staff o2)
            {
                return o1.getName().compareTo(o2.getName());
            }
        });
    }

    public static void sortByDateHired(Staff[] staffs)
    {
        Arrays.sort(staffs, new Comparator<Staff>()
        {
            @Override
            public int compare(Staff o1, Staff o2)
            {
                return o1.getDataHired().compareTo(o2.getDataHired());
            }
        });
    }

    public static void print(Staff[] staffs)
    {
        for (Staff staff : staffs)
            System.out.println(staff.toString());
        System.out.println("\n\n");
    }
}

Dean类(与实验四同,无修改)

package exp2;

import java.time.LocalDate;

public class Dean extends Teacher
{
    private double adminAward;

    public Dean(String name, String address, String sex, int age, double salary, LocalDate dataHired, String department, String speciality, double postAllowance, double adminAward)
    {
        super(name, address, sex, age, salary, dataHired, department, speciality, postAllowance);
        this.adminAward = adminAward;
    }

    public double getAdminAward()
    {
        return adminAward;
    }

    public void setAdminAward(double adminAward)
    {
        this.adminAward = adminAward;
    }

    public double getSumSalary(){return super.getSumSalary()+adminAward;}
    @Override
    public String toString()
    {
        return "Dean{" +
                "adminAward=" + adminAward +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", salary=" + getSumSalary() +
                ", dataHired=" + dataHired +
                '}';
    }
}

SecurityGuard 类(与实验四同,无修改)

package exp2;

import java.time.LocalDate;

public class SecurityGuard extends Staff
{
    private String skills;
    private double dangerousAllowance;

    public SecurityGuard(String name, String address, String sex, int age, double salary, LocalDate dataHired, String skills, double dangerousAllowance)
    {
        super(name, address, sex, age, salary, dataHired);
        this.skills = skills;
        this.dangerousAllowance = dangerousAllowance;
    }

    public String getSkills()
    {
        return skills;
    }

    public void setSkills(String skills)
    {
        this.skills = skills;
    }

    public double getDangerousAllowance()
    {
        return dangerousAllowance;
    }

    public void setDangerousAllowance(double dangerousAllowance)
    {
        this.dangerousAllowance = dangerousAllowance;
    }

    public double getSumSalary(){return super.getSumSalary()+getDangerousAllowance();}

    @Override
    public String toString()
    {
        return "SecurityGuard{" +
                "skills='" + skills + '\'' +
                ", dangerousAllowance=" + dangerousAllowance +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", salary=" + getSumSalary() +
                ", dataHired=" + dataHired +
                '}';
    }
}

Teacher 类(与实验四同,无修改)

package exp2;

import java.time.LocalDate;

public class Teacher extends Staff
{
    private String department;
    private String speciality;
    protected double postAllowance;

    public Teacher(String name, String address, String sex, int age, double salary, LocalDate dataHired, String department, String speciality, double postAllowance)
    {
        super(name, address, sex, age, salary, dataHired);
        this.department = department;
        this.speciality = speciality;
        this.postAllowance = postAllowance;
    }

    public String getDepartment()
    {
        return department;
    }

    public void setDepartment(String department)
    {
        this.department = department;
    }

    public String getSpeciality()
    {
        return speciality;
    }

    public void setSpeciality(String speciality)
    {
        this.speciality = speciality;
    }

    public double getPostAllowance()
    {
        return postAllowance;
    }

    public void setPostAllowance(double postAllowance)
    {
        this.postAllowance = postAllowance;
    }

    public double getSumSalary(){return super.getSumSalary()+postAllowance;}
    @Override
    public String toString()
    {
        return "Teacher{" +
                "department='" + department + '\'' +
                ", speciality='" + speciality + '\'' +
                ", postAllowance=" + postAllowance +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", salary=" + getSumSalary() +
                ", dataHired=" + dataHired +
                '}';
    }
}

测试数据设计

3个职工,3个老师,3个保安,3个院长

测试结果分析

打印名字-------------------------------------------- 
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
张三10
张三11
张三12


打印薪水-------------------------------------------- 
5865.013286479973
5645.101089689207
4131.130394177577
1366.5247904880703
4467.609144333133
4949.127485104683
2784.756380796071
4422.828379359303
8739.216839198101
9107.852933826107
10210.518851524717
5863.136518032674
工资降序
(仅打印工资)
9413.417909244647
7469.1136435471835
6904.001140059544
6185.161279714392
4648.70052154496
4261.355205662827
4555.329912932398
3814.4327301334124
2870.7409052259823
2542.4122056635547
2537.4464930176123
2261.7745885328777
按照工资降序排序
(打印所有信息)
SecurityGuard{skills='查看健康码', dangerousAllowance=255.19945357671492, name='张三11', address='翻斗大街翻斗花园二号楼1011', sex='女', age=24, salary=10210.518851524717, dataHired=2010-06-02}
Teacher{department='音乐系', speciality='钢琴系', postAllowance=963.2910329132834, name='张三10', address='翻斗大街翻斗花园二号楼1010', sex='男', age=36, salary=9107.852933826107, dataHired=2004-02-25}
Staff{name='张三9', address='翻斗大街翻斗花园二号楼109', sex='男', age=48, salary=8739.216839198101, dataHired=2006-01-15}
Staff{name='张三1', address='翻斗大街翻斗花园二号楼101', sex='女', age=1, salary=5865.013286479973, dataHired=2009-01-24}
Dean{adminAward=950.1295659399076, name='张三12', address='翻斗大街翻斗花园二号楼1012', sex='男', age=64, salary=5863.136518032674, dataHired=2004-09-02}
Teacher{department='音乐系', speciality='电子琴系', postAllowance=435.8243423482432, name='张三2', address='翻斗大街翻斗花园二号楼102', sex='男', age=96, salary=5645.101089689207, dataHired=2017-09-17}
Teacher{department='音乐系', speciality='作曲系', postAllowance=277.6722989042707, name='张三6', address='翻斗大街翻斗花园二号楼106', sex='男', age=28, salary=4949.127485104683, dataHired=2001-05-19}
Staff{name='张三5', address='翻斗大街翻斗花园二号楼105', sex='女', age=73, salary=4467.609144333133, dataHired=2007-06-01}
Dean{adminAward=733.5199557969281, name='张三8', address='翻斗大街翻斗花园二号楼108', sex='女', age=55, salary=4422.828379359303, dataHired=2012-07-13}
SecurityGuard{skills='巡逻校园', dangerousAllowance=200.47650075084667, name='张三3', address='翻斗大街翻斗花园二号楼103', sex='男', age=81, salary=4131.130394177577, dataHired=2006-07-20}
SecurityGuard{skills='保卫校园', dangerousAllowance=778.1411392841513, name='张三7', address='翻斗大街翻斗花园二号楼107', sex='男', age=31, salary=2784.756380796071, dataHired=2018-07-09}
Dean{adminAward=932.3427637998242, name='张三4', address='翻斗大街翻斗花园二号楼104', sex='女', age=85, salary=1366.5247904880703, dataHired=2014-11-01}
年龄升序
(仅打印年龄)
5
8
26
33
35
35
49
50
61
92
97
98
按照年龄升序排序(打印所有信息)-------------------------------------------- 
Staff{name='张三1', address='翻斗大街翻斗花园二号楼101', sex='女', age=1, salary=5865.013286479973, dataHired=2009-01-24}
SecurityGuard{skills='查看健康码', dangerousAllowance=255.19945357671492, name='张三11', address='翻斗大街翻斗花园二号楼1011', sex='女', age=24, salary=10210.518851524717, dataHired=2010-06-02}
Teacher{department='音乐系', speciality='作曲系', postAllowance=277.6722989042707, name='张三6', address='翻斗大街翻斗花园二号楼106', sex='男', age=28, salary=4949.127485104683, dataHired=2001-05-19}
SecurityGuard{skills='保卫校园', dangerousAllowance=778.1411392841513, name='张三7', address='翻斗大街翻斗花园二号楼107', sex='男', age=31, salary=2784.756380796071, dataHired=2018-07-09}
Teacher{department='音乐系', speciality='钢琴系', postAllowance=963.2910329132834, name='张三10', address='翻斗大街翻斗花园二号楼1010', sex='男', age=36, salary=9107.852933826107, dataHired=2004-02-25}
Staff{name='张三9', address='翻斗大街翻斗花园二号楼109', sex='男', age=48, salary=8739.216839198101, dataHired=2006-01-15}
Dean{adminAward=733.5199557969281, name='张三8', address='翻斗大街翻斗花园二号楼108', sex='女', age=55, salary=4422.828379359303, dataHired=2012-07-13}
Dean{adminAward=950.1295659399076, name='张三12', address='翻斗大街翻斗花园二号楼1012', sex='男', age=64, salary=5863.136518032674, dataHired=2004-09-02}
Staff{name='张三5', address='翻斗大街翻斗花园二号楼105', sex='女', age=73, salary=4467.609144333133, dataHired=2007-06-01}
SecurityGuard{skills='巡逻校园', dangerousAllowance=200.47650075084667, name='张三3', address='翻斗大街翻斗花园二号楼103', sex='男', age=81, salary=4131.130394177577, dataHired=2006-07-20}
Dean{adminAward=932.3427637998242, name='张三4', address='翻斗大街翻斗花园二号楼104', sex='女', age=85, salary=1366.5247904880703, dataHired=2014-11-01}
Teacher{department='音乐系', speciality='电子琴系', postAllowance=435.8243423482432, name='张三2', address='翻斗大街翻斗花园二号楼102', sex='男', age=96, salary=5645.101089689207, dataHired=2017-09-17}
名字升序
(仅打印名字)
张三1
张三10
张三11
张三12
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
按照名字升序排序(打印所有信息)-------------------------------------------- 
Staff{name='张三1', address='翻斗大街翻斗花园二号楼101', sex='女', age=1, salary=5865.013286479973, dataHired=2009-01-24}
Teacher{department='音乐系', speciality='钢琴系', postAllowance=963.2910329132834, name='张三10', address='翻斗大街翻斗花园二号楼1010', sex='男', age=36, salary=9107.852933826107, dataHired=2004-02-25}
SecurityGuard{skills='查看健康码', dangerousAllowance=255.19945357671492, name='张三11', address='翻斗大街翻斗花园二号楼1011', sex='女', age=24, salary=10210.518851524717, dataHired=2010-06-02}
Dean{adminAward=950.1295659399076, name='张三12', address='翻斗大街翻斗花园二号楼1012', sex='男', age=64, salary=5863.136518032674, dataHired=2004-09-02}
Teacher{department='音乐系', speciality='电子琴系', postAllowance=435.8243423482432, name='张三2', address='翻斗大街翻斗花园二号楼102', sex='男', age=96, salary=5645.101089689207, dataHired=2017-09-17}
SecurityGuard{skills='巡逻校园', dangerousAllowance=200.47650075084667, name='张三3', address='翻斗大街翻斗花园二号楼103', sex='男', age=81, salary=4131.130394177577, dataHired=2006-07-20}
Dean{adminAward=932.3427637998242, name='张三4', address='翻斗大街翻斗花园二号楼104', sex='女', age=85, salary=1366.5247904880703, dataHired=2014-11-01}
Staff{name='张三5', address='翻斗大街翻斗花园二号楼105', sex='女', age=73, salary=4467.609144333133, dataHired=2007-06-01}
Teacher{department='音乐系', speciality='作曲系', postAllowance=277.6722989042707, name='张三6', address='翻斗大街翻斗花园二号楼106', sex='男', age=28, salary=4949.127485104683, dataHired=2001-05-19}
SecurityGuard{skills='保卫校园', dangerousAllowance=778.1411392841513, name='张三7', address='翻斗大街翻斗花园二号楼107', sex='男', age=31, salary=2784.756380796071, dataHired=2018-07-09}
Dean{adminAward=733.5199557969281, name='张三8', address='翻斗大街翻斗花园二号楼108', sex='女', age=55, salary=4422.828379359303, dataHired=2012-07-13}
Staff{name='张三9', address='翻斗大街翻斗花园二号楼109', sex='男', age=48, salary=8739.216839198101, dataHired=2006-01-15}
日期升序
(仅打印日期)
2003-05-21
2004-11-26
2006-08-19
2009-07-04
2009-09-07
2010-09-23
2014-10-08
2015-02-24
2017-06-18
2017-10-08
2020-03-13
2020-10-03
按照日期升序排序
(打印所有日期)-------------------------------------------- 
Teacher{department='音乐系', speciality='作曲系', postAllowance=277.6722989042707, name='张三6', address='翻斗大街翻斗花园二号楼106', sex='男', age=28, salary=4949.127485104683, dataHired=2001-05-19}
Teacher{department='音乐系', speciality='钢琴系', postAllowance=963.2910329132834, name='张三10', address='翻斗大街翻斗花园二号楼1010', sex='男', age=36, salary=9107.852933826107, dataHired=2004-02-25}
Dean{adminAward=950.1295659399076, name='张三12', address='翻斗大街翻斗花园二号楼1012', sex='男', age=64, salary=5863.136518032674, dataHired=2004-09-02}
Staff{name='张三9', address='翻斗大街翻斗花园二号楼109', sex='男', age=48, salary=8739.216839198101, dataHired=2006-01-15}
SecurityGuard{skills='巡逻校园', dangerousAllowance=200.47650075084667, name='张三3', address='翻斗大街翻斗花园二号楼103', sex='男', age=81, salary=4131.130394177577, dataHired=2006-07-20}
Staff{name='张三5', address='翻斗大街翻斗花园二号楼105', sex='女', age=73, salary=4467.609144333133, dataHired=2007-06-01}
Staff{name='张三1', address='翻斗大街翻斗花园二号楼101', sex='女', age=1, salary=5865.013286479973, dataHired=2009-01-24}
SecurityGuard{skills='查看健康码', dangerousAllowance=255.19945357671492, name='张三11', address='翻斗大街翻斗花园二号楼1011', sex='女', age=24, salary=10210.518851524717, dataHired=2010-06-02}
Dean{adminAward=733.5199557969281, name='张三8', address='翻斗大街翻斗花园二号楼108', sex='女', age=55, salary=4422.828379359303, dataHired=2012-07-13}
Dean{adminAward=932.3427637998242, name='张三4', address='翻斗大街翻斗花园二号楼104', sex='女', age=85, salary=1366.5247904880703, dataHired=2014-11-01}
Teacher{department='音乐系', speciality='电子琴系', postAllowance=435.8243423482432, name='张三2', address='翻斗大街翻斗花园二号楼102', sex='男', age=96, salary=5645.101089689207, dataHired=2017-09-17}
SecurityGuard{skills='保卫校园', dangerousAllowance=778.1411392841513, name='张三7', address='翻斗大街翻斗花园二号楼107', sex='男', age=31, salary=2784.756380796071, dataHired=2018-07-09}

exp3:Lambda表达式

需求

使用Lambda表达式简化上述"Staff与Comparator接口"题目中的NameComparator,AgeComparator和ComplexComparator比较器。测试方法与上述相同。

实验步骤
Shape类

按照日期升序排序
(打印所有日期)-------------------------------------------- 
Teacher{department='音乐系', speciality='作曲系', postAllowance=277.6722989042707, name='张三6', address='翻斗大街翻斗花园二号楼106', sex='男', age=28, salary=4949.127485104683, dataHired=2001-05-19}
Teacher{department='音乐系', speciality='钢琴系', postAllowance=963.2910329132834, name='张三10', address='翻斗大街翻斗花园二号楼1010', sex='男', age=36, salary=9107.852933826107, dataHired=2004-02-25}
Dean{adminAward=950.1295659399076, name='张三12', address='翻斗大街翻斗花园二号楼1012', sex='男', age=64, salary=5863.136518032674, dataHired=2004-09-02}
Staff{name='张三9', address='翻斗大街翻斗花园二号楼109', sex='男', age=48, salary=8739.216839198101, dataHired=2006-01-15}
SecurityGuard{skills='巡逻校园', dangerousAllowance=200.47650075084667, name='张三3', address='翻斗大街翻斗花园二号楼103', sex='男', age=81, salary=4131.130394177577, dataHired=2006-07-20}
Staff{name='张三5', address='翻斗大街翻斗花园二号楼105', sex='女', age=73, salary=4467.609144333133, dataHired=2007-06-01}
Staff{name='张三1', address='翻斗大街翻斗花园二号楼101', sex='女', age=1, salary=5865.013286479973, dataHired=2009-01-24}
SecurityGuard{skills='查看健康码', dangerousAllowance=255.19945357671492, name='张三11', address='翻斗大街翻斗花园二号楼1011', sex='女', age=24, salary=10210.518851524717, dataHired=2010-06-02}
Dean{adminAward=733.5199557969281, name='张三8', address='翻斗大街翻斗花园二号楼108', sex='女', age=55, salary=4422.828379359303, dataHired=2012-07-13}
Dean{adminAward=932.3427637998242, name='张三4', address='翻斗大街翻斗花园二号楼104', sex='女', age=85, salary=1366.5247904880703, dataHired=2014-11-01}
Teacher{department='音乐系', speciality='电子琴系', postAllowance=435.8243423482432, name='张三2', address='翻斗大街翻斗花园二号楼102', sex='男', age=96, salary=5645.101089689207, dataHired=2017-09-17}
SecurityGuard{skills='保卫校园', dangerousAllowance=778.1411392841513, name='张三7', address='翻斗大街翻斗花园二号楼107', sex='男', age=31, salary=2784.756380796071, dataHired=2018-07-09}

Test类

package exp3;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import static java.lang.Math.random;

public class Test
{
    static private int personNum = 4 * 3;
    private static int id;
    static String[] departments;
    static HashMap<String, String[]> specialists;
    static String[] skills;


    static void init()
    {
        id = 0;
        departments = new String[]{"中文系", "美术系", "音乐系", "计算机系"};
        specialists = new HashMap<>();
        specialists.put("中文系", new String[]{"汉语言文学", "汉语言", "秘书学"});
        specialists.put("美术系", new String[]{"美术教育", "书法", "雕塑", "美术"});
        specialists.put("音乐系", new String[]{"作曲系", "声乐系", "民声系", "钢琴系", "电子琴系"});
        specialists.put("计算机系", new String[]{"软件工程", "网络工程", "计算机与科学", "智能科学与技术"});
        skills = new String[]{"抓小偷", "使用防暴叉", "看门", "查看健康码", "巡逻校园", "保卫校园"};
    }


    static String getName()
    {
        return "张三" + (++id);
    }

    static String getAddress()
    {
        return "翻斗大街翻斗花园二号楼10" + id;
    }

    static String getSex()
    {
        return random() <= 0.5 ? "男" : "女";
    }

    static int getAge()
    {
        return (int) (random() * 100);
    }

    static double getSalary()
    {
        return random() * 10000;
    }

    static LocalDate getData()
    {
        int year = 2000 + (int) (random() * 22);
        int month = 1 + (int) (random() * 11);
        int day = 1 + (int) (random() * 27);
        return LocalDate.of(year, month, day);
    }

    static double getPostAllowance()
    {
        return random() * 1000;
    }

    static String getDepartment()
    {
        return departments[(int) (random() * departments.length)];
    }

    static String getSpeciality(String department)
    {
        String[] speciality = specialists.get(department);
        return speciality[(int) (random() * speciality.length)];
    }

    static String getSkill()
    {
        return skills[(int) (random() * skills.length)];
    }

    static double getDangerousAllowance()
    {
        return random() * 1000;
    }

    static double getAdminAward()
    {
        return random() * 1000;
    }


    public static void main(String[] args)
    {
        init();
        Staff[] staffs = new Staff[personNum];

        for (int i = 0; i < personNum; i += 4)
        {
            Staff staff = new Staff(getName(), getAddress(), getSex(), getAge(), getSalary(), getData());
            String department = getDepartment();
            Teacher teacher = new Teacher(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), department, getSpeciality(department), getPostAllowance());
            SecurityGuard securityGuard = new SecurityGuard(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), getSkill(), getDangerousAllowance());
            Dean dean = new Dean(getName(), getAddress(), getSex(), getAge(), getSalary(), getData(), department, getSpeciality(department), getPostAllowance(), getAdminAward());

            staffs[i] = staff;
            staffs[i + 1] = teacher;
            staffs[i + 2] = securityGuard;
            staffs[i + 3] = dean;
        }

        System.out.println("测试" + (int) (7.5 - 7.4));

        System.out.println("打印名字-------------------------------------------- ");
        printName(staffs);
        System.out.println("打印薪水-------------------------------------------- ");
        printSalary(staffs);


        System.out.println("按照工资降序排序-------------------------------------------- ");
        sortBySalary(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getSalary());
        System.out.println("按照年龄升序排序-------------------------------------------- ");
        sortByAge(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getAge());
        System.out.println("按照名字升序排序-------------------------------------------- ");
        sortByName(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getName());
        System.out.println("按照日期升序排序-------------------------------------------- ");
        sortByDateHired(staffs);
//        print(staffs);
        for(Staff i:staffs)
            System.out.println(i.getDataHired());
    }

    public static void printName(Staff[] persons)
    {
        for (Staff staff : persons)
            System.out.println(staff.getName());
    }

    public static void printSalary(Staff[] staffs)
    {
        for (Staff staff : staffs)
            System.out.println(staff.getSumSalary());
    }

    //薪水降序
    public static void sortBySalary(Staff[] staffs)
    {
        Arrays.sort(staffs, (Staff o1, Staff o2)->Double.compare(o2.getSumSalary(),o1.getSumSalary()));
    }

    public static void sortByAge(Staff[] staffs)
    {
        Arrays.sort(staffs,(Staff o1, Staff o2)->o1.getAge()-o2.getAge());
    }

    public static void sortByName(Staff[] staffs)
    {
        Arrays.sort(staffs, (Staff o1, Staff o2)->o1.getName().compareTo(o2.getName()));
    }

    public static void sortByDateHired(Staff[] staffs)
    {
        Arrays.sort(staffs, (Staff o1, Staff o2) ->o1.getDataHired().compareTo(o2.getDataHired()));
    }

    public static void print(Staff[] staffs)
    {
        for (Staff staff : staffs)
            System.out.println(staff.toString());
        System.out.println("\n\n");
    }
}

测试数据设计

与问题二同

测试结果分析

与问题二同

五、实验小结

实验中遇到的问题及解决过程

实验中产生的错误及原因分析

实验体会和收获

掌握了接口的定义与实现;

熟悉了内部类机制;

学会使用Comparable、Comparator接口;

学会使用简单的Lambda表达式。

posted @ 2023-08-22 19:08  jijfurhg  阅读(19)  评论(0编辑  收藏  举报