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

I\'ve written a code that reads a file and then takes it as input to create box

ID: 3585691 • Letter: I

Question

I've written a code that reads a file and then takes it as input to create box objects then creates an array of box objects. How would I begin to store the objects in a Linked List instead of an array? Or is there a way to convert the arrays to a linked list instead?

Package class:

import java.util.*;

import java.lang.*;

import java.io.*;

class Package{

private double l, h, w;

public Package(double l, double h, double w, Node next){

this.l = l;

this.h = h;

this.w = w;

}

public boolean isCube() {

return l == h && h == w;

}

public double getVolume() {

return l * w * h;

}

public void printDimensions() {

System.out.println("Dimensions in length, height, width: " + l + ", " + h + ", " + w);

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Package class:

public static Package[] readFile() {

Package array[] = null;

try {

File f = new File("input.txt");

Scanner sc = new Scanner(f);

double l, h, w;

int count = 0;

while(sc.hasNext()){

sc.nextLine();

count++;

}

array = new Package[count];

sc = new Scanner(f);

for(int i = 0; i < array.length; i++){

l = sc.nextDouble();

h = sc.nextDouble();

w = sc.nextDouble();

Package p = new Package(l, h, w);

array[i] = p;

}

}

catch (Exception e){

e.printStackTrace();

}

return array;

}

public static void getLargestPack(Package[] array) {

//finding largest package

Package maxp = array[0];

int maxpind = 0;

for(int i = 1; i < array.length; i++){

if(array[i].getVolume() > maxp.getVolume()) {

maxp = array[i];

maxpind = i;

}

}

System.out.println("index of largest package is " + maxpind);

maxp.printDimensions();

System.out.println("Volume: "+maxp.getVolume());

}

public static void main (String[] args) throws java.lang.Exception {

Package[] array = readFile();

getLargestPack(array);

LinkedList = new LinkedList(

//cubic and non cubic packages

int c = 0, nc = 0;

for(int i = 0; i < array.length; i++){

if(array[i].isCube()) c++;

else nc++;

}

System.out.println("number of cubic packages are: " + c);

System.out.println("number of non cubic packages are: " + nc);

//details of cubic packages

float sum_vol = 0;

for(int i = 0; i < array.length; i++){

if(array[i].isCube()){

array[i].printDimensions();

sum_vol += array[i].getVolume();

}

}

System.out.println("average volume is: " + sum_vol/c);

}

}

Explanation / Answer

first create array of packgae class in the method readffile and change the return type of the method to LinkedList<Package> and in the last line of the function instead of returnng the array return the Linkedlist

using return new LinkedList(Arrays.asList(array));

and remember to import for Linkedlist and Arrays

public static LinkedList<Package> readFile() {
Package array[] = null;
try {
File f = new File("input.txt");
Scanner sc = new Scanner(f);
double l, h, w;
int count = 0;
while(sc.hasNext()){
sc.nextLine();
count++;
}
array = new Package[count];
sc = new Scanner(f);
for(int i = 0; i < array.length; i++){
l = sc.nextDouble();
h = sc.nextDouble();
w = sc.nextDouble();
Package p = new Package(l, h, w);
array[i] = p;
}
}
catch (Exception e){
e.printStackTrace();
}
return new LinkedList(Arrays.asList(array));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote