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

From dftwiki3
Jump to: navigation, search
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:03, 30 March 2015 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 09:03, 30 March 2015 (EDT)
 
----
 
----
 +
=Die=
 +
<br />
 +
::<source lang="python">
 +
# dice.py
 +
# D. Thiebaut
 +
# Program for Week 9
 +
# an example program that defines a Die class, and
 +
# demonstrates how to create 2 objects from this class.
 +
 +
import random
 +
 +
 +
class Die:
 +
    """a class implementing a Die, with a given number
 +
    of sides, and a value (the number of the top face)"""
 +
   
 +
    def __init__( self, n ):
 +
        """initializes the die with n faces, and a value 1,
 +
        by default"""
 +
        self.noSides = n
 +
        self.value  = 1
 +
 +
    def roll( self ):
 +
        """roll the die and pick a new random value for it"""
 +
        self.value = random.randrange( 1, self.noSides+1 )
 +
 +
    def getValue( self ):
 +
        """return the current value, i.e. the number on the top
 +
        side"""
 +
        return self.value
 +
 +
 +
def main():
 +
    # Create 2 dice, one with 6 sides, one with 8
 +
    d1 = Die( 6 )
 +
    d2 = Die( 8 )
 +
 +
    while True:
 +
        # Roll both dice
 +
        d1.roll( )
 +
        d2.roll( )
 +
 +
        # display their value
 +
        print( "Die 1: ",  d1.getValue() )
 +
        print( "Die 2: ",  d2.getValue() )
 +
 +
        ans = input( "Press enter to continue.  Any other key to quit: " )
 +
        if len( ans.strip() )!= 0:
 +
            break
 +
 +
if __name__=="__main__":
 +
    main()
 +
   
 +
 +
</source>
 +
<br />
 
=Cats=
 
=Cats=
 
<br />
 
<br />

Revision as of 09:19, 30 March 2015

--D. Thiebaut (talk) 09:03, 30 March 2015 (EDT)


Die


# dice.py
# D. Thiebaut
# Program for Week 9
# an example program that defines a Die class, and
# demonstrates how to create 2 objects from this class.

import random


class Die:
    """a class implementing a Die, with a given number
    of sides, and a value (the number of the top face)"""
    
    def __init__( self, n ):
        """initializes the die with n faces, and a value 1,
        by default"""
        self.noSides = n
        self.value   = 1

    def roll( self ):
        """roll the die and pick a new random value for it"""
        self.value = random.randrange( 1, self.noSides+1 )

    def getValue( self ):
        """return the current value, i.e. the number on the top
        side"""
        return self.value


def main():
    # Create 2 dice, one with 6 sides, one with 8
    d1 = Die( 6 )
    d2 = Die( 8 )

    while True:
        # Roll both dice
        d1.roll( )
        d2.roll( )

        # display their value
        print( "Die 1: ",  d1.getValue() )
        print( "Die 2: ",  d2.getValue() )

        ans = input( "Press enter to continue.  Any other key to quit: " )
        if len( ans.strip() )!= 0:
            break

if __name__=="__main__":
    main()


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()