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

(Java language) Do linked list by hand CS 2401 Assignment #4 Due Date: Friday, O

ID: 3586638 • Letter: #

Question

(Java language) Do linked list by hand CS 2401 Assignment #4 Due Date: Friday, October 06, 11:59PM (See the syllabus for late policy) Objective: The goal of this assignment is to practice linked list of objects. Background: El Paso Packaging and Supply Co. comes back to you. They need your help to convert the array-based packaging to a linked-list based packaging. Additionally. the client would like to change the name of the class Package to Box. From Lab Assignment 3. you know that a sample 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 3, you could read the input file twice. For Lab assignment 4 you can read the input file exactly once. Yes, sometimes clients can be very difficult.& The client is NOT allowing the use of any array to store Box objects, or java.util lists for this revised software. Assignment: You need to write a program using object oriented programming idea for boxes. That is, each box has to be considered an object. To achieve this, you must write a class named Box. As you already know, this client likes to keep each class in a separate Java file. Therefore, make sure to create a Java file for the Box class. Some other required properties of the Box class are as follows. 1. You are allowed to keep only one of the status variables 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. The program file (the Java file that contains the main method) must be written in Runner.java. Runner must have the following functionalities. Each functionality must be implemented in a separate method in Runner.

Explanation / Answer

//Box.java file

public class Box {

public Box next;

private float width;

private float height;

private float length;

public Box(){

width=0f;

height=0f;

length=0f;

}

public Box(float wdt,float hgt,float len){

width=wdt;

height=hgt;

length=len;

}

public float getVolume(){

float volume=0f;

volume=width*height*length;

return volume;

}

public boolean isCube(){

if(width==height && width==length){

return true;

}

return false;

}

public void setNextBox(Box b){

next=b;

}

public Box getNextBox(){

return next;

}

public float[] getDimens(){

float[] dimens=new float[3];

dimens[0]=width;

dimens[1]=height;

dimens[2]=length;

return dimens;

}

} //end of Box.java

//Runner.java file

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

public class Runner {

Box boxes;

public static void main(String args[]){

String line;

Runner object=new Runner();

try {

FileReader fileReader=new FileReader(new File("G://Workspace/samplefile")); //sampile file path

BufferedReader bufferedReader=new BufferedReader(fileReader);

while((line=bufferedReader.readLine())!=null){

String[] array=line.split(" ");

object.insertBox(Float.parseFloat(array[0]), Float.parseFloat(array[1]), Float.parseFloat(array[2]));

}

//object.displayBoxes();

object.findSmallestBox();

object.findLargestBox();

object.countCubicBoxes();

object.findSmallestCubicBox();

object.findAverageVolume();

object.findAverageVolumeofCubicBoxes();

} catch (Exception e) {

e.printStackTrace();

}

}

public void insertBox(float width,float height,float length){

if(boxes==null){

boxes=new Box(width,height,length); //inserting first box to the linked list

}

else {

Box box=boxes.getNextBox();

if(box==null){

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

boxes.setNextBox(tmpBox); //adding as the second box

}

else{

while(true){

if(box.getNextBox()==null){ //looping until the last box is found

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

box.setNextBox(tmpBox); // added at the end of the list

break;  

}

box=box.getNextBox();

}

}

}

}

public void displayBoxes(){ //displaying boxes

if(boxes==null){

System.out.println("empty list");

}else{

System.out.print(boxes.getVolume()+" -> ");

Box box=boxes.getNextBox();

while(box!=null){

System.out.print(box.getVolume()+" -> ");

box=box.getNextBox();

}

System.out.println();

}

  

}

public void findSmallestBox(){ //finding smallest box

int i=0,position=0;

float minVolume;

float[] dimens;

if(boxes==null){

System.out.println("empty list");

}else{

minVolume=boxes.getVolume();

dimens=boxes.getDimens();

position=i;

Box box=boxes.getNextBox();

while(box!=null){

i++;

float tmp=box.getVolume();

if(tmp<minVolume){

minVolume=tmp;

dimens=box.getDimens();

position=i;

}

box=box.getNextBox();

}

System.out.println("Smallest box is of volume : "+minVolume);

System.out.println("Width: "+dimens[0]+" Height: "+dimens[1]+" Length: "+dimens[2]);

System.out.println("Position: "+position);

}

}

public void findLargestBox(){ //finding largest box

int i=0,position=0;

float maxVolume;

float[] dimens;

if(boxes==null){

System.out.println("empty list");

}else{

maxVolume=boxes.getVolume();

dimens=boxes.getDimens();

position=i;

Box box=boxes.getNextBox();

while(box!=null){

i++;

float tmp=box.getVolume();

if(tmp>maxVolume){

maxVolume=tmp;

dimens=box.getDimens();

position=i;

}

box=box.getNextBox();

}

System.out.println("Largest box is of volume : "+maxVolume);

System.out.println("Width: "+dimens[0]+" Height: "+dimens[1]+" Length: "+dimens[2]);

System.out.println("Position: "+position);

}

}

public void countCubicBoxes(){ //count the number of cubic boxes

int count=0;

if(boxes==null){

System.out.println("empty list");

}else{

if(boxes.isCube()){

count++;

}

Box box=boxes.getNextBox();

while(box!=null){

if(box.isCube()){

count++;

}

box=box.getNextBox();

}

System.out.println("There are "+count+" cubic box/boxes");

}

}

public void findSmallestCubicBox(){ //finding the smallest cubic box

int count=0;

float volume=-1f;

float[] dimens = new float[3];

int position=0,i=0;

if(boxes==null){

System.out.println("empty list");

}else{

if(boxes.isCube()){

count++;

volume=boxes.getVolume();

dimens=boxes.getDimens();

position=i;

}

Box box=boxes.getNextBox();

while(box!=null){

i++;

if(box.isCube()){

count++;

float tmp=box.getVolume();

if(tmp<volume || volume==-1f){

volume=tmp;

dimens=box.getDimens();

position=i;

}

}

box=box.getNextBox();

}

if(count>0){

System.out.println("Smallest cubic box has volume : "+volume);

System.out.println("Width: "+dimens[0]+" Height: "+dimens[1]+" Length: "+dimens[2]);

System.out.println("Position: "+position);

}else {

System.out.println("There are no cubic boxes");

}

}

}

public void findAverageVolume(){ //finding the average volume

int i=1;

float avg=0,total=0;

if(boxes==null){

System.out.println("empty list");

}else {

total=total+boxes.getVolume();

Box box=boxes.getNextBox();

while(box!=null){

total+=box.getVolume();

box=box.getNextBox();

i++;

}

avg=(float)total/i;

System.out.println("Average Volume: "+avg);

}

}

public void findAverageVolumeofCubicBoxes(){ //finding the average volume of cubic boxes

int i=1;

float avg=0,total=0;

if(boxes==null){

System.out.println("empty list");

}else {

if(boxes.isCube()){

total=total+boxes.getVolume();

}

Box box=boxes.getNextBox();

while(box!=null){

if(box.isCube()){

total+=box.getVolume();

i++;

}

box=box.getNextBox();

}

avg=(float)total/i;

System.out.println("Average Volume of Cubic boxes: "+avg);

}

}

} //end of Runner.java

//Samplefile

5 5 5
10 1 6
2.5 2 2
3 3 3
1 2 3
5 5 6