Question
Use java language Below are my codes Box.java (Runner.java) class BoxNode{ private Box b; private BoxNode next; public boolean isCubic; public double volume; public int
Use java language
Below are my codes
Box.java
(Runner.java)
class BoxNode{
private Box b;
private BoxNode next;
public boolean isCubic;
public double volume;
public int position;
public BoxNode(Box node) {
this.b = node;
this.isCubic = node.isCubic();
this.volume = node.getVolume();
}
public void setData(Box data) {
this.b = data;
}
public Box getData() {
return this.b;
}
public void setNext(BoxNode node) {
this.next = node;
}
public BoxNode getNext() {
return this.next;
}
}
//Runner Class
public class Runner {
public BoxNode head=null;
//Adding boxes to the lists
public void addBoxToList(Box b) {
BoxNode bnode = new BoxNode(b);
bnode.setNext(head);
head = bnode;
}
//Counting of Cubic Boxes
public int countCubicBoxes() {
BoxNode temp = head;
int count = 0;
if(head == null) {
System.out.println("List does not contain any box");
return 0;
}
while(temp != null) {
if(temp.isCubic) {
count++;
}
temp = temp.getNext();
}
return count;
}
//Getting the Smallest Boxes
public BoxNode getSmallestBox(){
if(head == null) {
System.out.println("List does not contain any box");
return null;
}
BoxNode retBox = head, temp = head.getNext();
double volume = head.volume;
int count = 1, pos = 1;
while(temp!= null) {
count++;
if(temp.volume
volume = temp.volume;
retBox = temp;
pos = count;
}
temp = temp.getNext();
}
retBox.position = pos;
return retBox;
}
//getting the Largest Boxes
public BoxNode getLargestBox(){
if(head == null) {
System.out.println("List does not contain any box");
return null;
}
BoxNode retBox = head, temp = head.getNext();
double volume = head.volume;
int count = 1, pos = 1;
while(temp!= null) {
count++;
if(temp.volume > volume) {
volume = temp.volume;
retBox = temp;
pos = count;
}
temp = temp.getNext();
}
retBox.position = pos;
return retBox;
}
//Finding the Smallest Cubes
public BoxNode findSmallestCube() {
if(head == null){
System.out.println("List does not contain any box");
return null;
}
BoxNode ret = null, temp = head;
double volume = 0;
int count = 0, pos = 0;
while(temp != null) {
count++;
if(temp.isCubic) {
if(temp.volume
volume = temp.volume;
ret = temp;
pos = count;
}
}
temp = temp.getNext();
}
if(ret != null) {
ret.position = pos;
}
return ret;
}
//Finding the Largest
public BoxNode findLargestCube() {
if(head == null){
System.out.println("List does not contain any box");
return null;
}
BoxNode ret = null, temp = head;
double volume = 0;
int count = 0, pos = 0;
while(temp != null) {
count++;
if(temp.isCubic) {
if(temp.volume > volume) {
volume = temp.volume;
ret = temp;
pos = count;
}
}
temp = temp.getNext();
}
if(ret != null) {
ret.position = pos;
}
return ret;
}
//Finding the Average of Volume
public double avgVolume() {
if(head == null){
System.out.println("List does not contain any box");
return 0;
}
double sum = 0;
int count = 0;
BoxNode temp = head;
while(temp!= null){
sum = sum+temp.volume;
++count;
temp = temp.getNext();
}
return sum/count;
}
//Avg Volumes of the Cubic
public double avgCubeVolume() {
if(head == null){
System.out.println("List does not contain any box");
return 0;
}
double sum = 0;
int count = 0;
BoxNode temp = head;
while(temp!= null){
if(temp.isCubic) {
sum = sum + temp.volume;
++count;
}
temp = temp.getNext();
}
return (count>0)?sum/count : 0;
}
//Main Method
public static void main(String[] args) {
Runner blist = new Runner();
Box b1;
double length=0,width=0,height=0;
int numberOfCubes = 0, numberOfBoxes = 0;
//File Reader
try {
File file = new File("input.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
while (stringTokenizer.hasMoreElements()) {
width = Double.parseDouble(stringTokenizer.nextElement().toString());
length = Double.parseDouble(stringTokenizer.nextElement().toString());
height = Double.parseDouble(stringTokenizer.nextElement().toString());
b1 = new Box(width,length, height);
blist.addBoxToList(b1);
numberOfBoxes++;
}
}
fileReader.close();
}catch (IOException e) {
e.printStackTrace();
}
//Print out of everything
System.out.println("Number of Boxes: " + numberOfBoxes);
numberOfCubes = blist.countCubicBoxes();
System.out.println("Number of cubic boxes: " + numberOfCubes);
BoxNode ret = blist.getLargestBox();
if(ret != null) {
System.out.println("Largest Box is at position " + ret.position);
System.out.println("Width: " + ret.getData().getWidth() + " Length: " + ret.getData().getLength() + " Height: " + ret.getData().getHeight());
System.out.println("Volume: " + ret.volume);
}
BoxNode ret2 = blist.getSmallestBox();
if(ret2 != null) {
System.out.println("Smallest Box is at position: " + ret2.position);
System.out.println("Width: " + ret2.getData().getWidth() + " Length: " + ret2.getData().getLength() + " Height: " + ret2.getData().getHeight());
System.out.println("Volume: " + ret2.volume);
}
BoxNode ret3 = blist.findLargestCube();
if(ret3 != null){
System.out.println("Largest Cubic Box is at position " + ret3.position);
System.out.println("Width: " + ret3.getData().getWidth() + " Length: " + ret3.getData().getLength() + " Height: " + ret3.getData().getHeight());
System.out.println("Volume: " + ret3.volume);
}
BoxNode ret4 = blist.findSmallestCube();
if(ret4 != null){
System.out.println("Smallest Cubic Box is at position " + ret4.position);
System.out.println("Width: " + ret4.getData().getWidth() + " Length: " + ret4.getData().getLength() + " Height: " + ret4.getData().getHeight());
System.out.println("Volume: " + ret4.volume);
}
System.out.println("Average volume of all boxes: " + blist.avgVolume());
System.out.println("Average volume of cubes: " + blist.avgCubeVolume());
}
-----------------------------------------------------------------------------------------------------------------------------------------
(BoxNode)
class BoxNode{ private Box b; private BoxNode next; public boolean isCubic; public double volume; public int position; public BoxNode(Box node) { this.b = node; this.isCubic = node.isCubic(); this.volume = node.getVolume(); } public void setData(Box data) { this.b = data; } public Box getData() { return this.b; } public void setNext(BoxNode node) { this.next = node; } public BoxNode getNext() { return this.next; } } //Runner Class public class Runner { public BoxNode head=null; //Adding boxes to the lists public void addBoxToList(Box b) { BoxNode bnode = new BoxNode(b); bnode.setNext(head); head = bnode; } //Counting of Cubic Boxes public int countCubicBoxes() { BoxNode temp = head; int count = 0; if(head == null) { System.out.println("List does not contain any box"); return 0; } while(temp != null) { if(temp.isCubic) { count++; } temp = temp.getNext(); } return count; } //Getting the Smallest Boxes public BoxNode getSmallestBox(){ if(head == null) { System.out.println("List does not contain any box"); return null; } BoxNode retBox = head, temp = head.getNext(); double volume = head.volume; int count = 1, pos = 1; while(temp!= null) { count++; if(temp.volume out.println("List does not contain any box"); return null; } BoxNode retBox = head, temp = head.getNext(); double volume = head.volume; int count = 1, pos = 1; while(temp!= null) { count++; if(temp.volume > volume) { volume = temp.volume; retBox = temp; pos = count; } temp = temp.getNext(); } retBox.position = pos; return retBox; } //Finding the Smallest Cubes public BoxNode findSmallestCube() { if(head == null){ System.out.println("List does not contain any box"); return null; } BoxNode ret = null, temp = head; double volume = 0; int count = 0, pos = 0; while(temp != null) { count++; if(temp.isCubic) { if(temp.volume out.println("List does not contain any box"); return null; } BoxNode ret = null, temp = head; double volume = 0; int count = 0, pos = 0; while(temp != null) { count++; if(temp.isCubic) { if(temp.volume > volume) { volume = temp.volume; ret = temp; pos = count; } } temp = temp.getNext(); } if(ret != null) { ret.position = pos; } return ret; } //Finding the Average of Volume public double avgVolume() { if(head == null){ System.out.println("List does not contain any box"); return 0; } double sum = 0; int count = 0; BoxNode temp = head; while(temp!= null){ sum = sum+temp.volume; ++count; temp = temp.getNext(); } return sum/count; } //Avg Volumes of the Cubic public double avgCubeVolume() { if(head == null){ System.out.println("List does not contain any box"); return 0; } double sum = 0; int count = 0; BoxNode temp = head; while(temp!= null){ if(temp.isCubic) { sum = sum + temp.volume; ++count; } temp = temp.getNext(); } return (count>0)?sum/count : 0; } //Main Method public static void main(String[] args) { Runner blist = new Runner(); Box b1; double length=0,width=0,height=0; int numberOfCubes = 0, numberOfBoxes = 0; //File Reader try { File file = new File("input.txt"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); StringTokenizer stringTokenizer = new StringTokenizer(line, " "); while (stringTokenizer.hasMoreElements()) { width = Double.parseDouble(stringTokenizer.nextElement().toString()); length = Double.parseDouble(stringTokenizer.nextElement().toString()); height = Double.parseDouble(stringTokenizer.nextElement().toString()); b1 = new Box(width,length, height); blist.addBoxToList(b1); numberOfBoxes++; } } fileReader.close(); }catch (IOException e) { e.printStackTrace(); } //Print out of everything System.out.println("Number of Boxes: " + numberOfBoxes); numberOfCubes = blist.countCubicBoxes(); System.out.println("Number of cubic boxes: " + numberOfCubes); BoxNode ret = blist.getLargestBox(); if(ret != null) { System.out.println("Largest Box is at position " + ret.position); System.out.println("Width: " + ret.getData().getWidth() + " Length: " + ret.getData().getLength() + " Height: " + ret.getData().getHeight()); System.out.println("Volume: " + ret.volume); } BoxNode ret2 = blist.getSmallestBox(); if(ret2 != null) { System.out.println("Smallest Box is at position: " + ret2.position); System.out.println("Width: " + ret2.getData().getWidth() + " Length: " + ret2.getData().getLength() + " Height: " + ret2.getData().getHeight()); System.out.println("Volume: " + ret2.volume); } BoxNode ret3 = blist.findLargestCube(); if(ret3 != null){ System.out.println("Largest Cubic Box is at position " + ret3.position); System.out.println("Width: " + ret3.getData().getWidth() + " Length: " + ret3.getData().getLength() + " Height: " + ret3.getData().getHeight()); System.out.println("Volume: " + ret3.volume); } BoxNode ret4 = blist.findSmallestCube(); if(ret4 != null){ System.out.println("Smallest Cubic Box is at position " + ret4.position); System.out.println("Width: " + ret4.getData().getWidth() + " Length: " + ret4.getData().getLength() + " Height: " + ret4.getData().getHeight()); System.out.println("Volume: " + ret4.volume); } System.out.println("Average volume of all boxes: " + blist.avgVolume()); System.out.println("Average volume of cubes: " + blist.avgCubeVolume()); } }Background: El Paso Packaging and Supply Co. comes back to you (again). They are very happy with your linked list based program. They would like to add more linked list functionality in the program. As you know from Lab Assignment 4, an input file may look like the following one. 20 10 8 4.5 8.45 12.2 8.0 2.5 4.0 1.0 15.0 18.0 3.5 3.5 3.5 6.0 5.0 10.0 Each line contains the width, height and length of a box. The dimensions are separated by spaces. In Lab Assignment 4. you created your own linked list; you were not allowed to use any java.util lists for this revised software. The same conditions apply for this new assignment. Assignment: All the following conditions from the previous assignment are valid in this assignment. You will not make any change to the Box class. 1. You are allowed to keep only the next variable public. The rest of the status variables of the Box class must be private. 2. Write no more than two constructors. 3. The Box class must have a public method named getVolume () that will return the volume of the box. 4. The Box class must have a public method named iscube ) that will return true if the box is cubic, false otherwise. 5- The Box class must NOT contain any main method. Feel free to write any additional method in the Box class, as you see fit. In this assignment, you will write two additional java files LinkedList.java and Runner.java. Notice that in the previous assignment you coded all the linked list operations, including iteration of the linked list, in the Runner.java file. In the new assignment, you will write the basic linked list operations in the LinkedList.java file. You will use a LinkedList object in the Runner.java file as your linked list. The skeleton of LinkedList.java is provided below
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