Array Of Objects: Table - C# Programming Exercise

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. The class should include a method called "ShowData", which will display the width and height of the table on the screen. Additionally, you should create an array containing 10 tables, with random sizes between 50 and 200 cm, and display the data for all of the tables. This exercise will help you work with constructors, methods, and arrays in C# to store and display information about objects.

 Category

OOP More On Classes

 Exercise

Array Of Objects: Table

 Objective

Create a class named "Table". It must have a constructor, indicating the width and height of the board. It will have a method "ShowData" which will write on the screen the width and that height of the table. Create an array containing 10 tables, with random sizes between 50 and 200 cm, and display all the data.

 Write Your C# Exercise

// Import the System namespace for basic functionality
using System;

public class ArrayOfObjectsDemo
{
    // Define the Table class
    public class Table
    {
        // Integer fields to store the width and height of the table
        private int width;
        private int height;

        // Constructor to initialize the table's width and height
        public Table(int tableWidth, int tableHeight)
        {
            // Set the width of the table
            width = tableWidth;
            // Set the height of the table
            height = tableHeight;
        }

        // Method to show the table's width and height
        public void ShowData()
        {
            // Display the width and height of the table
            Console.WriteLine($"Width: {width} cm, Height: {height} cm");
        }
    }

    // Define the Main entry point of the program for testing
    public static void Main()
    {
        // Create an array to hold 10 Table objects
        Table[] tables = new Table[10];

        // Create a random number generator for generating table sizes
        Random random = new Random();

        // Loop to populate the array with 10 tables having random sizes between 50 and 200 cm
        for (int i = 0; i < tables.Length; i++)
        {
            // Randomly generate width and height between 50 and 200 cm
            int width = random.Next(50, 201);
            int height = random.Next(50, 201);
            
            // Create a new Table object with the generated width and height and add it to the array
            tables[i] = new Table(width, height);
        }

        // Display the data for each table in the array
        for (int i = 0; i < tables.Length; i++)
        {
            Console.WriteLine($"Table {i + 1}:");
            // Call the ShowData method to display the table's dimensions
            tables[i].ShowData();
            Console.WriteLine();  // Print an empty line for separation
        }
    }
}

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

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

  •  Encrypter & Decrypter

    In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, whic...

  •  Complex numbers

    In this exercise of C#, the concept of complex numbers is introduced, which consist of two parts: the real part and the imaginary part. In an expression like a+bi (fo...

  •  Table + coffetable + leg

    In this exercise of C#, the example of tables and coffee tables is extended by adding a class called "Leg". This class should include a method named ShowData, ...

  •  Catalog

    In this exercise of C#, you are required to create a class diagram and then, using Visual Studio, develop a project with the corresponding classes for a catalog utili...