Write Java Program to find Area Of Rectangle and Perimeter of Rectangle with example.

## Java Area of a Rectangle

- If we know the width and height of a rectangle, we can calculate the area of a rectangle using the formula: Area = Width * Height
- Perimeter is the distance around the edges. We can calculate the perimeter of a rectangle using the formula: Perimeter = 2 * (Width + Height)

## Java Program to find Area of Rectangle & Perimeter of Rectangle

This Java program allows the user to enter the width and height of the rectangle. By using those values, this Java program will calculate the area of a rectangle and perimeter of a rectangle.

// Java Program to find Area of Rectangle and Perimeter of Rectangle package Area; import java.util.Scanner; public class AreaOfRctangle { private static Scanner sc; public static void main(String[] args) { double width, height, Area, Perimeter; sc = new Scanner(System.in); System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); Area = width * height; Perimeter = 2 * (width + height); System.out.format(" The Area of a Rectangle = %.2f\n",Area); System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter); } }

**OUTPUT**

**ANALYSIS**

Within the Java Program to find Area Of Rectangle, the following statements allow the User to enter the Width and Height of a rectangle. Then we are going to assign those values to already declared variables called Width and Height.

**TIP:** You can remove the second System.out.println statement to shorten the code.

System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble();

Next, we are using the Mathematical Formula to calculate the area of the rectangle.

Area = width * height;

In the next line, We are calculating the Perimeter of the rectangle

Perimeter = 2 * (width + height);

The following System.out.format statements help us to print the Perimeter and Area of a rectangle

System.out.format(" The Area of a Rectangle = %.2f\n",Area); System.out.format(" The Perimeter of a Rectangle = %.2f\n", Perimeter);

## Java Program to find Area of Rectangle using Functions

This Java program uses the logic that we specified in the first example. But we will separate the Java Area of a rectangle logic and place it in a method.

// Java Program to find Area of Rectangle using Functions package Area; import java.util.Scanner; public class AreaOfRctangleUsingMethods { private static Scanner sc; public static void main(String[] args) { double width, height; sc = new Scanner(System.in); System.out.println("\n Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println("\n Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); AreaofRectangle(width, height); } public static void AreaofRectangle( double width, double height ) { double Area, Perimeter; Area = width * height; Perimeter = 2 * (width + height); System.out.format("\n The Area of a Rectangle = %.2f\n",Area); System.out.format("\n The Perimeter of a Rectangle = %.2f\n", Perimeter); } }

**OUTPUT**

## Java Program to find Area of Rectangle using Oops

In this Java Program to find Area of Rectangle example, we are dividing the code using the Object Oriented Programming. To do this, first, we will create a class which holds a methods.

**TIP:** In general, You don’t have to write the first method. We used this method to show the available options

package Area; public class AreaOfaRectangle { double Area, Perimeter; public void AreaofRectangle( double width, double height ) { Area = width * height; Perimeter = 2 * (width + height); System.out.format(" The Area of Rectangle = %.2f\n", Area); System.out.format(" The Perimeter of Rectangle = %.2f\n", Perimeter); } public double RectangleArea( double width, double height ) { Area = width * height; return Area; } }

Within the Main Java Program to find Area of Rectangle, we will create an instance of the above-specified class and call the methods.

package Area; import java.util.Scanner; public class AreaOfRectangleUsingClass { private static Scanner sc; public static void main(String[] args) { double width, height, Area; sc = new Scanner(System.in); System.out.println(" Please Enter the Width of a Rectangle = "); width = sc.nextDouble(); System.out.println(" Please Enter the Height of a Rectangle = "); height = sc.nextDouble(); AreaOfaRectangle ar = new AreaOfaRectangle(); ar.AreaofRectangle(width, height); Area = ar.RectangleArea(width, height); System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area); } }

**OUTPUT**

**ANALYSIS**

**AreaOfARectangle Class Analysis:**

- First, we declared a function
**AreaofRectangle**with two arguments. Within the function, we are calculating the Area and Perimeter of Rectangle using respective mathematical formulas. Next, we used the**System.out.println**statement to print the output. - Next, we declared a
**RectangleArea**function with two arguments of type double. Within the function, it will calculate the Area of Rectangle, and we are returning the Value.

**Java Area of Rectangle Main Class Analysis:**

First, we created an instance/Object of the **AreaOfARectangle** Class

AreaOfaRectangle ar = new AreaOfaRectangle();

Next, we are calling the **AreaofRectangle** method. Note, this is the first method with a void keyword that will calculate Area and Perimeter and print the output from the **AreaOfARectangle** Class itself.

ar.AreaofRectangle(width, height);

Next, we are calling the **RectangleArea** method. It is the second method with a double data type that calculates Area and returns a value. So, we are assigning the return value to the Area variable.

Area = ar.RectangleArea(width, height);

Lastly, the following System.out.format statement to print the Area of a rectangle, which is calculated by the **RectangleArea** method.

System.out.format("\n Second Method: The Area of Rectangle = %.2f ", Area);