0% found this document useful (0 votes)
7 views4 pages

Exp (3) - 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Exp (3) - 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXP NO : 3

QUESTION :

Develop a Java program to implement constructor overloading and method overloading.

CODE:

class Rectangle {

private double length;

private double width;

public Rectangle() {

this.length = 1.0;

this.width = 1.0;

public Rectangle(double side) {

this.length = side;

this.width = side;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

public double area() {


return length * width;

public double area(double length, double width) {

return length * width;

public double perimeter() {

return 2 * (length + width);

public double perimeter(double length, double width) {

return 2 * (length + width);

public void display() {

System.out.printf("Rectangle (Length: %.2f, Width: %.2f)%n", length, width);

System.out.printf("Area: %.2f%n", area());

System.out.printf("Perimeter: %.2f%n", perimeter());

public class RectangleTest {

public static void main(String[] args) {

Rectangle rect1 = new Rectangle();


rect1.display();

Rectangle rect2 = new Rectangle(5);

rect2.display();

Rectangle rect3 = new Rectangle(4, 6);

rect3.display();

System.out.println("Area of rectangle (7, 3): " + rect3.area(7, 3));

System.out.println("Perimeter of rectangle (7, 3): " + rect3.perimeter(7, 3));

OUTPUT:

Rectangle (Length: 1.00, Width: 1.00)

Area: 1.00

Perimeter: 4.00

Rectangle (Length: 5.00, Width: 5.00)

Area: 25.00

Perimeter: 20.00

Rectangle (Length: 4.00, Width: 6.00)

Area: 24.00

Perimeter: 20.00

Area of rectangle (7, 3): 21.00

Perimeter of rectangle (7, 3): 20.00

You might also like