Write a Java Program to find Area of rectangle .

Q.13 Write a Java Program to find Area of rectangle .

Solution  :- 

class Rectangle {
private double length, width;

// Parameterized Constructor
Rectangle(double x, double y) {
length = x;
width = y;
}

// No-Argument Constructor
Rectangle() {
length = 0;
width = 0;
}

// Method to calculate the area
double area() {
return length * width;
}

// Method to display rectangle details
void display() {
System.out.println(“Rectangle with length = ” + length + ” width = ” + width);
}

// Getter for length
double getLength() {
return length;
}

// Getter for width
double getWidth() {
return width;
}
}

public class VisibilityPrivateB {
public static void main(String[] args) {
// Create rectangles
Rectangle rect1 = new Rectangle(); // Using no-argument constructor
Rectangle rect2 = new Rectangle(10, 15); // Using parameterized constructor

// Display details
rect1.display();
rect2.display();

// Calculate and display area of rect2
System.out.println(
“Area of rectangle with length = ” + rect2.getLength() +
“, width = ” + rect2.getWidth() +
” is ” + rect2.area()
);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.