Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named Demo Squares that instantiates an array of ten Square objects with sides that have values of 1 through10. Display the values for each Square. Save the class as DemoSquares.cs.
Loading
Abhishek YadavPosted Sep 1, 2023, 6:20 AM
Here is the code for the classes Square and DemoSquares:
In the
Squareclass, there are private fieldssideandarea, representing the length of a side and the area of the square respectively. The constructor takes a parameter for the length of one side of the square, assigns it to thesidefield, and then calls theCalculateArea()method to compute the area. TheSideandAreaproperties are read-only properties that allow external access to the values ofsideandarea.The
DemoSquaresclass is where the actual demonstration of theSquareclass takes place. It instantiates an array of tenSquareobjects, with side lengths ranging from 1 to 10. Then, it iterates over the array and displays the values for each square, including its side length and area.Note that you need to save the
DemoSquaresclass asDemoSquares.csfor it to compile and run as a standalone program.Tahir AnsariPosted Sep 1, 2023, 5:17 AM