OO Programming and Data Structures | CS 241

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.

  1. Copy the template file /home/cs241/check07a.py to your working directory.

  2. Create a class for a Car that 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.

  3. Fill in a default get_door_specs() method that returns the string "Unknown doors". In the init funtion, set the name variable to be "Unknown model".

  4. Next create a class Civic that inherits from the Car class and overrides the get_door_specs() to return the string "4 doors". In the constructor, set the name variable to be "Civic".

  5. Next create a class Odyssey that inherits from the Car class and overrides the get_door_specs() to return the string "2 front doors, 2 sliding doors, 1 tail gate". In the constructor, set the name variable to be "Odyssey".

  6. Next create a class Ferrari that inherits from the Car class and overrides the get_door_specs() to return the string "2 butterfly doors". In the constructor, set the name variable to be "Ferrari".

  7. 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.)

  8. You must make a single function attach_doors that 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