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.

import java.util.Scanner;

class Test {

    int area;

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

    void display() {
        System.out.println("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("Enter width of rectangle:");
        width = sc.nextInt();

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

Output:

Enter length of rectangle:12
Enter width of rectangle:3
Area of rectangle is: 36