Calculate area of rectangle using class in java

Let’s learn calculate area of rectangle using class in java.

Also read – rectangle java program

calculate area of rectangle using class in java

Calculate area of rectangle using class in java

To calculate area,

  • first create class “RectangleShape”. This class contain two public integer variables length, breadth.
  • to set values for variables length and breadth setValue() method is used.
  • findArea() method calculates area of rectangle and returns area.
  • In the main() method create object of class RectangleShape.
  • assign values for length and breadth. Finally calculate area using findArea() method.
import java.io.*;
class RectangleShape 
{
   int length, breadth; 
   void setValue(int l, int b) 
   { 
       length = l; 
       breadth = b; 
   }
   int findArea() 
   { 
       return(length * breadth); 
   }
}
public class RectangleAreaDemo
{
   public static void main(String[] args)
   {
      RectangleJava obj = new RectangleJava(); 
      obj.setValue(10, 5); 
      System.out.println("Area of rectangle : " + obj.findArea());
   }
}

Output:

Area of rectangle : 50