Skip to content

3.5 The Rectangle class

  1. Create a new class according to the class diagram below. Use the following extra information:

    • The default value for length is 2;
    • The default value for width is 1;
    • The default value for topLeftCorner is determined by the Point class itself;
    • The default value for color = "white";
    • Use constructor chaining;
    • A rectangle is a quadrilateral with four right angles;
    • The perimeter is twice the length plus twice the width (or twice the sum of length and width);
    • The area is length times width;
    • The DescribeSize() method is identical to that of the Circle class;
    • Do not forget to add the getters and setters (properties);
  2. Test your class by running the following code in the Main method:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    Console.WriteLine(Rectangle.Definition());
    
    Rectangle[] myRectangleArray = new Rectangle[3];
    myRectangleArray[0] = new Rectangle(4, 3, new Point(2, 5), "blue");
    myRectangleArray[1] = new Rectangle();
    myRectangleArray[2] = new Rectangle(25, 10);
    
    for (int i = 0; i < myRectangleArray.Length; i++)
    {
        Console.WriteLine(myRectangleArray[i].CalculatePerimeter());
        Console.WriteLine(myRectangleArray[i].CalculateArea());
        Console.WriteLine(myRectangleArray[i].TopLeftCorner.X);
        Console.WriteLine(myRectangleArray[i].TopLeftCorner.Y);
        Console.WriteLine(myRectangleArray[i].DescribeSize());
    }
    
  3. Your output will look like this:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    A rectangle is a quadrilateral with four right angles.
    14.0
    12.0
    2.0
    5.0
    I am small!!!
    6.0
    2.0
    0.0
    0.0
    I am small!!!
    70.0
    250.0
    0.0
    0.0
    I am big!!!
    
  4. Try some other values and view the output.