# Your Name Here # Fall 2016 # CS 152 Project 7 # # First class design # # Elephant class import random # define an Elephant class that holds all necessary information about an elephant class Elephant: # global fixed properties of elephants # assign JuvenileAge, MaxAge as constants here # init function def __init__(self, calvingInterval = 3.1, age=None): # if age is not None, then assign to self.age the value in age. # else, assign age a random number between 1 and Elephant.MaxAge # assign to self.gender a random gender # assign to self.pregnant and to self.contraception the value zero # if the elephant is female and adult, then check whether the # elephant is pregnant. If so, then assign to self.pregnant a # random value between 1 and 22. # before returning, it might be helpful to put a print # statement here so you know when the init method is called return # Create get methods for age, gender, months pregnant, contraception # Call these functions the following. Each will have self as the only parameter. # getAge (provided as an example) # getGender # getMonthsPregnant # getContraception def getAge(self): return self.age # Create a set of useful evaluation functions that return True or False. Each of these # functions takes only self as a parameter. # isFemale (provided as an example) # isPregnant # isCalf # isJuvenile # isAdult # isSenior def isFemale(self): return self.gender == 'f' # Set functions for age, gender, months pregnant, contraception. # setAge (provided as an example) # setGender # setMonthsPregnant # setContraception # # Each of these functions takes both self and then a new value for # the field. Assign the new value to the appropriate field of # self. def setAge(self, a): self.age = a # utility functions for updating age, darting an elephant, simulating a month # increment the elephant's age by 1 year def incrementAge(self): # the elephant's age is in self.age pass # handle darting an elephant def dart(self, monthsEffective = 22): # set self.pregnant to 0 and self.contraception to # monthsEffective. pass # Makes an elephant one month older by modifying self.contraception or self.pregnant appropriately # returns True if a baby should be created, False otherwise def progressMonth(self, calvingInterval): # check contraceptive first # check pregnancy second pass # function that returns a string appropriate for printing def __str__(self): s = "Age: %2d Sex: %s" % (self.age, self.gender) if self.isFemale() and self.contraception > 0: s += " Contraception: %d" % (self.contraception) elif self.gender == 'f' and self.pregnant > 0: s += " Pregnant: %d" % (self.pregnant) return s # test function that makes 20 elephants and prints them out def test(): print "Making 20 elephants" random.seed(3) for i in range(20): # Make an Elephant object and store in the the variable e e = Elephant() print(e) if __name__ == "__main__": test()