3.2 Classes as attribute
- If needed, start from the project up to chapter 2. This contains the classes
ProgramandCircleas you left them at the end of chapter 2. - Create a new C# class named
Pointin theModelsfolder. -
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);
The
Circleclass is rebuilt below with an attribute of thePointclass instead of theCenterXandCenterYattributes. -
Remove:
- the attributes
CenterXandCenterY; - the all-args constructor and the constructor with only the radius;
- the getters and setters of
CenterXandCenterY.
- the attributes
-
Add a
Centerattribute of typePointto theCircleclass:1 2 3 4
private const double LimitValueForBigShape = 100.0; private double _radius; private Point _center; private string _color;The
Circleclass now has an attribute namedCenterof typePoint. -
Add the getter and setter for the
Centerattribute:1 2 3 4 5
public Point Center { get => _center; set => _center = value; }- Note that the return type of
Centeris an object of thePointclass. - The setter parameter is also an object of the
Pointclass.
- Note that the return type of
-
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
Circleclass. -
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.Centeris determined by the default constructor of thePointclass. - The default constructor for
Circledoes not need to be changed. It still calls the more specific constructor with the default value 1 for radius.
-
Verify that you have the three constructors.
The code in the
Mainmethod must also be adjusted. -
Modify the code in the
Mainmethod 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 typePointwith coordinates (1, 4). - This object is then passed as the second parameter to the all-args constructor.
- The line
-
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 -
Try some other values and view the output.
-
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 theCircleclass.myCircleArray[i].Centeris the property in theCircleclass that returns an object of thePointclass.myCircleArray[i].Center.Xis the property in thePointclass that returns the value of the X attribute.
-
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 -
Try some other values and view the output.