算法-第四版-练习1.2.11-1.2.14解答

题目

11.根据Date的API实现一个SmartDate类型,在日期非法时抛出一个异常。
12.为SmartDate添加一个方法dayOfTheWeek(),为日期中每周的日返回Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday中的适当值。你可以假定时间是21世纪。
13.用我们对Date的实现作为模板实现Transaction类型
14.用我们对Date中的equals()方法的实现作为模板,实现Transaction中的equals()方法。

代码

package homework1_2;

/**
 * @description: ${description}
 * @create: 2019-02-08
 **/
public class SmartDate {
    private int month;
    private int day;
    private int year;

    public SmartDate(int month, int day, int year) throws Exception {
        if(month<=12){
            this.month = month;
        }else {
            throw new Exception("月份错误");
        }
        if(day<=31){
            this.day=day;
        }else {
            throw new Exception("日期错误");
        }
        this.year = year;
    }

    @Override
    public String toString() {
        return "SmartDate{" +
                "month=" + month +
                ", day=" + day +
                ", year=" + year +
                '}';
    }
    public String dayOfTheWeek(){
        //W=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
        // w:星期;c:世纪-1;y:年(两位数);m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);
        // d:日;[ ]代表取整,即只要整数部分。(C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值。)
        //算出来的W除以7,余数是几就是星期几。如果余数是0,则为星期日
        //以上公式只适合于1582年10月15日之后的情形
        //2019.2.8
        //w=18+18/4+20/4-2*20+26*(14+1)/10+8-1=18+4+5-40+39+8-1=33
        //33%7=5
        //https://www.cnblogs.com/dxywx/p/3362626.html
        int m=month;
        int y=year;
        if(m==1 || m==2){
            m=m+12;
            y=y-1;
        }
        int week= (int) (y-2000+Math.floor((y-2000)/4)+5-40+Math.floor(26*(m+1)/10)+day-1);
        int weekday=week % 7;
        String[] w={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        return w[weekday];
    }

    public static void main(String[] args) {
        try {
            SmartDate sd=new SmartDate(2,8,2019);
            System.out.println(sd);
            String s = sd.dayOfTheWeek();
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

package homework1_2;

import java.util.Objects;

/**
 * @description: ${description}
 * @create: 2019-02-08
 **/
public class Transaction implements Comparable<Transaction> {
    @Override
    public int compareTo(Transaction o) {
        //asdasdasdqweqwsxgredf2324
        return 0;
    }
    private String who;
    private SmartDate date;
    private double amount;
    private String transaction;

    public Transaction(String who, SmartDate date, double amount) {
        this.who = who;
        this.date = date;
        this.amount = amount;
    }

    public Transaction(String transaction) {
        this.transaction = transaction;
    }

    public String getWho() {
        return who;
    }

    public SmartDate getDate() {
        return date;
    }

    public double getAmount() {
        return amount;
    }

    @Override
    public String toString() {
        return "Transaction{" +
                "who='" + who + '\'' +
                ", date=" + date +
                ", amount=" + amount +
                ", transaction='" + transaction + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Transaction that = (Transaction) o;
        return Double.compare(that.amount, amount) == 0 &&
                Objects.equals(who, that.who) &&
                Objects.equals(date, that.date) &&
                Objects.equals(transaction, that.transaction);
    }

    @Override
    public int hashCode() {
        return Objects.hash(who, date, amount, transaction);
    }
}

显示结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解题心得

1.考虑不够全面 闰年 月份的天数等
2.https://blog.csdn.net/furzoom/article/details/52680192这个写的好
3.先不纠结了 理解下思路就好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值