// BetterDateBetter.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-16 File created. // 2006-02-16 Last modification. // When this program is compiled, Date.java and DateDistance.java // must be in the same folder (directory) together with this file. // A solution to Exercise 12-1. import java.util.* ; class BetterDate extends Date { public BetterDate( String date_as_string ) { super( date_as_string ) ; } public BetterDate( int given_day, int given_month, int given_year ) { super( given_day, given_month, given_year ) ; } /**** The following is another possibility to write the required constructor: public BetterDate( int given_day, int given_month, int given_year ) { this_day = given_day ; this_month = given_month ; this_year = given_year ; } ****/ public String to_string_with_month_name() { String[] names_of_months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; return ( names_of_months[ this_month - 1 ] + " " + this_day + ", " + this_year ) ; } public String to_american_format_string() { char saved_date_print_format = date_print_format ; date_print_format = 'A' ; String string_to_return = this.toString() ; date_print_format = saved_date_print_format ; return string_to_return ; } } class BetterDateTester { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; BetterDate birthday_of_einstein = new BetterDate( 14, 03, 1879 ); System.out.print( "\n Albert Einstein was born on " + birthday_of_einstein ) ; birthday_of_einstein.increment() ; System.out.print( "\n Albert was one day old on " + birthday_of_einstein.to_string_with_month_name() ) ; birthday_of_einstein.increment() ; System.out.print( "\n Albert was two days old on " + birthday_of_einstein.to_american_format_string() ) ; } }