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

public class Box { private double length; private double width; private double h

ID: 3550654 • Letter: P

Question

public class Box
{
private double length;
   private double width;
   private double height;

   public Box(double l, double w, double h)
   {
   length = l;
     width = w;
     height = h;
   }

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

1. Write the statement to instantiate a Box object, blueBox, with a length of 6, width of 4, and height of 2. This could be done in one statement

2.Write a statement to print out the volume of the blue box.

3.Write another constructor for the class Box, which has only 1 parameter, side, and initializes the dimensions of the Box as a cube, that is, all the dimensions of the Box are the same value as the side parameter.

4.Write an accessor method called getWidth that returns the width of the Box.

5.Write a mutator method setWidth that sets the width of the Box.

Explanation / Answer




1. Box blueBox=new Box(6,4,2); 2. System.out.println(blueBox.volume());
3. public Box(double s) { length=s; width=s; height=s;
}
4. public double getWidth() { return width; }
5. public void setWidth(double w) { width=w; }