Exercise
Class Screentext
Objective
Create a class ScreenText, to display a certain text in specified screen coordinates. It must have a constructor which will receive X, Y and the string to write. It must also have 3 setters and a "Display" method.
Create a class CenteredText, based on ScreenText, to display text centered (horizontally) in a certain row of the screen. Its constructor will receive only Y and the text. SetX should not change the horizontal position.
Create a class FramedText, to display text centered and inside a rectangle. It will receive the starting row and the text.
Finally, create a test program for all of them, which will create an object of each type and display them.
Write Your C# Exercise
C# Exercise Example
// Importing the System namespace to handle basic functionalities and console output
using System;
public class ScreenText
{
// Coordinates and text to be displayed
private int x, y;
private string text;
// Constructor that sets the X, Y coordinates and text
public ScreenText(int x, int y, string text)
{
this.x = x;
this.y = y;
this.text = text;
}
// Setter for X coordinate
public void SetX(int x)
{
this.x = x;
}
// Setter for Y coordinate
public void SetY(int y)
{
this.y = y;
}
// Setter for the text
public void SetText(string text)
{
this.text = text;
}
// Method to display the text at the X, Y coordinates
public void Display()
{
Console.SetCursorPosition(x, y);
Console.WriteLine(text);
}
}
public class CenteredText : ScreenText
{
// Constructor that only needs Y and text. X will be calculated to center the text
public CenteredText(int y, string text) : base(0, y, text)
{
// Calculate the X position to center the text (in a console window with 80 columns)
int centeredX = (Console.WindowWidth - text.Length) / 2;
SetX(centeredX); // Set X to the calculated centered position
}
// Override the Display method to center the text
public new void Display()
{
base.Display(); // Call the base class method to display the text
}
}
public class FramedText : CenteredText
{
// Constructor that receives the row and text and calls the base class constructor
public FramedText(int row, string text) : base(row, text) { }
// Override Display method to display the text inside a frame
public new void Display()
{
int width = text.Length + 4; // Adding space for the frame
string frame = new string('*', width); // Create a frame line
// Print top frame
Console.SetCursorPosition(0, 0);
Console.WriteLine(frame);
// Print the framed text in the center
Console.SetCursorPosition(2, 1);
Console.WriteLine("*" + text + "*");
// Print bottom frame
Console.SetCursorPosition(0, 2);
Console.WriteLine(frame);
}
}
// Auxiliary class with the Main function to test the functionality of the classes
public class Program
{
public static void Main()
{
// Create a ScreenText object and display it at the given coordinates (X, Y)
ScreenText screenText = new ScreenText(5, 3, "This is ScreenText!");
screenText.Display();
// Create a CenteredText object and display it in the center of the screen (horizontally)
CenteredText centeredText = new CenteredText(5, "This is CenteredText!");
centeredText.Display();
// Create a FramedText object and display it inside a frame
FramedText framedText = new FramedText(7, "This is FramedText!");
framedText.Display();
}
}