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