JAVA语言程序设计(第三版)10.5,10.6, 10.7

JAVA语言程序设计(第三版)10.5,10.6, 10.7

10.5 编写程序,修改Employee的定义,使它能够根据员工的年龄(age字段值)进行比较,年龄大的员工排在前面。

package com.leox;


import java.util.Arrays;

/**
 * @Description 修改Employee类,年龄大的员工排在前面。
 * @Author xrxleo
 * @Date 2020/12/17 4:57 下午
 */
public class Employee implements Comparable<Employee> {
    private int id;
    private String name;
    private int age;
    public Employee(){
        this(0,"",0);
    }
    public Employee(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString(){
        return "Employee[id = " + id +",name =" + name +",age = " + age +"]";
    }
    @Override
    public int compareTo(Employee e) {
        return e.age - this.age;
    }
    public static void main(String[] args) {
        Employee[] emps = {new Employee(101,"张大海",20),
        new Employee(102,"王小明",25)};
        Arrays.sort(emps);
        for (Employee e:emps){
            System.out.println(e);
        }
    }
}

10.6 编写程序,修改Circle方法,使它能够按圆的面积大小比较,要求compareTO()方法返回两个圆的面积差。

package com.leox;

import java.util.Arrays;

/**
 * @Description  
 * 主要代码 
 * @Override
    public int compareTo(Circle circle) {
        return (int)(getArea() - circle.getArea());
    }
 * @Author xrxleo
 * @Date 2020/12/17 4:06 下午
 */
public class Circle implements Comparable<Circle>{
    private double radius;
    public Circle(){
    }
    public Circle(double radius) {

        this.radius = radius;
    }
    public double getArea() {

        return Math.PI * radius * radius;
    }

    public double getPerieter() {

        return 2 * Math.PI * radius;
    }

    @Override
    public int compareTo(Circle circle) {
        return (int)(getArea() - circle.getArea());
    }

    public static void main(String[] args) {
        Circle[] circles = new Circle[]{
                new Circle(3.4), new Circle(2.5), new Circle(5.8),};
        System.out.println(circles[0].compareTo(circles[1]));
        //
        Arrays.sort(circles);
        for (Circle c : circles)
            System.out.printf("%6.2f%n",c.getArea());
    }
}

10.7 设计一个Position类,该类有x和y两个成员变量表示坐标。要求该类实现Comparable接口的compareTO() 方法,实现比较两个Position对象原点(0,0)的距离之差。

package com.leox;

/**
 * @Description TODO
 * @Author xrxleo
 * @Date 2020/12/17 5:19 下午
 */
public class Position implements Comparable<Position>{
    private int x;
    private int y;
    public Position() {
    }

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    //实现compareTO() 方法
    @Override
    public int compareTo(Position obj) {
        Position pos = (Position)obj;
        double dist1 = Math.sqrt(x*x + y*y);
        double dist2 = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
        return (int)(dist1 - dist2);
    }

    public static void main(String[] args) {
        Position pos1 = new Position(0,0);
        Position pos2 = new Position(3,4);
        System.out.println(pos1.compareTo(pos2));
    }
}

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值