0

I'm having a problem with my code and it seems that this portion seems to be causing it.

public static boolean[] determine(double avgMilesFlown, double[] numMilesFlown, boolean[] bonusEligibility) {  
      boolean result = false;    
      for (double d : numMilesFlown) {
         if (d > avgMilesFlown) {  

            result = true;
         }

      }  
      bonusEligibility = Arrays.copyOf(bonusEligibility, bonusEligibility.length + 1);
      bonusEligibility[bonusEligibility.length - 1] = result;    
      return bonusEligibility;
   }

This block of code is supposed to determine if someone earns a bonus. If their number of miles flown is more than the average miles flown across all entered (including themselves) then they are eligible. My question is am I comparing each individual number of miles flown to the average of miles flown correctly, so that if the number of miles flown is greater than the average, then it will return a list of true's that correspond to each individual.

Normal for loop:

for (int i = 0; i < numMilesFlown.length; i++) {
    if (numMilesFlown[i] > avgMilesFlown) {  
        result = true;
    }    
} 

Example input/output:

Names: [tim, jim]
Years Flown: [2, 2]
Miles flown: [45, 43]
Avg Miles: 44
Bonus: [515.00, 515.00]

Only Tim should get a bonus because his miles flown is greater than the average, but Jim earns one too, even though his miles flown is below the average.

6
  • am I comparing each individual number of miles flown to the average of miles flown correctly? What did the JVM say? Commented Jul 19, 2015 at 15:02
  • you may add a break statement after result=true; as apart of enhancement. Commented Jul 19, 2015 at 15:03
  • Use the normal for loop instead of the enhanced for loop Commented Jul 19, 2015 at 15:05
  • It didn't find any errors, but people that shouldn't be earning a bonus, do in the output Commented Jul 19, 2015 at 15:06
  • 1
    why are you resizing bonusEligibility? Could you show the invocation code for this method? Commented Jul 19, 2015 at 15:12

1 Answer 1

1

result can only hold one value at once, so once it is set to true in the for loop, it will remain true the through the entire method and cause everything in bonusEligibility[] to also be set to true.

Instead you directly fill bonusEligibility[] with corresponding values for avgMilesFlown[]. For example:

Arrays.fill(bonusEligibility, false);

for (int i = 0; i < numMilesFlown.length; i++) {
    if (numMilesFlown[i] > avgMilesFlown) {  
        bonusEligibility[i] = true;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, I see where you're getting at..instead of making a new array, could I just use bonusEligibility[]?
Good point, that will save the extra step of duplicating an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.