101k views
3 votes
C# Create a program named DemoSquares that instantiates an array of 10 Square objects with sides that have values of 1 through 10 and that displays the values for each Square. The Square class contains fields for area and the length of a side, and a constructor that 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.

User Ekans
by
5.0k points

1 Answer

3 votes

Answer:

Here is the C# program.

using System; //namespace for organizing classes

public class Square //Square class

//private fields/variables side and area of Square class

{ private double side;

private double area;

//Side has read only property to get Square side

public double Side {

get { return this.side; }

}

//Area has read only property to get Square area

public double Area {

get { return this.area; } }

/*constructor Square(double length) that 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 SquareArea() that computes the area field */

public Square(double length) {

this.side = length;

this.area = SquareArea(); }

//method to calcuate area of a square

private double SquareArea() {

//Pow method is used to compute square the side i.e. side^2

return Math.Pow(this.side, 2); } }

public class DemoSquares {

public static void Main() { //start of main() function body

//displays the side and area on output screen

Console.WriteLine("{0}\t{1}\t{2}", "No.", "Side Length ", "Area");

//instantiates an array to input values 1 through 10 into Side

Square[] squares = new Square[10]; //squares is the instance

for (int i = 1; i < squares.Length; i++) {

//traverses through the squares array until the i exceeds the length of the //array

squares[i] = new Square(i);

//parameter is passed in the constructor

//new object is created as arrray is populated with null members

Square sq = squares[i];

/*display the no, side of square in area after setting the alignment to display the output in aligned way and with tab spaces between no side and area */

Console.WriteLine("{0}\t{1,11}\t{2,4}", i, sq.Side, sq.Area); }

//ReadKey method is used to make the program wait for a key press from //the keyboard

Console.ReadKey(); } }

Step-by-step explanation:

The program is well explained in the comments mentioned with each statement of the program. The program simply has Square class which contains area and length of side as fields and a constructor Square which has length of one side of Square as parameter and assigns its parameter to length of Square side. The a private method SquareArea() is called that computes the area field. get methods are used to include read-only properties to get a Square’s side and area. The program along with its output is attached in the screenshot.

C# Create a program named DemoSquares that instantiates an array of 10 Square objects-example-1
C# Create a program named DemoSquares that instantiates an array of 10 Square objects-example-2
User Noscreenname
by
5.4k points