Text on Screen Class - Python Programming Exercise

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

Text On Screen Class

 Objective

Devlope a python class DisplayText, which allows you to show text at specific coordinates on the screen. This class should have a constructor that takes in X, Y, and the text to display. It must also include 3 setter methods and a show method to print the text at the specified position.

Next, create a subclass called CenteredDisplayText that inherits from DisplayText. This subclass should position the text horizontally centered within a given row (Y), while leaving the X-coordinate unchanged. The constructor should only receive the Y-coordinate and the text.

Then, create another subclass called FramedDisplayText, which will display text within a rectangular frame and horizontally centered. It will take the starting row (Y) and the text as inputs.

Lastly, develop a test program that creates instances of each of these classes and displays their content.

 Example Python Exercise

 Copy Python Code
class DisplayText:
    def __init__(self, x, y, text):
        """
        Initializes the DisplayText class with X, Y coordinates, and the text to display.

        Parameters:
            x (int): The X-coordinate to display the text at.
            y (int): The Y-coordinate to display the text at.
            text (str): The text to be displayed.
        """
        self.x = x
        self.y = y
        self.text = text

    # Setter method for X-coordinate
    def set_x(self, x):
        self.x = x

    # Setter method for Y-coordinate
    def set_y(self, y):
        self.y = y

    # Setter method for text
    def set_text(self, text):
        self.text = text

    def show(self):
        """
        Displays the text at the specified coordinates (X, Y).
        """
        print(f"Displaying text at ({self.x}, {self.y}): {self.text}")


class CenteredDisplayText(DisplayText):
    def __init__(self, y, text):
        """
        Initializes the CenteredDisplayText subclass with Y-coordinate and the text to display.
        The text will be horizontally centered within the given row.

        Parameters:
            y (int): The Y-coordinate to display the text at.
            text (str): The text to be displayed.
        """
        # Calculate the X-coordinate to center the text within a fixed width (let's assume a width of 50)
        x = (50 - len(text)) // 2  # Centering the text within 50 characters
        super().__init__(x, y, text)

    def show(self):
        """
        Displays the text horizontally centered at the specified Y-coordinate.
        """
        print(f"Displaying centered text at ({self.x}, {self.y}): {self.text}")


class FramedDisplayText(DisplayText):
    def __init__(self, y, text):
        """
        Initializes the FramedDisplayText subclass with Y-coordinate and the text to display.
        This subclass displays the text within a rectangular frame, horizontally centered.

        Parameters:
            y (int): The Y-coordinate to display the text at.
            text (str): The text to be displayed.
        """
        # Calculate the X-coordinate to center the text within a fixed width (let's assume a width of 50)
        x = (50 - len(text)) // 2  # Centering the text within 50 characters
        super().__init__(x, y, text)

    def show(self):
        """
        Displays the text within a rectangular frame and horizontally centered.
        """
        border = '*' * (len(self.text) + 4)  # Frame border with padding
        print(border)
        print(f"* {self.text} *")
        print(border)


# Test program to create and display instances of each class
def main():
    # Create an instance of DisplayText
    text1 = DisplayText(10, 5, "Hello, World!")
    text1.show()

    # Create an instance of CenteredDisplayText
    text2 = CenteredDisplayText(8, "Centered Text")
    text2.show()

    # Create an instance of FramedDisplayText
    text3 = FramedDisplayText(10, "Framed Text")
    text3.show()


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

 Output

Displaying text at (10, 5): Hello, World!
Displaying centered text at (17, 8): Centered Text
*************************
* Framed Text *
*************************

 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.

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

  •  Catalog & Navigation

    In this exercise, you will develop a Python program to enhance the Catalog application, where the "Main" function displays a menu that allows users to input new data ...

  •  Object array: tables

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

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