Question

I have a program that reads a file and then creates objects from the contents of...

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

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the below code without using util pacakge.

Runner.class

package com.list;

import java.util.Scanner;

import java.io.*;

class Runner {

public static void readFile() {

try {

  

}

catch (Exception e){

e.printStackTrace();

}

//return array;

}

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

/*node newN = null;

node head = null;

node current = null;*/

LinkedList Llist = new LinkedList();

File f = new File("C:/Santhosh/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();

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

DataItems data = new DataItems(count,p);

Llist.insertNode(data);

//sc.nextLine();

count++;

}

Llist.displayList();

//readFile();

Llist.getLargestPack();

//LinkedList = new LinkedList(

Llist.cubic();

}

}

Package.java

package com.list;

import java.util.Scanner;

import java.io.*;

class Runner {

public static void readFile() {

try {

  

}

catch (Exception e){

e.printStackTrace();

}

//return array;

}

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

/*node newN = null;

node head = null;

node current = null;*/

LinkedList Llist = new LinkedList();

File f = new File("C:/Santhosh/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();

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

DataItems data = new DataItems(count,p);

Llist.insertNode(data);

//sc.nextLine();

count++;

}

Llist.displayList();

//readFile();

Llist.getLargestPack();

//LinkedList = new LinkedList(

Llist.cubic();

}

}

Node.java

package com.list;
public class Node{

// immutable class representing head node of linked list

private DataItems dataItems;
private Node nextNode;

public void setNextNode(Node _nextNode){
this.nextNode=_nextNode;
}

public Node getNextNode(){
return nextNode;
}

public DataItems getDataItems(){
return dataItems;
}

public void setDataItems(DataItems _dataItems){
this.dataItems=_dataItems;
}

}

HeadNode.java

package com.list;

public class HeadNode{

// immutable class representing head node of linked list

Node nextNode;

public void setNextNode(Node _nextNode) {

nextNode=_nextNode;

}

public Node getNextNode() {

return nextNode;

}

}

DataItems.java

package com.list;

public class DataItems{

private int key;
private Package p;

public DataItems(int _key, Package p){
this.key=_key;
this.p=p;
}

public int getKey() {
return key;
}

public Package getP() {
return p;
}

public String toString() {
return "("+getKey()+","+getP()+")";
}

}

LinkedList.java

package com.list;

public class LinkedList{

HeadNode head;

public LinkedList(){

head = new HeadNode();

}

// insert node at the beginning of the list

public void insertNode(DataItems _data){

Node newNode = new Node();

newNode.setDataItems(_data);

Node nextNode = head.getNextNode();

head.setNextNode(newNode);

newNode.setNextNode(nextNode);

}

// delete node at the beginning of the list

public void deleteNode(){

Node toBeDeletedNode = head.getNextNode();

if(toBeDeletedNode!=null) {

Node nextNode = toBeDeletedNode.getNextNode();

head.setNextNode(nextNode);

toBeDeletedNode.setNextNode(null);

} else {

System.out.println("No nodes to be deleted");

}

}

// display all nodes data

public void displayList(){

Node nodes = head.getNextNode();

int i=0;

while(nodes!=null) {

DataItems data = nodes.getDataItems();

System.out.println("Node "+i+" : "+data.toString());

nodes = nodes.getNextNode();

i++;

}

}

// reverse order of linked list

public void reverseLinkedList(){

int sizeOfList = size();

Node lastNode = nodeAtIndex(sizeOfList-1);

Node snode, tnode;

for(int i=sizeOfList-2;i>=0;i--){

snode = nodeAtIndex(i);

tnode = snode.getNextNode();

tnode.setNextNode(snode);

}

nodeAtIndex(0).setNextNode(null);

head.setNextNode(lastNode);

}

// reverse order of linked list

public void searchKey(int _key){

int i=0;

DataItems data = dataAtNodeIndex(i);

while(data!=null){

if(data.getKey()== _key){

System.out.println("Node at index : "+i+" has data item : "+data.toString());

}

i++;

data = dataAtNodeIndex(i);

}

}

// insert a node at index

public void insertNodeAtIndex(int _index, DataItems _data){

Node newNode = new Node();

newNode.setDataItems(_data);

if(_index==0) {

insertNode(_data);

} else {

Node prevNode = nodeAtIndex(_index-1);

if(prevNode!=null) {

Node nextNode = prevNode.getNextNode();

newNode.setNextNode(nextNode);

prevNode.setNextNode(newNode);

}

}

}

// delete a node at index

public void deleteNodeAtIndex(int _index){

if(_index==0) {

deleteNode();

} else {

Node prevNode = nodeAtIndex(_index-1);

if(prevNode!=null) {

Node targetNode = prevNode.getNextNode();

Node nextNode = targetNode.getNextNode();

targetNode.setNextNode(null);

prevNode.setNextNode(nextNode);

}

}

}

// return data item at particular node

public DataItems dataAtNodeIndex(int _index){

Node nodes = nodeAtIndex(_index);

if(nodes!=null) {

return nodes.getDataItems();

} else {

return null;

}

}

// return node at particular index

private Node nodeAtIndex(int _index){

if(_index<0) {

return null;

} else {

Node nodes = head.getNextNode();

int i=0;

while(i<_index && nodes!=null) {

nodes = nodes.getNextNode();

i++;

}

return nodes;

}

}

// return the size of linked list

public int size() {

int count=0;

Node nodes = nodeAtIndex(count);

while(nodes!=null) {

nodes = nodeAtIndex(++count);

}

return count;

}

public Package getData(){

Node nodes = head.getNextNode();

DataItems data = nodes.getDataItems();

return data.getP();

}

public void getLargestPack() {

//finding largest package

Node nodes = head.getNextNode();

DataItems data = nodes.getDataItems();

Package maxp = data.getP();

int maxpind = 0;

int i=0;

while(nodes!=null) {

data = nodes.getDataItems();

//System.out.println("Node "+i+" : "+data.toString());

Package p1 = data.getP();

if(p1.getVolume() > maxp.getVolume()) {

maxp = p1;

maxpind = i;

}

nodes = nodes.getNextNode();

i++;

}

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

maxp.printDimensions();

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

}

public void cubic(){

//cubic and non cubic packages

int c = 0, nc = 0;

int i=0;

Node nodes = head.getNextNode();

float sum_vol = 0;

while(nodes!=null) {

DataItems data = nodes.getDataItems();

//System.out.println("Node "+i+" : "+data.toString());

Package p1 = data.getP();

p1.printDimensions();

if(p1.isCube()) {

c++;

//p1.printDimensions();

sum_vol += p1.getVolume();

}

else nc++;

nodes = nodes.getNextNode();

i++;

}

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

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

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

}

}

Thank you .............all the best.....

Add a comment
Know the answer?
Add Answer to:
I have a program that reads a file and then creates objects from the contents of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • I have this program that works but not for the correct input file. I need the...

    I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main {    public static void main(String[]...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT