Table, Coffee Table, and Legs - Python Programming Exercise

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 perfect for practicing class definition, inheritance, and method implementation in Python. By implementing these classes, you will gain hands-on experience in handling class definitions, inheritance, and method implementation in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Python Classes in OOP

 Exercise

Table, Coffee Table, And Legs

 Objective

Develop a python project based on the tables and coffee tables example, but now introduce a new class named "Leg". This class should have a method called "ShowData" that prints "I am a leg" followed by the data of the table it is attached to.

In the project, select one of the tables, add a leg to it, and call the "ShowData" method of that leg to display the details. This will allow you to demonstrate how the leg is associated with a table and how it can access and display the table's information.

 Example Python Exercise

 Copy Python Code
import random

class Table:
    def __init__(self, width, height):
        """
        Constructor that initializes the width and height of the table.
        """
        self.width = width
        self.height = height

    # ShowData method to display the dimensions of the table
    def ShowData(self):
        print(f"I am a table, my width is {self.width} cm and my height is {self.height} cm")

class CoffeeTable(Table):
    def ShowData(self):
        """
        Overrides the ShowData method from Table, adding the type of table.
        """
        print(f"I am a coffee table, my width is {self.width} cm and my height is {self.height} cm")

class Leg:
    def __init__(self, table):
        """
        Constructor that initializes the leg with the table it is attached to.
        """
        self.table = table

    def ShowData(self):
        """
        Displays the data of the table the leg is attached to.
        """
        print(f"I am a leg, attached to a table with width {self.table.width} cm and height {self.table.height} cm")

# Test program
if __name__ == "__main__":
    # Create a table and a coffee table with random dimensions
    table = Table(random.randint(50, 200), random.randint(50, 200))
    coffee_table = CoffeeTable(random.randint(40, 120), random.randint(40, 120))

    # Show the details of the table and coffee table
    print("Table Details:")
    table.ShowData()
    print("\nCoffee Table Details:")
    coffee_table.ShowData()

    # Create a leg for the table
    leg_for_table = Leg(table)
    print("\nLeg Details (for Table):")
    leg_for_table.ShowData()

    # Create a leg for the coffee table
    leg_for_coffee_table = Leg(coffee_table)
    print("\nLeg Details (for Coffee Table):")
    leg_for_coffee_table.ShowData()

 Output

Table Details:
I am a table, my width is 187 cm and my height is 151 cm

Coffee Table Details:
I am a coffee table, my width is 100 cm and my height is 104 cm

Leg Details (for Table):
I am a leg, attached to a table with width 187 cm and height 151 cm

Leg Details (for Coffee Table):
I am a leg, attached to a table with width 100 cm and height 104 cm

 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.

  •  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...

  •  Converting Text to HTML

    In this exercise, you will develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. This ...

  •  Text on Screen Class

    In this exercise, you will develop a Python class called "DisplayText" that allows you to show text at specific coordinates on the screen. This exercise is per...

  •  Improved ComplexNumber Class

    In this exercise, you will develop a Python program to enhance the "ComplexNumber" class by overloading the addition (+) and subtraction (-) operators. This exerci...

  •  3D Coordinates

    In this exercise, you will develop a Python class "Point3D" that will represent a point in three-dimensional space with coordinates X, Y, and Z. This exercise ...