House - Python Programming Exercise

In this exercise, you will 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.

 Category

Mastering Python Classes in OOP

 Exercise

House

 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

 Copy 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

 More Python Programming Exercises of Mastering Python Classes in OOP

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Array of Tables and Coffee Tables

    In this exercise, you will develop a Python project called "Tables2," extending the "Tables" project. In this project, define a class called "CoffeeTable" that inheri...

  •  Encryptor & Decryptor

    In this exercise, you will develop a Python class called "Encryptor" for text encryption and decryption. This exercise is perfect for practicing class definiti...

  •  Advanced Number Systems

    In this exercise, you will develop a Python program to represent complex numbers, which consist of a real part and an imaginary part. This exercise is perfect ...

  •  Table, Coffee Table, and Legs

    In this exercise, you will develop a Python project based on the tables and coffee tables example, but now introduce a new class named "Leg". This exercise is ...

  •  Catalog

    In this exercise, you will develop a Python class diagram for a catalog utility that stores details about music files, movies, and computer programs. This exercise...

  •  Random Value Generation

    In this exercise, you will develop a Python project with a class RandomNumber that includes three static methods. This exercise is perfect for practicing class...