算法-第四版-练习1.2.12解答

来源:互联网 发布:京挑客cms系统 编辑:程序博客网 时间:2024/05/18 15:51

为SmartDate添加一个方法dayOfTheWeek(),为日期中每周的日返回Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday中的适当值。你可以假定时间是21世纪。


/** * Description :  * Author      : mn@furzoom.com * Date        : Sep 27, 2016 9:45:00 AM * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved. */package com.furzoom.lab.algs.ch102;class SmartDate{    private final int month;    private final int day;    private final int year;    private final int[] months =         {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    private final int[] days =         {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};    private final int[] daysLeap =          {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};    private final String[] weekdays = {"Sunday", "Monday", "Tuesday", "Wednesday",             "Thursday", "Friday", "Saturday"};        public SmartDate(int m, int d, int y)    {        if (!validate(m, d, y))            throw new IllegalArgumentException("Argument illegal " + m + "/" + d + "/" + y);        this.month = m;        this.day = d;        this.year = y;    }        public int month()    {        return month;    }        public int day()    {        return day;    }        public int year()    {        return year;    }        public String dayOfTheWeek()    {        // based on 1/1/2000        int totalDays;        if (isLeapYear())        {            totalDays = daysLeap[month] + day;        }        else        {            totalDays = days[month] + day;        }                for (int i = 2000; i < year; i++)        {            if (isLeapYear(i))                totalDays += 366;            else                totalDays += 365;        }                // 1/1/2000 is Saturday        return weekdays[((totalDays - 1) % 7 + 6) % 7];    }        public String toString()    {        return month + "/" + day + "/" + year;    }        private boolean validate(int m, int d, int y)    {        if (y == 0 || y < -1000 || y > 10000)            return false;                if (m < 1 || m > 12)            return false;                if (d < 1 || d > 31)            return false;                if (d > months[m])            return false;                if (!isLeapYear() && d > 28)            return false;                return true;    }        private boolean isLeapYear(int y)    {        if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))        {            return true;        }        else        {            return false;        }    }        private boolean isLeapYear()    {        return isLeapYear(year);    }}

测试:

/** * Description :  * Author      : mn@furzoom.com * Date        : Sep 27, 2016 9:42:39 AM * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved. */package com.furzoom.lab.algs.ch102;/** * ClassName    : E10212 <br> * Function     : TODO ADD FUNCTION. <br> * date         : Sep 27, 2016 9:42:39 AM <br> *  * @version  */public class E10212{    public static void main(String[] args)    {        SmartDate date = new SmartDate(1, 1, 2000);        System.out.println(date + " " + date.dayOfTheWeek());                date = new SmartDate(2, 1, 2000);        System.out.println(date + " " + date.dayOfTheWeek());                date = new SmartDate(12, 29, 2000);        System.out.println(date + " " + date.dayOfTheWeek());                date = new SmartDate(12, 29, 2010);        System.out.println(date + " " + date.dayOfTheWeek());    }}

结果:

1/1/2000 Saturday2/1/2000 Tuesday12/29/2000 Friday12/29/2010 Wednesday


算法-第四版-1.2 数据抽象-习题索引汇总

算法-第四版习题索引汇总

0 0
原创粉丝点击