Code:
import java.util.*;
import java.text.*;
/** A "less than simple" date class; an augmented and refactored version of SimpleDate by Anderson, Franceschi
** devloped by P.M.J. and R.W.M., October 28, 2012
*/
public class SimpleDate {
//Instance Variables
private int month;
private int day;
private int year;
//Default values
private static final int MONTH = 1;
private static final int DAY = 1;
private static final int YEAR = 2000;
//Public Constants (for the days of the week)
public final int SUNDAY = 1;
public final int MONDAY = 2;
public final int TUESDAY = 3;
public final int WEDNESDAY = 4;
public final int THURSDAY = 5;
public final int FRIDAY = 6;
public final int SATURDAY = 7;
///////////////////////////////////////////////////////////////////////////////////////////////////////
// C o n s t r u c t o r M e t h o d s
///////////////////////////////////////////////////////////////////////////////////////////////////////
/** Default constructor;
* Uses the default values for the new object.
*/
public SimpleDate( ) {
month = MONTH;
day = DAY;
year = YEAR;
}
/** Overloaded constructor;
* sets object's value to the given triplet
* @param mm initial value for month
* @param dd initial value for day
* @param yyyy initial value for year
*/
public SimpleDate(int mm, int dd, int yyyy) {
if(!isValidDay(mm,dd,yyyy)) {
mm = MONTH;
dd = DAY;
yyyy = YEAR;
}
setDate(mm,dd,yyyy);
}
/** Overloaded additional constructor; recognizes mm/dd/yyyy and mm-dd-yyyy forms
* @param date expressed in either mm/dd/yyyy or mm-dd-yyyy form
*/
public SimpleDate(String date) {
setDate(date);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// A c c e s s o r / O b s e r v e r M e t h o d s
///////////////////////////////////////////////////////////////////////////////////////////////////////
public int getMonth( ) {
return month; }
public int getDay( ) {
return day; }
public int getYear( ) {
return year; }
/** Returns a value representing the day of the week; corresponding to the public constants, SUNDAY,
* MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY.
*/
public int getDayOfWeek() {
Calendar c = Calendar.getInstance();
c.setTime(new Date((year-1900),(month-1),day));
int dow = c.get(Calendar.DAY_OF_WEEK); //1=sunday, 2=monday, 3=wenesday, etc.
return dow;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// M u t a t o r M e t h o d s
///////////////////////////////////////////////////////////////////////////////////////////////////////
/** Sets the object's value to the given triplet. In the event the given date value is invalid, the
* object is unchanged.
* @param mm new value for month
* @param dd new value for day
* @param yyyy new value for year
*/
public void setDate(int mm, int dd, int yyyy) {
if(!isValidDay(mm,dd,yyyy)) {
mm = MONTH; dd = DAY; yyyy = YEAR;
}
month = mm; day = dd; year = yyyy;
}
/** Sets the object's value to that expressed in the given string; recognizes mm/dd/yyyy and mm-dd-yyyy forms.
* In the event the given date value is invalid, the object is unchanged.
* @param date expressed in either mm/dd/yyyy or mm-dd-yyyy form
*/
public void setDate(String date) {
SimpleDate sd = parseDate(date);
month = sd.getMonth();
day = sd.getDay();
year = sd.getYear();
}
/** Sets the month value part of the object's value to the given value. In the event the given month
* value is invalid, the object is unchanged. Note that the day value part of the resultant date value
* may be adjusted earlier to represent a valid date.
* @param mm new value for month
*/
public void setMonth(int mm) {
if(isValidMonth(mm)) {
month = mm;
rollBack();
}
}
/** Sets the day value part of the object's value to the given value. In the event the given day
* value is invalid, the object is unchanged.
* @param dd new value for day
*/
public void setDay(int dd) {
if(isValidDay(month,dd,year)) {
day = dd;
}
}
/** Sets the year value part of the object's value to the given value. Note that the day value part of
* the resultant date value may be adjusted earlier to represent a valid date.
* @param yyyy new value for year
*/
public void setYear(int yyyy) {
year = yyyy;
rollBack();
}
//-----------------------------------------------------------------------------------------------------
/** Modifies the object's value to represent the day before the current value.
*/
public void prevDay( ) {
//CODE NEEDED HERE!!!
day = day - 1;
if (!isValidDay(month,day,year)) {
month = month - 1;
if (!isValidMonth(month)) {
month = 12;
year = year - 1;
}
day = monthDays(month,year);
}
}
/** Modifies the object's value to represent the day after the current value.
*/
public void nextDay( ) {
day = day + 1;
if (!isValidDay(month,day,year)) {
day = 1;
month = month + 1;
if (!isValidMonth(month)) {
month = 1;
year = year + 1;
}
}
}
/** Modifies the object's value to represent the date correspondng to the
specified number of days before the current value.
* @param n the specified number of days to be used to modify the date value; Note n must be >= 0.
*/
public void prevDay(int n) {
//CODE NEEDED HERE!!!
while(n > 0) {
prevDay();
n = n - 1;
}
}
/** Modifies the object's value to represent the date corresponding to the
specified number of days after the current value.
* @param n the specified number of days to be used to modify the date value; Note n must be >= 0.
*/
public void nextDay(int n) {
//CODE NEEDED HERE!!!
while(n > 0) {
nextDay();
n = n - 1;
}
}
//-----------------------------------------------------------------------------------------------------
/** Modifies the object's value to represent the date one week before the current value.
*/
public void prevWeek( ) {
//CODE NEEDED HERE!!!
prevDay(7);
}
/** Modifies the object's value to represent the date one week after the current value.
*/
public void nextWeek( ) {
//CODE NEEDED HERE!!!
nextDay(7);
}
/** Modifies the object's value to represent the date correspondng to the
specified number of weeks before the current value.
* @param n the specified number of weeks to be used to modify the date value; Note n must be >= 0.
*/
public void prevWeek(int n) {
//CODE NEEDED HERE!!!
while(n > 0) {
prevWeek();
n = n - 1;
}
}
/** Modifies the object's value to represent the date corresponding to the
specified number of weeks after the current value.
* @param n the specified number of weeks to be used to modify the date value; Note n must be >= 0.
*/
public void nextWeek(int n) {
//CODE NEEDED HERE!!!
while(n > 0) {
nextWeek();
n = n - 1;
}
}
//-----------------------------------------------------------------------------------------------------
/** Modifies the object's value to represent the "corresponding date" in the preceeding month.
*/
public void prevMonth( ) {
prevMonth(1);
}
/** Modifies the object's value to represent the "corresponding date" in the following month.
*/
public void nextMonth( ) {
nextMonth(1);
}
/** Modifies the object's value to represent the "correspondng date" in the month specified by
* the given number of months before the current value.
* @param n the specified number of months to be used to modify the date value; Note n must be >= 0.
*/
public void prevMonth(int n) {
//CODE NEEDED HERE!!!
if(month > n) {
setMonth(month - n);
}
else {
year = year - (((n - month) / 12) + 1);
n = n % 12;
setMonth( (((((month - 1) - n) + 12) % 12) + 1));
}
rollBack();
}
/** Modifies the object's value to represent "corresponding date" in the month specified by
* the given number of months after the current value.
* @param n the specified number of months to be used to modify the date value; Note n must be >= 0.
*/
public void nextMonth(int n) {
//CODE NEEDED HERE!!!
year = year + ((month+n-1)/12);
setMonth( ((((month - 1) + n) % 12) + 1));
rollBack();
}
//-----------------------------------------------------------------------------------------------------
/** Modifies the object's value to represent the "corresponding date" one year before the current value.
*/
public void prevYear( ) {
prevYear(1);
}
/** Modifies the object's value to represent the "corresponding date" one year after the current value.
*/
public void nextYear( ) {
nextYear(1);
}
/** Modifies the object's value to represent the "correspondng date" the specified number of years
* before the current value.
* @param n the specified number of years to be used to modify the date value; Note n must be >= 0.
*/
public void prevYear(int n) {
//CODE NEEDED HERE!!!
setYear(year-n);
rollBack();
}
/** Modifies the object's value to represent the "corresponding date" the specified number of years
* after the current value.
* @param n the specified number of years to be used to modify the date value; Note n must be >= 0.
*/
public void nextYear(int n) {
//CODE NEEDED HERE!!!
setYear(year+n);
rollBack();
}
//-----------------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////
// S t a n d a r d / C o n v e n t i o n a l M e t h o d s
///////////////////////////////////////////////////////////////////////////////////////////////////////
/** Returns a string representation of the object's current value.
* @return an expression of the date value in mm/dd/yyyy format (with 2 digit mm and dd)
*/
public String toString( ) {
String fmt = "%02d";
DecimalFormat nf = new DecimalFormat("##");
return String.format(fmt,month) + "/" + String.format(fmt,day) + "/" + year;
}
/** Compares this object to the given object for equality.
* @param that, Object to compare to this object (required to be a SimpleDate object)
* @return true if this object represents a date value equivalent to the value of the
* given object, false, otherwise.
*/
public boolean equals(Object that) {
boolean result = false;
if (that instanceof SimpleDate) { //Make sure that is a SimpleDate object
SimpleDate date = (SimpleDate)that;
result = (month == date.month) && (day == date.day) && (year == date.year);
}
return result;
}
/** Compares this object to the given object.
* @param that, Object to compare to this object (required to be a SimpleDate object)
* @return a positive integer if this object represents a date value later
* in time than the value represented by the given object, a negative integer
* if this represents a date earlier in time, and zero if the two objects
* represent the same date value.
*/
public int compareTo(Object that) {
int result = 0;
if (that instanceof SimpleDate) { //Make sure that is a SimpleDate object
//CODE NEEDED HERE!!!
SimpleDate date = (SimpleDate)that;
result = this.getYear() - date.getYear();
if(result == 0) {
result = this.getMonth() - date.getMonth();
if(result == 0) {
result = this.getDay() - date.getDay();
}
}
}
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// P r i v a t e M e t h o d s
///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//Returns a SimpleDate object representing the date value expressed by the given string, or the
//default value otherwise. This method recognizes valid date values expressed in either the
//mm/dd/yyyy or mm-dd-yyyy forms.
private SimpleDate parseDate(String date) {
SimpleDate result = new SimpleDate();
SimpleDate attempt;
attempt = parseDate_mmddyyyy(date,'/');
if(attempt != null) {
result = attempt;
}
else {
attempt = parseDate_mmddyyyy(date,'-');
if(attempt != null) {
result = attempt;
}
}
return result;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//Returns a SimpleDate object representing the date value expressed by the given string, null
//otherwise. delimiter serves to specify the particular character used to separate the date
//value parts; normally '/' or '-'. This method can thus readily recognize valid date values
//expressed in either the mm/dd/yyyy or mm-dd-yyyy form.
private SimpleDate parseDate_mmddyyyy(String date, char delimiter) {
SimpleDate result = null;
int m, d, y;
int index1 = date.indexOf(delimiter);
int index2 = date.lastIndexOf(delimiter);
if((index1 >= 0) && (index2 > index1)) {
try {
m = Integer.parseInt(date.substring(0,index1));
d = Integer.parseInt(date.substring((index1+1),index2));
y = Integer.parseInt(date.substring(index2+1));
result = new SimpleDate(m,d,y);
}
catch (NumberFormatException e)
{ //No remedial action necessary; result will equal null
}
}
return result;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//Modifies the instance variable day, as necessary, until the triplet, month, day, year is valid
private void rollBack() {
//CODE NEEDED HERE!!!
if(!isValidDay(month,day,year)) {
day = monthDays(month,year);
}
/* while(!isValidDay(month,day,year)) {
day = day - 1;
}*/
}
//Returns true iff the given triplet corresponds to a valid date value
/////////////////////////////////////////////////////////////////////////////////////////////////
private boolean isValidDay(int month, int day, int year) {
return isValidMonth(month) &&
((day >= 1) && (day <= monthDays(month,year)));
}
//Returns true iff the given month corresponds to a valid month value
/////////////////////////////////////////////////////////////////////////////////////////////////
private boolean isValidMonth(int month) {
return ((month >= 1) && (month <= 12));
}
//Returns the number of days in the given month and year
/////////////////////////////////////////////////////////////////////////////////////////////////
private int monthDays(int month, int year) {
int result;
// Assert: (month >= 1) && (month <=12)
if ((month==4) || (month==6) || (month==9) || (month==11)) {
result = 30;
}
else if(month == 2) {
if (isLeapYear(year)) {
result = 29;
}
else {
result = 28;
}
}
else { // (month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)
result = 31;
}
return result;
}
// Returns true iff the given year value qualifies as a "leap year".
/////////////////////////////////////////////////////////////////////////////////////////////////
private boolean isLeapYear(int year) {
return !( ((year % 4) != 0) || ( ((year % 100) == 0) && ((year % 400) != 0) ) );
}
}
That is the completed program he gave us. We need to make our own from scratch, and import specific lines of a text file (most common birthdays) to it. This is all I have so far.