2.3 Constants
-
Add the following line to the
Circleclass (above the other attributes):1private const double LimitValueForBigShape = 100.0;Your attributes should look like this:
1 2 3 4 5
private const double LimitValueForBigShape = 100.0; private double _radius; private double _centerX; private double _centerY; private string _color;constmeans the variable can no longer be changed and is therefore a constant. In C#,constis implicitly static (equivalent to Java'sstatic final), so the constant is shared by all instances of the class. Per C# coding conventions, constants use PascalCase (not SCREAMING_CAPS as in Java). -
Add the following method to the
Circleclass:1 2 3 4 5 6 7
public string DescribeSize() { if (CalculateArea() > LimitValueForBigShape) return "I am big!!!"; else return "I am small!!!"; } -
Modify the
Mainmethod as follows:1 2 3 4 5 6 7 8 9 10 11 12 13
static void Main(string[] args) { Circle myDefaultCircle = new Circle(); Console.WriteLine(myDefaultCircle.Radius); Console.WriteLine(myDefaultCircle.CalculateArea()); Console.WriteLine(myDefaultCircle.DescribeSize()); myDefaultCircle.Radius = 3; Console.WriteLine(myDefaultCircle.CalculateArea()); Console.WriteLine(myDefaultCircle.DescribeSize()); myDefaultCircle.Radius = 6; Console.WriteLine(myDefaultCircle.CalculateArea()); Console.WriteLine(myDefaultCircle.DescribeSize()); } -
Run your program. Your output will look like this:
1 2 3 4 5 6 7
1.0 3.141592653589793 I am small!!! 28.274333882308138 I am small!!! 113.09733552923255 I am big!!! -
Try some other values and view the output.