Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

1.4 The Time ADT

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.

  • Time(hours, minutes, seconds)

    Creates a new Time instance and initializes it with the given values. The hours is given as a value between 0--23, while the minutes and seconds are given as a value between 0-59.

  • hours()

    Returns the hour part of the time in the range 0--23.

  • minutes()

    Returns the minutes part of the time in the range 0--59.

  • seconds()

    Returns the seconds part of the time in the range 0--59.

  • isAM()

    Returns True if this time is ante meridiem or before midday (before 12 o'clock noon) or False if this time is post meridiem or after midday(at or after 12 o'clock noon).

  • elapsed(otherTime)

    Returns the number of seconds that have elapsed between this time and the otherTime.

  • add(otherTime)

    Returns a new Time instance that results from incrementing this time by the otherTime amount.

  • subtract(otherTime)

    Returns a new Time instance that represents the time difference between this time and the otherTime.

  • comparable(otherTime)

    Compares this time to the otherTime to determine their logical ordering.

  • toString()

    Returns a string representing the time in the 24-hour format hh:mm:ss.

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
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
Program: checktimes.py
  1. # Processes a collection of times, which are read from standard input, to
  2. # determine how many of them occur during the working hours of 8AM to 5PM.
  3. from mytime import Time
  4.  
  5. def main():
  6.    # Create two Time objects to represent the beginning and ending times.
  7.   startTime = Time(8, 0, 0)   # 8am
  8.   endTime = Time(17, 0, 0)    # 5pm
  9.  
  10.    # Read and process the times.
  11.   numValid = 0
  12.   time = promptAndReadTime()
  13.   while time is not None :
  14.     if time >= startTime and time <= endTime :
  15.       numValid = numValid + 1
  16.     time = promptAndReadTime()
  17.    
  18.    # Print the results.
  19.   print(numValid, "of the times occurred during the working day.")
  20.  
  21. # Prompts for and reads the components of a 24-hour time. A Time object
  22. # is returned or None when the user enters a negative value for the hours.
  23. def promptAndReadTime() :
  24.   print("Enter a time in 24-hour format.")
  25.   hours = int(input("hours (0 - 23 or < 0 to quit): "))
  26.   if hours < 0 :
  27.     return None
  28.   else :
  29.     minutes = int(input("minutes (0 - 59): "))
  30.     seconds = int(input("seconds (0 - 59): "))
  31.     return Time(hours, minutes, seconds)
  32.      
  33. 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.

Page last modified on July 30, 2023, at 02:21 PM