Difference between revisions of "CSC111 Programs for Week 9 2015"
(Created page with "--~~~~ ---- =Cats= <br /> ::<source lang="python"> # cats1.py # D. Thiebaut # Program for Week #9 # Define a Cat class, and # use it to create a collection of # cats. class...") |
|||
Line 18: | Line 18: | ||
tattooed, and age.""" | tattooed, and age.""" | ||
− | def __init__( self, na, brd, vacc | + | def __init__( self, na, brd, vacc, ag ): |
"""constructor. Builds a cat with all its information""" | """constructor. Builds a cat with all its information""" | ||
self.name = na | self.name = na | ||
self.breed = brd | self.breed = brd | ||
self.vaccinated = vacc | self.vaccinated = vacc | ||
− | |||
self.age = ag | self.age = ag | ||
Line 29: | Line 28: | ||
"""returns True if cat is vaccinated, False otherwise""" | """returns True if cat is vaccinated, False otherwise""" | ||
return self.vaccinated | return self.vaccinated | ||
− | |||
− | |||
− | |||
− | |||
def getAge( self ): | def getAge( self ): | ||
Line 47: | Line 42: | ||
if not self.vaccinated: | if not self.vaccinated: | ||
vacc = "not vaccinated" | vacc = "not vaccinated" | ||
− | |||
− | |||
− | |||
− | |||
− | return "{0:20}:==> {1:1}, {2:1}, {3 | + | return "{0:20}:==> {1:1}, {2:1}, {3:1} yrs old".format( |
− | self.name, self.breed, vacc | + | self.name, self.breed, vacc, self.age ) |
def main(): | def main(): | ||
""" main program. Creates a list of cats and displays | """ main program. Creates a list of cats and displays | ||
groups sharing the same property. | groups sharing the same property. | ||
− | Minou, 3, vac | + | Minou, 3, vac, stray |
− | Max, 1, not-vac | + | Max, 1, not-vac, Burmese |
− | Gizmo, 2, vac | + | Gizmo, 2, vac, Bengal |
− | Garfield, 4, not-vac | + | Garfield, 4, not-vac, Orange Tabby |
""" | """ | ||
cats = [] | cats = [] | ||
− | cat = Cat( "Minou", "stray" | + | cat = Cat( "Minou", "stray", True, 3 ) |
cats.append( cat ) | cats.append( cat ) | ||
− | cats.append( Cat( "Max", "Burmese", False | + | cats.append( Cat( "Max", "Burmese", False, 1 ) ) |
− | cats.append( Cat( "Gizmo", "Bengal", True | + | cats.append( Cat( "Gizmo", "Bengal", True, 2 ) ) |
− | cats.append( Cat( "Garfield", "Orange Tabby", False | + | cats.append( Cat( "Garfield", "Orange Tabby", False, 4 ) ) |
# print the list of all the cats | # print the list of all the cats | ||
Line 82: | Line 73: | ||
print( cat ) | print( cat ) | ||
− | |||
# print cats older than 2 | # print cats older than 2 | ||
print( "\nCats 2 or older:" ) | print( "\nCats 2 or older:" ) | ||
Line 94: | Line 84: | ||
+ | |||
</source> | </source> |
Revision as of 09:06, 30 March 2015
--D. Thiebaut (talk) 09:03, 30 March 2015 (EDT)
Cats
# cats1.py # D. Thiebaut # Program for Week #9 # Define a Cat class, and # use it to create a collection of # cats. class Cat: """a class that implements a cat and its information. Name, breed, vaccinated, tattooed, and age.""" def __init__( self, na, brd, vacc, ag ): """constructor. Builds a cat with all its information""" self.name = na self.breed = brd self.vaccinated = vacc self.age = ag def isVaccinated( self ): """returns True if cat is vaccinated, False otherwise""" return self.vaccinated def getAge( self ): """returns cat's age""" return self.age def getName( self ): """return name of cat""" return self.name def __str__( self ): """default string representation of cat""" vacc = "vaccinated" if not self.vaccinated: vacc = "not vaccinated" return "{0:20}:==> {1:1}, {2:1}, {3:1} yrs old".format( self.name, self.breed, vacc, self.age ) def main(): """ main program. Creates a list of cats and displays groups sharing the same property. Minou, 3, vac, stray Max, 1, not-vac, Burmese Gizmo, 2, vac, Bengal Garfield, 4, not-vac, Orange Tabby """ cats = [] cat = Cat( "Minou", "stray", True, 3 ) cats.append( cat ) cats.append( Cat( "Max", "Burmese", False, 1 ) ) cats.append( Cat( "Gizmo", "Bengal", True, 2 ) ) cats.append( Cat( "Garfield", "Orange Tabby", False, 4 ) ) # print the list of all the cats print( "\nComplete List:" ) for cat in cats: print( cat ) # print only the vaccinated cats print( "\nVaccinated cats:" ) for cat in cats: if cat.isVaccinated()==True: print( cat ) # print cats older than 2 print( "\nCats 2 or older:" ) for cat in cats: if cat.getAge()>=2: print( cat ) if __name__=="__main__": main()