Ads

Monday, December 28, 2020

Make a class named "Rectangle" having two data members, length and breadth and a method area () for calculating the area. The following three constructors should be there in your class: a. No args/parameter less constructor: Length and breadth values should be assigned zero. b. Parameterized constructor (1 parameter): Length and breadth values should be same. c. Parameterized constructor (2 parameters): Length and breadth values should be assigned by 2 parameters respectively.




here's an implementation of the Rectangle class 

public class Rectangle {

    private double length;

    private double breadth;


    public Rectangle() {

        this.length = 0;

        this.breadth = 0;

    }

    public Rectangle(double side) {

        this.length = side;

        this.breadth = side;

    }

    public Rectangle(double length, double breadth) {

        this.length = length;

        this.breadth = breadth;

   

    public double getLength() {

        return length;

    }

    public void setLength(double length) {

        this.length = length;

    }

    public double getBreadth() {

        return breadth;

    }

    public void setBreadth(double breadth) {

        this.breadth = breadth;

    }

    public double area() {

        return length * breadth;

    }

}

 

The Rectangle class has two private instance variables, length and breadth, and a public constructor that initializes them. We have defined three constructors:

 

A no-arg constructor, which sets both length and breadth to 0.

A parameterized constructor that takes a single argument side and sets both length and breadth to side.

A parameterized constructor that takes two arguments length and breadth and sets the instance variables accordingly.

We also have getter and setter methods for length and breadth which are public. Additionally, there is a method named area() which returns the area of the rectangle by multiplying length and breadth.

 

To use this class, we can create objects of Rectangle and call methods on them as shown below:

 

// create a rectangle with length 5 and breadth 10

Rectangle rect1 = new Rectangle(5, 10);

 

// calculate the area of the rectangle

double area1 = rect1.area();

System.out.println("Area of rect1: " + area1);

 

// create a square with side 7

Rectangle square1 = new Rectangle(7);

 

// calculate the area of the square

double area2 = square1.area();

System.out.println("Area of square1: " + area2);

 

In the above code, we create an object rect1 of Rectangle class using the parameterized constructor with two arguments. We calculate the area of the rectangle using the area() method and print it to the console.

 

Similarly, we create an object square1 of Rectangle class using the parameterized constructor with one argument. We calculate the area of the square using the area() method and print it to the console.

 

The output of the above code would be:

Area of rect1: 50.0

Area of square1: 49.0

 

 


No comments:

Post a Comment