Chapter 10 Review and Programming Exercises
Chapter 10 Review and Programming Exercises
25 def get_total_charges(self):
26 return _ _parts_charges + _ _labor_charges + \
27 (_ _parts_charges * TAX_RATE)
Checkpoint
10.15 The typical UML diagram for a class has three sections. What appears in these
three sections?
10.16 What is a problem domain?
10.17 When designing an object-oriented application, who should write a description of
the problem domain?
10.18 How do you identify the potential classes in a problem domain description?
10.19 What are a class’s responsibilities?
10.20 What two questions should you ask to determine a class’s responsibilities?
10.21 Will all of a class’s actions always be directly mentioned in the problem domain
description?
Review Questions
Multiple Choice
1. The ______________ programming practice is centered on creating functions that are
separate from the data that they work on.
a. modular
b. procedural
c. functional
d. object-oriented
2. The ______________ programming practice is centered on creating objects.
a. object-centric
b. objective
c. procedural
d. object-oriented
566 Chapter 10 Classes and object-oriented Programming
11. In one approach to identifying the classes in a problem, the programmer identifies the
______________ in a description of the problem domain.
a. verbs
b. adjectives
c. adverbs
d. nouns
12. In one approach to identifying a class’s data attributes and methods, the programmer
identifies the class’s ______________.
a. responsibilities
b. name
c. synonyms
d. nouns
True or False
1. The practice of procedural programming is centered on the creation of objects.
2. Object reusability has been a factor in the increased use of object-oriented programming.
3. It is a common practice in object-oriented programming to make all of a class’s data
attributes accessible to statements outside the class.
4. A class method does not have to have a self parameter.
5. Starting an attribute name with two underscores will hide the attribute from code
outside the class.
6. You cannot directly call the _ _str_ _ method.
7. One way to find the classes needed for an object-oriented program is to identify all of
the verbs in a description of the problem domain.
Short Answer
1. What is meant by data hiding?
2. How are an object’s data attributes made inaccessible to code outside the class
definition?
3. What is the difference between a class and an instance of a class?
4. The following statement calls an object’s method. What is the name of the method?
What is the name of the variable that references the object?
wallet.get_dollar()
5. When the _ _init_ _ method executes, what does the self parameter reference?
6. In a Python class, how do you hide an attribute from code outside the class?
7. The _ _str_ _ method in Python returns a string representation of an object’s state.
Describe how the _ _str_ _ method is used to print the state of an object.
Algorithm Workbench
1. Write a definition for a class Book. The Book class has data attributes for a title, an
author name and the number of pages. The class also has the following methods:
a. An _ _init_ _ method for the class. The method should accept arguments for each
of the data attributes.
568 Chapter 10 Classes and object-oriented Programming
Programming Exercises
1. Pet Class
VideoNote
Write a class named Pet, which should have the following data attributes:
The Pet class
• _ _name (for the name of a pet)
• _ _animal_type (for the type of animal that a pet is. Example values are ‘Dog’, ‘Cat’,
and ‘Bird’)
• _ _age (for the pet’s age)
The Pet class should have an _ _init_ _ method that creates these attributes. It should also
have the following methods:
• set_name
This method assigns a value to the _ _name field.
• set_animal_type
This method assigns a value to the _ _animal_type field.
• set_age
This method assigns a value to the _ _age field.
• get_name
This method returns the value of the _ _ name field.
• get_animal_type
This method returns the value of the _ _animal_type field.
• get_age
This method returns the value of the _ _age field.
Programming Exercises 569
Once you have written the class, write a program that creates an object of the class and
prompts the user to enter the name, type, and age of his or her pet. This data should be
stored as the object’s attributes. Use the object’s accessor methods to retrieve the pet’s
name, type, and age and display this data on the screen.
2. Car Class
Write a class named Car that has the following data attributes:
• _ _year_model (for the car’s year model)
• _ _make (for the make of the car)
• _ _speed (for the car’s current speed)
The Car class should have an _ _init_ _ method that accepts the car’s year model and
make as arguments. These values should be assigned to the object’s _ _year_model and
_ _make data attributes. It should also assign 0 to the _ _speed data attribute.
The class should also have the following methods:
• accelerate
The accelerate method should add 5 to the speed data attribute each time it is called.
• brake
The brake method should subtract 5 from the speed data attribute each time it is called.
• get_speed
The get_speed method should return the current speed.
Next, design a program that creates a Car object then calls the accelerate method five
times. After each call to the accelerate method, get the current speed of the car and dis-
play it. Then call the brake method five times. After each call to the brake method, get the
current speed of the car and display it.
3. Personal Information Class
Design a class that holds the following personal data: name, address, age, and phone num-
ber. Write appropriate accessor and mutator methods. Also, write a program that creates
three instances of the class. One instance should hold your information, and the other two
should hold your friends’ or family members’ information.
4. Employee Class
Write a class named Employee that holds the following data about an employee in attrib-
utes: name, ID number, department, and job title.
Once you have written the class, write a program that creates three Employee objects to
hold the following data:
The program should store this data in the three objects, then display the data for each
employee on the screen.
570 Chapter 10 Classes and object-oriented Programming
5. RetailItem Class
Write a class named RetailItem that holds data about an item in a retail store. The class
should store the following data in attributes: item description, units in inventory, and price.
Once you have written the class, write a program that creates three RetailItem objects
and stores the following data in them:
6. Patient Charges
Write a class named Patient that has attributes for the following data:
• First name, middle name, and last name
• Address, city, state, and ZIP code
• Phone number
• Name and phone number of emergency contact
The Patient class’s _ _init_ _ method should accept an argument for each attribute. The
Patient class should also have accessor and mutator methods for each attribute.
Next, write a class named Procedure that represents a medical procedure that has been
performed on a patient. The Procedure class should have attributes for the following data:
• Name of the procedure
• Date of the procedure
• Name of the practitioner who performed the procedure
• Charges for the procedure
The Procedure class’s _ _init_ _ method should accept an argument for each attribute.
The Procedure class should also have accessor and mutator methods for each attribute.
Next, write a program that creates an instance of the Patient class, initialized with sample
data. Then, create three instances of the Procedure class, initialized with the following data:
The program should display the patient’s information, information about all three of the
procedures, and the total charges of the three procedures.
Programming Exercises 571
To create this program, write a Question class to hold the data for a trivia question. The
Question class should have attributes for the following data:
• A trivia question
• Possible answer 1
• Possible answer 2
• Possible answer 3
• Possible answer 4
• The number of the correct answer (1, 2, 3, or 4)
The Question class also should have an appropriate _ _init_ _ method, accessors, and
mutators.
The program should have a list or a dictionary containing 10 Question objects, one for
each trivia question. Make up your own trivia questions on the subject or subjects of your
choice for the objects.