Question
// BoxGUI.java 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
// BoxGUI.java
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 */
}
}
// BoxList.java
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;
}
}
// BoxNode.java
public class BoxNode {
//attributes
protected Box box;
protected BoxNode next;
//one arg constructor
public BoxNode(Box box) {
this.box = box;
}
}
// SortedBoxList.java
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;
}
}
}
}
// UnsortedBoxList.java
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);
}
}
// Project2.java
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
*/
sortedBoxList.add(box);
unsortedBoxList.add(box);
}
/**
* passing both lists as parameters for the BoxGUI
*/
new BoxGUI(sortedBoxList, unsortedBoxList);
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (Exception e) {
System.out.println("Invalid file format!");
}
}
}
// Box.java
3 public class Box private int lengeh: private int width: private int height: //default constructor 9 public Box ) 10 length-1; width = 1; height 1: 12 13 14 15 16 public Box (int length, int width, int height) 17 18 19 20 21 //constructor with arguments if any values are less than 1, making it 1 if (length
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