CS 115 Lab 2, Part C: Computing the area of a circle

[Back to Lab 2] [Back to CS 115 schedule]


Background

Note: This "Background" section is a high-level overview of what you will do in Part C. The "Instructions" section will walk you through the process step by step.

In this part of the lab, you will extend your program from Part B to compute the area of a circle, given the length of the radius.

Recall that the area of a circle with radius r can be calculated as
A = π * r2

Your new program will ask the user for only one numeric value. Then, it will use that value to compute the areas of both the circle and the square.

Here is a sample of what your revised program will do. The user's input is italicized and underlined.

Enter a numeric value: 10
The area of a square with side length 10.0 is 100.0.
The area of a circle with radius length 10.0 is 314.159265359.

To earn a perfect grade on the lab, your output will have to match the sample output exactly, except that the number of decimal places of the output may be different.

Instructions

  1. Since you are extending your program, do not delete any of your existing code as you follow the instructions below!
  2. To do your computation, you can use the built-in definition of π from the math library. Start by placing an import statement between the docstring and the def main():
    import math
  3. From now on, you will be able to use the constant
    math.pi
    whenever you need the value of π for a calculation.
  4. In your current program, notice that the statement
    square_area = length * length
    computes the area of a square whose side length is length and saves the result in a variable called square_area.
  5. Just after that statement, add a similar statement to your program to calculate the area of a circle whose radius length is also length. Your statement should store the result of this calculation in a new variable with an appropriate name.
  6. After your current print statement, add a statement to the program to print the area of the circle. This statement should be similar to the print statement that prints the area of the square, but be sure to change the wording to match the sample output above.
  7. Update the docstring at the top of your program so that it accurately describes what the program does. Make sure that it lists you as the author of the program.
  8. Run your program, typing in the number 10 when prompted. Make sure that your output looks exactly like the example below (with the exception that the floating point accuracy or the number of digits could be different because that may depend on the specific machine you are using):
    Enter a numeric value: 10
    The area of a square with side length 10.0 is 100.0.
    The area of a circle with radius length 10.0 is 314.159265359.
    
  9. If you have not done so already, click "Next" in your Moodle writeup to advance to Question 8.
  10. Execute the program several times to complete the table in Question 8 of your Moodle writeup.
  11. Continue to Part D.