KnowledgeBoat Logo
|

Computer Applications

Create a class Rectangle with data members length, breadth and height. Use a parameterised constructor to initialize the object. With the help of function surfacearea() and volume(), calculate and display the surface area and volume of the rectangle.

Java Constructors

77 Likes

Answer

class Rectangle {
    double length;
    double breadth;
    double height;

    public Rectangle(double l, double b, double h) {
        length = l;
        breadth = b;
        height = h;
    }

    public void surfacearea() {
        double sa = 2 * (length * breadth + breadth * height + length * height);
        System.out.println("Surface Area = " + sa);
    }

    public void volume() {
        double vol = length * breadth * height;
        System.out.println("Volume = " + vol);
    }
}

Answered By

33 Likes


Related Questions