< previous page page_840 next page >

Page 840
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.
TYPE
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
TimeType
DOMAIN
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Each TimeType value is a time of day in the form of hours, minutes, and seconds.
OPERATIONS
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Set the time.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Print the time.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Increment the time by one second.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Compare two times for equality.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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.

 
< previous page page_840 next page >