OO Programming and Data Structures | CS 241

06 Teach : Team Activity

Overview

Create a class to represent a Circle, based on a provided Point class. It has a center (x, y) and a radius. Because a Circle has an x and a y coordinate, and more, one way to view it, would be to consider that a Circle IS-A Point. In this case, the Circle class would inherit from the Point class and simply add a radius to it.

On the other hand, another way to view the problem is to consider that a circle HAS-A point as its center and then also has a radius. With this approach the Circle would not inherit from the Point class, but rather, would have a member variable that is of type Point.

For this activity, we will explore both options, first focusing on the IS-A relationship, and then the HAS-A approach.

Instructions

Begin by creating a simple point class to match the following diagram:

Point
x : float
y : float
prompt_for_point() : None
display() : None

Core Requirements

  1. Create the Point class as defined above. Then, create the Circle class as follows:

    1. Create a Circle class that inherits from, or extends, the Point class.

    2. Create a blank method for prompt_for_circle and display.

    3. In main, call these functions and ensure that everything compiles.

  2. Implement the prompt_for_circle method. It should ask for a Circle as follows:

    
    Enter x: 1
    Enter y: 2
    Enter radius: 3
    

    Please note that the Point class will have a method called prompt_for_point. Make sure you call this from your method to reuse code!

  3. Implement the display method. It should display a Circle as follows:

    
    Center:
    (1, 2)
    Radius: 3
    

    Please note that the Point class will have a method called display. Make sure you call this from your method to reuse code! (Yes, these methods should both be called "display".)

Stretch Challenges

After completing the above steps, make sure that everyone on the entire team is to this point and understands the material. Then, if you have time, move onto the following stretch challenges.

For the stretch challenge, you will repeat the activity using a HAS-A relationship.

  1. Make a copy of your code from before, and start with the same Point class as before. This time your Circle should NOT inherit from Point, but rather should have a Point as a member variable called center.

  2. Implement the prompt_for_circle and display methods for your Circle class as before.

  3. Discuss with your team the pros and cons of the IS-A vs HAS-A approach to this problem. Determine which approach you feel is best and why. You'll provide your thoughts on this in your ponder submission for the week.

Instructor's Solution

As a part of this team activity, you are expected to look over a solution from the instructor to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.

Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.

After working with your team for the one hour activity, click here for the instructor's solution.

Submission

When you have finished this activity, please fill out the assessment in I-Learn. You are welcome to complete any additional parts of this activity by yourself or with others after the team meeting before submitting the assessment.