Shape Areas Project
Shape Areas Project
Python OOP coding project
Write a Python class that includes three methods for calculating the areas of three different geometric shapes: circles, rectangles, and cylinders. The formulas for each shape are:
– Circle: Area = πr^2, where π is the mathematical constant Math.PI and r is the radius of the circle.
– Rectangle: Area = Width x Length, where Width and Length are the dimensions of the rectangle.
– Cylinder: Area = 2πrh + 2πr^2, where π is Math.PI, r is the radius of the cylinder’s base, and h is the height of the cylinder.
Ensure that your program prompts the user to enter the dimensions (in centimeters) for the radius, height, width, and length of the rectangle.
Steps
- Define a Python class called
"GeometryCalculator"with three methods for calculating the areas of circles, rectangles, and cylinders based on the provided formulas. - Create a method to prompt the user to enter the dimensions of the shapes (radius for circle, height, width and length for rectangle).
- Instantiate the
"GeometryCalculator"class and call the methods to calculate the areas based on the user input. - Display the calculated areas to the user.
Expected Ouput
Enter the radius of the circle in centimeters: 5 Enter the width of the rectangle in centimeters: 2 Enter the length of the rectangle in centimeters: 6 Enter the height of the rectangle in centimeters: 3 The area of the circle is: 78.53981633974483 square centimeters The area of the rectangle is: 12.0 square centimeters The area of the cylinder is: 408.4070449666731 square centimeters
Source Code
import math
## Define a class to claculate areas
class GeometryCalculator:
@staticmethod
def circle_area(radius):
return math.pi * radius ** 2
@staticmethod
def rectangle_area(width, length):
return width * length
@staticmethod
def cylinder_area(radius, height):
return 2 * math.pi * radius * (radius + height)
@staticmethod
def prompt_user_for_dimensions():
radius = float(input("Enter the radius of the circle in centimeters: "))
width = float(input("Enter the width of the rectangle in centimeters: "))
length = float(input("Enter the length of the rectangle in centimeters: "))
height = float(input("Enter the height of the rectangle in centimeters: "))
return radius, width, length
# Prompt the user for dimensions and calculate the areas
radius, width, length = GeometryCalculator.prompt_user_for_dimensions()
circle_area = GeometryCalculator.circle_area(radius)
rectangle_area = GeometryCalculator.rectangle_area(width, length)
cylinder_area = GeometryCalculator.cylinder_area(radius, height)
# Display the calculated areas to the user
print(f"The area of the circle is: {circle_area} square centimeters")
print(f"The area of the rectangle is: {rectangle_area} square centimeters")
print(f"The area of the cylinder is: {cylinder_area} square centimeters")
If you need to practice object oriented programming, visit our real Python OOP projects.
