Difference between revisions of "CSC11 Homework 10 Solution Programs 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Program 1= <br /> <source lang="python"> # hw10a.py # Joyce Xue ( aa ) # (slightly edited by D. Thiebaut) # 04.15.2014 # # This program contains a class Cat that ...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 06:53, 23 April 2014 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 06:53, 23 April 2014 (EDT)
 
----
 
----
 +
<onlydft>
 
=Program 1=
 
=Program 1=
 
<br />
 
<br />
Line 168: Line 169:
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
 +
# hw10b.py
 +
# Katerina Popova ( af )
 +
# (Slightly edited by D. Thiebaut)
 +
# 04.15.2014
 +
# =====================================================================
 +
# A program containing 3 classes: Person, Dog, and ListPeopleDog
 +
# Descpription of each class and its method is found within each class.
 +
# This program creates a list object of people and their dog (if they have one).
 +
# Only 1 dog per person, at most, is allowed in this program.
 +
# The program gets the data from a string provided in main()
 +
# An example of the output generated by the program is shown below:
 +
# =====================================================================
 +
#
 +
# Joe, 23 years old has a dog:  Fido (vaccinated)
 +
# Debra, 30 years old has a dog:  Rex (not vaccinated)
 +
# Frida, 10 years old has a dog:  Ralph (vaccinated)
 +
# Maria, 21 years old has a dog:  Gizmo (vaccinated)
 +
# Frank, 31 years old does not have a dog
 +
# Millie, 25 years old has a dog:  Maxi (not vaccinated)
 +
#
 +
# Debra, 30 years old has a non vaccinated dog called Rex
 +
# Millie, 25 years old has a non vaccinated dog called Maxi
 +
#
 +
# Frank, 31 years old doesn't have a dog
 +
#
 +
# The oldest person to own a dog is Debra,
 +
#    30 years old and the dog is Rex (not vaccinated)
 +
#
 +
# =====================================================================
 +
 +
class Person:
 +
    # a class containing 2 private member variables:
 +
    # - name of the person (string)
 +
    # - age (integer)
 +
    # methods:
 +
    # getName(): returns the name of the person (string)
 +
    # getAge(): returns the age of the person (int)
 +
    # setAge(x): sets the age of the person to x (int)
 +
 +
    # constructor: intializes a person with all fields
 +
    def __init__(self, n, a ):
 +
        self._name = n
 +
        self._age  = a
 +
 +
    # returns a string with the name and age.
 +
    def __str__( self ):
 +
        return "%s, %d years old" % ( self._name, self._age )
 +
 +
    # returns the name
 +
    def getName( self ):
 +
        return self._name
 +
 +
    # set the age to a new value x
 +
    def setAge( self, x ):
 +
        self._age = x
 +
 +
    # returns the age of the person
 +
    def getAge( self ):
 +
        return self._age
 +
 +
 +
class Dog:
 +
    # a class containing 2 private member variables:
 +
    # - name-tag of the dog (string)
 +
    # - whether the dog is vaccinated or not (boolean)
 +
    # methods:
 +
    # isVaccinated(): returns True if the dog is vaccinated, False otherwise
 +
    # getName(): returns the name-tag of the dog
 +
    # setName(x): sets the name-tag of the dog
 +
    # constructor
 +
    def __init__( self, tag, vaccinated ):
 +
        self._tag = tag
 +
        self._vaccinated = vaccinated
 +
 +
    # default string representation for the dog
 +
    def __str__( self ):
 +
        if self._vaccinated == True:
 +
            return "%s (vaccinated)" % self._tag
 +
        return "%s (not vaccinated)" % self._tag
 +
 +
    # isVaccinated: returns True if the dog is vaccinated, False otherwise
 +
    def isVaccinated( self ):
 +
        return self._vaccinated
 +
 +
    # returns the name of the dog
 +
    def getNameTag( self ):
 +
        return self._tag
 +
 +
    # changes the name-tag
 +
    def setNameTag( self, tag ):
 +
        self._tag = tag
 +
 +
 +
class ListPeopleDog:
 +
    # a class that maintains a list of pairs.  The first
 +
    # element of a pair is a Person object, the second a Dog object
 +
    # if the person doesn't have a dog, the second element of the pair is None
 +
    # Methods supported:
 +
    # isEmpty(): returns True if the list is empty, False othewise
 +
    # append( p, d ): appends a pair( person, dog) to the list
 +
    # getDogOf( name ): returns the dog object associated with the given person's name
 +
    # getOwnerOf( tag ): returns the person object associated with the given dog tag
 +
    # size( ): returns the number of pairs in the list
 +
    # getPersonDogAt( i ): returns the person object and the dog object located
 +
    # at Index i in the list.
 +
 +
    # constructor
 +
    def __init__( self ):
 +
        self._L = []
 +
 +
    # returns a string formatted as several lines, each representing
 +
    # a person and his/her dog.
 +
    def __str__( self ):
 +
        s = ""
 +
        for person, dog in self._L:
 +
            if dog == None:
 +
                s = s + str( person )+ " [No dogs]\n"
 +
            else:
 +
                s = s + str( person )+ " ["+str( dog ) +"]\n"
 +
        return s
 +
   
 +
    # returns True if the list is empty.  False otherwise.
 +
    def isEmpty( self ):
 +
        return len( self._L )==0
 +
 +
    # add a new pair to the list
 +
    def append( self, person, dog ):
 +
        self._L.append( (person, dog) )
 +
 +
    # returns the dog associated with the first person who's name is passed.
 +
    def getDogOf( self, name ):
 +
        name = name.strip().lower()
 +
        for person, dog in self._L:
 +
            if dog == None:
 +
                continue
 +
            if person.getName().lower() == name:
 +
                return dog
 +
        return None
 +
 +
    # returns the person object associated with the first dog in the list with the
 +
    # tag passed as a parameter.
 +
    def getOwnerOf( self, name ):
 +
        name = name.strip().lower()
 +
        for person, dog in self._L:
 +
            if dog == None:
 +
                continue
 +
            if dog.getNameTag()==name:
 +
                return person
 +
        return None
 +
 +
    # returns the number of pairs in the list.
 +
    def size( self ):
 +
        return len( self._L )
 +
 +
    # returns the person and dog located at Index i in the list.
 +
    def getPersonDogAt( self, i ):
 +
        if i < len( self._L ):
 +
            return self._L[i][0], self._L[i][1]
 +
        return None, None
 +
       
 +
# ================================================
 +
#                    main
 +
# ================================================
 +
def main():
 +
    text = """Joe, 23, Fido, vaccinated
 +
              Debra, 30, Rex, not vaccinated
 +
              Frida, 10, Ralph, vaccinated
 +
              Maria, 21, Gizmo, vaccinated
 +
              Frank, 31, ,
 +
              Millie, 25, Maxi, not vaccinated"""
 +
   
 +
    # create a new ListPeopleDog object   
 +
    L = ListPeopleDog()
 +
   
 +
    # parse the text and add it as people-dog pairs in the list
 +
    for line in text.split( "\n" ):
 +
        if len( line.split(',') ) != 4:
 +
            continue
 +
        name, age, tag, vacc = line.split(',')
 +
        name = name.strip()
 +
        age  = int( age.strip() )
 +
        tag  = tag.strip()
 +
        vacc = "not" not in vacc.lower()
 +
 +
        if len( tag )== 0:
 +
            L.append( Person(name, age), None  )
 +
        else:
 +
            L.append( Person(name, age), Dog( tag, vacc )  )
 +
 +
    # Display the list
 +
    for i in range( L.size() ):
 +
        p, d = L.getPersonDogAt( i )
 +
        if d != None:
 +
            print( p, "has a dog: ", d )
 +
        else:
 +
            print( p, "does not have a dog" )
 +
    print ()
 +
 +
    # =====================================================
 +
    # Challenge 1
 +
    # Prints the list of people who have unvaccinated dogs.
 +
    # =====================================================
 +
 +
    # for every item in the L list
 +
    # get the person-dog binome
 +
    for i in range( L.size()):
 +
        p, d = L.getPersonDogAt( i )
 +
 +
        # if a person does not own a dog
 +
        # continue (skip)
 +
        if d == None:
 +
            continue
 +
 +
        # if dog is not vaccinated
 +
        # output the owner with the name of the dog
 +
        if d.isVaccinated() == False:
 +
            print ( "%s has a non vaccinated dog called %s."
 +
                    % (p, d.getNameTag()))
 +
    print()
 +
 +
    # =======================================
 +
    # Challenge 2
 +
    # Prints the list of people without dogs.
 +
    # =======================================
 +
    for i in range( L.size()):
 +
        p, d = L.getPersonDogAt( i )
 +
 +
        # if a person does not own a dog
 +
        # output their name
 +
        if d == None:
 +
            print ( p, "doesn't have a dog." )
 +
 +
    print()
 +
 +
    # ========================================
 +
    # Challenge 3
 +
    # Displays the oldest person who has a dog.
 +
    # ========================================
 +
 +
    # initializing a maxAge to 0
 +
    maxAge = 0
 +
   
 +
    for i in range( L.size()):
 +
        p, d = L.getPersonDogAt( i )
 +
 +
        # if person does own a dog
 +
        if d != None:
 +
 +
            # if the age of a person is
 +
            # bigger than maxAge
 +
            # the new maxAge is reset
 +
            # and this person and his dog
 +
            # are initialized in new variables
 +
            if p.getAge() > maxAge:
 +
                maxAge = p.getAge()
 +
                oldestPerson = p
 +
                dogOldestPerson = d
 +
 +
    # desired output
 +
    print( "The oldest person to own a dog is %s and the dog is %s."
 +
          % (oldestPerson, dogOldestPerson))
 +
 +
 +
# main entry point for the program
 +
main()
  
 
</source>
 
</source>
 
<br />
 
<br />
 +
</onlydft>
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]][[Category:Homework]]

Latest revision as of 08:24, 30 August 2014

--D. Thiebaut (talk) 06:53, 23 April 2014 (EDT)



...