< previous page page_877 next page >

Page 877
Increment()
Increment day by 1
IF day > number of days in month mo
   Set day = 1
   Increment mo by 1
   IF mo > 12
      Set mo = 1
      Increment yr by 1

We can code the algorithm for finding the number of days in a month as a separate functionan auxiliary (helper) function that is not a member of the DateType class. In this function, we set up a local 12-element int array, numDays, containing the number of days in each month. That is, numDays[0] = 31, numDays[1] = 28, numDays[2] = 31, and so on, giving the number of days in January, February, March, . The number of days in February might need to be adjusted for leap year, so this function must receive as parameters both a month and a year.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
DaysInMonth (In: month, year)
Out: Function value
Declare a 12-element integer array, numDays, and initialize numDays[0] to 31,
  numDays[1] to 28, and so forth
IF month isn't 2
  Return numDays[month1]
// It's February. Check for leap year
IF (year MOD 4 is 0 AND year MOD 100 isn't 0) OR year MOD 400 is 0
  Return 29
ELSE
  Return 28

Testing: To test the Increment function, we need to create a driver that calls the function with different values for the date. Values that cause the month to change must be tested, as well as values that cause the year to change. Leap year must be tested, including a year with the last two digits 00. Case Study Follow-Up Exercise 3 asks you to carry out this testing.
Here is the implementation file that contains function definitions for all of the ADT operations:

 
< previous page page_877 next page >