forked from Ranabetuluzun/Python_Modul_Week_5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.py
More file actions
62 lines (46 loc) · 1.91 KB
/
Q3.py
File metadata and controls
62 lines (46 loc) · 1.91 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
# Create a "Shape" class. Under this class, create two subclasses, the "Rectangle" and "Square" classes.
# - Let the "shape" class have two properties: "width" and "height."
# - Let the "Rectangle" class inherit from the "Shape" class and add an additional "calculate_area()" method.
# - Let the "Square" class inherit from the "Shape" class and calculate the area of the square using the same "area_calculate()" method.
# - Create a "Rectangle" and a "Square" instance, determine the width and height of each, and calculate the area of each and print the results.
class Shape:
def __init__(self, width, height):
self.width = width
self.height = height
class Rectangle(Shape):
def calculate_area(self):
return self.width * self.height
class Square(Shape):
def calculate_area(self):
return self.width * self.height #for square width == height
my_rectangle = Rectangle(7,10)
my_square = Square(8,8)
print("Rectangle Area:", my_rectangle.calculate_area())
print("Square Area:", my_square.calculate_area())
class Shape:
def __init__(self, width, height):
self.width = width
self.height = height
class Rectangle(Shape):
def calculate_area(self):
return self.width * self.height
def display_information(self):
print("Shape: Rectangle")
print(f"Width: {self.width}, Height: {self.height}")
print(f"Area: {self.calculate_area()}")
print()
class Square(Shape):
def __init__(self, side):
super().__init__(side, side)
self.side = side
def calculate_area(self):
return self.side * self.side
def display_information(self):
print("Shape: Square")
print(f"Side: {self.side}")
print(f"Area: {self.calculate_area()}")
print()
my_rectangle = Rectangle(7, 10)
my_square = Square(8)
my_rectangle.display_information()
my_square.display_information()