Skip to content

Commit 546bd91

Browse files
committed
Ploygon Area Calculator Done
1 parent 0a100a0 commit 546bd91

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

PolygonCalculator.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Rectangle:
2+
def __init__(self, width, height):
3+
# Initialize a Rectangle object with width and height attributes.
4+
self.width = width
5+
self.height = height
6+
7+
def set_width(self, width):
8+
# Set the width attribute of the Rectangle.
9+
self.width = width
10+
11+
def set_height(self, height):
12+
# Set the height attribute of the Rectangle.
13+
self.height = height
14+
15+
def get_area(self):
16+
# Calculate and return the area of the Rectangle.
17+
return self.width * self.height
18+
19+
def get_perimeter(self):
20+
# Calculate and return the perimeter of the Rectangle.
21+
return 2 * (self.width + self.height)
22+
23+
def get_diagonal(self):
24+
# Calculate and return the diagonal length of the Rectangle.
25+
return (self.width ** 2 + self.height ** 2) ** 0.5
26+
27+
def get_picture(self):
28+
# Generate a string representation of the Rectangle using '*'.
29+
# If the dimensions are too big, return "Too big for picture."
30+
if self.width > 50 or self.height > 50:
31+
return "Too big for picture."
32+
return '\n'.join(['*' * self.width for _ in range(self.height)]) + '\n'
33+
34+
def get_amount_inside(self, shape):
35+
# Calculate how many times the given shape can fit inside this Rectangle.
36+
width_fit = self.width // shape.width
37+
height_fit = self.height // shape.height
38+
return width_fit * height_fit
39+
40+
def __str__(self):
41+
# Return a string representation of the Rectangle object.
42+
return f"Rectangle(width={self.width}, height={self.height})"
43+
44+
45+
class Square(Rectangle):
46+
def __init__(self, side):
47+
# Initialize a Square object with a given side length.
48+
# Both width and height attributes are set to the side length.
49+
super().__init__(side, side)
50+
51+
def set_side(self, side):
52+
# Set the side length of the Square by updating width and height.
53+
self.width = side
54+
self.height = side
55+
56+
def set_width(self, width):
57+
# Override set_width method to ensure Square maintains its properties.
58+
self.set_side(width)
59+
60+
def set_height(self, height):
61+
# Override set_height method to ensure Square maintains its properties.
62+
self.set_side(height)
63+
64+
def __str__(self):
65+
# Return a string representation of the Square object.
66+
return f"Square(side={self.width})"

0 commit comments

Comments
 (0)