# CSC 15 Lab 9 # Due Next Thursday ######## Basic mechanics of object oriented programming. ######## ## Reread the notes and examples that I gave you last week on OOP to freshen # your memory. Here's another example below: # Sample class construction. Early in the semester we wrote a program that # added minutes and seconds. The values for minutes and seconds were # kept in different variables. We are now going to rewrite the program # in the OOP style. Such an organization will allow the program to be usable # in a variety of situations (i.e., as parts of larger programs). ## I must first *design* my objects by being clear as to: # 1. What are the pieces of data that the object will consist of. In other # words: what variables will belong to each object. # Answer: minutes, seconds # 2. What methods (functions) do I wish to call on each object. # Answer: *add*: add two minutes. *increment*: increment time by one sec. # *reset*: sets time to 0:0. *tostring*: represent time in string format # Now I can write my class of time objects: class mstime: # minutes-seconds time obects def __init__(self,m,s): # constructor initializes variables. self.min = m # initialize minutes self.sec = s # and seconds # constructor def reset(self): # resets values to 0 min, 0 sec. self.min = 0 self.sec = 0 # reset def inc(self): # increment time destructively by one second. self.sec += 1 # destructive means the 'self' object is changed. if (self.sec>=60): self.min = self.min + self.sec / 60 self.sec = self.sec % 60 # if # inc def add(self, B): # non-destructively add self and B time objects s = self.sec + B.sec m = self.min + B.min extramin = s / 60 s = s % 60 m = m + extramin newtime = mstime(m,s) # new object to be returned return newtime # add # The function above needs to be called in the form # myobject.add(yourobject). For common operators like +, -, *, etc.. # it's possible to redefine the operators for your class using the # keywords __add__, __sub__, __mul__ and __div__: def __add__(self, B): # redefines + operator for mstime objects s = self.sec + B.sec m = self.min + B.min extramin = s / 60 s = s % 60 m = m + extramin newtime = mstime(m,s) # new object to be returned return newtime # add def tostring(self): # convert to string for display representation return str(self.min) + ":" + str(self.sec) # tostring # end mstime ### usage of class mstime: t1 = mstime(3,40) # 3 min, 40 sec. t2 = mstime(1,59) # 1 min, 50 sec. t2.inc() t3 = t1.add(t2) t4 = t3.add(t1) print t2.tostring() print t3.tostring() print t4.tostring() t5 = mstime(3,50) + t3 # implicitly calls t5.__add__(t3) print t5.tostring() ############################################################################# ## Part I: ## I'm a gas station owner and I've hired you to program for me. Each # gas station sells only one type of gas: regular unleaded. # Each station will have an initial supply of 100 gallons of gas. # Each station also has a manager, who will be fired if sales aren't good. # I want a computer to keep track of the sales and supply situation at each # gas station. I want to be able to run the following code: # Create station objects with manager's name and initial price per gallon: st1 = gasstation("john makeabuck",3.39) st2 = gasstation("jane gouger",3.79) st1.sellgas(10.5) # station1 just sold 10.5 gallons of gas st2.sellgas(20) # station2 just sold 20 gallons of gas st1.sellgas(3000) # should print not enough gas, no sale. st1.sellgas(11) print st1.totalsales() # prints the dollar amount of total sales. (3.39*21.5) print st1.currentsupply() # prints gallons of gas left in supply. st1.resupply() # reset gas supply to 100 gallons st2.sale() # decrease gas price by 10% st2.gouge() # double price during natural disasters st2.replacemanager("jimmy manageron") # fire old manager, hire jimmy. if st1.moresales(st2): print "station1 has better sales than station2" ## Now write the class to make this work. Think first: what variables # will each class contain? ###### Part II: Dates. ##################################################### # For the second class, you need to implement objects similar to the mstime # object. You want a class of objects called 'date'. Each date object will # have a year, a month, and a day. Here's the code you'd like to write: date1 = date(4,15,2008) # create date object for april 15th, 2008 date2 = date(12,31,2004) # december 31st, 2004 print date1.tostring() # prints "4/15/2008" print date2.leapyear() # prints True, since 2004 % 4 == 0 date1.nextday() # advance date (destructively) to 4/16/2008 date2.nextday() # advance date to 1/1/2005 if date2.before(date1): print "date2 comes before date1" #### don't forget! ## Hint: the difficult method is nextday. For this, you need to know ## the number of days in each month. The following array may help you: Days = [31,28,31,30,31,30,31,31,30,31,30,31] # However, you also need to adjust the value of Days[1] (Feburary) if # the current year is a leap year. You can define this array inside your # nextday function, or as a variable of your object (self.Days). # Alternatively, instead of using an array you can write another function # (also inside your class) that returns the number of days given the month # number. # Hint for the 'before' method: To check if one date comes before another, # first compare the year. If the years are the same, compare the month. If # the months are also the same, compare the day. ############################################# ## (CHALLENGE) The formula for determining leap years is not as simple # as dividing by 4. Research the real formula and implement it. ## (MEGACHALLENGE): implement a method to compute the number of days until # the given date: christmas = date(12,25,2008) print date1.daysuntil(christmas), "days until Christmas" # Hint: convert the date to one number: christmas is day 359 of 365. ## (GIGACHALLENGE) Write a method to return the day of the week # of a data object. It should work as follows: date1 = date(4,14,2008) print date1.dayofweek() # should print Monday # Knowing that 4/14/2008 is a Monday should be enough for you to figure # out the day-of-week of all the other dates. ## (TERACHALLENGE) Friday the 13th is an unlucky day (and a very bad movie). # Write a function to find the NEXT friday the 13th after a given date: date1.nextfri13() # should return a date object representing the next # friday the 13th after date1.