Answer to Question #233889 in C# for cato

Question #233889

Create a class named 'Rectangle' with two data members- length and breadth and a function to calculate the area which is 'length*breadth'. The class has three constructors which are : 1 - having no parameter - values of both length and breadth are assigned zero. 2 - having two numbers as parameters - the two numbers are assigned as length and breadth respectively. 3 - having one number as parameter - both length and breadth are assigned that number. Now, create objects of the 'Rectangle' class having none, one and two parameters and display their areas. 


1
Expert's answer
2021-09-06T13:17:07-0400
namespace Rectangle
{
    public class Rectangle
    {
        private readonly int _length;
        private readonly int _breadth;


        public Rectangle()
        {
            _length = 0;
            _breadth = 0;
        }


        public Rectangle(int side)
        {
            _length = _breadth = side;
        }


        public Rectangle(int length, int breadth)
        {
            _length = length;
            _breadth = breadth;
        }


        public int Area()
        {
            return _length * _breadth;
        }
    }


    class Program
    {
        static void Main()
        {
            Rectangle zero = new Rectangle();
            int zeroArea = zero.Area();
            System.Console.WriteLine(zeroArea);


            Rectangle square = new Rectangle(10);
            int squareArea = square.Area();
            System.Console.WriteLine(squareArea);


            Rectangle rectangle = new Rectangle(10, 20);
            int rectangleArea = rectangle.Area();
            System.Console.WriteLine(rectangleArea);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS