Java Program to Find Out the Area and Perimeter of Rectangle Using Class Concept


The Perimeter of any particular shape can be considered an outline of a particular length. To be more precise, it is the length of the outline of any particular shape. Following the definition, it goes logically that the perimeter of a rectangle or square is a summation of the lengths of the four sides. The variable ‘x’ normally denotes the length of the rectangle while ‘y’ denotes the breadth of the rectangle. The rectangle’s area is the shape’s surface given in square units of length. The task at hand is to create a class that can calculate the value of the area and perimeter of a rectangle. We define the class for a rectangle because a square is a special form of a rectangle with all equal sides.


Area of a Rectangle = Length * Breadth
Perimeter of Rectangle = 2 * (Length + Breadth)


Length = 10
Breadth = 20
The area of the rectangle is = 200
The perimeter of the rectangle is = 60


  • The first step is to create a class called Rectangle. The fields of the class or the variables will contain the length and the breadth of the rectangle. The class will have two methods, namely, Area and Perimeter. The Area method will calculate the area of the rectangle using the fields of the class, while the Perimeter will calculate the perimeter.
  • Thereafter, you will need to create another class that will have a main method. We will make an object of the Rectangle class in the class.
  • Inside the main method of this class, the first step is to create an object of the Rectangle class. Alongside object creation, you will also need to pass values for the length and breadth. Once the values have been assigned, call the two methods, namely Area and Perimeter.
  • Both the methods of the Rectangle class will use the values of length and breadth to calculate the area and perimeter. The result will be printed out on the console.

Sample Code

import java.util.*;

public class Rectangle
//introducing new double variables
double length;
double width;

//the method below calculates the area of the rectangle

void Area ()
double area;
area = this.length * this.width;
System.out.println (“Area of the rectangle is :” + area);

//the method below calculates the perimeter of the rectangle

void Peri ()
double peri;
peri = 2 * (this.length + this.width);
System.out.println (“Perimeter of Rectangle is :” + perimeter);

class Use_rec
public static void main (String args [])
//instantiating the class ( object creation)
Rectangle rect = new Rectangle ();

//Assigning length of the rectangle
rect.length = 18.854;

//Assigning width of the rectangle
rect.width = 22.65;

System.out.println (“Length = ” + rect.length);
System.out.println (“Width = ” + rect.width);

//invoking the Perimeter function to find the perimeter
rect.Perimeter ();


Length = 15.854
Width = 22.65
The area of Rectangle is: 359.0930999
The perimeter of the Rectangle is: 77.008



  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.