1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| import edu.princeton.cs.algs4.*; interface Date{ int day(); int month(); int year(); String toString(); boolean equals(Object that); int compareTo(smartDate that); int hashCode(); } class smartDate implements Date { private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private final int month; private final int day; private final int year;
public smartDate(int month, int day, int year) { if (!isValid(month, day, year)) throw new IllegalArgumentException("Invalid date"); this.month = month; this.day = day; this.year = year; }
public smartDate(String date) { String[] fields = date.split("/"); if (fields.length != 3) { throw new IllegalArgumentException("Invalid date"); } month = Integer.parseInt(fields[0]); day = Integer.parseInt(fields[1]); year = Integer.parseInt(fields[2]); if (!isValid(month, day, year)) throw new IllegalArgumentException("Invalid date"); }
public int month() { return month; }
public int day() { return day; }
public int year() { return year; }
private static boolean isValid(int m, int d, int y) { if (m < 1 || m > 12) return false; if (d < 1 || d > DAYS[m]) return false; if (m == 2 && d == 29 && !isLeapYear(y)) return false; return true; }
private static boolean isLeapYear(int y) { if (y % 400 == 0) return true; if (y % 100 == 0) return false; return y % 4 == 0; }
@Override public String toString() { return month + "/" + day + "/" + year; }
@Override public boolean equals(Object other) { if (other == this) return true; if (other == null) return false; if (other.getClass() != this.getClass()) return false; smartDate that = (smartDate) other; return (this.month == that.month) && (this.day == that.day) && (this.year == that.year); }
public smartDate next() { if (isValid(month, day + 1, year)) return new smartDate(month, day + 1, year); else if (isValid(month + 1, 1, year)) return new smartDate(month + 1, 1, year); else return new smartDate(1, 1, year + 1); }
public boolean isAfter(smartDate that) { return compareTo(that) > 0; }
public boolean isBefore(smartDate that) { return compareTo(that) < 0; }
public int compareTo(smartDate that) { if (this.year < that.year) return -1; if (this.year > that.year) return +1; if (this.month < that.month) return -1; if (this.month > that.month) return +1; if (this.day < that.day) return -1; if (this.day > that.day) return +1; return 0; }
public int numBetween(smartDate b){ smartDate t = new smartDate(this.month, this.day, this.year); int num = 0; while (t.isBefore(b)){ t=t.next(); num++; } return num; }
public String week(){ smartDate a = new smartDate(12, 31, 1999); final String[] WEEK = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thurday", "Friday"}; return WEEK[a.numBetween(this)%7-1]; }
@Override public int hashCode() { int hash = 17; hash = 31*hash + month; hash = 31*hash + day; hash = 31*hash + year; return hash; }
} public class q1211{ public static void main(String[] args) { smartDate today = new smartDate(2, 25, 2018); smartDate birthday = new smartDate(10, 16, 1971); StdOut.println(today); StdOut.println(birthday); StdOut.println(birthday.equals(today)); StdOut.println(today.week()); } }
|