Skip to content

3.2 Classes as attribute

  1. If needed, start from the project up to chapter 2. This contains the classes Program and Circle as you left them at the end of chapter 2.
  2. Create a new C# class named Point in the Models folder.
  3. Implement the class according to the following class diagram:

    • Use default value 0 for X and Y;
    • Use constructor chaining;
    • Add the getters and setters (properties);

    Mermaid diagram Mermaid diagram

    The Circle class is rebuilt below with an attribute of the Point class instead of the CenterX and CenterY attributes.

  4. Remove:

    • the attributes CenterX and CenterY;
    • the all-args constructor and the constructor with only the radius;
    • the getters and setters of CenterX and CenterY.
  5. Add a Center attribute of type Point to the Circle class:

    1
    2
    3
    4
    private const double LimitValueForBigShape = 100.0;
    private double _radius;
    private Point _center;
    private string _color;
    

    The Circle class now has an attribute named Center of type Point.

  6. Add the getter and setter for the Center attribute:

    1
    2
    3
    4
    5
    public Point Center
    {
        get => _center;
        set => _center = value;
    }
    
    • Note that the return type of Center is an object of the Point class.
    • The setter parameter is also an object of the Point class.
  7. Add the all-args constructor:

    1
    2
    3
    4
    5
    6
    public Circle(double radius, Point center, string color)
    {
        _radius = radius;
        _center = center;
        _color = color;
    }
    

    Note the constructor signature. There are three parameters that correspond to the attributes of the Circle class.

  8. Add the following Circle(double radius) constructor using constructor chaining:

    1
    2
    3
    public Circle(double radius) : this(radius, new Point(), "white")
    {
    }
    
    • As in 2.4 Constructor chaining, this constructor refers to the all-args constructor.
    • The all-args constructor expects a parameter of type Point. This is obtained by creating a new instance and passing it as a parameter.
    • The default value for Circle.Center is determined by the default constructor of the Point class.
    • The default constructor for Circle does not need to be changed. It still calls the more specific constructor with the default value 1 for radius.
  9. Verify that you have the three constructors.

    The code in the Main method must also be adjusted.

  10. Modify the code in the Main method as follows (you only need to adjust the all-args constructor call):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    Circle[] myCircleArray = new Circle[3];
    myCircleArray[0] = new Circle(3, new Point(1, 4), "green");
    myCircleArray[1] = new Circle();
    myCircleArray[2] = new Circle(6);
    
    for (int i = 0; i < myCircleArray.Length; i++)
    {
        Console.WriteLine(myCircleArray[i].CalculatePerimeter());
        Console.WriteLine(myCircleArray[i].CalculateArea());
    }
    
    • The line myCircleArray[0] = new Circle(3, new Point(1, 4), "green") first creates an object of type Point with coordinates (1, 4).
    • This object is then passed as the second parameter to the all-args constructor.
  11. Run your program. Your output will look like this:

    1
    2
    3
    4
    5
    6
    18.84955592153876
    28.274333882308138
    6.283185307179586
    3.141592653589793
    37.69911184307752
    113.09733552923255
    
  12. Try some other values and view the output.

  13. Add the following two lines to your for-loop:

    1
    2
    Console.WriteLine(myCircleArray[i].Center.X);
    Console.WriteLine(myCircleArray[i].Center.Y);
    
    • myCircleArray[i] refers to an object of the Circle class.
    • myCircleArray[i].Center is the property in the Circle class that returns an object of the Point class.
    • myCircleArray[i].Center.X is the property in the Point class that returns the value of the X attribute.
  14. Run your program. Your output will look like this:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    18.84955592153876
    28.274333882308138
    1.0
    4.0
    6.283185307179586
    3.141592653589793
    0.0
    0.0
    37.69911184307752
    113.09733552923255
    0.0
    0.0
    
  15. Try some other values and view the output.