1

I've created an ADT called "Time" and need to create a new python file that tests it. The following is what I have:

The code below implements the Time ADT using time capabilities of the datetime module.

import datetime

class Time :
     # Creates an new time instance and initializes it with the given time.
    def __init__ (self, hours, minutes, seconds):
        self.hr = hours
        self.min = minutes
        self.sec = seconds

    def hour(self):
        # Returns the hour as an integer between 0 to 23.
        return int(self.hr)

    def min(self):
        # Returns the minute as an integer between 0 to 59.
        return int(self.min)

    def sec(self):
        # Returns the seconds as an integer between 0 to 59.
        return int(self.sec)

    def numSec(self, otherTime):
        # Returns the number of seconds elapsed between this time and the otherTime.
        return abs(self.sec - otherTime.seconds)

    def advanceBy(self, numSeconds):
        # Advances the time by the given number of seconds.
        if (numSeconds + self.sec) < 60:
             seconds = seconds + numSeconds
        if ((numSeconds / 60) + self.min) < 60:
                minutes = minutes + int(numSeconds / 60)
                seconds = seconds + numSeconds % 60
        else:
                hours = hours + int(numSeconds / 3600)
                minutes = minutes + (numSeconds % 3600)
                seconds = seconds + numSeconds % 60

    def isPM(self):
        # Returns a Boolean indicating if this time is at or after 12 o'clock noon.
        if self.hr > 11:
            return True
        else:
            return False

    def comparable(self, otherTime):
        # Compares this time to the otherTime to determine their logical ordering.
        if datetime.time(otherTime) > datetime.time(self.hr, self.min, self.sec):
            return otherTime + "is after" + (self.hr, self.min, self.sec)
        else:
            return otherTime + "is before"+ (self.hr, self.min, self.sec)

    def toString(self):
        # Returns "HH:MM:SS XX", in the 12-hr format where XX is either AM/PM.
        if self.hr > 11:
            return "%d:%d%d PM" % (hours, minutes, seconds)
        else:
            return "%d:%d%d AM" % (hours, minutes, seconds)

I've created another file that is supposed to import this module (saved as timeADT.py) and test every operation. However, I can't figure out how to properly call each function. I want to put the time as (hours, minutes, seconds), but when I call timeADT.hour(5,12,49) for example, it gives me an error and says I should only have one argument.

import timeADT

timeADT.hour(5,49,13)

I'm new with python and very confused as to where I go from here. Any help on how to create this "testing" file would be much appreciated!

Thank you.

User100
  • 83
  • 4
  • For testing a `.py` script, have a look at these links. http://pymbook.readthedocs.org/en/latest/testing.html https://docs.python.org/2/library/unittest.html . They should give you a good idea of how to test your script – ashwinjv Jun 01 '15 at 22:53

2 Answers2

2

You should construct the object first and then use it. Try this:

from timeADT import Time
time = Time(5,49,13)
print time.hour, time.min, time.sec
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
0

But there are other problems too:

def numSec(selfotherTime):

These is a comma missing after self

def advanceBy(numSeconds):

Here the self, is completely missing.

jcoppens
  • 5,306
  • 6
  • 27
  • 47