Java Reference
In-Depth Information
public static double recalculate(double fundAmount, double rate){
return fundAmount*(1+rate);
}
figure 6-6  
That corrects the syntax error, so you can try running the program now. The output should now read:
100 at 10% annual interest is: 110.00000000000001
David Johnson will have $41 per month for retirement.
So the method seems to be calculating correctly now, since 100 + 10% return is 110.
note It's worth mentioning the rounding error you see here. Doubles are
not appropriate data types for dealing with things where precision is impor-
tant, including money. BigDecimal would be a much more appropriate choice
because it allows the developer control over the rounding of floating point
values. An example of how this program could use BigDecimal is included at
the end of this section.
So you now know the error is in the for loop. You might first check the initialization and termination
conditions of the loop, just to be sure it's iterating properly. It should repeat for each year between age
30 and 65 (presumably retirement age). That shouldn't be a problem. Therefore, it must be in the body
of the for loop. In fact, the change you made to the return type is already a hint as to what needs to
be edited here, too. The method now returns a double , but that double is not used in any way dur-
ing the loop. Therefore, every time the loop is executed, the same retirementFund = 10000 is used
in the calculation again. What you need to do is assign the newly calculated double value to the
Search WWH ::




Custom Search