Java Reference
In-Depth Information
Enter full year (e.g., 2012):
2012
Enter month as a number between 1 and 12:
3
March 2012
————————————————————————————-
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Let us use this example to demonstrate the divide-and-conquer approach.
5.12.1 Top-Down Design
How would you get started on such a program? Would you immediately start coding? Begin-
ning programmers often start by trying to work out the solution to every detail. Although
details are important in the final program, concern for detail in the early stages may block the
problem-solving process. To make problem solving flow as smoothly as possible, this exam-
ple begins by using method abstraction to isolate details from design and only later imple-
ments the details.
For this example, the problem is first broken into two subproblems: get input from the
user, and print the calendar for the month. At this stage, you should be concerned with what
the subproblems will achieve, not with how to get input and print the calendar for the
month. You can draw a structure chart to help visualize the decomposition of the problem
(see Figure 5.9a).
printCalendar
(main)
printMonth
readInput
printMonth
printMonthTitle
printMonthBody
(a)
(b)
F IGURE 5.9 The structure chart shows that the printCalendar problem is divided into two subproblems, readInput
and printMonth in (a), and that printMonth is divided into two smaller subproblems, printMonthTitle and
printMonthBody in (b).
You can use Scanner to read input for the year and the month. The problem of printing the
calendar for a given month can be broken into two subproblems: print the month title, and
print the month body, as shown in Figure 5.9b. The month title consists of three lines: month
and year, a dashed line, and the names of the seven days of the week. You need to get the
month name (e.g., January) from the numeric month (e.g., 1). This is accomplished in
getMonthName (see Figure 5.10a).
In order to print the month body, you need to know which day of the week is the first day of
the month ( getStartDay ) and how many days the month has ( getNumberOfDaysInMonth ),
 
 
Search WWH ::




Custom Search