Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Having Issues Wish this Program

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question 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.
    Last edited by userct; February 6th, 2012 at 01:47 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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;
    }
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  10. The Following User Says Thank You to Norm For This Useful Post:

    userct (February 9th, 2012)

  11. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having Issues Wish this Program

    '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?

  13. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #13
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default 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.

  15. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  16. #15
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by userct; February 6th, 2012 at 01:41 PM.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Having Issues Wish this Program

    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.

  18. #17
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  19. #18
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having Issues Wish this Program

    Thank you all for your assistance.

Similar Threads

  1. Address Book Program Issues
    By Gamb1t in forum What's Wrong With My Code?
    Replies: 208
    Last Post: August 25th, 2011, 08:43 PM
  2. ISSUES in java.awt
    By mailkamlesh in forum AWT / Java Swing
    Replies: 2
    Last Post: August 25th, 2011, 09:36 AM
  3. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM
  4. [SOLVED] Image issues
    By Toll in forum Java Theory & Questions
    Replies: 7
    Last Post: May 19th, 2011, 09:43 PM
  5. ResultSet issues
    By _lithium_ in forum JDBC & Databases
    Replies: 3
    Last Post: March 4th, 2011, 03:20 PM

Tags for this Thread