// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 7 - Loops // Program to compute amortization schedule // Amortizer.java 1 JAN 05 import java.text.NumberFormat; public class Amortizer { public static void main (String args[]) { // declare variables float balance = 1000F; float apr = .08F; int months = 6; float interestPaid = 0F; float principalPaid = 0F; float totalInterestPaid = 0F; // compute payment double doublePayment = (balance * (apr / 12))/ (1 - 1 / Math.pow ((1 + apr/12), months)); float payment = (float) doublePayment; // cast to float payment = roundOff (payment); // round to cents // print heading System.out.println ("Mo Payment Interest Principal Balance"); // beginning of while loop int monthNumber = 1; while (monthNumber <= months) { interestPaid = roundOff (balance * apr / 12); totalInterestPaid = totalInterestPaid + interestPaid; if (monthNumber == months) payment = interestPaid + balance; // adjust last payment principalPaid = payment - interestPaid; balance = roundOff(balance - principalPaid); // print a line System.out.print (" " + monthNumber); System.out.print (" " + payment); System.out.print (" " + interestPaid); System.out.print (" " + principalPaid); System.out.println (" " + balance); monthNumber = monthNumber + 1; } // end of while loop System.out.println ("Total Interest Paid " + roundOff(totalInterestPaid)); } // end of main method // roundOff method static public float roundOff (double value) { value = value * 100; // move decimal 2 places to right // Math.round returns long - recast to float float roundedValue = (float) Math.round(value); roundedValue = roundedValue / 100; // move decimal back to left return roundedValue; } // end roundOff } // end of Amortizer.java