CHAPTER 11 : MEMBER FUNCTIONS

example 11.1 page no :112

In [1]:
'''
example 11.1 page no :112
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 


c = Time( 9, 14, 30.0 )
c.printTime ()
9 : 14 : 30.0
Time is  9  hour  14  minutes  30.0

example 11.2 page no :114

In [2]:
'''
example 11.2 page  no :114
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

    def increment (self,secs):
        self.second += secs
        while (self.second >= 60.0):
            self.second -= 60.0;
            self.minute += 1;

        while (self.minute >= 60):
            self.minute -= 60.0;
            self.hour += 1;

c = Time (9, 14, 30.0)
c.increment (500.0);
c.printTime ();
9 : 22 : 50.0
Time is  9  hour  22  minutes  50.0

example 11.3 page no :115

In [3]:
'''
example 11.3 page no :115
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

    def convertToSeconds(self):
        minutes = self.hour * 60 + self.minute;
        seconds = minutes * 60 + self.second;
        return seconds;


c = Time (9, 14, 30.0)
print c.convertToSeconds()
33270.0

example 11.4 page no :115

In [4]:
'''
example 11.4 page no :115
'''


class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

    def convertToSeconds(self):
        minutes = self.hour * 60 + self.minute;
        seconds = minutes * 60 + self.second;
        return seconds;

    def after (self,time2):
        if (self.hour > time2.hour):
            return True;
        if (self.hour < time2.hour): 
            return False;
        if (self.minute > time2.minute): 
            return True;
        if (self.minute < time2.minute):
            return False;
        if (self.second > time2.second): 
            return True;
        return False;

t1 = Time( 11, 59, 3.14159 )
t2 = Time( 12, 42, 3.234 )

if (t2.after(t1)):
    print "The bread will be done after it starts." 
The bread will be done after it starts.

example 11.5 page no :116

In [5]:
'''
example 11.5 page no :116
'''

class Time:
    def __init__(self,h,m=None,s=None):
        if m==None:
            self.hour = int (secs / 3600.0);
            secs -= self.hour * 3600.0;
            self.minute = int (secs / 60.0);  
            secs -= self.minute * 60.0;
            self.second = secs;
        else:
            self.hour = h
            self.minute = m
            self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

c = Time (9, 14, 30.0)
c.printTime()
9 : 14 : 30.0
Time is  9  hour  14  minutes  30.0

example 11.6 page no : 117

In [6]:
'''
example 11.6 page no : 117
'''
class Time:
    def __init__(self,h,m,s):
        self.hour = h
        self.minute = m
        self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

c = Time (9, 14, 30.0)
c.printTime()
9 : 14 : 30.0
Time is  9  hour  14  minutes  30.0

example 11.7 page no :119

In [7]:
'''
example 11.7 page no :119
'''
class Time:
    def __init__(self,h,m=None,s=None):
        if m==None:
            self.hour = int (secs / 3600.0);
            secs -= self.hour * 3600.0;
            self.minute = int (secs / 60.0);  
            secs -= self.minute * 60.0;
            self.second = secs;
        else:
            self.hour = h
            self.minute = m
            self.second = s

    def printTime(self):
        print self.hour , ":" , self.minute , ":" , self.second
        print "Time is " , self.hour , " hour " , self.minute , " minutes " , self.second 

    def after (self,time2):
        if (self.hour > time2.hour):
            return True;
        if (self.hour < time2.hour): 
            return False;
        if (self.minute > time2.minute): 
            return True;
        if (self.minute < time2.minute):
            return False;
        if (self.second > time2.second): 
            return True;
        return False;

    def convertToSeconds(self):
        minutes = self.hour * 60 + self.minute;
        seconds = minutes * 60 + self.second;
        return seconds;

    def increment (self,secs):
        self.second += secs
        while (self.second >= 60.0):
            self.second -= 60.0;
            self.minute += 1;

        while (self.minute >= 60):
            self.minute -= 60.0;
            self.hour += 1;

    def add(self,t2):
        a = t2.convertToSeconds()
        self.increment(a)

c = Time(9, 14, 30.0)
c.increment (500.0);
c.printTime();
b = Time(3, 35, 0.0);
curent = c
c.add(b)
d = c
d.printTime()
if(d.after(curent)):
    print "The bread will be done after it starts." 
9 : 22 : 50.0
Time is  9  hour  22  minutes  50.0
12 : 57.0 : 50.0
Time is  12  hour  57.0  minutes  50.0
In [ ]: