//---------------------------------------------------------------------------- // TDIncDate241ArrayExceptions.java by Dale/Joyce/Weems Chapter 1 //includes Date and IncDate classes for all-in-one-file example. //*** added throwing exception in constructors. catching in main. //*** added DateOutOfBoundsException class p. 126-129 // 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 TDIncDate241ArrayExceptions { public static void main(String[] args) //***if main does not catch the exception, this must be uncommented: //throws DateOutOfBoundsException { String command; 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 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); //***IncDate constructor throws exception, so must be either catch'ed here //or main must be declared to throws it. try { //**test this by run with good values and run with bad: IncDate theDate = new IncDate(month, day, year); //get index after so if exception is thrown, get index won't happen. int i = getIndex(); theDates[i] = theDate; } catch (DateOutOfBoundsException e) { System.out.println(" catching DateOutOfBoundsException"); //**every exception object has getMessage() System.out.println(" e.getMessage(): " + e.getMessage()); System.out.println(" that date is rejected. try again"); } } /* 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()]); } } 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 throwing exception in constructor class Date //can not be public in this file { protected int year; protected int month; protected int day; //***added throws clause public Date(int newMonth, int newDay, int newYear) throws DateOutOfBoundsException { if (newMonth<1 || newMonth>12) //*** throw exception throw new DateOutOfBoundsException("bad month value"); else month = newMonth; if (newDay<1 || newDay>31) //needs more work! throw new DateOutOfBoundsException("bad day value"); else day = newDay; if (newYear<1) throw new DateOutOfBoundsException("bad year value"); else year = newYear; } public int yearIs() { return year; } public int monthIs() { return month; } public int dayIs() { return day; } public String toString() { return(month + "/" + day + "/" + year); } } //same as in ch01 class IncDate extends Date //can not be public in this file { //** added throws clause public IncDate(int newMonth, int newDay, int newYear) throws DateOutOfBoundsException { super(newMonth, newDay, newYear); } public void increment() { // increment algorithm goes here // it updates the year, month, and day attributes day = day + 1; } } //*** non-public so can be in this all-in-one-file example. //*** subclass of Exception class DateOutOfBoundsException extends Exception { //no-arg constructor. call Exception's no-arg constructor. public DateOutOfBoundsException() { super(); } //String arg constructor. call Exception's String arg constructor. public DateOutOfBoundsException(String message) { super(message); } }