Calculate area of rectangle using parameterised constructor in java

Let’s learn to calculate area of rectangle using parameterised constructor in java.

Calculate area of rectangle using parameterised constructor in java

wap to find area of rectangle using parameterized constructor in java is the most common java interview question.

Parameterized constructor is a constructor with specified number of parameters.

calculate area of rectangle using parameterised constructor in java

Now let’s calculate area of rectangle using parameterised constructor in java.

Also read – java program to find area of rectangle using constructor overloading

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class RectangleShape 
{
   int area, length, breadth;
   // Parameterized constructor
   RectangleShape(int l, int b) 
   { 
      length = l; 
      breadth = b; 
   } 
   void getArea() 
   { 
      area = length * breadth; 
      System.out.println("Area of rectangle : " + area); 
   } 
}
public class FindArea 
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        
      int length, breadth; 
      System.out.println("Please enter length : ");
      String strLength = br.readLine(); 
      length = Integer.parseInt(strLength);
      System.out.println("Please enter breadth : ");
      String strBreadth = br.readLine(); 
      breadth = Integer.parseInt(strBreadth); 
      RectangleShape rs = new RectangleShape(length, breadth); 
      rs.getArea();
   }
}


Output:

Please enter length : 50
Please enter breadth : 20
Area of rectangle : 1000

Also read – java program to find area of rectangle using method