算法練習(24):日期類設計(1.2.11-1.2.12)

本系列博客習題來自《算法(第四版)》,算是本人的讀書筆記,如果有人在讀這本書的,歡迎大家多多交流。為了方便討論,本人新建了一個微信群(算法交流),想要加入的,請?zhí)砑游业奈⑿盘枺簔hujinhui207407 謝謝。另外,本人的個人博客 http://www.kyson.cn 也在不停的更新中,歡迎一起討論

算法(第4版)

知識點

  • 日期類的設計
  • 異常處理
  • 蔡勒公式

題目

1.2.11 根據Date的API實現一個SmartDate類型,在日期非法時拋出一個異常。


1.2.11 Develop an implementation SmartDate of our Date API that raises an excep- tion if the date is not legal.

答案

分析

本人所有簡書的算法文章詳細分析已經移入小專欄:算法四習題詳解,歡迎大家訂閱

public class SmartDate {
    
    @SuppressWarnings("unused")
    private final int year;
    @SuppressWarnings("unused")
    private final int month;
    @SuppressWarnings("unused")
    private final int day;
    
    public SmartDate(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 + "月小于31號");
                throw exception;
            }
            
            int leapYear = year % 4;
            if (leapYear != 0) {
                if (day > 28) {
                    Exception exception = new Exception(month + "月小于29號");
                    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;
    }
    

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        SmartDate date = new SmartDate(2007, 1, 50);
    }

}

代碼索引

SmartDate1.java

視頻講解

點此觀看分析視頻

題目

1.2.12 為SmartDate添加一個方法dayOfTheWeek(),為日期中每周的日返回Monday、Tuesday、Wednesday、Thursday……假定是21世紀


1.2.12 Add a method dayOfTheWeek() to SmartDate that returns a String value Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday, giving the ap- propriate day of the week for the date. You may assume that the date is in the 21st century.

答案

public class SmartDate2 {

    @SuppressWarnings("unused")
    private final int year;
    @SuppressWarnings("unused")
    private final int month;
    @SuppressWarnings("unused")
    private final int day;
    
    private static final int YEARFIRSTTWO = 20;
    private static final int DAYPERWEEK = 7;

    public SmartDate2(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 + "月小于31號");
                throw exception;
            }

            int leapYear = year % 4;
            if (leapYear != 0) {
                if (day > 28) {
                    Exception exception = new Exception(month + "月小于29號");
                    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;
    }
    
    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 static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        SmartDate2 date = new SmartDate2(2012, 2, 28);
        String week = date.dayOfTheWeek();
        System.out.println( date + " is :" + week);
    }

}

代碼索引

SmartDate2.java

視頻講解

點此觀看分析視頻

注意

這里的SmartDate1,SmartDate2,實現還不夠完善,會在下一篇文章
算法練習(25):Transaction(1.2.13-1.2.14)中進行更改。

廣告

我的首款個人開發(fā)的APP壁紙寶貝上線了,歡迎大家下載。

本人所有簡書的算法文章已經移入小專欄:算法四習題詳解,歡迎大家訂閱

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容