Eggnog
Eggnog

Reputation: 33

Getting object with max value from an array using recursion

Packet class

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

Packages Class

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()
    {
    }
}

Assignment

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.

I am trying to slowly complete each one of the methods for this project. I struggling with recursion especially on maxGetWeight. I am hoping if I can solve that one than I can use that knowledge to solve the other recursive methods.

Upvotes: 0

Views: 88

Answers (1)

plalx
plalx

Reputation: 43718

Using recursion just to loop over a list is quite strange, but I guess that is what assignments are sometimes.

It's not java, but the same algorithm could be used:

let packets = [
  { id: 1001, weight: 8.37 },
  { id: 1002, weight: 2.17 },
  { id: 1003, weight: 11.35 }
];

console.log(heavyestPacketFrom(packets));

function heavyestPacketFrom(packets, index, heavyestPacket) {
    
    index = index || 0;
    heavyestPacket = heavyestPacket || packets[index] || null;

    if (index >= packets.length) return heavyestPacket;

    let packet = packets[index];

    if (packet.weight > heavyestPacket.weight) {
        heavyestPacket = packet;
    }

    return heavyestPacketFrom(packets, index + 1, heavyestPacket);
}

Upvotes: 1

Related Questions