Java Program to find Area of Rectangle

Write Java Program to find the Area Of the Rectangle and Perimeter of the Rectangle with an 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 program allows the user to enter the width and height of the rectangle. By using those values, this program will calculate the area of a rectangle and the perimeter of a 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);
	}
}
Java Program to find Area of Rectangle 1

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

TIP: You can remove the second Java 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

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 Area of a rectangle logic and place it in a method.

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 =  ");
		width = sc.nextDouble();
		System.out.println("\n Please Enter the Height = ");
		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

 Please Enter the Width =  
7

 Please Enter the Height = 
8

 The Area of a Rectangle = 56.00

 The Perimeter of a Rectangle = 30.00

Java Program to find Area of Rectangle using Oops

In this Java Program example, we divide the code using Object Oriented Programming. To do this, first, we will create a class that holds 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 the 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 =  ");
		width = sc.nextDouble();
		System.out.println(" Please Enter the Height = ");
		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);
	}
}
 Please Enter the Width =  
5

 Please Enter the Height = 
7

 The Area of a Rectangle = 35.00
 The Perimeter of a Rectangle = 24.00

 Second Method: The Area of Rectangle = 35.00

AreaOfARectangle Class Analysis:

  • First, we declared a function AreaofRectangle with two arguments. We are calculating the Area and Perimeter of the Rectangle within the function 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 the Rectangle, and we return the Value.

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 that this is the first method with a void keyword that will calculate Area and Perimeter and print the output from the AreaOfARectangle Class.

ar.AreaofRectangle(width, height);

Next, we are calling the RectangleArea method. The second double data type method 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 Java System.out.format statement to print the Area is calculated by the RectangleArea method.

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

Java Program to find the Perimeter of a Rectangle

Write a Java Program to find the Perimeter of a Rectangle with an example. This example allows entering rectangle width and height, and the addition of both multiplies by two gives the area. Thus, the Perimeter of a Rectangle equals 2 * (width + height).

package Area;

import java.util.Scanner;

public class PerimOfRectangle1 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		double width, height, Perimeter; 
		
		System.out.print("Enter the Width =  ");
		width = sc.nextDouble();
		
		System.out.print("Enter the Height = ");
		height = sc.nextDouble();

		Perimeter = 2 * (width + height);

		System.out.format("The Perimeter = %.2f", Perimeter);
	}
}
Enter the Width =  17
Enter the Height = 44
The Perimeter  = 122.00

In this program, we declared a rectanglePerimeter function that returns the rectangle perimeter.

package Area;

import java.util.Scanner;

public class PerimOfRectangle2 {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		float width, height, Perimeter; 
		
		System.out.print("Enter the Rectangle Width =  ");
		width = sc.nextFloat();
		
		System.out.print("Enter the Rectangle Height = ");
		height = sc.nextFloat();

		Perimeter = rectanglePerimeter(width, height);

		System.out.format("The Perimeter of a Rectangle = %.2f", Perimeter);
	}
	
	public static float rectanglePerimeter(float width, float height) {
		return 2 * (width + height);
	}
}
Java Program to find Perimeter of a Rectangle 1