Objective
Develop a Python program with the following classes:
- House: Create a class called "House" with an attribute for "area". Include a constructor to set the area and a method "ShowData" that will print a statement like "I am a house, my area is 200 m2" (replacing 200 with the actual area). Implement getter and setter methods for the area.
- Door: A house has a door with a "color" attribute (a string). The door will have a method "ShowData" that displays something like "I am a door, my color is brown" (using the actual color). Provide getter and setter methods for the door’s color. Additionally, create a method "GetDoor" in the House class to retrieve the door.
- SmallApartment: This is a subclass of House, which will have a predefined area of 50 m2.
- Person: Create a "Person" class that contains a "name" attribute (string) and has a house. The "ShowData" method of a person will print their name, followed by the details of their house and the door.
- Main Function: Write a "Main" function to instantiate a "SmallApartment" and a "Person" who lives in it. The main function should then display the person’s data, including their house and door information.
Example Python Exercise
Show Python Code
# Class for Door
class Door:
def __init__(self, color):
self._color = color # Color attribute of the door
# Getter method for color
def get_color(self):
return self._color
# Setter method for color
def set_color(self, color):
self._color = color
# Method to display door information
def show_data(self):
print(f"I am a door, my color is {self._color}")
# Class for House
class House:
def __init__(self, area):
self._area = area # Area of the house
self.door = Door("brown") # Default door color
# You can customize the door color if needed using setter
# Getter method for area
def get_area(self):
return self._area
# Setter method for area
def set_area(self, area):
self._area = area
# Method to display house information
def show_data(self):
print(f"I am a house, my area is {self._area} m2")
# Method to get the door
def get_door(self):
return self.door
# SmallApartment class (subclass of House)
class SmallApartment(House):
def __init__(self):
super().__init__(50) # Default area for SmallApartment is 50 m2
# Class for Person
class Person:
def __init__(self, name, house):
self._name = name # Name of the person
self.house = house # The house that the person owns
# Method to display person's data including house and door information
def show_data(self):
print(f"My name is {self._name}.")
self.house.show_data() # Show house data
self.house.get_door().show_data() # Show door data
# Main function to test the classes
def main():
# Create a SmallApartment object
apartment = SmallApartment()
# Create a Person object who lives in the SmallApartment
person = Person("John", apartment)
# Display the person's data, including house and door info
person.show_data()
# Call the main function to run the program
if __name__ == "__main__":
main()
Output
My name is John.
I am a house, my area is 50 m2
I am a door, my color is brown
Share this Python Exercise