//same Date BUT with added equals() and hashcode() methods (***see below) //so Date can be used in a HashSet public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to the parameters //Postcondition: dMonth = month; dDay = day; // dYear = year; public Date(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to set the date //Data members dMonth, dDay, and dYear are set //according to the parameters //Postcondition: dMonth = month; dDay = day; // dYear = year; public void setDate(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to return the month //Postcondition: The value of dMonth is returned public int getMonth() { return dMonth; } //Method to return the day //Postcondition: The value of dDay is returned public int getDay() { return dDay; } //Method to return the year //Postcondition: The value of dYear is returned public int getYear() { return dYear; } //Method to return the date in the form mm-dd-yyyy@hashcode //show the overridden hashcode value public String toString() { return (dMonth + "-" + dDay + "-" + dYear + super.toString()); } //***override equals() of Object. //***used to determine if two objects (of Date class) are "equal". //***MUST be overridden to use Date objects (uniquely, as keys) in a HashSet. //***equality is defined by this method. //***without this method, two objects are equal only if their references are the same. //***must be this signature: parameter MUST be type Object. public boolean equals( Object other ) { //parameter will be the other Date //***check that other really is Date, //**then (safely) downcast to Date to access instance variables if ((other instanceof Date) && ((((Date)other).dYear==dYear) && (((Date)other).dMonth==dMonth) && (((Date)other).dDay==dDay))) return true; else return false; //if other is null, or not a Date, or doesn't equal } //equals must be //1. reflexive: x.equals(x) is true //2. symmetric: x.equals(y) == y.equals(x) //3. transitive: if x.equals(y) and y.equals(z) then x.equals(z) //also: x.equals(null) is false //***override hashcode() of Object //***if two Dates are equal, they must have same hashcode. //***must be this signature: public int hashCode() { //return 1492; //would work but inefficient //?? how to make good hash?? //some function of the instance variables modded by some prime... return (dYear + dMonth + dDay) % 101; } }