Java Reference
In-Depth Information
/** A stub for getMonthName may look like this */
{
public static
String getMonthName(
int
month)
return
"January"
;
// A dummy value
}
/** A stub for getStartDay may look like this */
public static int
getStartDay(
int
year,
int
month)
{
return
1
;
// A dummy value
}
/** A stub for getTotalNumberOfDays may look like this */
public static int
getTotalNumberOfDays(
int
year,
int
month)
{
return
10000
;
// A dummy value
}
/** A stub for getNumberOfDaysInMonth may look like this */
public static int
getNumberOfDaysInMonth(
int
year,
int
month)
{
return
31
;
// A dummy value
}
/** A stub for isLeapYear may look like this */
{
public static Boolean
isLeapYear(
int
year)
return true
;
// A dummy value
}
}
Compile and test the program, and fix any errors. You can now implement the
printMonth
method. For methods invoked from the
printMonth
method, you can again use stubs.
The bottom-up approach implements one method in the structure chart at a time from the
bottom to the top. For each method implemented, write a test program, known as the
driver
,
to test it. The top-down and bottom-up approaches are equally good: Both approaches imple-
ment methods incrementally, help to isolate programming errors, and make debugging easy.
They can be used together.
bottom-up approach
driver
5.12.3 Implementation Details
The
isLeapYear(int year)
method can be implemented using the following code from
Section 3.12:
return
(year %
400
==
0
|| (year %
4
==
0
&& year %
100
!=
0
));
Use the following facts to implement
getTotalNumberOfDaysInMonth(int year, int
month)
:
January, March, May, July, August, October, and December have 31 days.
■
April, June, September, and November have 30 days.
■
February has 28 days during a regular year and 29 days during a leap year. A regular
year, therefore, has 365 days, a leap year 366 days.
■
To implement
getTotalNumberOfDays(int year, int month)
, you need to compute
the total number of days (
totalNumberOfDays
) between January 1, 1800, and the first day
of the calendar month. You could find the total number of days between the year 1800 and the
calendar year and then figure out the total number of days prior to the calendar month in the
calendar year. The sum of these two totals is
totalNumberOfDays
.
To print a body, first pad some space before the start day and then print the lines for every
week.
The complete program is given in Listing 5.12.