//---------------------------------------------------------------------------- // TDIncDate241ArrayEquals.java by Dale/Joyce/Weems Chapter 1 //includes Date and IncDate classes for all-in-one-file example. //*** added equals() and compareTo() methods to Date. // modified to be interactive. further modified for homework. ch01 //further modified to have array of IncDates //also discarded some clutter e.g. frame // //---------------------------------------------------------------------------- import javax.swing.*; public class TDIncDate241ArrayEquals { public static void main(String[] args) { String command; //array of 100 IncDate objects. array elements automatically initialized to null IncDate [] theDates = new IncDate[100]; int month, day, year; do { command = JOptionPane.showInputDialog("Enter command: \n" + " IncDate\n IncDate2\n IncDate3\n yearIs\n monthIs\n dayIs\n" + " increment\n toString\n equals\n compareTo\n quit"); if (command.equalsIgnoreCase("IncDate")) { month = Integer.parseInt(JOptionPane.showInputDialog("Enter month")); day = Integer.parseInt(JOptionPane.showInputDialog("Enter day")); year = Integer.parseInt(JOptionPane.showInputDialog("Enter year")); System.out.println("Constructor invoked with " + month + " " + day + " " + year); theDates[getIndex()] = new IncDate(month, day, year); } /* else if (command.equalsIgnoreCase("IncDate2")) { //**** HMK string ctor String stringDate = JOptionPane.showInputDialog("Enter date as mm/dd/yyyy"); System.out.println("Constructor invoked with " + stringDate); theDates[getIndex()] = new IncDate(stringDate); } else if (command.equalsIgnoreCase("IncDate3")) { //**** HMK copy ctor int fromIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to copy from")); System.out.println("copy Constructor invoked with " + theDates[fromIndex]); theDates[getIndex()] = new IncDate(theDates[fromIndex]); } */ else if (command.equalsIgnoreCase("yearIs")) System.out.println("Year is " + theDates[getIndex()].yearIs()); else if (command.equalsIgnoreCase("monthIs")) System.out.println("Month is " + theDates[getIndex()].monthIs()); else if (command.equalsIgnoreCase("dayIs")) System.out.println("Day is " + theDates[getIndex()].dayIs()); else if (command.equalsIgnoreCase("increment")) { int i = getIndex(); theDates[i].increment(); System.out.println("increment invoked: " + theDates[i]); //new value } else if (command.equalsIgnoreCase("toString")) { System.out.println("toString invoked: " + theDates[getIndex()]); } //*** added: else if (command.equalsIgnoreCase("equals")) { int thisIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter index of object whose equals method to invoke")); int thatIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to be parameter to equals")); System.out.println("equals invoked: " + theDates[thisIndex].equals(theDates[thatIndex])); } //*** added: else if (command.equalsIgnoreCase("compareTo")) { int thisIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter index of object whose compareTo method to invoke")); int thatIndex = Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to be parameter to compareTo")); System.out.println("compareTo invoked: " + theDates[thisIndex].compareTo(theDates[thatIndex])); } } while(!command.equalsIgnoreCase("quit")); } //input int from user //could add some checking to ensure index is valid...to avoid subsequent crash public static int getIndex() { return Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to access")); } } //*** same as in ch01. added equals and compareTo class Date //can not be public in this file { protected int year; protected int month; protected int day; public Date(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } public int yearIs() { return year; } public int monthIs() { return month; } public int dayIs() { return day; } public String toString() { return(month + "/" + day + "/" + year); } //**** 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 year are //the same. In that case this equals would need changing (or have another //method, say equalMonths, 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 is two objects are "equal", a method must be made. public boolean equals( Date 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( Date 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. } } //***same as in ch01 class IncDate extends Date //can not be public { public IncDate(int newMonth, int newDay, int newYear) { super(newMonth, newDay, newYear); } public void increment() { // increment algorithm goes here // it updates the year, month, and day attributes day = day + 1; } }