CS 115 Lab 2, Part D: Compute the volume of a cube, the volume of a sphere, and the area of an equilateral triangle
[Back to lab instructions]
Background
In this part of the lab, you will extend your program from Part C
to compute three more quantities:
- Volume of a cube with edge length $s$: $V = s^{3}$
- Volume of a sphere with radius length $r$: $V = \frac{4}{3} \pi r^{3}$
- Area of an equilateral triangle with side length $s$: $A = \frac{s^{2} \sqrt{3}}{4}$
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.159.
The volume of a cube with edge length 10.0 is 1000.0.
The volume of a sphere with radius length 10.0 is 4188.790.
The area of an equilateral triangle with side length 10.0 is 43.301.
To take the square root of a number in Python, you have two options:
- You can use the math.sqrt() function. For example, math.sqrt(5) gives you the square root of 5.
You can take the square root of anything that's numeric, including integer and floating-point variables or expressions with numeric values. For example:
- math.sqrt(square_area)
- math.sqrt(5 + 12 * 7.5)
- You can take advantage of the fact that $\sqrt{x}$ is the same thing as $x^{1/2}$. Just be careful with order of operations.
Instructions
- Go back to lab02bcd.py. Remember not to delete any of the existing code. Also, do not prompt the user to enter another value. You should be using the same
input value for all of the calculations.
- Just after the other calculations, add a line to the program to calculate the volume of a cube whose
edge length is length. Store the result of this calculation in a new variable with an
appropriate name.
- Add a line to the program to print the volume of the cube, rounded to 3 decimal places. Be sure the wording matches the sample output exactly.
- Run your program. Make sure it matches the example:
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.159.
The volume of a cube with edge length 10.0 is 1000.0.
- Run your program. Record the volumes of the cubes in Question 8 of your writeup.
- Repeat this process for the sphere. Remember to round the result, and answer Question 9 in your writeup.
- Finally, repeat this process for the equilateral triangle. Remember to
round the result, and answer Question 10 in your writeup. Make sure that your output looks identical to the sample output
when the user inputs a value of 10. 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.159.
The volume of a cube with edge length 10.0 is 1000.0.
The volume of a sphere with radius length 10.0 is 4188.790.
The area of an equilateral triangle with side length 10.0 is 43.301.
- Update your docstring to reflect what your program is doing now. Be sure that it also includes your name.
- Call an instructor or lab assistant over to demo your program.
- Continue to Part E.