Write a class Box which contains the data members width, height and depth all of type double

Write a class Box which contains the data members widthheight and depth all of type double.

Write the implementation for the below 3 overloaded constructors in the class Box :

  • Box() – default constructor which initializes all the members with -1
  • Box(length) – parameterized constructor with one argument and initialize all the members with the value in length
  • Box(width, height, depth) – parameterized constructor with three arguments and initialize

Write a method public double volume() in the class Box to find out the volume of the given box.

Write the main method within the Box class and assume that it will receive either zero arguments, or one argument or three arguments.

For example, if the main() method is passed zero arguments then the program should print the output as:

Volume of Box() is : -1.0

Similarly, if the main() method is passed one argument : 2.34, then the program should print the output as:

Volume of Box(2.34) is : 12.812903999999998

then the program should print the output as: Likewise, if the main() method is passed three arguments : 2.34, 3.45, 1.59, then the program should print the output as:

Volume of Box(2.34, 3.45, 1.59) is : 12.836070000000001

Solution:

package q11267;

public class Box {

    double width;

    double height;

    double depth;

    Box(double w, double h, double d) {

        width = w;

        height = h;

        depth = d;
    }

    Box() {

        width = -1;

        height = -1;

        depth = -1;

    }

    Box(double length) {

        width = length;

        height = length;

        depth = length;

    }

    public double volume() {
        return width * height * depth;

    }

    public void display() {

        if (width == -1 && height == -1 && depth == -1) {
            System.out.println("Volume of Box() is : " + volume());

        } else if (width != -1 && width == height) {

            System.out.println("Volume of Box(" + height + ") is : " + volume());

        } else {
            System.out.println("Volume of Box(" + width + ", " + height + ", " + depth + ") is : " + volume());

        }

    }

    public static void main(String[] args)

    {
        double[] arr = new double[3];

        for (int i = 0; i < args.length; i++) {

            arr[i] = Double.parseDouble(args[i]);

        }

        if (arr[0] == 0) {

            Box b = new Box();

            b.display();

        } else if (arr[1] == 0) {

            Box b = new Box(arr[0]);

            b.display();

        } else {
            Box b = new Box(arr[0], arr[1], arr[2]);
            b.display();
        }
    }

}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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