×

算法练习(25):Transaction类设计(1.2.13-1.2.14)

96
kyson老师
2017.09.29 01:19* 字数 396

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

  • Transaction类的设计
  • Date类的纠错

题目

1.2.13 用我们对Date的实现作为模板实现Transaction类型


1.2.13 Using our implementation of Date as a model(page91),develop an implementation of Transaction.

题目

1.2.14 用我们对Date的实现作为模板实现Transaction的euqal()函数


1.2.14 Using our implementation of equals() in Date as a model(page103),develop implementations of equals() for Transaction.

分析

本人所有简书的算法文章详细分析已经移入小专栏:算法四习题详解,欢迎大家订阅

答案

public class Transaction {
    
    private Date when;
    private String who;
    private double amount;
    
    public Date when(){
        return this.when;
    }
    
    public double amount(){
        return this.amount;
    }
    
    public String who(){
        return this.who;
    }
    
    public Transaction(String who,Date when,double amount){
        this.who = who;
        this.when = when;
        this.amount = amount;
    }
    
    public String toString(){
        return "" + this.who + " at " + this.when + " transaction " + this.amount;
    }
    
    
    public boolean equals(Object x){
        if (this == x)return true;
        if (x == null) return false;
        if(this.getClass()!=x.getClass()) return false;
        Transaction that = (Transaction) x;
        if (!this.when.equals(that.when))  return false;
        if (!this.who.equals(that.who)) return false;
        if (this.amount  != that.amount)  return false;
        
        return true;
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Date date=new Date(1996,12,5);
        Transaction tran=new Transaction("Kyson",date,1000);
        StdOut.println(tran.toString());
    }

}

其中对于Date类有纠错部分如下

public class Date {

    @SuppressWarnings("unused")
    private final int year;
    @SuppressWarnings("unused")
    private final int month;
    @SuppressWarnings("unused")
    private final int day;
    
/**
* 纠错一:这里原来是写死的,现在不写死了,因为yearFirstTwo的意思是获取年份的前两位,在构造函数中再指定
*/
    private final int yearFirstTwo;
    private static final int DAYPERWEEK = 7;

    public Date(int year, int month, int day) throws Exception {
        if (year < 0 || month < 0 || day < 0) {
            Exception exception = new Exception("年月日要大于0");
            throw exception;
        }
        if (month > 12) {
            Exception exception = new Exception("年份要小于等于12");
            throw exception;
        }

        switch (month) {
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        case 1: {
            if (day > 31) {
                Exception exception = new Exception(month + "月小于31号");
                throw exception;
            }
        }
            break;
        case 2: {
            if (day > 29) {
                Exception exception = new Exception(month + "月小于等于29号");
                throw exception;
            }

            boolean leapYear = this.isLeapYear(year);
            if (!leapYear) {
                if (day > 28) {
                    Exception exception = new Exception(month + "月小于等于28号");
                    throw exception;
                }
            }

        }
            break;
        case 4:
        case 6:
        case 9:
        case 11: {
            if (day > 30) {
                Exception exception = new Exception(month + "月小于30号");
                throw exception;
            }
        }
            break;

        default:
            break;
        }

        this.day = day;
        this.year = year;
        this.month = month;
        
        yearFirstTwo = year / 100;
    }
    

/**
* 纠错二:判断闰年的方法单独写出来,以前只是单纯的取余4,现在单独写一个方法
*/

    /**
     * 判断是否为闰年
     * 判断闰年的方法:闰年满足两个条件(满足一个即为闰年)
     * 1、能被4整除但不能被100整除
     * 2、能被400整除
     * @param year
     * @return
     */
    private boolean isLeapYear(int year)  
    {
        if(year%100!=0&&year%4==0)
            return true;
        else if(year%100==0&&year%400==0)
            return true;
        else
            return false;
    }
    
    public String dayOfTheWeek(){
        String resultWeek = "";
        int tempMonth = this.month;
        int tempYear = this.year;
        int tempDay = this.day;
        if (this.month == 1 || this.month == 2) {
            tempMonth += 12;
            tempYear --;
        }
        
        int y = tempYear - yearFirstTwo * 100;
        int floor1 = (int) Math.floor(y/4);
        int floor2 = (int) (yearFirstTwo / 4);
        int floor3 = (int) Math.floor(26 * (tempMonth+1)/10);
        
        int w = y + floor1 + floor2 -2 * yearFirstTwo + floor3 + tempDay - 1;
        int key = w % DAYPERWEEK;
        
        if (key <0) {
            key = key + 7;
        }
        
        
        switch (key) {
        case 0:
            resultWeek = "星期日";
            break;
        case 1:
            resultWeek = "星期一";
            break;
        case 2:
            resultWeek = "星期二";
            break;
        case 3:
            resultWeek = "星期三";
            break;
        case 4:
            resultWeek = "星期四";
            break;
        case 5:
            resultWeek = "星期五";
            break;
        case 6:
            resultWeek = "星期六";
            break;

        default:
            break;
        }
        
        return resultWeek;
    }
    
    public String toString(){
        
        return ""+ month + "/" + day + "/" + year;
    }
    
    public boolean equals(Object that){
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        
        Date xDate = (Date)that;

        if (this.year != xDate.year) return false;
        if (this.month != xDate.month) return false;
        if (this.day != xDate.day) return false;
        
        return true;
    }
    
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Date date = new Date(1996, 12, 5);
        String week = date.dayOfTheWeek();
        System.out.println( date + " is :" + week);
    }
    

}

纠错一:这里原来是写死的,现在不写死了,因为yearFirstTwo的意思是获取年份的前两位,在构造函数中再指定
纠错二:判断闰年的方法单独写出来,以前只是单纯的取余4,现在单独写一个方法

代码索引

Date.java
Transaction.java

视频讲解

点此观看分析视频

广告

我的首款个人开发的APP壁纸宝贝上线了,欢迎大家下载。

日记本
Web note ad 1