Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list

I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program.

Runner class:

import java.util.Scanner;

import java.io.*;

class Runner {

public static Package[] readFile() {

try {

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

Scanner sc = new Scanner(f);

double l, h, w;

int count = 0;

while(sc.hasNext()){

l = sc.nextDouble();

h = sc.nextDouble();

w = sc.nextDouble();

sc.nextLine();

count++;

}

}

}

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 {

node newN = null;

node head = null;

node current = null;

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

}

}

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

Package Class:

import java.util.*;

import java.lang.*;

import java.io.*;

class Package{

private double l, h, w;

public Box (double l, double h, double w, int ) {

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

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago