Calculate area of rectangle using parameterised constructor in java

In this article we are going to learn to calculate area of rectangle using parameterised constructor in java.

Calculate area of rectangle using parameterised constructor in java

calculate area of rectangle using parameterised constructor in java

Parameterised constructor is a constructor that has parameters. These parameters initialize fields of a class.

Also read – preface to java virtual machine and architecture

Now let’s see java program on area of rectangle using parameterised constructor,

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