0

"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;
    }
}
4

5 回答 5

7

Don't confuse read-only properties with read-only fields.

   public class Square
   {
        public Square(double lengths)
        {
           Length = lengths;
           Area = computeArea();
        }

        //Read only property for Length (privately settable)
        public double Length {get; private set;}

        //Read only property for Area (privately settable)
        public double Area {get; private set;}

        //Private method to compute area.
        private double ComputeArea()
        {
            return Length * Length;
        }
    }
于 2013-10-06T07:50:05.297 回答
2

The question mentions readonly properties, not readonly fields.

a readonly field can only be assigned in a constructor or by a field initializer. A readonly property can only be assigned inside the class.

public class Square
{
   // Readonly field, can only be assigned in constructor or initializer
   private readonly double _sideLength;

   // Readonly property since it only contains a getter
   public double SideLength { get { return _sideLength; } }

   // Readonly property from outside the class since the setter is private
   public double Area {get; private set;}

   public Square( double sideLength )
   {
        _sideLength = sideLength;
        CalcSquare();
   }

   private void CalcSquare()
   {
      this.Square = _sideLength * _sideLength;
   }
}
于 2013-10-06T07:48:25.437 回答
2

A read-only variable can indeed be assigned in the constructor, but not in methods called from the constrctor. There are ways to do that, i.e. : link. The correct way would be to calculate the area and store the result in the area variable.

I believe, though, that the meaning was different in the question. Quoting you :

Also include read-only properties to get a Squares side and area.

meaning, the question meant that you use Properties. Meaning, you would use private variables for length and area, and implement a get-only property for each :

public double Area 
{
    get
    {
       return area;
    }
}
于 2013-10-06T07:49:32.607 回答
0

Consider how you would restructure the code if you didn't try to assign the computed area to the area field, but instead returned the value from computeArea.

As an additional exercise, try making computeArea static and see how that affects the code.

于 2013-10-06T07:46:13.340 回答
0

You assignment never said your private function should assign area. It said the constructor should assign area with the result of a private method:

public class Square
{
    private readonly double length;
    private readonly double area;

    public Square(double length) 
    {
        this.length = length;
        this.area = computeArea(length); // assignment of area in constructor!
    }

    private double ComputeArea(double length) 
    {
         return length * length;
    }
}
于 2013-10-06T07:48:56.237 回答