Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) Write a class called Box. Each object of type Box represents a cardboard box

ID: 3709820 • Letter: #

Question

(1) Write a class called Box. Each object of type Box represents a cardboard box. The class Box has the following instance variables of type int: width, height, and length. The class has two constructors: one without parameters that creates a box with width-1, height-1, and length-1, and another one, which has a parameter corresponding to each instance variable. There are also getter and setter methods for each instance variable, and two other methods: getTotalArea0, which returns the total surface area of all sides of the box, and getVolume0, which returns the volume of the box. (2) Write an application class that contains the mainO method. In the mainO method create two objects of class Box- one with the constructor with no parameters and another one with the constructor with three parameters. Then print the total surface area and the volume of each box (using the corresponding methods). Then double the width of the first box and the height of the second one using the corresponding getter and setter methods, and print again their total surface area and volume.

Explanation / Answer

private double height, width, depth;
private boolean full;

public Box( double h, double w, double d){
height = h;
width = w;
depth = d;
full = false;
}

public boolean hasContents(){
if(full){return true;}
else {return false;}
}

public void fillBox(){ full = true; }
public void emptyBox() { full = false; }

public double getHeight(){return height;}
public double getWidth(){ return width;}
public double getDepth(){ return depth;}

public void setAll(double h, double w, double d){
height = h;
width = w;
depth = d;
}

public void setHeight(double h){height = h;}
public void setWidth(double w){width = w;}
public void setDepth(double d){depth = d;}

public double getVolume(){return (height*width*depth);}
public double getSurface(){return ( 2*(height*width) + 2*(height*depth) + 2*(depth*height) );} // assumes box has no opening for stuff to go in/out

public String toString(){
if(full) {return ("A full box that is " + height + " by " + width + " by " + depth + ".");}
else   {return ("An empty box that is " + height + " by " + width + " by " + depth + ".");}
}

}

public class boxTest{

public static void main(String [ ] args){

Box Box( 1.0, 2.0, 3.0);
Box two = new Box( 2.0, 3.0, 4.0);
Box three = new Box( 3.0, 4.0, 5.0);

//fill all boxes with candy, but then eat all of box two's candy

one.fillBox();
two.fillBox();
three.fillBox();

two.emptyBox();

//makes you cry when you realize how much you've eaten

System.out.println("You ate " + two.getVolume() + " gallons of candy!");

//You modify the boxes so they can hold more candy.

one.setAll(5.0, 5.0, 5.0);
two.setAll(5.0, 5.0, 5.0);
three.setAll(5.0, 5.0, 5.0);

//syntax error

sag84htj jvbkdsnsd(fhgdsgd)

//info about boxes

System.out.println(one.toString());
System.out.println(two.toString());
System.out.println(three.toString());

}