In: Computer Science
in c#:
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 DemoSquares that instantiates an array of ten Square objects with sides that have values of 1 through 10. Display the values for each Square. Save the class as DemoSquares.cs
PLEASE SHOW THE OUTPUT.
checking with array size of 5 you can change it to what ever you like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoSquares
{
class Square
{
public int length;
public int area;
public Square() // constructor
{
Length = 1; //intializing to 1 because its from 1 to 10
}
public int Length // length getter and setter
{
get
{
return length;
}
set
{
length = value;
findArea();
}
}
public int Area
{
get
{
return area;
}
}
private void findArea()
{
area = length * length; // calculate area
}
}
public class DemoSquares
{
public static void Main()
{
Square[] sqr = new Square[5];
int i;
for (i = 0; i < sqr.Length; ++i)
sqr[i] = new Square();
sqr[1].Length = 2;
sqr[2].Length = 3;
sqr[3].Length = 4;
sqr[4].Length = 5;
Console.WriteLine();
Console.WriteLine(" Square 1 length is {0}, and area is {1}", sqr[0].length, sqr[0].area);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Square 2 length is {0},and area is {1}", sqr[1].length, sqr[1].area);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Square 3 length is {0}, and area is {1}", sqr[2].length, sqr[2].area);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Square 4 length is {0}, and area is {1}", sqr[3].length, sqr[3].area);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(" Square 5 length is {0},and area is {1}", sqr[4].length, sqr[4].area);
Console.ReadLine();
}
}
}
OUTPUT:
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP