Java Program to find the area of a rectangle using parameterized constructor


In this program, You will learn how to find the area of a rectangle using a parameterized constructor in Java.


area = length * width


Example: How to find the area of a rectangle using parameterized constructor in Java

Run it Now
import java.util.Scanner;

class Test {

    int area;

    Test(int l, int w) {
        area = l * w;
    }

    void display() {
        System.out.println("\nThe area of rectangle is : " + area);
    }
}

class Main {

    public static void main(String args[]) {
        int length, width;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter length of rectangle : ");
        length = sc.nextInt();
        System.out.print("\nEnter width of rectangle : ");
        width = sc.nextInt();

        Test t = new Test(length, width);
        t.display();
    }
}

Output:

Enter length of rectangle : 5
Enter width of rectangle : 3
The area of rectangle is : 15