import javax.swing.*; import java.text.*; class LoanCalculator { public static void main(String[] args) { String initLoanStr, interestStr, yearStr; double initLoan, interest, monthPay, amountLeft; double minMonthPay, maxMonthPay,monthPayGuess, monthInterest,totalInterest; int year,month; // Get Data initLoanStr = JOptionPane.showInputDialog(null,"Enter Loan Amount"); interestStr = JOptionPane.showInputDialog(null,"Enter Annual Interest"); yearStr = JOptionPane.showInputDialog(null,"Enter number of years to payoff"); initLoan = Double.parseDouble(initLoanStr); interest = Double.parseDouble(interestStr); year = Integer.parseInt(yearStr); interest = interest/100.0; // convert to decimal minMonthPay = initLoan*interest/12.0; maxMonthPay = initLoan; monthPayGuess = (minMonthPay+maxMonthPay)/2.0; while ((monthPayGuess-minMonthPay)>0.0001) { amountLeft = initLoan; month = 0; while ((amountLeft >0)&&(month < 12*year)) { monthInterest = amountLeft*interest/12.0; amountLeft = amountLeft - (monthPayGuess-monthInterest); month++; } if (amountLeft > 0) minMonthPay = monthPayGuess; else maxMonthPay = monthPayGuess; monthPayGuess = (minMonthPay+maxMonthPay)/2.0; } DecimalFormat df = new DecimalFormat("0.00"); JOptionPane.showMessageDialog(null,"Your monthly payment will be $" + df.format(monthPayGuess)); System.exit(0); } }