|
|
|
|
|
|
|
plementation details reduces complexity for the user and also shields the user from changes in the implementation. |
|
|
|
|
|
|
|
|
Below is the specification of another ADT, one that might be useful for representing time in a program. |
|
|
|
 |
|
|
|
|
TimeType |
|
|
|
 |
|
|
|
|
Each TimeType value is a time of day in the form of hours, minutes, and seconds. |
|
|
|
 |
|
|
|
|
Set the time. |
|
|
|
 |
|
|
|
|
Print the time. |
|
|
|
 |
|
|
|
|
Increment the time by one second. |
|
|
|
 |
|
|
|
|
Compare two times for equality. |
|
|
|
 |
|
|
|
|
Determine if one time is less than (comes before) another. |
|
|
|
|
|
|
|
|
The specification of an ADT defines abstract data values and abstract operations for the user. Ultimately, of course, the ADT must be implemented in program code. To implement an ADT, the programmer must do two things: |
|
|
|
|
|
|
|
|
1. Choose a concrete data representation of the abstract data, using data types that already exist. |
|
|
|
|
|
|
|
|
2. Implement each of the allowable operations in terms of program instructions. |
|
|
|
 |
|
 |
|
|
Data Representation The concrete form of data used to represent the abstract values of an abstract data type. |
|
|
|
|
|
|
|
|
To implement the IntList ADT, we could choose a concrete data representation consisting of two items: a 100-element int array and an int variable that keeps track of the current length of the list. To implement the IntList operations, we must create algorithmssuch as the searching and sorting routines we wrote in Chapter 12based on the chosen data representation. |
|
|
|
|
|
|
|
|
To implement the TimeType ADT, we might use three int variables for the data representationone for the hours, one for the minutes, and one for the seconds. Or we might use three strings (char arrays) as the data representation, or even a three-element int array. The specification of the ADT does not confine us to any particular data representation. As long as we satisfy the specification, we are free to choose among alternative data representations and their associated algorithms. Our choice may be based on time efficiency (the speed at which the algorithms execute), space efficiency (the economical use of memory space), or simplicity and readability of the algorithms. Over time, you will acquire knowledge and experience that help you decide which implementation is best for a particular context. |
|
|
|
|
|