17 Classes and Functions
1. There is a
very simple function to calculate the current time
statement is a null operation; nothing happens when it executes. You
can delete it if you
Now you know the
concept of adding more time to the starting time. Let's make this
function more complete:
We’ll define a
class called Time that records the time of day. The class definition
looks like this:
the starting time of a movie, and the duration of a movie, the
following function will figure out when the movie will be done:
result, 10:80:00 might not be what you were hoping for. The problem is
that this function does not deal with cases where the number of seconds
or minutes adds up to more than sixty. When that happens, we have to
“carry” the extra seconds into the minute column or the extra minutes
into the hour column.
However, what if
the movie is pretty long and there are carries in 'hour'.
concept, just make the following modification:
Prototypoing vs planning
alternative is designed development, in which high-level insight into
the problem can make the programming much easier. In this case, the
insight is that a Time object is really a three-digit number in base 60
(see http://en.wikipedia.org/wiki/Sexagesimal.)! The second attribute
is the “ones column”, the minute attribute is the “sixties column”, and
the hour attribute is the “thirty-six hundreds column”.
We can convert
Time objects to integers and take advantage of the fact that the
computer knows how to do integer arithmetic.
parentheses in 'class Time():' is still redundant)
1. If you put
'add_time(t1, t2)' inside the class as a method, how you can modify the
code to deliver the same result?
2. Modify the code
to report the results in the '01:20:00' version but not the '13:20:00'