//DateDriver.java //test driver of basic Date class of 241 import javax.swing.*; public class DateDriver { public static void main(String[] args) { String command; //array of 100 Date objects. array elements automatically initialized to null Date [] theDates = new Date[100]; int month, day, year; do { command = JOptionPane.showInputDialog("Enter command: \n" + " noargctor\n 3argctor\n stringctor\n copyctor\n year\n month\n day\n" + " increment\n toString\n equals\n hashcode\n compareTo\n numObjs\n isVaild\n quit"); if (command.equalsIgnoreCase("3argctor")) { 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 Date(month, day, year); } else if (command.equalsIgnoreCase("noargctor")) { //no arg ctor System.out.println("no arg Constructor invoked"); theDates[getIndex()] = new Date(); } else if (command.equalsIgnoreCase("stringctor")) { //string ctor String stringDate = JOptionPane.showInputDialog("Enter date as mm/dd/yyyy"); System.out.println("Constructor invoked with " + stringDate); theDates[getIndex()] = new Date(stringDate); } else if (command.equalsIgnoreCase("copyctor")) { //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 Date(theDates[fromIndex]); } else if (command.equalsIgnoreCase("year")) System.out.println("Year is " + theDates[getIndex()].getYear()); else if (command.equalsIgnoreCase("month")) System.out.println("Month is " + theDates[getIndex()].getMonth()); else if (command.equalsIgnoreCase("day")) System.out.println("Day is " + theDates[getIndex()].getDay()); //to be implemented for homework 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()]); 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])); } else if (command.equalsIgnoreCase("hashCode")) System.out.println("hashCode = " + theDates[getIndex()].hashCode()); 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])); } else if (command.equalsIgnoreCase("numObjs")) System.out.println("Number of Dates instantiated: " + Date.getNumberDates()); //to be implemented for homework else if (command.equalsIgnoreCase("isValid")) { month = Integer.parseInt(JOptionPane.showInputDialog("Enter month")); day = Integer.parseInt(JOptionPane.showInputDialog("Enter day")); year = Integer.parseInt(JOptionPane.showInputDialog("Enter year")); System.out.println("isValidDay: " + Date.isValidDay(month,day,year)); } } while(!command.equalsIgnoreCase("quit")); } //input int from user that will used as index into the array of Dates. //Note that this program is brittle, inputting an invalid int or using an //invalid index or trying to access //an array element that doesn't have a Date object will crash the program. public static int getIndex() { return Integer.parseInt(JOptionPane.showInputDialog("Enter index of object to access")); } }