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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#!/usr/bin/env python3
# Create a class
class Plant():
def __init__(self: 'Plant', name: str, height: float, age: int) -> None:
self.name_is = name
self._height_is = height
self._age_is = age
# Method to grow the age
def age(self: 'Plant') -> None:
self._age_is += 1
# Method to grow the height
def grow(self: 'Plant') -> None:
self._height_is *= 1.02
# Getter methods to access private properties
def get_age(self: 'Plant') -> int:
return self._age_is
def get_height(self: 'Plant') -> float:
return self._height_is
# Setter methods to modify properties
def set_age(self: 'Plant', age: int) -> None:
if age > 0:
self._age_is = age
print("Plant age was updated.")
else:
print("Plant age cannot be negative.")
def set_height(self: 'Plant', height: int) -> None:
if height > 0:
self._height_is = round(height)
print("Plant height was updated.")
else:
print("Plant height cannot be negative.")
# Method to show the plant information
def show(self: 'Plant') -> None:
print(self.name_is + ": " + str(round(self.get_height(), 1)) + "cm, "
+ str(self.get_age()) + " days old")
class Flower(Plant):
def __init__(self: 'Flower', name: str, height: int,
age: int, color: str) -> None:
super(). __init__(name, height, age)
self.color_is = color
self.isblooming = 0
def bloom(self: 'Flower') -> None:
self.isblooming = 1
def show(self: 'Flower') -> None:
super().show()
print(f" Color: {self.color_is}")
if self.isblooming == 1:
print(self.name_is + " is blooming beautifully!")
else:
print(self.name_is + " has not bloomed yet")
class Tree(Plant):
def __init__(self: 'Tree', name: str, height: int, age: int,
trunk_diameter: int) -> None:
super(). __init__(name, height, age)
self.trunk_diameter_is = trunk_diameter
self.is_producing_shade = 0
def produce_shade(self: 'Tree') -> None:
self.is_producing_shade = 1
print(self.name_is + " now produces a shade of "
+ str(self.get_height())
+ "cm long and "
+ str(round(self.trunk_diameter_is, 1))
+ "cm wide.")
def show(self: 'Tree') -> None:
super().show()
print(f" Trunk diameter: {self.trunk_diameter_is}cm")
class Vegetable(Plant):
def __init__(self: 'Vegetable', name: str, height: int, age: int,
harvest_season: str, nutritional_value: int) -> None:
super(). __init__(name, height, age)
self.harvest_season_is = harvest_season
self.nutritional_value_is = nutritional_value
def update_nutritional_value(self: 'Vegetable') -> None:
self.nutritional_value_is = 20
def show(self: 'Vegetable') -> None:
super().show()
print(f" Harvest season: {self.harvest_season_is}")
if self.get_age() >= 20:
self.update_nutritional_value()
print(f" Nutritional value: {self.nutritional_value_is}")
if __name__ == "__main__":
print("=== Garden Plant Types ===")
print("=== Flower")
rose = Flower("Rose", 5, 15, "red")
rose.show()
print("[asking the rose to bloom]")
rose.bloom()
rose.show()
print("=== Tree")
oak = Tree("Oak", 200, 365, 5)
oak.show()
print("[asking the oak to produce shade]")
oak.produce_shade()
print("=== Vegetable")
tomato = Vegetable("Tomato", 5, 10, "April", 0)
tomato.show()
print("[make tomato grow and age for 20 days]")
for _ in range(20):
tomato.age()
tomato.grow()
tomato.update_nutritional_value()
tomato.show()
|