OO Programming and Data Structures | CS 241

07 Prepare : Checkpoint B - Abstract Base Classes

Please go back and read over the supplementary reading material on abstract base classes from the preparation material for this week. Then, complete the following exercise.

Overview

This Checkpoint is intended to help you practice the syntax of abstract base classes, and use these principles to have lists of related objects.

For this checkpoint, you will create an abstract base class for a Shape, and two derived classes for specific shapes. The base class will have an abstract method, get_area(), that is overriden by each derived class. You will then create a certain number of new shapes, as specified by the user, put them in the same array, and loop through and display the area of each.

Instructions

For this assignment, because the classes we are going to create are so small and simplistic, and to help you focus on just the elements of inheritance and abstract functions, rather than the mechanics of creating lots of files, you will put all of your classes at the top of the main file, rather than creating separate files like we would for larger, real-world classes.

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

  2. A Shape class has been provided that has a name and a display method. You need to convert it to an abstract base class and add an abstract method get_area(), thus it will look as follows:

  3. Shape
    name : string
    __init__() : None
    display() : None
    get_area() : float
  4. Add a Circle class that derives from the base Shape class.

    • Add an __init__ function, and in it, call the super class __init__ function, set the name variable to be "Circle", and the radius to be 0.0.

    • Redefine the get_area() method to return "3.14 * radius * radius".

  5. Add a Rectangle class that also is derived from the Shape class.

    • Add an __init__ function, and in it, call the super class __init__ function, set the name variable to be "Rectangle", and a length and width to each be 0.0.

    • Redefine the get_area() method to return "length * width".

  6. The provided code supplies you with a main function. It takes care of prompting the user for the correct values and looping through. You need to add the following to main (at the places indicated in the comments):

    • Declare a new list for your shapes.

    • Declare a new Circle, set the radius, and add it to the list.

    • Declare a new Rectangle, set the radius, and add it to the list.

    • At the end of main, loop through each shape in the list and call its display function.

Sample Output

The following is an example of output for this program:


Please enter 'c' for circle, 'r' for rectangle or 'q' to quit: c
Enter the radius: 3
Please enter 'c' for circle, 'r' for rectangle or 'q' to quit: r
Enter the length: 2
Enter the width: 4
Please enter 'c' for circle, 'r' for rectangle or 'q' to quit: r
Enter the length: 4 
Enter the width: 10
Please enter 'c' for circle, 'r' for rectangle or 'q' to quit: q
Circle - 28.26
Rectangle - 8.00
Rectangle - 40.00

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.

Please note that it is possible to do this assignment without using abstract methods, but this will not help you understand the concept, and you will not receive credit if you do not follow the instructions.

Helpful commands


testBed cs241/check07b check07b.py
submit check07b.py