Bifalgorithm post · Computer science one · Paying the Piper

What is 28?

What is 28? (Computer science one · Paying the Piper) — "28. Loops 1 public class LoanAmortization { 2 3 public static void main(String args[]) { 4 5 if(args.l…

Ideas in this flowchart

  • Loan amortization program using a payment loop
  • Checks whether exactly four command-line arguments were provided
  • Prints the usage message and exits when the argument count is invalid
  • Parses the principal, APR, and number of months
  • Initializes the balance to the loan principal
  • Converts the annual percentage rate into a monthly interest rate
  • Calculates the fixed monthly payment from the loan formula
  • Rounds the monthly payment to the nearest cent
  • Prints the principal, APR, term, and monthly payment
  • Repeats the amortization process for the first n−1 payments
  • Computes and rounds the interest for the current balance
  • Computes the principal payment by subtracting interest from the payment
  • Updates the balance by subtracting the principal payment
  • Prints the payment number, interest, principal payment, and new balance
Feed/

What is 28?

No cover

Channel

Computer science one

1 page

"28. Loops 1 public class LoanAmortization { 2 3 public static void main(String args[]) { 4 5 if(args.length != 4) { 6 System.err.println(\"Usage: principle apr terms\"); 7 System.exit(1); 8 } 9 10 double principle = Double.parseDouble(args[0]); 11 double apr = Double.parseDouble(args[1]); 12 int n = Integer.parseInt(args[2]); 13 14 double balance = principle; 15 double monthlyInterestRate = apr / 12.0; 16 17 //monthly payment 18 double monthlyPayment = (monthlyInterestRate * principle) / 19 (1 - Math.pow( (1 + monthlyInterestRate), -n)); 20 //round to the nearest cent 21 monthlyPayment = Math.round(monthlyPayment * 100.0) / 100.0; 22 23 System.out.printf(\"Principle: $%.2f\n\n\", principle); 24 System.out.printf(\"APR: %.4f%%\n\n\", apr*100.0); 25 System.out.printf(\"Months: %d\n\n\", n); 26 System.out.printf(\"Monthly Payment: $%.2f\n\n\", monthlyPayment); 27 28 //for the first n-1 payments in a loop: 29 for(int i=1; i<n; i++) { 30 // compute the monthly interest, rounded: 31 double monthlyInterest = 32 Math.round( (balance * monthlyInterestRate) * 100.0) / 100.0; 33 // compute the monthly principle payment 34 double monthlyPrinciplePayment = monthlyPayment - monthlyInterest; 35 // update the balance 36 balance = balance - monthlyPrinciplePayment; 37 // print i, monthly interest, monthly principle, new balance 38 System.out.printf(\"%d\\t$%10.2f $%10.2f $%10.2f"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.