Banking & Java

| Posted in , | Posted on 10:55 PM

As a banker of course I gravitated toward a banking related example. This problem asks that given a loan term, apr, and principal what the monthly payment would be. I was more interested in the construction of the code than the underlying math so I lifted the equation from the book.

((loanamount x monthlyinterestrate) / 1 )
/ (1-(1/(1+monthlyinterestrate)^(number of years x 12)))


Yeah...

It's really exciting to have a gui based prompt and not be confined to a terminal. I'm still not really familiar with the JOptionPane. There was another surprise. I thought that having an exponent could be expressed with a carrot aka "^" character. Such is not the case. After a quick Google search I found that you have to use the method Math.pow(number, exponent). Easy enough but it was still a surprise.

package interestcalculation;

/**
* @author Neil
*/

//Pull in Javax class
import javax.swing.JOptionPane;

public class Main {



public static void main(String[] args) {

//Prompt user for annual interest rate
String rateString = JOptionPane.showInputDialog(null, "Enter the Annual" + " Percentage Rate", "Interest Calculator", JOptionPane.QUESTION_MESSAGE);

//Convert rateString into a double variable
double rate = Double.parseDouble(rateString);
double monthlyrate = rate / 1200;

//Prompt user for term of loan in years
String termString = JOptionPane.showInputDialog(null, "Enter the term of" + " the loan in years", "Interest Calculator",
JOptionPane.QUESTION_MESSAGE);

//Convert termString into an double variable;
double term = Double.parseDouble(termString);

//Prompt user for loan amount
String amountString = JOptionPane.showInputDialog(null,
"Enter the full" + " loan amount.", "Interest Calculator",
JOptionPane.QUESTION_MESSAGE);

//Convert amountString into a double variable;
double amount = Double.parseDouble(amountString);

//Calculate Payment
double payment = amount * monthlyrate / (1-1 / Math.pow(1 + monthlyrate, term *12));

//Display Payment

JOptionPane.showMessageDialog(null, "The monthly payment is $"
+ payment);

}

}

Comments Posted (0)

Post a Comment