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

Write a class Box that contains instance data: width, height, and depth. Define

ID: 3587657 • Letter: W

Question

Write a class Box that contains instance data: width, height, and depth. Define Box’s constructor to accept and initialize all instance data. Provide the following methods in the class Box:

String toString() //returns one line description of the box as String.

int volume() // returns volume which is product of width, height, and depth.

int surface() //returns surface as 2* (width * height + depth * width + height * depth)

int linearInchSize() //returns sum of depth, width and height.

boolean small() // returrns true if linearInchSize is less than 30 inches, and also each side

// individually is less than 10. Otherwise it returns false.

boolean large() //returns true if linearInchSize is above 100 inches, and false otherwise.

boolean regular() //returns true if box is neither small nor large.

                                                                                                                              

Create a TestBox class with main method in it. Within main method instantiate three Box objects. Provide width, height, and depth in such a way that first box is small, second box is large, and third box is regular. For each box invoke all of the seven methods. Provide appropriate comments to inform user what is the meaning of resulting values.

Explanation / Answer

TestBox.java

//class defination Box
class Box
{
//varible declation part
public int width;
int height;
int depth;
int linearInchSize;
String boxsize="";
//constrctor definaton of Box class
Box(int w,int h,int d)
{
width=w;
height=h;
depth=d;
}
  
//to string() defination
public String toString()
{
return "One line description of the box as String";
}
//defination for volume functon
int volume()
{
return width*height*depth;
}
//defination for surface function
int surface()
{
return 2* (width * height + depth * width + height * depth);
}
//defination for linearInchSize function
int linearInchSize()
{
linearInchSize=width+height+depth;
return width+height+depth;
}
//defination for function small to check box is small or not
boolean small()
{
if(linearInchSize<30 && width<10 && height<10 && depth <10)
{
boxsize="small";
return true;
}
else
{
return false;
}
}
//defination for function large to check box is large or not
boolean large()
{
if(linearInchSize>100)
{
boxsize="large";
return true;
}
else
{
return false;
}
}
//defination for function regular to check box is regular or not
boolean regular()
{
if(boxsize=="small")
return false;
else if(boxsize=="large")
return false;
else
return true;
}

}

//Class creation for TestBox
public class TestBox
{
//main method defination
public static void main(String []args)
{

//Objects creation for Box class with parameterized constructor
Box b1=new Box(10,20,30);
Box b2=new Box(40,20,10);
Box b3=new Box(1,2,3);
////////////////////b1 outputs///////////////
System.out.println("Description of the box1 : "+b1.toString());
System.out.println("Volume of the box1 : "+b1.volume());
System.out.println("Surface of the box1 : "+b1.surface());
System.out.println("linearInchSize of the box1 : "+b1.linearInchSize());
System.out.println("Small box1 ? : "+b1.small());
System.out.println("Large box1 : "+b1.large());
System.out.println("Regular box1 : "+b1.regular());
///////////////////b1 outputs///////////////
System.out.println("Description of the box2 : "+b2.toString());
System.out.println("Volume of the box2 : "+b2.volume());
System.out.println("Surface of the box2 : "+b2.surface());
System.out.println("linearInchSize of the box2 : "+b2.linearInchSize());
System.out.println("Small box2 ? : "+b2.small());
System.out.println("Large box2 : "+b2.large());
System.out.println("Regular box2 : "+b2.regular());
  
///////////////////b3 outputs///////////////
System.out.println("Description of the box3 : "+b3.toString());
System.out.println("Volume of the box3 : "+b3.volume());
System.out.println("Surface of the box3 : "+b3.surface());
System.out.println("linearInchSize of the box3 : "+b3.linearInchSize());
System.out.println("Small box3 ? : "+b3.small());
System.out.println("Large box3 : "+b3.large());
System.out.println("Regular box3 : "+b3.regular());
  
  
  
}
}

Output :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote