// HistoricalEvents.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-12-30 File created. // 2017-02-24 Last modification. // This is a new version of the textbook program Events.java. // This version uses the new standard Java class LocalDate to // handle time-related information. /* This program shows - how a class can implement the Comparable interface - how to create and use ArrayList-based arrays - how to use the static Collections.sort() method. */ import java.time.LocalDate ; import java.util.ArrayList ; import java.util.Collections ; class Event implements Comparable { protected LocalDate date_of_event ; protected String event_description ; public Event( int year_of_event, int month_of_event, int day_of_event, String given_event_description ) { date_of_event = LocalDate.of( year_of_event, month_of_event, day_of_event ) ; event_description = given_event_description ; } public String toString() { return ( date_of_event + " " + event_description ) ; } public int compareTo( Event event_to_compare_to ) { int comparison_result = 0 ; // Events of the same date. if ( date_of_event.isBefore( event_to_compare_to.date_of_event ) ) { comparison_result = -1 ; // "this" has earlier date. } else if ( date_of_event.isAfter( event_to_compare_to.date_of_event ) ) { comparison_result = 1 ; // "this" has later date. } return comparison_result ; } } class HistoricalEvents { public static void main( String[] not_in_use ) { Event birth_of_lennon = new Event( 1940, 10, 9, "John Lennon was born.") ; Event birth_of_einstein = new Event( 1879, 3, 14, "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( 1980, 12, 8, "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( 1926, 6, 1, "Marilyn Monroe was born." ) ) ; another_event_list.add( 0, new Event( 1962, 8, 5, "Marilyn Monroe died." ) ) ; another_event_list.add( new Event( 1769, 8, 15, "Napoleon Bonaparte was born." ) ) ; another_event_list.add( new Event( 1881, 10, 25, "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 ) ; // Next, we use a 'foreach' loop to print all objects in the // ArrayList-based list. System.out.print( "\n\nEvents of list_of_events: \n" ) ; for ( Event event_to_print : list_of_events ) { System.out.print( "\n " + event_to_print ) ; } System.out.print( "\n\n" ) ; } } /* NOTES: In modern Java, another possibility to sort the above arrays is to use the statements list_of_events.sort( null ) ; another_event_list.sort( null ) ; The null as a parameter means that the natural ordering of the objects should be used. Useful pages related to this program: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html */