Java Program Find Area Of Rectangle

Sharing is healthy !!

Hey guys!! Welcome to flower brackets blog. Today we are going to learn java program find area of rectangle.

In this program user will be prompted to enter the length and width of a rectangle.

We will be using three classes to find area of rectangle namely BufferedReader, IOException and InputStreamReader of java.io package.

So let’s see how to write a java program to find area of rectangle,

Java Program Find Area Of Rectangle

example: Java Program Find Area Of Rectangle

Also Read – Area of Circle Java Program

/* Area of rectangle is, Area = length * width. */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class RectangleArea
{
   public static void main(String[] args)
   {
      int width = 0;
      int length = 0;

      try
      {
         // reading the length from console
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter length of a rectangle: ");
         length = Integer.parseInt(br.readLine());

         // reading the width from console
         System.out.println("Enter width of a rectangle: ");
         width = Integer.parseInt(br.readLine());
      }

      // to check if invalid value is entered
      catch(NumberFormatException nfe)
      {
         System.out.println("Invalid value " + nfe);
         System.exit(0);
      }

      catch(IOException ioe)
      {
         System.out.println("IOError: " +ioe);
         System.exit(0);
      }
      int area = length * width;
      System.out.println("Area of rectangle is " + area);
   }
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 4
Area of rectangle is 20


finding perimeter of rectangle

Here we are going find the perimeter of rectangle using the formula as below

/* Perimeter of rectangle is, Perimeter = 2 * ( length + width.) */

import java.util.Scanner;

public class RectanglePerimeterDemo
{
   public static void main(String[] args)
   {
      int length, width, perimeter;
      Scanner sc = new Scanner(System.in);

      System.out.println("Enter length of a rectangle: ");
      length = sc.nextInt();

      System.out.println("Enter length of a rectangle: ");
      width = sc.nextInt(); 

      perimeter = 2 * ( length + width ); 
      System.out.println("The perimeter of Rectangle is : " + perimeter);

      sc.close();
   }
}

Output:

Enter length of a rectangle: 5
Enter width of a rectangle: 10
The perimeter of Rectangle is : 30

Java Program Find Area Of Rectangle using function

In this program I am using a function which computes the rectangle area java using two parameters length and width.

Then the value “area” is returned to the main function from where it is called.

Let’s see the code execution,

import java.util.*;
 
public class AreaRectangle
{ 
   public static void main(String []args)
   {
      double length, width, area;
 
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter length of a rectangle: ");
      length = sc.nextDouble();
      System.out.print("Enter width of a rectangle: ");
      width = sc.nextDouble();
 
      area = AreaOfRectangle(length, width);

      System.out.println("Area of Rectangle: " + area);
      sc.close();
   }
 
   public static double AreaOfRectangle(double length, double width)
   {
      double area;
      area = length * width;
      return area;
   }
}

Output:

Enter length of a rectangle: 60
Enter width of a rectangle: 40
Area of Rectangle: 2400.0

without using scanner class and functions

We can also write java program to calculate area of rectangle without using scanner class and functions.

This is how we can accomplish,

public class AreaRectangle
{ 
   public static void main(String []args)
   {
      double length = 60.0;
      double width = 40.0;
      double area, perimeter;
 
      area = length * width;
      System.out.print("The area of rectangle is : " + area + "\n");
 
      perimeter = 2 * ( length + width );
      System.out.print("The perimeter of rectangle is : " + perimeter);
   } 
}

Output:

The area of rectangle is : 2400.0
The perimeter of rectangle is : 200.0

using class and objects

Let’s see java program to find the area of rectangle and circle using class and object. Here’s the code,

public class RectangleAreaDemo
{
   public static void main(String[] args)
   {
      Area area = new Area();
      area.circleArea(5);
      area.squareArea(10);
      area.rectangleArea(10, 4);
   }

   public class Area
   {
      void squareArea(float a)
      {
         System.out.println("Area of the square is : " + Math.pow(a, 2) + " sq units");
      }
      void rectangleArea(float a, float b)
      {
         System.out.println("Area of the rectangle is : " + a * b + " sq units");
      }
      void circleArea(double a)
      {
         double c = 3.14 * a * a;
         System.out.println("Area of the circle is : " + c + " sq units");
      }
   }
}

Output:

Area of the circle is : 78.5 sq units
Area of the square is : 100.0 sq units
Area of the rectangle is : 40.0 sq units


conclusion

That’s it guys. So this is how to find area of a rectangle program in java. You also learnt java program to find area of rectangle using constructor.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

If you like this post share it across social networks.

Sharing is healthy !!