Difference between revisions of "CSC111 Homework 10 2014"

From dftwiki3
Jump to: navigation, search
(Output)
Line 77: Line 77:
 
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
 
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
 
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
 
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
 +
 
Please enter the information for the new cat:
 
Please enter the information for the new cat:
 
Cat name?        Ralph
 
Cat name?        Ralph
Line 99: Line 100:
 
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
 
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
 
Cat: Ralph, age: 4, vaccinated, neutered (Angora)</source>
 
Cat: Ralph, age: 4, vaccinated, neutered (Angora)</source>
 
+
</source>
 +
<br />
 
;Question 1
 
;Question 1
 
Recreate the original program with its Cat class.  Make sure that it will generate the same output show above if fed the same cat information illustrated in the output, also shown above.
 
Recreate the original program with its Cat class.  Make sure that it will generate the same output show above if fed the same cat information illustrated in the output, also shown above.

Revision as of 14:29, 9 April 2014

--D. Thiebaut (talk) 10:30, 8 April 2014 (EDT)


Page under construction!

UnderConstruction.jpg

Problem #1


The program below is missing its class. It used to have a class defined, with methods and member variables, but unfortunately it got erased.

We do have the output of a run of the program, though, when it still had its class definition. Both the incomplete code and the output are shown below.


def getNewCat():
    print( "Please enter the information for the new cat:")
    name = input(       "Cat name?         " )
    age  = int( input(  "Age, in years?    " ) )
    vac  = input(       "Vaccinated (Y/N)? " ).strip().lower()
    vac  = vac in ['y', 'yes']
    neut = input(       "Neutered (Y/N)?   " ).strip().lower()
    neut = neut in ['y', 'yes' ]
    breed= input(       "Breed?            " ).strip()
    
    return Cat( name, age, vac, neut, breed )

def main():
    text="""Minou, 3, Yes, Yes, stray
            Max, 1, Yes, No, Burmese
            Gizmo, 2, No, No, Bengal
            Garfield, 4, Yes, Yes, Orange Tabby"""

    for line in text.split( "\n" ):
        words = line.strip().split( "," )
        if len( words ) != 5:
            continue
        name, age, vaccinated, neutered, breed = words
        age = int( age.strip() )
        if vaccinated.strip().lower() == "yes":
            vaccinated = True
        else:
            vaccinated = False

        if neutered.strip().lower() == "yes":
            neutered = True
        else:
            neutered = False
        
        cat = Cat( name.strip(), age, vaccinated, neutered, breed.strip() )
        try:
            cats.append( cat )
        except NameError:
            cats = []
            cats.append( cat )

    display( cats, "List of cats:" )

    cats.append( getNewCat() )

    display( cats, "List of cats with new addition:" )

    vaccinatedNeuteredCats = []
    for cat in cats:
        if cat.isVaccinated() and cat.isNeutered():
            vaccinatedNeuteredCats.append( cat )

    display( vaccinatedNeuteredCats, "Vaccinated and neutered cats" )
    
    
main()

Output


List of cats:
=============
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Max, age: 1, vaccinated, not neutered (Burmese)
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)

Please enter the information for the new cat:
Cat name?         Ralph
Age, in years?    4
Vaccinated (Y/N)? y
Neutered (Y/N)?   y
Breed?            Angora


List of cats with new addition:
===============================
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Max, age: 1, vaccinated, not neutered (Burmese)
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
Cat: Ralph, age: 4, vaccinated, neutered (Angora)


Vaccinated and neutered cats
============================
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
Cat: Ralph, age: 4, vaccinated, neutered (Angora)

</source>

Question 1
Recreate the original program with its Cat class. Make sure that it will generate the same output show above if fed the same cat information illustrated in the output, also shown above.