Array of Tables and Coffee Tables - Python Programming Exercise

In this exercise, you will develop a Python project called "Tables2," extending the "Tables" project. In this project, define a class called "CoffeeTable" that inherits from the "Table" class. 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

Array Of Tables And Coffee Tables

 Objective

Develop a Python project called "Tables2," extending the "Tables" project.

In this project, define a class called "CoffeeTable" that inherits from the "Table" class. In addition to displaying the table’s width and height, the "ShowData" method should also print "(Coffee table)."

Create an array with 5 tables and 5 coffee tables. The tables should have random dimensions between 50 and 200 cm, while the coffee tables should range from 40 to 120 cm. Display the details for all the tables and coffee tables in the array.

 Example Python Exercise

 Copy Python Code
import random

# Class for Table
class Table:
    def __init__(self, width, height):
        self.width = width  # Width of the table
        self.height = height  # Height of the table

    # Method to display table information
    def show_data(self):
        print(f"Table dimensions: {self.width} cm x {self.height} cm")

# Class for CoffeeTable (inherits from Table)
class CoffeeTable(Table):
    def __init__(self, width, height):
        super().__init__(width, height)  # Call the constructor of the parent class (Table)

    # Override the show_data method to include "(Coffee table)" label
    def show_data(self):
        print(f"Table dimensions: {self.width} cm x {self.height} cm (Coffee table)")

# Main function to create and display tables and coffee tables
def main():
    tables = []  # List to store tables
    coffee_tables = []  # List to store coffee tables

    # Create 5 tables with random dimensions between 50 and 200 cm
    for _ in range(5):
        width = random.randint(50, 200)  # Random width between 50 and 200 cm
        height = random.randint(50, 200)  # Random height between 50 and 200 cm
        table = Table(width, height)
        tables.append(table)

    # Create 5 coffee tables with random dimensions between 40 and 120 cm
    for _ in range(5):
        width = random.randint(40, 120)  # Random width between 40 and 120 cm
        height = random.randint(40, 120)  # Random height between 40 and 120 cm
        coffee_table = CoffeeTable(width, height)
        coffee_tables.append(coffee_table)

    # Display the details for all the tables and coffee tables
    print("Tables:")
    for table in tables:
        table.show_data()

    print("\nCoffee Tables:")
    for coffee_table in coffee_tables:
        coffee_table.show_data()

# Run the main function
if __name__ == "__main__":
    main()

 Output

Tables:
Table dimensions: 150 cm x 112 cm
Table dimensions: 160 cm x 183 cm
Table dimensions: 79 cm x 106 cm
Table dimensions: 186 cm x 188 cm
Table dimensions: 104 cm x 121 cm

Coffee Tables:
Table dimensions: 95 cm x 55 cm (Coffee table)
Table dimensions: 108 cm x 70 cm (Coffee table)
Table dimensions: 66 cm x 102 cm (Coffee table)
Table dimensions: 107 cm x 111 cm (Coffee table)
Table dimensions: 56 cm x 97 cm (Coffee table)

 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.

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

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