// Events.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2002-11-25 Java file created. // 2005-07-25 Last modification. // When this program is compiled, Date.java and DateDistance.java // must be in the same folder (directory) together with this file. // There are notes at the end of this file. import java.util.ArrayList ; import java.util.Collections ; class Event extends Date implements Comparable { protected String event_description ; public Event( int day_of_event, int month_of_event, int year_of_event, String given_event_description ) { this_day = day_of_event ; this_month = month_of_event ; this_year = year_of_event ; event_description = given_event_description ; } public String toString() { Date date_of_event = new Date( this_day, this_month, this_year, date_print_format ) ; return ( date_of_event + " " + event_description ) ; } public int compareTo( Event event_to_compare_to ) { int comparison_result = 0 ; // Events of the same date. if ( this.is_earlier_than( event_to_compare_to ) ) { comparison_result = -1 ; // "this" has earlier date. } else if ( this.is_later_than( event_to_compare_to ) ) { comparison_result = 1 ; // "this" has later date. } return comparison_result ; } } class Events { 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 ) ; } } } /*** NOTES // The following would be a shorter version of a toString() // method that is suitable to class Event. public String toString() { return ( super.toString() + " " + event_description ) ; } // This program uses the Date class presented in the book. // To avoid confusion, java.util.Date is not imported into this program. ****/