CSC111 Programs for Week 9 2015

From dftwiki3
Revision as of 08:03, 30 March 2015 by Thiebaut (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--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, tat, ag ):
        """constructor.  Builds a cat with all its information"""
        self.name       = na
        self.breed      = brd
        self.vaccinated = vacc
        self.tattooed   = tat
        self.age        = ag

    def isVaccinated( self ):
        """returns True if cat is vaccinated, False otherwise"""
        return self.vaccinated

    def isTattooed( self ):
        """returns True if cat is tattooed, False otherwise"""
        return self.tattooed

    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"

        tat = "tattooed"
        if not self.tattooed:
            tat = "not tattooed"
        
        return "{0:20}:==> {1:1}, {2:1}, {3:1}, {4:1} yrs old".format(
            self.name, self.breed, vacc, tat, self.age )
    
def main():
    """ main program.  Creates a list of cats and displays
    groups sharing the same property.
        Minou, 3, vac, tat, stray
        Max, 1, not-vac, tat, Burmese
        Gizmo, 2, vac, not-tat, Bengal
        Garfield, 4, not-vac, tat, Orange Tabby
    """
    cats = []
    cat = Cat( "Minou", "stray", True, True, 3 )
    cats.append( cat )
    
    cats.append( Cat( "Max", "Burmese", False, True, 1 ) )
    cats.append( Cat( "Gizmo", "Bengal", True, False, 2 ) )
    cats.append( Cat( "Garfield", "Orange Tabby", False, True, 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()