Write a Java program to create a class named "Rectangle." In the "Rectangle" class, take two instance variables length and breadth. Now create a parameterized constructor to initialize variables and create a method area() which returns the area of the rectangle. Test the class "Rectangle."
import java.util.*;
class Rectangle{
int length;
int breadth;
Rectangle(int l, int b) {
length = l;
breadth = b;
}
public int area() {
return length*breadth;
}
}
class T17 {
public static void main(String[] args) {
int l, b, ar;
Scanner sc = new Scanner(System.in);
System.out.println("-----------------------------------");
System.out.print("Enter the length of Rectangle : ");
l = sc.nextInt();
System.out.print("Enter the breadth of Rectangle : ");
b = sc.nextInt();
Rectangle r = new Rectangle(l, b);
ar = r.area();
System.out.println("-----------------------------------");
System.out.println("Area of Rectangle : " + ar);
System.out.println("-----------------------------------");
}
}
Copy Code