//Date4.java // equals and compareTo methods public class Date4 { //********** static variables *************** private static int numberDates=0; public static final int VALID_START_YEAR=1584; //********** instance variables ************* private int year; private int month; private int day; //********** constructors ******************* public Date4() { month = 1; day = 1; year = 1970; numberDates++; // Date4.numberDates++; } public Date4(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; numberDates++; } public Date4( Date4 other) { month = other.month; day = other.day; year = other.year; numberDates++; } public Date4( String date) { 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()); numberDates++; } //********** accessor methods *************** public int getYear() { return year; } public int getMonth() { return month; } public int getDay() { return day; } public String toString() { return(month + "/" + day + "/" + year); } public static int getNumberDates() { return numberDates; } //********** mutator methods *************** public void increment() { day++; //stub for the actual method to be written later... } public static void clearNumberDates() { numberDates = 0; } public static boolean isValidDay(int month, int day, int year) { //bunch of code will go here to determine if month day year //combination is valid or not. return true; //or false... } //**** you define what "equality" means for a class. //for Dates, it makes sense that two dates are equal if their day, month, and year //are the same, so that is what is being done here. //You might, however, decide that dates are equal if only their month and day are //the same. In that case this equals would need changing (or have another //method, say equalMonthDay, that tests that version of equality.) //In general, for a class, it might be a subset of the instance variables //that determines equality. //Note: == only tests if two object references have the same value, i.e. //they are references to the same object (i.e. they are aliases). // == does not and can not look inside the object at the instance variables. //so to test if two objects are "equal", a method must be made. public boolean equals( Date4 other ) { if (day==other.day && month==other.month && year==other.year) return true; else return false; } //***caveat: this is not what an "official" equals method should be. //but the official one requires complicated stuff including inheritance, //overriding, instanceof, casting, check for null so we'll do that later. //***compareTo methods return negative if this object is "less than" parameter object, //positive if this object is "greater than" parameter object, //0 if this object is "equal to" parameter object. //You define what equal, less than, and greater than mean for a class. public int compareTo( Date4 other ) { if (equals(other)) //use the equals method. or can do: this.equals(other) return 0; //not the same, so see if years differ: else if (year < other.year) return -1; else if (year > other.year) return 1; //years are same, so see if months differ: else if (month < other.month) return -1; else if (month > other.month) return 1; //months are same too, so see how days differ: else if (day < other.day) return -1; else if (day > other.day) return 1; else //this else is not needed logically, but without it the compiler complains :( return 0; //will never be reached. } } /* client code: Date4 d1 = new Date4(12,19,2000); Date4 d2 = new Date4(); // 1/1/1970 Date4 d3 = new Date4(d1); if (d1.equals(d3)) // true because d3 is copy of d1 via copy ctor System.out.println("dates are equal"); if (!d1.equals(d2)) System.out.println("dates are not equal"); if (d1.compareTo(d2) < 0) //false, d1 is not < d2 if (d1.compareTo(d2) > 0) //true, d1 is > d2 if (d1.compareTo(d3) == 0) //true, d1 is = d3 Might as well use equals() */