Having Issues Wish this Program
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.
Re: Having Issues Wish this Program
Can you explain what the problem is?
For example there are errors: post the full text of the error messages
The output is incorrect: Post the output and explain what is wrong with it and show what it should be.
Re: Having Issues Wish this Program
For one the program doesn't compile completely if that isn't obvious already but here are some errors it shows.
When I compile class packet it says referring to shipment[count] = new Packet(idNumber, weight, state);
cannot find symbol - construction Packet(int,double,java.lang.String)
When I compile TrackPackages cannot find symbol - variable shipment
referring to: if (shipment[i].getWeight() > 10)
Re: Having Issues Wish this Program
You never define shipment in your TestPackages class. I see you define shipment in the main method of your Packet class; therefore, there is no way for your TestPackages class to reach that variable. If you want shipment to be reachable, you must define it outside of the main (or any other) method, and then reference either by Packet.shipment (class.variableName) or <Instance of Packet>.shipment (instance.variableName), depending on whether or not shipment is static.
Re: Having Issues Wish this Program
Please copy and paste here the full text of the error messages.
When the compiler's error says: cannot find symbol, the means the compiler can not find a definition that is in scope for where you have used it.
Where is the variable: shipment defined? Is it in the same scope (within the same pair of {}s) where you are trying to use it?
Re: Having Issues Wish this Program
I'm using BlueJ as my editor. It does not allow me to copy and paste the error message.
Re: Having Issues Wish this Program
That will make it harder for you to post the full text of the messages. Your earlier post left off important information.
Re: Having Issues Wish this Program
Thank you for your comments so far, I've been away the past couple days and haven't been able to log in to reply to anything.
I realized that I declared "shipment" as an array when the instructions say to declare it as ArrayList type.
I have used import java.util.*; in the beginning of the code now and declared shipment as an array list.
Here is my code for Class Packet.
I'm wondering how to correctly implement my arrayList in the While loop which I think will help me in the other two classes.
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();
shipment[count] = new Packet(idNumber, weight, state); //Array Required but arrayList found
totalWeight = totalWeight + weight;
count++;
}
}
public double isHeavy()
{
if (weight > 10)
{
return weight;
}
}
public double isLight()
{
if (weight < 5)
{
return weight;
}
}
public String toString()
{
return idNumber + "" + weight + "" + state;
}
}
Re: Having Issues Wish this Program
You have defined shipment as an ArrayList object NOT as an array, so you should not use the [] notation.
You need to use a method to work with an object like ArrayList.
Read the API doc to see which methods will be useful for what you want to do.
Re: Having Issues Wish this Program
Okay thank you. So I'll try to reference ArrayList by using say, getWeight rather than going through that while loop although that loop will still help to read the input file.
Re: Having Issues Wish this Program
Quote:
'll try to reference ArrayList by using say, getWeight
What are you doing when you are "referencing" the ArrayList?
Putting data into it or getting data from it?
Re: Having Issues Wish this Program
I believe I commented wrong.
What I want to do is assign data to that arrayList and then take it out.
For example, in my isHeavy method I need to "get the weight" from the arrayList and then if it's say "greater than 10" return weight I believe.
I need to look through that documentation again to try and use ArrayList correctly. Both putting data into it and reading from it.
Re: Having Issues Wish this Program
I would like to note that any method starting with is generally returns a boolean. (isLight can only be true or false). Possibly you could return if weight is less than some constant, or consider changing the name of the method.
Re: Having Issues Wish this Program
isLight and isHeavy are required methods and need to be named that way.
I didn't think of it like that but that's a great idea to have it return true if say, for isHeavy, weight > 10.
I just have to call weight correctly.
Re: Having Issues Wish this Program
In the requirements above the program it says:
• 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.
So I'll have to have it return true. To do this would calling my method, "public double isHeavy()" be incorrect?
I mean, would using "double" in my method declaration be incorrect?
Re: Having Issues Wish this Program
Quote:
would using "double" in my method declaration be incorrect?
That declaration says the method returns a double value. true is not a double, it is a boolean.
Re: Having Issues Wish this Program
Thank you, I was assuming that declaring it boolean would cause anything inside the method to be boolean and that would mess me up.
Re: Having Issues Wish this Program
Thank you all for your assistance.