write a program to find area of rectangle by using class and objects in java without using static method
// Instance variables to store length and width
double length;
double width;
// Constructor to initialize length and width
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Method to calculate and return the area
public double calculateArea() {
return length * width;
}
public static void main(String[] args) {
// Create an instance of the Rectangle class
Rectangle rect1 = new Rectangle(10.5, 5.0);
// Calculate the area using the instance method
double area = rect1.calculateArea();
// Print the area
System.out.println(“The area of the rectangle is: ” + area);
// Another instance of Rectangle
Rectangle rect2 = new Rectangle(7.2, 3.8);
System.out.println(“The area of the second rectangle is: ” + recpublic class Rectangle {
// Instance variables to store length and width
double length;
double width;
// Constructor to initialize length and width
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Method to calculate and return the area
public double calculateArea() {
return length * width;
}
public static void main(String[] args) {
// Create an instance of the Rectangle class
Rectangle rect1 = new Rectangle(10.5, 5.0);
// Calculate the area using the instance method
double area = rect1.calculateArea();
// Print the area
System.out.println(“The area of the rectangle is: ” + area);
// Another instance of Rectangle
Rectangle rect2 = new Rectangle(7.2, 3.8);
System.out.println(“The area of the second rectangle is: ” + rect2.calculateArea());
}
}t2.calculateArea());
}
}
Constructor
public class Rectangle {
// Instance variables to store length and width
private double length;
private double width;
// Constructor to initialize length and width
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Method to calculate and return the area
public double calculateArea() {
return length * width;
}
public static void main(String[] args) {
// Create an object of the Rectangle class
// The constructor is called here to initialize length and width
Rectangle rect1 = new Rectangle(10.5, 5.0);
// Calculate the area using the object’s method
double area1 = rect1.calculateArea();
System.out.println(“Area of rectangle 1: ” + area1);
// Create another object with different dimensions
Rectangle rect2 = new Rectangle(7.2, 3.8);
// Calculate the area for the second rectangle
double area2 = rect2.calculateArea();
System.out.println(“Area of rectangle 2: ” + area2);
}
}