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: 3585849 • 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? The assignment requires that I not use arrays at all so I can't use an array to store Box objects, use or java.util lists.

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

Please find my implementation using LinkedList.

import java.util.*;

import java.io.*;

public class Package{

   private double l, h, w;

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

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

   }

   public static LinkedList<Package> readFile() {

       LinkedList<Package> list = 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++;

           }

           list = new LinkedList<>();

           for(int i = 0; i < count; i++){

               l = sc.nextDouble();

               h = sc.nextDouble();

               w = sc.nextDouble();

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

               list.add(p);

           }

           sc.close();

       }

       catch (Exception e){

           e.printStackTrace();

       }

       return list;

   }

   public static void getLargestPack(LinkedList<Package> list) {

       //finding largest package

       Package maxp = list.get(0);

       int maxpind = 0;

       for(int i = 1; i < list.size(); i++){

           if(list.get(i).getVolume() > maxp.getVolume()) {

               maxp = list.get(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 {

       LinkedList<Package> list = readFile();

       getLargestPack(list);

       //cubic and non cubic packages

       int c = 0, nc = 0;

       for(int i = 0; i < list.size(); i++){

           if(list.get(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 < list.size(); i++){

           if(list.get(i).isCube()){

               list.get(i).printDimensions();

               sum_vol += list.get(i).getVolume();

           }

       }

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

   }

}

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