From 15115b4c52bfda0d1cca9fa1155beecbb873ec35 Mon Sep 17 00:00:00 2001 From: yctct Date: Sun, 7 Jun 2026 08:59:04 +0200 Subject: First commit, add all files --- py01/ex5/ft_plant_types.py | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 py01/ex5/ft_plant_types.py (limited to 'py01/ex5') diff --git a/py01/ex5/ft_plant_types.py b/py01/ex5/ft_plant_types.py new file mode 100755 index 0000000..d306117 --- /dev/null +++ b/py01/ex5/ft_plant_types.py @@ -0,0 +1,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() -- cgit v1.2.3