Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box.

Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box.

LANGUAGE: Java

CHALLENGE:

Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box.
These dimensions should be of a type that can represent linear measurements, including fractional ones.
Also include a boolean variable called full as instance data that represents whether the box is full or not.

Define the Box constructor to accept and initialize the height, width, and depth of the box.
Each newly created Box is empty (the constructor should initialize full to false).
Include getter and setter methods for all instance data.
Include a toString method that returns a one-line description of the box of the form “HxWxD” where H, W, and D are the current values respectively of the boxes height, width and depth.

SOLUTION:


class BOX{
    private double depth,width,height;
    BOX(double d,double b,double h){
        depth = d; width = b; height =h;
    }

    public void setDepth(double d) { 
        this.depth = d;
    }

    public void setWidth(double b) { 
        this.width = b; 
    }

    public void setHeight(double h) { 
        this.height = h;
    }

    public double getDepth() { 
        return this.depth;
    }

    public double getWidth() { 
        return this.width; 
    }

    public double getHeight() { 
        return this.height;
    }

    public double surfaceArea(){ 
        return 2 * (depth * width + width*height + height*depth);
    }

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

    public String toString(){
        return "Depth: "+Math.round(this.depth*100.0)/100.0 + ", Width:"+Math.round(this.width*100.0)/100.0+", Height: "+Math.round(this.height*100.0)/100.0;
    }

}


Add comment