Chapter 12 Worksheet

Return to worksheet index.

(13 pts) Section 1:

  1. What is the difference between a class and an object?
  2. What is the difference between a function and a method?
  3. Write code to create an instance of this class and set its attributes. Remember, don't store numbers as strings. Use 40 and not "40".
    class Dog():
        def __init__(self):
            self.age = 0
   = ""
            self.weight = 0
  4. Write code to create two different instances of this class and set attributes for both objects. While a phone number is a number, those should be stored as strings. So we can keep leading zeros and those dashes.
    class Person():
        def __init__(self):
   = ""
            self.cell_phone = ""
   = ""
  5. For the code below, write a class that has the appropriate class name and attributes that will allow the code to work.
    my_bird = Bird()
    my_bird.color = "green" = "Sunny"
    my_bird.breed = "Sun Conure"
  6. Define a class that would represent a character in a simple 2D game. Include attributes for the position, name, and strength.
  7. The following code runs, but it is not correct. What did the programmer do wrong?
    class Person():
        def __init__(self):
   = ""
   = 0
    nancy = Person()
    name = "Nancy"
    money = 100
  8. Take a look at the code. It does not run. What is the error that prevents it from running?
    class Person():
        def __init__(self):
   = ""
   = 0
    bob = Person()
    print(, "has", money, "dollars.")
  9. Even with that error fixed, the program will not print out:
    Bob has 0 dollars.
    Instead it just prints out:
    has 0 dollars.
    Why is this the case?
  10. Take pairs of the following items, and list some of the ``has-a'' relationships, and the ``is-a'' relationships between them. For example, if you were working with a different list, you might pull ``Dolphin'' and ``Mammal'' and then say ``Dolphin is a Mammal.'' Please don't use items that aren't on the list.
  11. In Python, how is an ``is-a'' relationship implemented? Give an example of how it is implemented.
  12. In Python, how is a ``has-a'' relationship implemented? Give an example of how it is implemented.
  13. How does this ``has-a'' relationship work if an object is allowed more than one item of a given type? For example, a Customer class might have an attribute for account. So a Customer ``has-a'' account. But what if a customer has two accounts? Or ten? How do you store any number of items? We learned this back in Chapter 7. How do we make an attribute that can hold more than one of a given type? (Ask if you aren't sure.)

(10 pts.) Section 2:

To answer the next four questions, create one program. In that program will be the answers for all four questions. Make sure the program runs, and then copy/paste from the program to answer each of the questions below.

You should have a program that starts with three class definitions, one each for the first three questions. Then code that will create instances of each class, and that will be the answer to the last problem.

  1. Write code that defines a class named Animal:
  2. A class named Cat:
  3. A class named Dog:
  4. A main program with: