chapter_12_beginning_javabook

Chapter_12_beginning_javabook

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rs, 10 months, and 16 days ago. Titanic.java - X. Here the program is executed on March 1, 2005. 391 392 Chapter 12: Inheritance and class hierarchies // Right after its creation, the object referenced by date_to_increment contains the current date of the computer. The object can be used like a Date object. Weddingdates.java (c) Kari Laitinen class Weddingdates { public static void main( String not_in_use ) { CurrentDate date_to_increment = new CurrentDate() ; int number_of_dates_printed = 0; System.out.print( "\n These are easy-to-remember dates for weddings and" + "\n other important events because the days and months" + "\n consist of the digits used in the year: \n" ) ; while ( number_of_dates_printed < { String day_as_string = String.format( "%02d", String month_as_string = String.format( "%02d", String year_as_string = "" + 60 ) date_to_increment.day() ) ; date_to_increment.month() ) ; date_to_increment.year() ; if ( year_as_string.indexOf( day_as_string.charAt( 0 ) ) != -1 && year_as_string.indexOf( day_as_string.charAt( 1 ) ) != -1 && year_as_string.indexOf( month_as_string.charAt( 0 ) ) != -1 && year_as_string.indexOf( month_as_string.charAt( 1 ) ) != -1 ) { // Now we have found a date that meets our requirements. if ( number_of_dates_printed % { System.out.print( "\n" ) ; } System.out.print( " " number_of_dates_printed + 5 == 0) date_to_increment ) ; ++ ; } date_to_increment.increment() ; } } } The object referenced by date_to_increment is incremented to the next date. This program simply checks hundreds of dates to see which dates fulfil the criteria for a nice wedding date. A valid wedding date has been found here. A newline is printed after every 5th date. The string concatenation operator + converts the CurrentDate object to a string. The method toString() that is provided in class Date, and inherited to class CurrentDate, is invoked automatically in the conversion operation. Weddingdates.java - 1.+ Using a CurrentDate object to find the next best wedding dates. 12.1 Base classes and derived classes The three "components" of the date are converted to String objects here. The format specifier %02d ensures that single digit days and months are converted to strings that have a leading zero digit. The format() method of class String uses...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online