Hello, I need help in these program if someone can help me I will appreciate it.
ID: 3544042 • Letter: H
Question
Hello,
I need help in these program if someone can help me I will appreciate it.
Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box as doubles. 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. We will assume that all supplied height and width values are positive values.
Add volume() and surfaceArea() methods to return calculated volume and surface area values based on instance data values.
Include a toString() method that returns a one-line description of the box that provides its dimensions and whether it's full or not. Format double values to 2 decimal places.
Create a driver class called BoxTest, whose main() method does the following:
Hello, I need help in these program if someone can help me I will appreciate it. Box Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box as doubles. 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. We will assume that all supplied height and width values are positive values. Add volume() and surfaceArea() methods to return calculated volume and surface area values based on instance data values. Include a toString() method that returns a one-line description of the box that provides its dimensions and whether it's full or not. Format double values to 2 decimal places. BoxTest Create a driver class called BoxTest, whose main() method does the following: Create an empty box with width 4, height 5, and depth 2. Print it out using the toString() method. Create a full box with width 1, height 1, and depth 1. Then change the dimensions to height 10, width 20, and depth 4. You should use the the setter methods to change the dimensions. Print the current height, width, and depth of this box using the getters. Print the volume and surface area of both boxes. You should be printing the output from the volume() and surfaceArea() methods. Don't calculate these values in main(). Thank you.Explanation / Answer
class BOX
{
private double depth,width,height;
BOX(double d,double b,double h)
{
depth = d; width = b; height =h;
}
// Mutator Methods
public void setDepth(double d) { this.depth = d;}
public void setWidth(double b) { this.width = b; }
public void setHeight(double h) { this.height = h;}
// Accessor Methods
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;
}
}
class BOXTest
{
public static void main(String args[])
{
BOX b1 = new BOX(2,4,5);
System.out.println("Dimensions of 1st Box are");
System.out.println(b1);//Same as calling toString method
BOX b2 = new BOX(1,1,1);
b2.setHeight(10);
b2.setWidth(20);
b2.setDepth(4);
System.out.println("Dimensions of new box are Depth : "+b2.getDepth
()+" Width: "+b2.getWidth()+" Height is: "+b2.getHeight());
System.out.println("Area of 1st Box is "+b1.surfaceArea());
System.out.println("Area of 2nd Box is "+b2.surfaceArea());
System.out.println("Volume of 1st Box is "+b1.volume());
System.out.println("Volume of 2nd Box is "+b2.volume());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.