Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 4

Homework 4

Be sure and use the C++ Programming Style Guide and follow the conventions described there. Up to 35% of the grade for this homework will be based on following these conventions and practicing good documentation.

Problem: The Logbook ADT

The purpose of this homework is for you to explore how you can use C++ classes to implement an abstract data type (ADT). We use a monthly logbook as our example abstract data type. A monthly logbook consists of a set of enties, one for each day of the month. Depending on the logbook, these entries might denote the amount of time spent exercising, o rthe number of cups of coffee consumed, or the number of students attending class for each day of the month. A display of an example logbook is shown below, in which each entry corresponds to a day of the month and consists of the day of the month and the integral value of the logbook for that day:

October 2006
1
100
2
95
3
5
4
0
5
18
6
71
7
15
8
102
9
90
10
87
11
67
12
0
13
14
14
35
15
4
16
18
17
100
18
12
19
93
20
1
21
32
22
20
23
30
24
40
25
50
26
60
27
70
28
80
29
5
30
122
31
14

C++ provides a set of predifined data types (int, char, double, etc.). Each of these predifined types has a set of operations associated with it. You use these operations to manipulate variables of the given type. For example, type int supports the basic arithmetic and relational operators, as well as a number of numerical functions (abs(), sqrt(), etc.). These predefined data types provide a foundation on which you can construct more sophisticated data types, data types that are collections of related data items (and their operations) rather than individual data items. In order to distinguish the data types you create from C++'s predefined data types, we refer to them as ADTs.

When specifying an ADT, you begin by describing what type of data items are in the ADT. These data items hold the attributes of an ADT and are the data that allows us to distinguish one instance from another. (Remember the Rectangle ADT.) Given the data items, we can then describe the structure that is used to organize the data items. In the case of the Logbook ADT, the data items are the entries associated with the days of the month, and the structure is linear: the entries are arranged in the same order as the corresponding days of the month.

Having specified the data items and the structure of the ADT, you then define how the ADT can be used by specifying the operations that are associated with the ADT. For each operation, you specify what must be true before the operation can be applied (its preconditions or requirements) as well as what conditions will be true after the operation has been completed (its postconditions). The following Logbook ADT specification includes operations that create a logbook for a given month in a given year, store and retrieve the logbook entry for a specific day, and provide general information about the month.

Attributes

Structure

Each integer value is the logbook entry for a particular day of the month. The number of (accessible) logbook entries varies depending on the month for which data is being recorded. The is referred to as the logbook month and is really the real world notion of a month/year combination.

Operations

Creation

Logbook ( int month, int year )

Preconditions: the month must be in the range (1 .. 12) and year must be in the range 1800 to 2200
Postconditions: object instance for the logbook month is created

Logbook ( )

Preconditions: none
Postconditions: object instance for a logbook with the current month and year as the logbook month

Mutator(s)

void setEntry ( int day, int value )

Preconditions: day is within the range of days in the logbook month (1 .. 28,29,30,31)
Postconditions: the logbook entry for the given day is updated with the given value

void setEntry ( int value )

Preconditions: logbook is for the current month
Postconditions: the logbook entry for the current day (in real time when the program is executed) is updated with the given value

Accessor(s)

int getEntry ( int day )

Preconditions: day is within the range of days in the logbook month
Postconditions: return value is the logbook entry for the specified day

int getMonth ( )

Preconditions: none
Postconditions: return value is the integer representing the month in the year of the logbook (1 .. 12)

int getYear ( )

Preconditions: none
Postconditions: return value is the year associated with the logbook (1901 .. 2099)

int getDaysInMonth ( )

Preconditions: none
Postconditions: return value is the number of days in the logbook month

int getDayOfWeek( int day )

Preconditions: day is within the range of days in the logbook month
Postconditions: return value is an integer representation of the day of the week for the given day, where 0 maps to Sunday, 1 maps to Monday, and so forth.

void printMonth ( )

Preconditions: none
Postconditions: prints the day/value pairs for the month in a standard calendar format

Assignment

Your task is to implement the Logbook ADT. To do this you will create a header file, named logbook.h which contains the class definition, and an implementation file, named logbook.cpp, which contains all of the definitions of the member functions corresponding to the operations described above.

You will also detail a test plan that gives good coverage demonstrating that your implementation works and _then_ implement the test plan with a driver program named testLogbook.cpp.

Hint for getDayOfWeek: The day of the week corresponding to a date (month/day/year) is given by the formula:

dayOfWeek = (1 + nYears + nLeapYears + nDaysToMonth + day) % 7

where nYears is the number of years since 1901, nLeapYears is the number of leap years since 1901, and nDaysToMonth is the number of days from the start of the given year to the start of month.