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

Write a program where the user enters in information about 5 boxes, and then the

ID: 3862797 • Letter: W

Question

Write a program where the user enters in information about 5 boxes, and then they are sorted by their volume. Create a class called Box This class has four instance variables Label: the name of the box Length: the length in feet Width: the width in feet Height: the height in feet Assume Length, Width, and Height are all decimal values Constructors Default Parameterized (MUST CHECK FOR VALID VALUES) Accessors for every instance variable Mutators for every instance variable CHECK FOR VALID VALUES! Length width and Height should all be greater than 0 Other methods getVolume: this method returns a decimal value corresponding to its volume. It also has no parameters toString: This method returns a string which is made up of the label of the box and its volume Create another class called BoxSorter This class has a main method An array of boxes Prompts the user for box information Sorts the boxes Displays the sorted boxes

Explanation / Answer

Box.java

import java.text.DecimalFormat;

public class Box implements Comparable {
   //Declaring instance variables
private String name;
private double length;
private double width;
private double height;

//Default constructor
public Box() {
   super();
  
}
//Parameterized constructor
public Box(String name, double length, double width, double height) {
   super();
   this.name=name;
   setLength(length);
   setWidth(width);
   setHeight(height);
}

//Setters and getters
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public double getLength() {
   return length;
}
public void setLength(double length) {
   if(length>0.0)
   this.length = length;
   else
       this.length=1.0;
}
public double getWidth() {
   return width;
}
public void setWidth(double width) {
   if(width>0.0)
   this.width = width;
   else
       this.width=1.0;
}
public double getHeight() {
   return height;
}
public void setHeight(double height) {
   if(height>0)
   this.height = height;
   else
       this.height=1.0;
}
@Override
public int compareTo(Object o) {
   int val = 0;
   double originalVol = getVolume();

   Box c = (Box) o;
   double vol = c.getVolume();

   if (originalVol > vol)
       val = 1;
   else if (originalVol < vol)
       val = -1;
   else if (originalVol == vol)
       val = 0;
   return val;
}

//this method will calculate the volume of the box
public double getVolume()
{
   return length*width*height;
}


//toString method is used to display the contents of an object inside it
@Override
public String toString() {
   //DecimalFormat class is used to format the output
           DecimalFormat df=new DecimalFormat("#.##");
   return "Box Name="+name+" Volume ="+df.format(getVolume());
}


}

______________________

BoxSorter.java

import java.util.Arrays;
import java.util.Scanner;

public class BoxSorter {

   public static void main(String[] args) {
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Declaring variables
       double len,width,height;
       String name;
      
       //creating an array of Box class
   Box box[]=new Box[5];
     
   //getting te values entered by the user
   for(int i=0;i<box.length;i++)
   {
       System.out.print(" Enter the name of the box "+(i+1)+":");
       name=sc.next();
System.out.print("Enter the length of the box "+(i+1)+":");
len=sc.nextDouble();
System.out.print("Enter the width of the box "+(i+1)+":");
width=sc.nextDouble();
System.out.print("Enter the height of the box "+(i+1)+":");
height=sc.nextDouble();
box[i]=new Box(name, len, width, height);
   }
     
   //Displaying before sorting
   System.out.println("** Before Sorting **");
       for(Box b: box){
           System.out.println(b.toString());
           }
      
       //performing sorting operation
   Arrays.sort(box);

   //Displaying after sorting
   System.out.println("** After Sorting Based on Volume **");
       for(Box b: box){
           System.out.println(b.toString());
           }
     


   }

}

____________________

Output:


Enter the name of the box 1:A
Enter the length of the box 1:1.1
Enter the width of the box 1:1.2
Enter the height of the box 1:1.4

Enter the name of the box 2:B
Enter the length of the box 2:2.1
Enter the width of the box 2:2.2
Enter the height of the box 2:2.3

Enter the name of the box 3:C
Enter the length of the box 3:4.4
Enter the width of the box 3:4.4
Enter the height of the box 3:4.4

Enter the name of the box 4:D
Enter the length of the box 4:3.3
Enter the width of the box 4:3.3
Enter the height of the box 4:3.3

Enter the name of the box 5:E
Enter the length of the box 5:5.5
Enter the width of the box 5:5.1
Enter the height of the box 5:5.6
** Before Sorting **
Box Name=A Volume =1.85
Box Name=B Volume =10.63
Box Name=C Volume =85.18
Box Name=D Volume =35.94
Box Name=E Volume =157.08
** After Sorting Based on Volume **
Box Name=A Volume =1.85
Box Name=B Volume =10.63
Box Name=D Volume =35.94
Box Name=C Volume =85.18
Box Name=E Volume =157.08

_____________Thank You

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