Date.java

Download
1/*2 * Date.java - a blueprint class for objects that represent3 * an individual date.4 */5 6public class Date {7    /* a class constant for the smallest allowed year */8    public static final int MIN_YEAR = 1583;    9    10    /*11     * A class-constant array containing the names of the months.12     * The positions of the names in the array correspond to the 13     * numbers of the months. For example, "January" is at position 114     * in the array because its month number is 1, and "December" is in 15     * position 12 because its month number is 12.16     * The string "none" appears in position 0, because there 17     * is no month that corresponds to the number 0.18     */19    public static final String[] MONTHS = {"none", "January", "February",20        "March", "April", "May", "June", "July", "August",21        "September", "October", "November", "December"};22    23    /*24     * A class-constant array containing the number of days in each25     * month. Here again, the positions of the values correspond to the 26     * numbers of the months. For example, NUM_DAYS[1] is 31, because27     * January (month 1) has 31 days, and NUM_DAYS[2] is 28, because28     * February usually has 28 days.29     * -1 appears in position 0, because there is no month that 30     * corresponds to the number 0.31     */32    public static final int[] NUM_DAYS = {-1, 31, 28, 31, 30, 31, 30,33        31, 31, 30, 31, 30, 31};34    35    /*36     * A class-constant array containing the names of the days of the week.37     */38    public static final String[] DAYS_OF_WEEK = {"Sunday", "Monday",39         "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};40    41    /*42     * dayOfWeekNumber - a static helper method that takes a month number, 43     * day number, and year as parameters, and returns the *number* of the44     * day of the week on which the corresponding date falls:45     * 0 for Sunday, 1 for Monday, 2 for Tuesday, etc.46     * The algorithm for computing the appropriate number comes from 47     * Robert Sedgewick and Kevin Wayne.48     */49    public static int dayOfWeekNumber(int month, int day, int year) {50        int y0 = year - (14 - month)/12;51        int x = y0 + y0/4 - y0/100 + y0/400;52        int m0 = month + 12*((14 - month)/12) - 2;53        54        return (day + x + (31*m0)/12) % 7;55    }    56    57    /* 58     * Implement the rest of the class below,59     * as specified in the assignment.60     */61 62}