Skip to content

2.3 Constants

  1. Add the following line to the Circle class (above the other attributes):

    1
    private 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;
    

    const means the variable can no longer be changed and is therefore a constant. In C#, const is implicitly static (equivalent to Java's static 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).

  2. Add the following method to the Circle class:

    1
    2
    3
    4
    5
    6
    7
    public string DescribeSize()
    {
        if (CalculateArea() > LimitValueForBigShape)
        return "I am big!!!";
        else
        return "I am small!!!";
    }
    
  3. Modify the Main method 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());
    }
    
  4. 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!!!
    
  5. Try some other values and view the output.