Difference between revisions of "CSC111 Programs for Week 9 2015"

From dftwiki3
Jump to: navigation, search
(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, tat, ag ):
+
     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.tattooed  = tat
 
 
         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 isTattooed( self ):
 
        """returns True if cat is tattooed, False otherwise"""
 
        return self.tattooed
 
  
 
     def getAge( self ):
 
     def getAge( self ):
Line 47: Line 42:
 
         if not self.vaccinated:
 
         if not self.vaccinated:
 
             vacc = "not 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(
+
         return "{0:20}:==> {1:1}, {2:1}, {3:1} yrs old".format(
             self.name, self.breed, vacc, tat, self.age )
+
             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, tat, stray
+
         Minou, 3, vac, stray
         Max, 1, not-vac, tat, Burmese
+
         Max, 1, not-vac, Burmese
         Gizmo, 2, vac, not-tat, Bengal
+
         Gizmo, 2, vac, Bengal
         Garfield, 4, not-vac, tat, Orange Tabby
+
         Garfield, 4, not-vac, Orange Tabby
 
     """
 
     """
 
     cats = []
 
     cats = []
     cat = Cat( "Minou", "stray", True, True, 3 )
+
     cat = Cat( "Minou", "stray", True, 3 )
 
     cats.append( cat )
 
     cats.append( cat )
 
      
 
      
     cats.append( Cat( "Max", "Burmese", False, True, 1 ) )
+
     cats.append( Cat( "Max", "Burmese", False, 1 ) )
     cats.append( Cat( "Gizmo", "Bengal", True, False, 2 ) )
+
     cats.append( Cat( "Gizmo", "Bengal", True, 2 ) )
     cats.append( Cat( "Garfield", "Orange Tabby", False, True, 4 ) )
+
     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()