"Create a program named "DemoSquare" that initiates an array of 10 Square objects with sides that have values of 1 -10 and that displays the values for each square. The Square class contains fields for the area and the length of a side, and a constructor that requires a parameter for the area and the length of a side. The constructor assigns its parameter to the length of a Square's side and calls a private method that computes the area field. Also include read-only properties to get a Squares side and area."
Now I think that it is a trick question as I can't get the private method to compute the area because of the read-only assignment but here is my code:
class demoSquares
{
static void Main(string[] args)
{
Square[] squares = new Square[10];//Declares the array of the object type squares
for (int i = 0; i < 10; i++)
{
//Console.WriteLine("Enter the length");
//double temp = Convert.ToDouble(Console.ReadLine());
squares[i] = new Square(i+1);//Initializes the objects in the array
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(squares[i]);
}//end for loop, prints the squares
}//end main
}//end class
This is the Square Class:
public class Square
{
readonly double length;
readonly double area;
public Square(double lengths)//Constructor
{
length = lengths;
area = computeArea();
}
private double computeArea()//getmethod
{
double areaCalc = length * length;
return areaCalc;
}
}