Bifalgorithm post · Computer science one · Paying the Piper

What is 28?

What is 28? (Computer science one · Paying the Piper) — "28.5. Examples 28.5.4. Paying the Piper Let’s adapt the solution for the loan amortization schedule we…

Ideas in this flowchart

  • Adaptation of a loan amortization schedule solution
  • Read the principal, loan terms, and interest as command-line inputs.
  • Calculate the monthly payment using the adapted formula and Math.pow().
  • Accuracy problems can produce payments containing fractions of a cent.
  • Currency values must be rounded to the nearest cent.
  • Multiply the value by 100, round to a whole number, and divide by 100.
  • In Java, round the monthly payment with Math.round(monthlyPayment * 100.0) / 100.0.
  • Apply the same rounding technique to monthly interest and other currency values.
  • Use System.out.printf() to format and align the output columns.
  • Handle the final month separately to correct overpayment or underpayment caused by rounding.
  • The complete adapted solution appears in Code Sample 28.10.
Feed/

What is 28?

No cover

Channel

Computer science one

1 page

"28.5. Examples 28.5.4. Paying the Piper Let’s adapt the solution for the loan amortization schedule we developed in Section 4.7.3. First, we’ll read the principle, terms, and interest as command line inputs. Adapting the formula for the monthly payment and using the math library’s Math.pow() function, we have the following. 1 double monthlyPayment = (monthlyInterestRate * principle) / 2 (1 - pow( (1 + monthlyInterestRate), -n)); However, recall that we may have problems due to accuracy. The monthly payment could come out to be a fraction of a cent, say $43.871. For accuracy, we need to ensure that all of the figures for currency are rounded to the nearest cent. The standard math library does have a Math.round() function, but it only rounds to the nearest whole number, not the nearest 100th. However, we can adapt the “off-the-shelf” solution to fit our needs. If we take the number, multiply it by 100, we get 4387.1 which we can now round to the nearest whole number, giving us 4387. We can then divide by 100 to get a number that has been rounded to the nearest 100th! In Java, we could simply do the following. monthlyPayment = Math.round(monthlyPayment * 100.0) / 100.0; We can use the same trick to round the monthly interest payment and any other number expected to be whole cents. To output our numbers, we use System.out.printf() and take care to align our columns to make make it look nice. To finish our adaptation, we handle the final month separately to account for an over/under payment due to rounding. The full solution can be found in Code Sample 28.10. 421"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.