Results 1 to 6 of 6
  1. #1
    eggnog026 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default Getting object with max value from an array using recursion

    I am trying to slowly complete each one of the methods for this project. I struggling with recursion especially on maxGetWeight. It is giving me an error right now and I dont know how to solve it. I am hoping if I can solve that one than I can use that knowledge to solve the other recursive methods.

    Packet objects have unique ID number, weight in pounds with 2 decimals, and state abbreviation for their destination..
    Class Packet describes one packet and has variables idNumber, weight, and destination of type int, double, and String respectively. In addition, it has the following methods
    • boolean isHeavy () that returns true when packet is above 10 pounds, and false otherwise.
    • String toString() returns String which is one line string representation of Packet objects.
    • double getWeight() returns packet weight.
    • String getDestination()returns String which is packets’ destination

    Create an input file called "packetData.txt" with the following 7 lines. Each line in the "packetData.txt" file has information about one packet object.
    1001 8.37 CA
    1002 2.17 CT
    1003 11.35 NY
    1004 3.77 MA
    1005 9.99 CT
    1006 14.91 VT
    1007 4.97 TX
    1008 14.91 CT
    Class Recursion has no variables and has the following methods: All of the methods must be recursive and if they display or return several resulting packets, they should display or return resulting packets in the same relative order as in the list.

    • String toString(Packet[] list, int n) which returns String representation of entire list of packets with one packet object specified per line.
    • void displayHeavyPackets(Packet[] list, int n) which displays all heavy packets.
    • void displayPacketsToDest(Packet[] list, int n, String dest) which displays all packets with the destination state dest.
    • int countPacketsToDest(Packet[] list, int n, String dest) which returns the number of packets with the destination dest.
    • Packet maxWeightPacket(Packet[] list, int n) returns the heaviest packet object. If more than one has the same max weight you can return any one of those packets.
    • double maxWeight () returns the weight (with two decimals) of a heaviest packet object.

    Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Recursion..In the main method, create an array packetList that can store up to 100 Packets. Next read data for packets from the input file and assign initial part array of Packets. Also maintain counter variable which will be the number of lines in the input file, and also the number of occupied positions in the array packetList. Next invoke each of recursive methods from class recursion.

    PROGRAM RUN outline:
    ALL PACKETS
    Display all packets by calling toString() method from class Packages.

    All HEAVY PACKETS
    Display all heavy packets

    All PACKETS with destination dest
    Display all packets that are shipped to destination state dest.

    The number of packets with destination dest is xxx.

    The packet object with max weight is: X X X X.

    The max weight of all packets is XXX.

    Java Code:
    public class Packet
    {
        private int idNumber;
        public double weight;
        private String Destination;
        public Packet(int id, double w, String D)
        {
             idNumber = id;
             weight = w;
             Destination = D;
        }
        public boolean isHeavy()
        {
            if (weight > 10)
                return true;
            else
                return false;
        }
        public String toString()
        {
             return idNumber + " " + weight + " " + Destination;
        }
        public double getWeight()
        {
            return weight;
        }
        public String getDestination() 
        {
           return Destination;
        }
    }
    Java Code:
    import java.io.*;
    public class Packages
    {
        public String toString(Packet[] list, int n)
        {
            
        }
        public void displayHeavyPackages(Packet[] list, int n)
        {
            
        }
        public void displayPacketsToDest(Packet[] list, int n, String dest)
        {
            
        }
        public int countPacketsToDest(Packet[] list, int n)
        {
            
        }
        public Packet maxWeightPacket(Packet[] list, int n)
        {
            if (n > 0)
            {
                return list[n];
            }
            if (list[n].getWeight() > list[n-1].getWeight())
            {
                  double pack = list[n-1].getWeight();
                  list[n-1].getWeight() = list[n].getWeight();
                  list[n].getWeight() = pack;
                  maxWeightPacket(list, n-1);
            }
        }
        public double maxWeight()
        {
            
        }
    }
    Last edited by eggnog026; 03-23-2017 at 02:16 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting object with max value from an array using recursion

    Do you have any specific java programming questions?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    eggnog026 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default Re: Getting object with max value from an array using recursion

    I need help with one of the methods in the packages class. It involves recursion.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Getting object with max value from an array using recursion

    Please explain and ask some specific java programming questions about your problem.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    eggnog026 is offline Member
    Join Date
    Mar 2017
    Posts
    3
    Rep Power
    0

    Default Re: Getting object with max value from an array using recursion

    public Packet maxWeightPacket(Packet[] list, int n)
    {
    if (n > 0)
    {
    return list[n];
    }
    if (list[n].getWeight() > list[n-1].getWeight())
    {
    double pack = list[n-1].getWeight();
    list[n-1].getWeight() = list[n].getWeight();
    list[n].getWeight() = pack;
    maxWeightPacket(list, n-1);
    }
    }

    This method is not working and I dont know how to solve this.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Getting object with max value from an array using recursion

    When posting code please wrap it in code tags, as you did in your first post above.

    Can you add some debugging statements in there like:
    Java Code:
    System.out.println("Iteration " +n);
    to show what the various values you are checking are.

    Also, does that actually compile?
    If not then copy and paste the exact error messages you are getting (not screenshots), highlighting the lines they occur on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Recursion: returning an array of odd numbers
    By NoobieCode in forum New To Java
    Replies: 5
    Last Post: 02-27-2014, 02:07 AM
  2. Replies: 3
    Last Post: 12-29-2012, 12:21 AM
  3. Object+recursion problem
    By Mapisto in forum New To Java
    Replies: 4
    Last Post: 01-30-2012, 08:33 PM
  4. Is an object array considered to be an object?
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 06:40 AM
  5. Replies: 23
    Last Post: 09-07-2010, 08:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •