//Date2.java //example of different constructors public class Date2 { private int year; private int month; private int day; //**can have multiple "overloaded" constructors. //differ by and are differentiated by parameter list. //convenience to client. //**"no-arg" (0 parameters) constructor. //this one sets date to Java/Unix "epoch" of 1/1/1970 public Date2() { month = 1; day = 1; year = 1970; } public Date2(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } //** "copy" constructor. parameter is another object of same class. //makes this object a copy of the other. //can access private members of other objects of the same class. public Date2( Date2 other) { month = other.month; day = other.day; year = other.year; } //**another possibility is a constructor with a String parameter in the // mm/dd/yyyy form. Extract the 3 values using a StringTokenizer public Date2( String date) { //use / as the tokens' delimiter java.util.StringTokenizer st = new java.util.StringTokenizer(date,"/"); month = Integer.parseInt(st.nextToken()); //extract next token day = Integer.parseInt(st.nextToken()); year = Integer.parseInt(st.nextToken()); } public void increment() { day++; //stub for the actual method to be written later... } public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public String toString() { return(month + "/" + day + "/" + year); } } /* client code: Date2 d0 = new Date2(5,15,1998); //3 int constructor Date2 d1 = new Date2(); //no-arg constructor Date2 d2 = new Date2("12/23/1999"); //String parameter constructor Date2 d3 = new Date2(d2); //copy constructor, d2 is parameter to be copied. */ /* Other features that could be added: incrementYear() incrementMonth() incrementDay() validate the initializing date add data member day_of_week julian day (day of year) epoch day (day since 1/1/1970) zodiac sign holiday (either particular day, eg. Dec. 25, or within range, eg. Thanksgiving) */