CSc 2310 Principles of Computer Programming I
Spring 1999
Programming Assignment #7
Due: 3 May 1999 (Monday)
Complete the specification of the Date class as defined below. Submit Date.java and Date.class I am providing the TestDate.java program which can be used to test the Date class.
public class Date {
private int day;
private int month;
private int year;
// Constructor
public Date(int m, int d, int y) {
day = d;
month = m;
year = y;
}
// Returns the Date obtained by adding nDays to the receiving
// Date object.
public Date add(int nDays) {
}
// Returns the Date obtained by subtracting nDays from the receiving
// Date object.
public Date sub(int nDays) {
}
// Returns the number of days between d and the receiving Date object
public int daysBetween(Date d) {
}
// Returns true if the receiving Date object is after Date d.
public boolean after(Date d) {
}
// Returns true if the receiving Date object is same as Date d.
public boolean equals(Date d) {
}
// Returns true if the receiving Date object is before Date d.
public boolean before(Date d) {
}
// Returns the Date object corresponding to the previous day for the
// receiving Data object.
public Date yesterday() {
}
// Returns the Date object corresponding to the first day of the
// previous month for the receiving Data object.
public Date firstOfPreviousMonth() {
}
// Returns the Date object corresponding to the next day for the
// receiving Data object.
public Date tomorrow() {
}
// Returns the Date object corresponding to the first day of the
// next month for the receiving Data object.
public Date firstOfNextMonth() {
}
// Pretty print method
public String toString() {
String sMonths =
"01January:02February:03March:04April:05May:06June:" +
"07July:08August:09September:10October:11November:12December:";
String m;
if (month < 10) m = "0"+month; else m = ""+month;
int i = sMonths.indexOf(m);
int j = sMonths.indexOf(":",i+1);
String sMonth = sMonths.substring(i+2,j);
return day + " " + sMonth + ", " + year;
}
private static boolean leapYear(int year) {
return ((year%4 == 0) && !(year%100 == 0 && year%400 != 0));
}
}
Raj Sunderraman