CSC111 Programs for Week 9 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()