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

I HAVE ASKED THIS QUESTION TWICE AND NO ONE HAS ANSWERED IT COMPLETELY. PLEASE A

ID: 3711447 • Letter: I

Question

I HAVE ASKED THIS QUESTION TWICE AND NO ONE HAS ANSWERED IT COMPLETELY. PLEASE ATTATCH THE EXACT CODE THAT IS NEEDED TO COMPLETE THIS PROJECT AND IS READY FOR ME TO SUBMIT. I want all the parts done including the searching for a box part as well, I believe that is to be done in the BOXGUI. I am attatching the code for project 2, IT RUNS TEST IT OUT. PLEASE MODIFY THE CODE AS NEEDED AND ATTATCH SOMETHING THAT WORKS!

//Project2

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project2 {

public static void main(String[] args) {

/**

* Defining sorted and unsorted box lists

*/

SortedBoxList sortedBoxList = new SortedBoxList();

UnsortedBoxList unsortedBoxList = new UnsortedBoxList();

File file = new File("boxes.txt");

/**

* Reading from data file and filling the array

*/

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNext()) {

// getting a line

String line = scanner.nextLine();

// splitting the line by comma

String fields[] = line.split(",");

int length = Integer.parseInt(fields[0].trim());

int width = Integer.parseInt(fields[1].trim());

int height = Integer.parseInt(fields[2].trim());

/**

* Defining a box object

*/

Box box = new Box(length, width, height);

/**

* Adding to both lists

*/

//Box

import java.io.File;

import java.io.FileNotFoundException;

public class Box{

private int length;

private int width;

private int height;

public Box() {

length = 1;

width = 1;

height = 1;

}

public Box(int length, int width, int height) {

if(length < 1 || width < 1 || height < 1) {

throw new IllegalArgumentException("");

/**

* if any values are less than 1, making it 1

*/

}

this.length = length;

this.width = width;

this.height = height;

/**

* Assigning validated values to the variables

*/

}

/**

* Getters and setters

*/

public int getLength() {

return length;

}

public void setLength(int length) {

if(length<1)

throw new IllegalArgumentException("Illegal length!");

this.length = length;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

if(width<1)

throw new IllegalArgumentException("Illegal width!");

  

this.width = width;

}

public int getHeight() {

return height;

}

public void setHeight(int height) {

if(height<1)

throw new IllegalArgumentException("Illegal height!");

this.height = height;

}

public int volume() {

return this.length * this.width *

this.height;

/**

* method to find and return the volume of the box

*/

}// this is the getters and setters part

public boolean equals (Box obj) {

if (obj.length == length && obj.width == width && obj.height == height && obj.volume()==volume()) {

return true;

}

/**

* returning true only if length, width and heights of two boxes

* are same

*/

return false;

}

public String toString() {

return ("L:" + length + "W:" + width + " H:" + height + "(V: " + volume() + ")");

}

//this string is what prints out the length width and height as well as th

public static boolean check(int value) {

return (value >= 1);

}

}

//BoxGUI

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class BoxGUI extends JFrame {

/**

* Constructor which takes an array of Box objects as parameter

*/

public BoxGUI(SortedBoxList sortedBoxList, UnsortedBoxList unsortedBoxList) {

setDefaultCloseOperation(EXIT_ON_CLOSE);

/**

* using GridLayout with 1 row and 2 columns, with 10 spaces gap between

* each cells horizontally and vertically

*/

setLayout(new GridLayout(1, 2, 10, 10));

/**

* Defining a Text area for displaying unsorted list

*/

JTextArea left = new JTextArea();

left.setEditable(false);// making it not editable (by user)

String data = "UNSORTED LIST OF BOXES ";

data+=unsortedBoxList.toString();

/**

* Setting text for unsorted list

*/

left.setText(data);

/**

* Adding the text area

*/

add(left);

/**

* Defining a text area for displaying sorted list, and adding the

* details of each boxes

*/

JTextArea right = new JTextArea();

right.setEditable(false);

data = "SORTED LIST OF BOXES ";

data+=sortedBoxList.toString();

right.setText(data);

add(right);

setVisible(true);

pack();/* adjusting the height and width of the window to fit perfectly */

}

}

//UnsortedBoxList

public class UnsortedBoxList extends BoxList {

public UnsortedBoxList() {

super();

}

/**

* method to add a box at the end of the list

*/

public void add(Box b){

super.append(b);

}

}

//SortedBoxList

public class SortedBoxList extends BoxList {

public SortedBoxList() {

super();

}

/**

* method to add a box to the list in proper position

*/

public void add(Box b) {

if (head == null) {

// first element

super.append(b);

} else {

// defining a node

BoxNode node = new BoxNode(b);

// here b1 represent current node and b2 represent next node (in

// each loop)

BoxNode b1 = head, b2 = head.next;

/**

* looping through all nodes, to find the proper position

*/

while (b1 != null) {

if (b2 == null) {

if (b1 == head) {

/**

* checking if the head box has more volume than the box

* to be added

*/

if (b.volume() < head.box.volume()) {

/**

* making as the head node

*/

node.next = head;

head = node;

break;

}

}

// adding as the next node, as the next node is null

b1.next = node;

break;

}

/**

* checking if the box should fit between current and next box

* (volume greater than current and less than next)

*/

if (b.volume() >= b1.box.volume()

&& b.volume() <= b2.box.volume()) {

/**

* Adding in the middle of the two

*/

node.next = b2;

b1.next = node;

break;

}

/**

* moving to the next nodes

*/

b1 = b2;

b2 = b1.next;

}

}

}

}

//BoxNode

public class BoxNode {

//attributes

protected Box box;

protected BoxNode next;

//one arg constructor

public BoxNode(Box box) {

this.box = box;

}

}

//BoxList

public abstract class BoxList{

//attributes

protected BoxNode head;

protected BoxNode last;

protected int length;

//default constructor

public BoxList() {

head=null;

last=null;

length=0;

}

//method to add a box at the end

public void append(Box box){

BoxNode node=new BoxNode(box);

if(head==null){

head=node;

last=node;

}else{

last.next=node;

last=node;

}

length++;

}

@Override

public String toString() {

/**

* returns the list as a String

*/

String data = "";

BoxNode node = head;

while (node != null) {

data += node.box.toString() + " ";

node = node.next;

}

return data;

}

}

Handling Errors Create a new Exception class called IllegalBoxException. This Exception should be thrown by the constructor of the Box class in case the length, width or height is less than 1 Modify project 2 by including a try/catch block where a new Box is instantiated. It should catch the IllegalBoxException and print an error message to the console. A data file will be provided on Blackboard that contains errors. Searching for a Box Include a second menu in the project called Tools. It should have one menu item called Search. boxes should be searched for any box that will fit (that is, it is at least as big as the box the user entered.) Display the boxes found in a new JFrame. Submitting the Project. Submit a jar file with all the necessary Java files for the project through Blackboard.

Explanation / Answer

here is your completed files : ------------>>>>>>>>>>>>

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project2 {
public static void main(String[] args) {
  /**
  * Defining sorted and unsorted box lists
  */
  SortedBoxList sortedBoxList = new SortedBoxList();
  UnsortedBoxList unsortedBoxList = new UnsortedBoxList();
  File file = new File("boxes.txt");
  /**
  * Reading from data file and filling the array
  */
   try {
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
     // getting a line
     String line = scanner.nextLine();
     // splitting the line by comma
     String fields[] = line.split(",");
     int length = Integer.parseInt(fields[0].trim());
     int width = Integer.parseInt(fields[1].trim());
     int height = Integer.parseInt(fields[2].trim());
     /**
     * Defining a box object
     */
     try{
      Box box = new Box(length, width, height);
      sortedBoxList.add(box);
      unsortedBoxList.add(box);
      /**
      * Adding to both lists
      */
     }catch(IllegalBoxException e){
      e.printStackTrace();
     }
    }
   }catch(Exception e){
    e.printStackTrace();
   }
  BoxGUI bg = new BoxGUI(sortedBoxList,unsortedBoxList);
}
}

public class IllegalBoxException extends Exception{
public IllegalBoxException(String msg){
  super(msg);
}
}

public class BoxNode {
//attributes
protected Box box;
protected BoxNode next;
//one arg constructor
public BoxNode(Box box) {
this.box = box;
}
public BoxNode getNext(){
return next;
}
public Box getData(){
return box;
}
}

public abstract class BoxList{
//attributes
protected BoxNode head;
protected BoxNode last;
protected int length;
//default constructor
public BoxList() {
head=null;
last=null;
length=0;
}
public Box get(int i){
if(i < 0 || i >= length){
  return null;
}
BoxNode cur = head;
for(int j = 1;j<=i;j++){
  cur = cur.getNext();
}

return cur.getData();
}
public int size(){
return length;
}
//method to add a box at the end
public void append(Box box){
BoxNode node=new BoxNode(box);
if(head==null){
head=node;
last=node;
}else{
last.next=node;
last=node;
}
length++;
}
@Override
public String toString() {
/**
* returns the list as a String
*/
String data = "";
BoxNode node = head;
while (node != null) {
data += node.getData().toString() + " ";
node = node.getNext();
}
return data;
}
}

public class SortedBoxList extends BoxList {
public SortedBoxList() {
super();
}
/**
* method to add a box to the list in proper position
*/
public void add(Box b) {
if (head == null) {
// first element
super.append(b);
} else {
// defining a node
BoxNode node = new BoxNode(b);
// here b1 represent current node and b2 represent next node (in
// each loop)
BoxNode b1 = head, b2 = head.next;
/**
* looping through all nodes, to find the proper position
*/
length++;
while (b1 != null) {
if (b2 == null) {
if (b1 == head) {
/**
* checking if the head box has more volume than the box
* to be added
*/
if (b.volume() < head.box.volume()) {
/**
* making as the head node
*/
node.next = head;
head = node;
break;
}
}

// adding as the next node, as the next node is null
b1.next = node;
break;
}
/**
* checking if the box should fit between current and next box
* (volume greater than current and less than next)
*/
if (b.volume() >= b1.box.volume()
&& b.volume() <= b2.box.volume()) {
/**
* Adding in the middle of the two
*/
node.next = b2;
b1.next = node;
break;
}
/**
* moving to the next nodes
*/
b1 = b2;
b2 = b1.next;
}
}
}
}

public class UnsortedBoxList extends BoxList {
public UnsortedBoxList() {
super();
}
/**
* method to add a box at the end of the list
*/
public void add(Box b){
super.append(b);
}
}

import java.io.File;
import java.io.FileNotFoundException;
public class Box implements Comparable<Box>{
private int length;
private int width;
private int height;
public Box(){
length = 1;
width = 1;
height = 1;
}
public Box(int length, int width, int height)throws IllegalBoxException{
if(length < 1 || width < 1 || height < 1) {
throw new IllegalBoxException("Argument pass is not corrrect");
/**
* if any values are less than 1, making it 1
*/
}
this.length = length;
this.width = width;
this.height = height;
/**
* Assigning validated values to the variables
*/
}
/**
* Getters and setters
*/
public int getLength() {
return length;
}
public void setLength(int length)throws IllegalBoxException{
if(length<1)
throw new IllegalBoxException("Illegal length!");
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width)throws IllegalBoxException{
if(width<1)
throw new IllegalBoxException("Illegal width!");
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height)throws IllegalBoxException{
if(height<1)
throw new IllegalBoxException("Illegal height!");
this.height = height;
}
public int volume() {
return this.length * this.width * this.height;
/**
* method to find and return the volume of the box
*/
}// this is the getters and setters part
public boolean equals (Box obj) {
if (obj.length == length && obj.width == width && obj.height == height && obj.volume()==volume()) {
return true;
}
/**
* returning true only if length, width and heights of two boxes
* are same
*/
return false;
}
public String toString() {
return ("L:" + length + "W:" + width + " H:" + height + "(V: " + volume() + ")");
}
public int compareTo(Box b){
if(volume() < b.volume()){
  return -1;
}
if(volume() > b.volume()){
  return 1;
}
return 0;
}
//this string is what prints out the length width and height as well as th
public static boolean check(int value) {
return (value >= 1);
}
}

import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTextArea;
public class BoxGUI extends JFrame {
/**
* Constructor which takes an array of Box objects as parameter
*/
SortedBoxList boxlist;
Box searchBox;
public BoxGUI(SortedBoxList sortedBoxList, UnsortedBoxList unsortedBoxList){
setDefaultCloseOperation(EXIT_ON_CLOSE);
/**
* using GridLayout with 1 row and 2 columns, with 10 spaces gap between
* each cells horizontally and vertically
*/
setLayout(new GridLayout(1, 2, 10, 10));
/**
* Defining a Text area for displaying unsorted list
*/
boxlist = sortedBoxList;
JTextArea left = new JTextArea();
left.setEditable(false);// making it not editable (by user)
String data = "UNSORTED LIST OF BOXES ";
data+=unsortedBoxList.toString();
/**
* Setting text for unsorted list
*/
left.setText(data);
/**
* Adding the text area
*/
add(left);
/**
* Defining a text area for displaying sorted list, and adding the
* details of each boxes
*/
JTextArea right = new JTextArea();
right.setEditable(false);
data = "SORTED LIST OF BOXES ";
data+=sortedBoxList.toString();
right.setText(data);
add(right);
setVisible(true);
pack();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem item = new JMenuItem("Tools");
item.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        JFrame j = new JFrame("Search");
        j.setSize(400,500);
        j.setResizable(false);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildGUI(j);
        j.setVisible(true);
    }
});
menu.add(item);
setJMenuBar(menuBar);
/* adjusting the height and width of the window to fit perfectly */
}

public void buildGUI(JFrame j){
JTextField wid = new JTextField(5);
JTextField hig = new JTextField(5);
JTextField len = new JTextField(5);
JTextArea boxes = new JTextArea();
j.setLayout(new FlowLayout());
j.add(new JLabel("Width : "));
j.add(wid);
j.add(new JLabel("Height : "));
j.add(hig);
j.add(new JLabel("Length : "));
j.add(len);
JButton srch = new JButton("Search");
srch.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
      // display/center the jdialog when the button is pressed
      int l = Integer.parseInt(len.getText());
      int w = Integer.parseInt(wid.getText());
      int h = Integer.parseInt(hig.getText());
      try{
      searchBox = new Box(l,w,h);
      }catch(IllegalBoxException ex){
      ex.printStackTrace();
      j.setVisible(false);
      }
      SortedBoxList lists = new SortedBoxList();
      for(int i = 0;i<boxlist.size();i++){
      if(boxlist.get(i).compareTo(searchBox) <= 0){
        lists.add(boxlist.get(i));
      }
      }
      String data = "SORTED LIST OF BOXES ";
      data += lists.toString();
      boxes.setText(data);
    }
  });
j.add(new JLabel("   "));
j.add(srch);
j.add(boxes);
}
}