Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Program to Find out the Area and Perimeter of Rectangle using...

Java Program to Find out the Area and Perimeter of Rectangle using Class Concept

The perimeter is the length of the outline of a shape. To find the perimeter of a rectangle or square you have to add the lengths of all four sides. x is in this case the length of the rectangle while y is the width of the rectangle. The area is the measurement of the surface of a shape.

The main task here is to create a class that can be used to find the Area and Perimeter of a rectangle. 

By using the formula for Area of Rectangle and Perimeter of Rectangle:

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

Example:

Length = 10

Breadth = 20

Area of rectangle is = 200

Perimeter of rectangle is = 60

Approach :

  • First, we will create a class called Rectangle in which we will define the variable length and breadth. And the method Area and Perimeter to calculate the Area and Perimeter of Rectangle.
  • Then we will create another class in which we will create a main method.
  • Inside the main method, the object of the rectangle is created using which we can call assign the values to the variable of Rectangle class, and after assigning the values the methods of the Rectangle class will be called to calculate the Area and Perimeter of Rectangle.
  • Both the methods of the Rectangle class will use the values of length and breadth that we had passed from the main method using the object of that class. And the result will get printed.

Example:

Java




// Java program to create a class to
// print the area and perimeter of a
// rectangle
 
import java.util.*;
 
// Rectangle Class File
public class Rectangle {
 
    // Variable of data type double
    double length;
    double width;
 
    // Area Method to calculate the area of Rectangle
    void Area()
    {
        double area;
        area = this.length * this.width;
        System.out.println("Area of rectangle is : "
                           + area);
    }
 
    // Perimeter Method to calculate the Perimeter of
    // Rectangle
    void Perimeter()
    {
        double perimeter;
        perimeter = 2 * (this.length + this.width);
        System.out.println("Perimeter of rectangle is : "
                           + perimeter);
    }
}
 
 class Use_Rectangle {
    
    public static void main(String args[])
    {
        // Object of Rectangle class is created
        Rectangle rect = new Rectangle();
 
        // Assigning the value in the length variable of
        // Rectangle Class
        rect.length = 15.854;
 
        // Assigning the value in the width variable of
        // Rectangle Class
        rect.width = 22.65;
 
        System.out.println("Length = " + rect.length);
        System.out.println("Width = " + rect.width);
 
        // Calling of Area method of Rectangle Class
        rect.Area();
 
        // Calling of Perimeter method of Rectangle Class
        rect.Perimeter();
    }
}


Output

Length = 15.854
Width = 22.65
Area of rectangle is : 359.09309999999994
Perimeter of rectangle is : 77.008

Time complexity: O(1) since performing constant operations

Auxiliary Space: O(1)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments