Area of a Rectangle

Easy
0/40
151 upvotes

Problem statement

Design a class called Rectangle. It contains two data members as length(L) and breadth(B); and a member function getArea(). The member function computes the area of the given rectangle and returns it to the caller.

Note:

Area of a rectangle = length x breadth
Data Members:
1. length: Length of the rectangle
2. breadth: Breadth of the rectangle 
Member Functions:
1. getArea(): that calculates the area of the rectangle and returns the value.

Note:

You do not have to take input, just create the Class and set the name of the Data Members and function as given in the above discription.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The only line of input contains two integers, both separated by a single space denoting the length and breadth respectively.
Output Format:
The only line of output prints the area of the rectangle.
Constraints:
0 <= L, B <= 100
Sample Input 1:
4 20
Sample Output 1:
80
Explanation of Sample Input 1:
Length of the rectangle is 4 and breadth is 20. 
Hence the area of the rectangle is (length*breadth). 
So the answer is 4*20=80.
Sample Input 2:
2 10
Sample Output 2:
20
Explanation of Sample Input 2:
Length of the rectangle is 2 and breadth is 10. 
Hence the area of the rectangle is (length*breadth). 
So the answer is 2*10=20.
Approaches (1)
Intuitive Approach
  • First, define a rectangle class that creates two variables, length and breadth, that has public as access specifier.
  • Then create a getArea method of this class which calculates the rectangle area and returns it.
  • Now in the main function, create an object of Rectangle class, take two inputs into two variables, i.e., length and breadth.
  • Then set this value using (.) and then call the function getArea() that calculates and returns the area, and then print the returned value.
Time Complexity

O(1)

Space Complexity

O(1)

Code Solution
(100% EXP penalty)
Area of a Rectangle
Full screen
Console