Write a Java Program to Calculate Area of Rectangle

0

In this tutorial we will see how to find the area of rectangle. Area of rectangle means the amount of space occupied by the rectangle.

To find Area of rectangle: Area = Width * Height

In this post we will learn to find Area  of rectangle using 4 different ways

  • Java Program to Find Area of Rectangle using Scanner
  • Java program to find area of rectangle using constructor
  •  Java program to find area of rectangle using method
  • Calculate area of rectangle in java using class and object


Example 1: Java Program to Find Area of Rectangle using Scanner


import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
      
       int length, breadth, area;
       Scanner scanner = new Scanner(System.in);
      
       //Taking length from user
       System.out.println("Enter the length");
       length = scanner.nextInt();
      
       //Taking breadth from user
       System.out.println("Enter the breadth");
       breadth = scanner.nextInt();
      
       //close scanner
       scanner.close();
      
       area = length * breadth;
       System.out.println("Area of Rectangle: "+area);

   }
}

output:


Enter the length
6
Enter the breadth
5
Area of Rectangle: 30


Example 2: Java Program to Find Area of Rectangle Using Constructor

In this program we use constructor to find the area of rectangle. Constructor is use to initialize the value for object attributes.  constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory

  • Constructor name must be the same as its class name
  • A Constructor must have no explicit return type
  • A Java constructor cannot be abstract, static, final, and synchronized


import java.util.Scanner;


class findRectangle {

  int length, breadth, area;

  // Parameterized constructor
  findRectangle(int len, int brdth) {
    length = len;
    breadth = brdth;
   }

  void getArea() {
    area = length * breadth;
    System.out.println("Area of rectangle:" + area);
   }
}

public class Main {
  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    int length, breadth;

    //accept length from user
    System.out.println("please enter the length: ");
    length = scanner.nextInt();

    //accept breadth from user
    System.out.println("Please enter breadth");
    breadth = scanner.nextInt();

    //close scanner object after use
    scanner.close();

    findRectangle findRect = new findRectangle(length, breadth);
    findRect.getArea();
  }
}


Example 3: Java Program to Find Area of Rectangle using Method

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. In this approach we using method to find the area of rectangle. Our method called findAra() accept two argument for width and height to calculate the area  and return the value.


import java.util.Scanner;


public class Main {
    
  private static Scanner scanner;

  public static void main(String[] args) {
    double width, height;
    scanner = new Scanner(System.in);

    //Accept user input for width
    System.out.println("Please Enter the Width:  ");
    width = scanner.nextDouble();
    
    //Accept user input for Height
    System.out.println("Please Enter the Height: ");
    height = scanner.nextDouble();

    findArea(width, height);
  }

  public static void findArea(double width, double height) {
      
    double Area;
    Area = width * height;
    //Print Area
    System.out.format("\n The Area of a Rectangle = %.2f\n", Area);
  }
}


Example 4: Calculate Area of Rectangle in Java using Class and Object

In this program we use object of RectangleArea class and pass the value of breadth and width to find the area.


import java.util.Scanner;


class RectangleArea 
{
   int length, breadth; 
   
   void setValueForRect(int l, int b) 
   { 
       length = l; 
       breadth = b; 
   }
   // get area of rectangle
   int findArea() 
   { 
       return (length * breadth); 
   }
}
public class Main
{
   public static void main(String[] args)
   {
       //RectangleArea object created
      RectangleArea obj = new RectangleArea();
      //calling method using obj
      obj.setValueForRect(10, 5); 
      System.out.println("Area of rectangle: " + obj.findArea());
   }
}


output:


Enter the length
5
Enter the breadth
5
Area of Rectangle: 25

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !