Object array: tables - Python Programming Exercise

In this exercise, you will develop a Python class called "Table". The class should include a constructor to initialize the width and height of the table. It should also have a method named "ShowData" that prints the table’s dimensions. This exercise is perfect for practicing class definition, method implementation, and object-oriented programming in Python. By implementing this class, you will gain hands-on experience in handling class definitions, method implementation, and object-oriented programming 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

Object Array: Tables

 Objective

Develop a Python class called "Table". The class should include a constructor to initialize the width and height of the table. It should also have a method named "ShowData" that prints the table’s dimensions. Then, create an array with 10 tables, each having random dimensions ranging from 50 cm to 200 cm. Finally, display the information for each table in the array.

 Example Python Exercise

 Copy Python Code
import random  # Importing the random module to generate random dimensions

# Table class with width and height attributes
class Table:
    def __init__(self, width, height):
        self.width = width  # Width of the table
        self.height = height  # Height of the table

    # Method to display the table's dimensions
    def ShowData(self):
        print(f"Table dimensions - Width: {self.width} cm, Height: {self.height} cm")

# Main program to create an array of 10 tables with random dimensions
if __name__ == "__main__":
    tables = []  # List to store the tables

    # Create 10 tables with random width and height between 50 and 200 cm
    for _ in range(10):
        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)  # Create a new Table object
        tables.append(table)  # Add the table to the list

    # Display the information for each table in the array
    print("Table Information:")
    for table in tables:
        table.ShowData()  # Call the ShowData method to display the table's dimensions

 Output

Table Information:
Table dimensions - Width: 157 cm, Height: 104 cm
Table dimensions - Width: 158 cm, Height: 112 cm
Table dimensions - Width: 172 cm, Height: 60 cm
Table dimensions - Width: 103 cm, Height: 138 cm
Table dimensions - Width: 141 cm, Height: 173 cm
Table dimensions - Width: 69 cm, Height: 148 cm
Table dimensions - Width: 187 cm, Height: 138 cm
Table dimensions - Width: 56 cm, Height: 148 cm
Table dimensions - Width: 110 cm, Height: 114 cm
Table dimensions - Width: 183 cm, Height: 161 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.

  •  House

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

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