-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle_Circle_Class.py
More file actions
80 lines (65 loc) · 2.63 KB
/
Rectangle_Circle_Class.py
File metadata and controls
80 lines (65 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# HackerRank Python Basic Certification Problem 2
# Problem Definition:
# This code defines two geometric shapes (Rectangle and Circle), calculates their areas using respective formulas.
# Author: Audity Ghosh
# Date: 2025-01-01
# Description:
# - The `Rectangle` class calculates the area of a rectangle given its length and width.
# - The `Circle` class calculates the area of a circle given its radius.
# Approach:
# - Create classes for each shape with methods to calculate areas.
# - Use Python's math library for the circle's area.
# - Define a main function to demonstrate usage of these classes.
# Comments: The main function demonstrates the creation of rectangle and circle objects, calling the area methods.
import math
class Rectangle:
def __init__(self, length, width):
"""
Constructor to initialize the dimensions of the rectangle.
:param length: Length of the rectangle.
:param width: Width of the rectangle.
"""
self.length = length
self.width = width
def area(self):
"""
Method to calculate the area of the rectangle.
:return: Area of the rectangle (length * width).
"""
return self.length * self.width
class Circle:
def __init__(self, radius):
"""
Constructor to initialize the radius of the circle.
:param radius: Radius of the circle.
"""
self.radius = radius
def area(self):
"""
Method to calculate the area of the circle.
:return: Area of the circle (π * radius^2).
"""
return math.pi * (self.radius ** 2)
def main():
"""
Main function to process multiple queries and calculate areas of different shapes.
"""
# Read the number of queries
q = int(input("Enter the number of queries: "))
for _ in range(q):
# Read each query
query = input("Enter query: ").split()
# Unpack the query dynamically
shape_type, *params = query # First item is shape_type, rest are parameters
if shape_type == "Circle":
radius = float(params[0]) # Only one parameter for Circle
circ = Circle(radius)
print(f"Circle Area: {circ.area():.2f}")
elif shape_type == "Rectangle":
length, width = map(float, params) # Two parameters for Rectangle
rect = Rectangle(length, width)
print(f"Rectangle Area: {rect.area()}")
else:
print("Invalid query. Please provide valid input.")
if __name__ == "__main__":
main()