Cover page images (keyboard)

Lab 09 - Top-down Design

Sue Evans

Calendar design courtesy of Eric S. Roberts, PhD.

Press space bar for next screen

Objectives












Step 0












Yearly Calendar













The Design Diagram












Step 1

Write the top-level functions

Testing Time












Step 2

Write printCalendar()

Testing Time













Step 3 - printMonth()

How are we handling the weekdays ?

Notice that we already have constants for the days of the week, with SUNDAY being 0. This has a great advantage, since it allows us to implement the operation of cycling past the end of one week using the modulus operator. If the variable weekday contains the integer corresponding to the current day of the week, the expression:

(weekday + n) % 7

indicates the day of the week that occurs n days later. For example, if today is a Saturday(when weekday has a the value 6), 10 days from today is a Tuesday, because the expression:

(6 + 10) % 7

evaluates to 2. This formula ends up being quite useful for moving ahead by one weekday.

weekday = (weekday + 1) % 7

So the code for the loop in this function is:

    for day in range(1, numDays + 1):
        print "%2d" % (day),
        if weekday == SATURDAY:
            print
        weekday = (weekday + 1) % 7
    if weekday != SUNDAY:
        print

By making use of stubs, we can write printMonth() in its entirety and can test it.












Step 4

The next step would be to write the 4 functions that are currently stubs, one at a time and test them. For now, let's just think about how each of these can be implemented.












Bonus Step -

Write and test the 5 remaining functions.