I'm relatively new to programming although I have one year of experience and can do basic code. I was given an assignment to work on by someone I work with that they had when they were in college and am having some issues. Posted below is the code for the three classes as well as what has to be done in the program. If you have a moment to spare and can tell me what I may be be doing wrong I'd really appreciate it. I've done a lot of work structuring the code already but there may be a good amount still to go. Thank you for any assistance or advice you may have.
What the program should do/have:
Packet objects have unique ID number, state abbreviation for their destination, and weight in pounds with 2 decimals.
Class Packet describes packets and has variables idNumber, state, and weight of type int, String and double, respectively. In addition, it has the following methods
isHeavy that returns true when packet is above 10 pounds, and false otherwise.
isLight that returns true when packet is below 5 pounds, and false otherwise.
toString returns String which is one line representation of Packet objects.
3 Classes in the project, Packets Packages describes array list packet… main
Use and create an input file called packetData.txt with the following 7 lines:
1001 7.37 CA
1002 5.17 CT
1003 11.35 NY
1004 3.17 MA
1005 9.99 FL
1006 14.91 VT
1007 4.97 TX
Each line in the packetData.txt file has information about one packet object.
Class Packages has shipment and totalWeight variables. Variable called shipment contains the collection of all packets. Variable shipment is of ArrayList type (type Packet). All objects in the ArrayList are of of Packet type. Variable totalWeight is initialized to 0.0. Totalweight specifies the total weight of all packets. The constructor assigns variables shipment and totalWeight (by reading the data from the input file specified above. Each line in the file has data about one packet object and data from it should be assigned to a Packet object first and then added to the collection. Use input file called packetData.txt to read data from the file. Class Packages has methods
Make SURE TO THROW IO Exception
toString which returns String representation about entire collection of packets with one packet object specified per line.
displayLightPackages which displays all packets that are light.
displayHeavyPackages which displays all packets that are heavy .
displayOtherPackages which displays all packets that are neither light nor heavy.
displayTotalWeight displays total weight of all packets with full sentence. (Use simplest code)
displayAverageWeight displays average weight (with two decimals) of all packets. Use full sentence. (Use simplest code)
Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Packages.
Packet.Java BELOW
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class Packet
{
public static void main (String[] args) throws IOException
{
String state;
int idNumber, count=0;
double weight;
ArrayList shipment = new ArrayList();
Scanner fileInput = new Scanner(new File("packetData.txt"));
while (fileInput.hasNext())
{
idNumber = fileInput.nextInt();
weight = fileInput.nextDouble();
state = fileInput.next();
double totalWeight = totalWeight + weight;
count++;
}
}
public boolean isHeavy()
{
if (weight > 10)
{
return weight;
}
}
public boolean isLight()
{
if (weight < 5)
{
return weight;
}
}
public String toString()
{
return idNumber + "" + weight + "" + state;
}
}
Packages.java BELOW
public class Packages
{
private String state;
private int idNumber;
private double weight, averageWeight;
double totalWeight = 0.0;
public Packages(int idNumber, double weight, String state)
{
this.idNumber = idNumber;
this.weight = weight;
this.state = state;
}
public double getWeight()
{
return weight;
}
public String displayLightPackages()
{
return idNumber + "" + weight + "" + state;
}
public String displayHeavyPackages()
{
return idNumber + "" + weight + "" + state;
}
public String displayOtherPackages()
{
return idNumber + "" + weight + "" + state;
}
public double displayTotalWeight()
{
return totalWeight;
}
public double displayAverageWeight()
{
return averageWeight;
}
public String toString()
{
//for (int x = 0; x < 20; x++)
// {
// System.out.println(idNumber + "" + weight + "" + state);
//
// }
return idNumber + "" + weight + "" + state;
}
}
TestPackages.java BELOW
/*
* Name: Connor Raymond
* Date: 2/9/2012
*/
import java.util.Scanner;
import java.io.*;
public class TestPackages
{
public static void main (String[] args) throws IOException
{
int count = 0;
System.out.println("Heavy Packets ");
for (int i = 0; i < count; i++)
{
if (shipment[i].getWeight() > 10)
{
System.out.println(shipment[i]);
}
}
System.out.println("Light Packets ");
for (int i = 0; i < count; i++)
{
if (shipment[i].getWeight() < 5)
{
System.out.println(shipment[i]);
}
}
System.out.println("Regular Packets ");
for (int i = 0; i < count; i++)
{
if (shipment[i].getWeight() < 10 && shipment[i].getWeight() > 5)
{
System.out.println(shipment[i]);
}
}
averageWeight = totalWeight/count;
System.out.println();
System.out.println("Total weight: " + totalWeight);
System.out.println();
System.out.println("Average weight: " + Math.round(average));
}
}
Thank you for your assistance. I'm not trying to have my work done for my I just need to see where I am going wrong.