Skip to content

3.8 Code for the Rectangle class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
namespace Geometry.Models
{
    public class Rectangle
    {
        private double _length;
        private double _width;
        private Point _topLeftCorner;
        private string _color;

        private const double LimitValueForBigShape = 100.0;

        public double Length
        {
            get => _length;
            set => _length = value;
        }

        public double Width
        {
            get => _width;
            set => _width = value;
        }

        public Point TopLeftCorner
        {
            get => _topLeftCorner;
            set => _topLeftCorner = value;
        }

        public string Color
        {
            get => _color;
            set => _color = value;
        }

        public Rectangle(double length, double width, Point topLeftCorner, string color)
        {
            _length = length;
            _width = width;
            _topLeftCorner = topLeftCorner;
            _color = color;
        }

        public Rectangle(double length, double width) : this(length, width, new Point(), "white")
        {
        }

        public Rectangle() : this(2, 1)
        {
        }

        public string DescribeSize()
        {
            if (CalculateArea() > LimitValueForBigShape)
                return "I am big!!!";
            else
                return "I am small!!!";
        }

        public double CalculateArea()
        {
            return _length * _width;
        }

        public double CalculatePerimeter()
        {
            return 2 * (_length + _width);
        }

        public static string Definition()
        {
            return "A rectangle is a quadrilateral with four right angles.";
        }
    }
}