An abstract data type is defined by specifying the domain of the data elements that compose the ADT and the set of operations that can be performed on that domain. The definition should provide a clear description of the ADT including both its domain and each of its operations as only those operations specified can be performed on an instance of the ADT. Next, we provide the definition of a simple abstract data type for representing the time of day.
ADT Definition
The Time ADT
A time instance represents the time of day within any 24-hour period independent of the time zone and is accurate to the second.
|
The abstract data types defined in the text will be implemented as Python classes. This allows for encapsulation. That is, the data attributes of an object and the methods that are defined for use with the object are combined in a single definition and implementation. The class definition in turn provides the interface for the user-defined abstract data type.
When defining an ADT, we specify the ADT operations as method prototypes. The class constructor, which is used to create an instance of the ADT, is indicated by the name of the class used in the implementation and the name of the method used to create instances of the class.
NOTE
|
|
You may have noticed that the names of some of the operations in the definition of the ADT are italicized. This indicates that the operation will be implemented as a special method that is applied using a Python operator instead of the given name. By defining all of the ADT operations as named methods, we can focus on the general ADT specification that can be easily translated to other languages if the need arises and at the same time take advantage of Python's simple syntax in various sample programs. AppendixA provides a list of the special methods that must be implemented for each of the ADT operations specified with an italicized name.
|
To illustrate the use of the Time ADT, consider the simple program below
that processes a collection of time values using Time
objects. The times are read from standard input and examined to determine how many of them occur during the working hours of 8AM and 5PM. The user is continuously prompted to enter a new time until a negative value is entered for the hours.
Program Listing
# Processes a collection of times, which are read from standard input, to
# determine how many of them occur during the working hours of 8AM to 5PM.
from mytime import Time
def main():
# Create two Time objects to represent the beginning and ending times.
startTime = Time(8, 0, 0) # 8am
endTime = Time(17, 0, 0) # 5pm
# Read and process the times.
numValid = 0
time = promptAndReadTime()
while time is not None :
if time >= startTime and time <= endTime :
numValid = numValid + 1
time = promptAndReadTime()
# Print the results.
print(numValid, "of the times occurred during the working day.")
# Prompts for and reads the components of a 24-hour time. A Time object
# is returned or None when the user enters a negative value for the hours.
def promptAndReadTime() :
print("Enter a time in 24-hour format.")
hours = int(input("hours (0 - 23 or < 0 to quit): "))
if hours < 0 :
return None
else :
minutes = int(input("minutes (0 - 59): "))
seconds = int(input("seconds (0 - 59): "))
return Time(hours, minutes, seconds)
main()
|
This simple example illustrates an advantage of working with an abstraction by focusing on what functionality the ADT provides instead of how that functionality is implemented. By hiding the implementation details, we can use an ADT independent of its implementation. In fact, the choice of implementation for the Time ADT will have no effect on the statements in our example program.