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

Write a complete Class for an object called a Box. The Box should have three int

ID: 3732954 • Letter: W

Question

Write a complete Class for an object called a Box. The Box should have three integer instance variables, one for the length, one for the width and one for the height of the Box. Provide two constructors: one with no parameters that creates a box with a length, width and height of one, and a three-argument constructor that creates a Box with the given length, width and height. Error checking should be done to ensure none of the parameters is less than one. Include get and set methods for each of the instance variables, again with appropriate error detection. Provide a public, no- parameter method called volume that will return the volume of the Box (i.e., length times width times height). Override the methods equals and toString that are inherited from class Object. A call to the toString method should result in a String as follows (given the input line cited below): L:12 W:4 H:15 (V: 720) Write a main program that will read Box information from a file and store the Boxes in an array. The format of the input file will be three integers per line separated by commas, representing the length, width and height of the Box: 12,4,15 Write a class called BoxGUl that extends JFrame. The BoxGUI should contain a grid layout with one row and two columns. Display the original array in the left column, and then sort the array (by volume) and display it in the right column. Use Selection Sort as shown in class to sort the array. The display of the Boxes should be done using the toString method of class Box.

Explanation / Answer

here is your file : ---------->>>>>>>>>

Box.java : -------->>>>>>

public class Box implements Comparable<Box>{
private int width;
private int height;
private int length;

public Box(){
  width = height = length = 1;
}
public Box(int w,int h,int l){
  setWidth(w);
  setHeight(h);
  setLength(l);
}

public void setLength(int l){
  if(l >= 1){
   length = l;
  }
}
public void setHeight(int h){
  if(h >= 1){
   height = h;
  }

}
public void setWidth(int w){
  if(w >= 1){
   width = w;
  }
}

public int getHeight(){
  return height;
}
public int getLength(){
  return length;
}
public int getWidth(){
  return width;
}

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

public boolean equals(Object obj){
  if(obj instanceof Box){
   if(toString().equals(((Box)obj).toString())){
    return true;
   }
   return false;
  }
  return false;
}

public int compareTo(Box b1){
  if(volume() < b1.volume()){
   return -1;
  }
  if(volume() > b1.volume()){
   return 1;
  }

  return 0;
}

public String toString(){
  return "L:"+length+" W:"+width+" H:"+height+" (V: "+volume()+")";
}
}

BoxGUI.java : -------->>>>>>>>>>

import java.awt.*;
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;


public class BoxGUI extends JFrame{
private ArrayList<Box> boxes;
private String sortedBox = "";
private String unSortedBox = "";
public BoxGUI(ArrayList<Box> boxes){
  super("BOX GUI");
  this.boxes = boxes;
  for(int i = 0;i<boxes.size();i++){
   unSortedBox = unSortedBox + boxes.get(i).toString();
  }
  sort();
  for(int i = 0;i<boxes.size();i++){
   sortedBox = sortedBox + boxes.get(i).toString();
  }
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setSize(600,600);
  setResizable(true);
  setLayout(new GridLayout(1,2));
  JLabel s1 = new JLabel(unSortedBox);
  JLabel s2 = new JLabel(sortedBox);
  add(s1);
  add(s2);
  setVisible(true);
}

public void sort(){
  boxes.sort(null);
}
public static void main(String[] args) {
  ArrayList<Box> boxes = new ArrayList<>();
  Box temp = null;
  String file;
  Scanner sc_con = new Scanner(System.in);
  System.out.println("Enter The File Name : ");
  file = sc_con.next();
  try{
   Scanner sc = new Scanner(new File(file));
   String line;
   while(sc.hasNextLine()){
    line = sc.nextLine();
    String[] arr = line.split(",");
    temp = new Box(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),Integer.parseInt(arr[2]));
    boxes.add(temp);
   }
  }catch(Exception e){
   e.printStackTrace();
  }

  BoxGUI gui = new BoxGUI(boxes);
}
}

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