Using C#: Create a program named DemoSquares that instantiates an array of 10 Sq
ID: 3669764 • Letter: U
Question
Using 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.
Explanation / Answer
Here we have two classes to implement to this program those are DemoSquares
And Square those are as below
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
public class Square
{
// read-only properties to get a Square's side and area
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.