Question
I HAVE POSTED THIS QUESTIONS TWICE BEOFRE AND IT IS NOT FULLY ANSWERED. I want the full answer to the question with code that actually
I HAVE POSTED THIS QUESTIONS TWICE BEOFRE AND IT IS NOT FULLY ANSWERED. I want the full answer to the question with code that actually works. I want the SEARCHING FOR A BOX part completed as well I believe it is done in the GUI. PLEASE solve this project in the picture with code that actually works!!!
I am attatching the code for project 2 that works. Run the code and test it out! I have no clue what you have to do with this project so PLEASE WRITE CODE THAT WORKS AND FILLS THE CRITERIA. (Please make a boxes.txt file and see if the code runs)
//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
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
throw new IllegalArgumentException("Illegal length!");
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
if(width
throw new IllegalArgumentException("Illegal width!");
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
if(height
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()
/**
* 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()
/**
* 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started