Calculate Area of Rectangle in Java

By | January 10, 2016

2 Ways To Find Area of Rectangle in Java Programming

Here, we have described Two Methods To Calculate Area of Rectangle in Java Programming Language. This is a Java Code To Find Area of Rectangle using Parameters with a self-explanatory program.

Area of Rectangle = Length * Breadth

Method 1: Java Program To Calculate Area of Rectangle without Parameters

import java.io.*;

class Rectangle
{
    double length, breadth;
    double area()
    {
        return length * breadth;
    }
}

class Area
{
    public static void main(String args[])
    {
        double area;
        Rectangle rec1 = new Rectangle();
        rec1.length = 20;
        rec1.breadth = 30;
        area = rec1.area();
        System.out.println("\nArea of Rectangle: " + area + "\n");
    }
}

Also Read: Find Area of Triangle Java Program

Method 2: Find Area of Rectangle in Java with Parameters

import java.io.*;

class Rectangle
{
    double length, breadth;
    double area()
    {
        return length * breadth;
    }
    void value(double l, double b)
    {
        length = l;
        breadth = b;
    }
}

class Area
{
    public static void main(String args[])
    {
        double area;
        Rectangle rec1 = new Rectangle();
        rec1.value(20, 30);
        area = rec1.area();
        System.out.println("\nArea of Rectangle: " + area + "\n");
    }
}

Also Read: Java Program To Find Armstrong Number

 

Output

Find Area of Rectangle in Java

In case you get any Compilation Errors with this Code To Calculate Area of Rectangle in Java or if you have any doubt about it, mention it in the Comment Section.

Let's Discuss