// EventsGregorianCalendar.cs (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-14 File created. // 2005-06-14 Last modification. // A solution to exercise 15-9. // A different kind of solution is provided at the end of this file. import java.util.* ; // As class GregorianCalendar already implements the Comparable // interface, the Event class does not need to implement it. // The compareTo() of class GregorianCalendar can compare // Event objects as well. class Event extends GregorianCalendar { protected String event_description ; public Event( int day_of_event, int month_of_event, int year_of_event, String given_event_description ) { super( year_of_event, month_of_event, day_of_event ) ; event_description = given_event_description ; } public String toString() { return String.format( "%tF %s", this, event_description ) ; } } /* The following is a longer version of a suitable toString() method for the above Event class. public String toString() { Calendar date_of_event = new GregorianCalendar( get( Calendar.YEAR ), get( Calendar.MONTH ), get( Calendar.DAY_OF_MONTH ) ) ; return String.format( "%tF %s", date_of_event, event_description ) ; } ***/ class EventsGregorianCalendar { public static void main( String[] not_in_use ) { Event birth_of_lennon = new Event( 9, 10, 1940, "John Lennon was born.") ; Event birth_of_einstein = new Event( 14, 3, 1879, "Albert Einstein was born." ) ; ArrayList list_of_events = new ArrayList() ; list_of_events.add( birth_of_lennon ) ; list_of_events.add( birth_of_einstein ) ; list_of_events.add( new Event( 8, 12, 1980, "John Lennon was shot in New York." ) ) ; System.out.print( "\nEvents of list_of_events: \n" ) ; int event_index = 0 ; while ( event_index < list_of_events.size() ) { System.out.print( "\n " + list_of_events.get( event_index ) ) ; event_index ++ ; } ArrayList another_event_list = new ArrayList() ; another_event_list.add( 0, new Event( 1, 6, 1926, "Marilyn Monroe was born." ) ) ; another_event_list.add( 0, new Event( 5, 8, 1962, "Marilyn Monroe died." ) ) ; another_event_list.add( new Event(15, 8, 1769, "Napoleon Bonaparte was born." ) ) ; another_event_list.add( new Event(25,10, 1881, "Pablo Picasso was born." ) ) ; System.out.print( "\n\nEvents of another_event_list: \n" ) ; for ( event_index = 0 ; event_index < another_event_list.size() ; event_index ++ ) { System.out.print( "\n " + another_event_list.get( event_index ) ); } Collections.sort( list_of_events ) ; Collections.sort( another_event_list ) ; list_of_events.addAll( 0, another_event_list ) ; System.out.print( "\n\nEvents of list_of_events: \n" ) ; for ( Event event_on_list : list_of_events ) { System.out.print( "\n " + event_on_list ) ; } } } /**** // The following version of class Event would also work. // This Event class has a Calendar object as a data field. class Event implements Comparable { protected Calendar date_of_event ; protected String event_description ; public Event( int day_of_event, int month_of_event, int year_of_event, String given_event_description ) { date_of_event = new GregorianCalendar( year_of_event, month_of_event, day_of_event ) ; event_description = given_event_description ; } public String toString() { return String.format( "%tF %s", date_of_event, event_description ) ; } public int compareTo( Event event_to_compare_to ) { return date_of_event.compareTo( event_to_compare_to.date_of_event ) ; } } ***/