Class Screentext - C# Programming Exercise

In this exercise of C#, you need to create a class called ScreenText, which will display a certain text at specified screen coordinates. The class must have a constructor that takes X, Y, and the string to write. It should also include three setters and a Display method to show the text on the screen.

Then, you need to create a class called CenteredText, based on the ScreenText class, to display text horizontally centered in a specific row of the screen. Its constructor will only receive Y and the text, and the SetX method should not change the horizontal position.

Next, you need to create a class called FramedText, which will display the text centered inside a rectangle. It will receive the starting row and the text to display.

Finally, create a test program for all of them, which will create an object of each type and display them using the Display method. This exercise is great for practicing inheritance, constructors, and using methods to manipulate the presentation of text on the screen with C#.

 Category

OOP More On Classes

 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

// 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();
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. 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 C#.

  •  Enhanced ComplexNumber class

    In this exercise of C#, you need to improve the ComplexNumber class by overloading the + and - operators to allow the addition and subtraction of...

  •  3D point

    In this exercise of C#, you need to create a class called Point3D to represent a point in 3-D space, with coordinates X, Y, and Z. The class must include the f...

  •  Catalog + Menu

    In this exercise of C#, you need to improve the catalog program so that the Main method displays a menu allowing the user to enter new data of any kind, as wel...

  •  Array of objects: table

    In this exercise, you need to create a class named "Table". This class should have a constructor that accepts the width and height of the table. ...

  •  House

    In this exercise, you need to create a class called "House" with an attribute called "area". The class should have a constructor to set the value of this attri...

  •  Table + coffetable + array

    In this exercise, you need to create a project named "Tables2", based on the "Tables" project. In this new project, you should create a class called "CoffeeTable" tha...