Java Reference
In-Depth Information
printMonthBody
printMonthTitle
getNumberOfDaysInMonth
getMonthName
getStartDay
(a)
(b)
F IGURE 5.10 (a) To printMonthTitle , you need getMonthName . (b) The printMonthBody
problem is refined into several smaller problems.
as shown in Figure 5.10b. For example, December 2013 has 31 days, and December 1, 2013, is
a Sunday.
How would you get the start day for the first date in a month? There are several ways to do
so. For now, we'll use an alternative approach. Assume you know that the start day for January
1, 1800, was a Wednesday ( START_DAY_FOR_JAN_1_1800 = 3 ). You could compute the
total number of days ( totalNumberOfDays ) between January 1, 1800, and the first date
of the calendar month. The start day for the calendar month is (totalNumberOfDays +
startDay1800) % 7 , since every week has seven days. Thus, the getStartDay problem
can be further refined as getTotalNumberOfDays , as shown in Figure 5.11a.
getStartDay
getTotalNumberOfDays
getNumberOfDaysInMonth
getTotalNumberOfDays
isLeapYear
(a)
(b)
F IGURE 5.11 (a) To getStartDay , you need getTotalNumberOfDays . (b) The
getTotalNumberOfDays problem is refined into two smaller problems.
To get the total number of days, you need to know whether the year is a leap year and the
number of days in each month. Thus, getTotalNumberOfDays can be further refined into
two subproblems: isLeapYear and getNumberOfDaysInMonth , as shown in Figure 5.11b.
The complete structure chart is shown in Figure 5.12.
5.12.2 Top-Down and/or Bottom-Up Implementation
Now we turn our attention to implementation. In general, a subproblem corresponds to a
method in the implementation, although some are so simple that this is unnecessary. You
would need to decide which modules to implement as methods and which to combine in other
methods. Decisions of this kind should be based on whether the overall program will be easier
to read as a result of your choice. In this example, the subproblem readInput can be simply
implemented in the main method.
You can use either a “top-down” or a “bottom-up” approach. The top-down approach
implements one method in the structure chart at a time from the top to the bottom. Stubs —a
simple but incomplete version of a method—can be used for the methods waiting to be
implemented. The use of stubs enables you to quickly build the framework of the program.
Implement the main method first, then use a stub for the printMonth method. For example,
top-down approach
stub
 
 
Search WWH ::




Custom Search