X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Getting Object With Max Value From An Array Using Recursion    
post Dec 12 2017, 09:53 PM

Posts: 1
Joined: 12-December 17

Hi to all, I have met some errors when I run my code where mac12m in my project, I can't solve it because of my poor knowledge, can anyone give me some advice, I'll so appreciated, thank you, kynix! The situation is following:
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:
Display all packets by calling toString() method from class Packages.

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

Go to the top of the page
post Mar 3 2018, 09:01 AM

Posts: 1,184
Joined: 26-January 06
From: .....the wiregrass (either you know or you don't)

This looks like a homework assignment for school. Not sure it is fair to ask for help.
Go to the top of the page

Custom Search

RSSSearch   Top   Lo-Fi    22nd October 2019 - 03:55 AM