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

THE PROMPT IN THE PICTURE NEEDS TO BE COMPLETED. I am attatching the code for Pr

ID: 3710361 • Letter: T

Question

THE PROMPT IN THE PICTURE NEEDS TO BE COMPLETED. I am attatching the code for Project 2 that is correct IT DOES RUN and needs to be modiefied in order to suit the criteria in the picture above. PLEASE HELP! This is due soon!!

//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

1. Creating a new Exception

To create a new Exception of type Illegalbox Exception, you need to create your own class named after the required exception and extend the Exceptions class.

Optionally, you can add your own Constructor with whatever may be the custom message that you want to pass on

//IllegalBoxException.java

public class IllegalBoxException extends Exception {

public IllegalBoxException (String msg) {

super(msg);

}

}

To use this exception in the main file - Project2.java, you need to throw IllegalBoxException at the appropriate exception point. In our case that point occours when the height, width or lenght becomes less than 1. We are reading these values from a file name boxes.txt. These values are comma seperated and that's where we have check for the values to be larger than 1.

The modified Project2.java will be

//Project2

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Project2 {

public static void main(String[] args) throws IllegalBoxException {

/**

* 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

System.out.println("Reading file");

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());

//check for exception here.

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

//throw the exception

throw new IllegalBoxException("Illegal Box Exception: <enter your msg here>");

}

/**

* Defining a box object

*/

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

/**

* Adding to both lists

*/


}

} catch(Exception exception) {

}

}

}