使用递归从数组中获取具有最大值的对象

时间:2017-03-23 00:24:33

标签: java arrays recursion methods

数据包类

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

包类

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

分配

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.

我正在尝试慢慢完成此项目的每个方法。我特意在maxGetWeight上努力进行递归。我希望我能解决这个问题,而不是使用这些知识来解决其他递归方法。

1 个答案:

答案 0 :(得分:1)

使用递归来循环遍历列表是很奇怪的,但我想这有时是分配。

这不是java,但可以使用相同的算法:

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