07 Prepare : Checkpoint A - Polymorphism
After completing (or while completing) the preparation material for this week, complete the following exercise.
Overview
This checkpoint is intended to help you practice the syntax of polymorphism.
Consider that you are writing software for a factory that builds cars. You produce Civics, Odysseys, and Ferraris. You will have a base class that has the name of the car, as follows:
| Car |
|---|
| name : string |
| __init__() |
| get_door_specs() : string |
Then, you should create three derived classes (Civic, Odyssey, and Ferrari) that inherit from this base class, and redefine the get_door_specs() method.
Instructions
For this assignment, because the classes you are going to create are so small and simplistic, and to help you focus on just the elements of inheritance, rather than the mechanics of creating lots of files, you can put all of your classes at the top of the main file, rather than creating separate files like you would for larger, real-world classes.
Copy the template file
/home/cs241/check07a.pyto your working directory.Create a class for a
Carthat has the member variables and methods as described above. Notice that the method,get_door_specs()does not print the data to the screen, but rather returns it.Fill in a default
get_door_specs()method that returns the string "Unknown doors". In the init funtion, set thenamevariable to be "Unknown model".Next create a class
Civicthat inherits from theCarclass and overrides theget_door_specs()to return the string "4 doors". In the constructor, set thenamevariable to be "Civic".Next create a class
Odysseythat inherits from theCarclass and overrides theget_door_specs()to return the string "2 front doors, 2 sliding doors, 1 tail gate". In the constructor, set thenamevariable to be "Odyssey".Next create a class
Ferrarithat inherits from theCarclass and overrides theget_door_specs()to return the string "2 butterfly doors". In the constructor, set thenamevariable to be "Ferrari".A main function is provided that creates one of each of these cars, and passes it to a method called "attach_doors". You need to write this method. It should accept any type of car and display the text: "Attaching doors to Civic - 4 doors". (Of course, replace "Civic" with the appropriate name, and "4 doors" with the result of calling the
get_door_specs()method.)You must make a single function
attach_doorsthat handles any type of car. You cannot have three separate functions or a single function that takes three parameters.
Sample Output
The following is an example of output for this program:
Attaching doors to Civic - 4 doors
Attaching doors to Odyssey - 2 front doors, 2 sliding doors, 1 tail gate
Attaching doors to Ferrari - 2 butterfly doors
Testbed
An auto-grading testBed script is provided to help you evaluate your program. This same testBed script will be used to grade your program. It is pass/fail, so your program must pass the testbed completely for you to receive credit for this assignment. You may run the testbed as many times as you like.
Helpful commands
testBed cs241/check07a check07a.py
submit check07a.py