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

Create a Box class with the following: — instance variables length, width, and h

ID: 3572994 • Letter: C

Question

Create a Box class with the following:

— instance variables length, width, and height

— a no-argument Box constructor

— use 1.0 as the default value for width, length and height.

— a Box constructor with three parameters

— use 1.0 as the default value for width, length and height. There can be NO zero or negative value for the instance variables. Be sure to validate the parameters and use the default value if necessary.

— get and set (accessor and mutator) methods for each instance variable. There can be NO zero or negative values for the instance variables so be sure to validate the parameter in the set methods. Do NOT update the instance variable if the parameter is invalid (zero or negative).

— a toString method that returns a String with the length, width and height, clearly labeled. Format each value to two places past the decimal point.

— a calculateArea method that calculates and returns the surface area of the box (all 6 sides)

— a calculateVolume method that calculates and returns the volume of the box

----------------------------------------

Create a BoxTest program with the following.

— an array named boxes that has size (length) 5

— 5 Box objects - place each Box in the array. One Box object must be created with user input for the length, width and height. One Box object must be created with the no-argument constructor. The other Boxes should be created with literal values (2, 3.5, 5.75, etc.).

— use an enhanced for loop to display the length, width, height, area and volume of each Box object, clearly labeled. Do this by calling the appropriate methods. Call the toString method for the length, width and height. Format the area and volume to two places past the decimal point.

— use the command prompt window for input and output

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;

public class Box
{
   private double length;
   private double width;
   private double height;
  
   public Box() //default constructor
   {
       length = 1.0;
       width = 1.0;
       height = 1.0;
   }
   public Box(double length,double width,double height) //parameterized constructor
   {
       if(length <= 0)
       this.length =1.0;
       else
       this.length = length;
       if(width <= 0)
       this.width =1.0;
       else
       this.width = width;
       if(height <= 0)
       this.height =1.0;
       else
       this.height = height;
   }
   public void setLength(double length) //set and get methods for length,width and height
   {
       if(length <= 0)
       System.out.println("Length cannot be less than or equal to 0");
       else
       this.length = length;
   }
   public double getLength()
   {
       return length;
   }
   public void setWidth(double width)
   {
       if(width <= 0)
       System.out.println("Width cannot be less than or equal to 0");
       else
       this.width = width;
   }
   public double getWidth()
   {
       return width;
   }
   public void setHeight(double height)
   {
       if(height <= 0)
       System.out.println("Height cannot be less than or equal to 0");
       else
       this.height = height;
   }
   public double getHeight()
   {
       return height;
   }
   public String toString() //overriding toString()
   {
       return " Length : "+getLength() +" Width : "+getWidth() +" Height : "+getHeight();
   }
   public double calculateArea() //method to compute surface area of box
   {
       double area;
       area = 2*(length*width +width*height +height*length);
       return area;
   }
   public double calculateVolume() //method to compute volume of box
   {
       double volume;
       volume = length*width*height;
       return volume;
   }
}
public class BoxTest
{
   public static void main (String[] args)
   {
       Box boxes[] = new Box[5];
       Scanner scan = new Scanner(System.in);
       double length,width,height;
      
       DecimalFormat df = new DecimalFormat("#.00");
      
       System.out.println("Enter length of box 1");
       length = scan.nextDouble();
           System.out.println("Enter width of box 1");
       width = scan.nextDouble();
           System.out.println("Enter height of box 1");
       height = scan.nextDouble();
      
       boxes[0] = new Box(length,width,height);
      
       boxes[1] = new Box();
      
       boxes[2] = new Box(2,3.5,5.75);
       boxes[3] = new Box(4.1,6.34,2.56);
       boxes[4] = new Box(2.1,3.1,4.1);
      
      
       for(int i=0;i<5;i++)
       {
       System.out.println("Box "+i+boxes[i].toString()+ " Area : "+df.format(boxes[i].calculateArea())+" Volume :"+df.format(boxes[i].calculateVolume()));
       }
      
      
      
   }
}

output: