0

The JOptionPane class is used to give the CompMortgage.java program a front-end GUI interface. The class is divided into numerous functions.

package computemortgage;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
// The following program gives the previous computeMortgage program a front-end
// GUI interface. I wrote the functions from bottom to top because I wasn't
// sure if they needed to be defined before they are used (as is necessary
// in C++.) Also, what about scope identifiers? (e.g. functions which are
// members of a class are usually defined outside of the class declaration
// in C++.)
public class ComputeMortgage
{
// pre: string asking for a value is passed in
// post: displays msgbox asking user for double and returns what user enters
static double getDouble(String askFor)
{ return Double.parseDouble(JOptionPane.showInputDialog(null, askFor)); }
// pre: string asking for a value is passed in
// post: displays msgbox asking user for integer and returns what user enters
static int getInt(String askFor)
{ return Integer.parseInt(JOptionPane.showInputDialog(null, askFor)); }
// pre: interest rate, number of years for loan, and loan amount passed in
// post: returns monthly loan payment
// used as helper function for void ShowPayment(double, int, double)
static double monthlyPayment(double rate, int years, double amount)
{ return amount*rate/(1 - (Math.pow(1/(1 + rate), years*12))); }
// pre: monthly loan payment and number of years for loan passed in
// post: returns total payment after loan + interest is payed off
// used as helper function for void totalPayment(double, int)
static double totalPayment(double monthly, int years)
{ return monthly*years*12; }
// pre: interest rate, number of years for loan, and loan amount passed in
// post: calculates and displays monthly and annual payment
static void showPayment(double annualRate, int numYears, double loanAmount)
{
double m = monthlyPayment(annualRate, numYears, loanAmount);
double a = totalPayment(m, numYears);
JOptionPane.showMessageDialog(null, "Monthly Payment = $" + m);
JOptionPane.showMessageDialog(null, "Total Payment = $" + a);
}
// pre: none
// post: computes how much $$$ is required to pay off a loan
public static void main(String[] args)
{
double annualRate = getDouble("Enter Annual Interest Rate")/100.0;
int numYears = getInt("Enter number of Years");
double loanAmount = getDouble("Enter loan Amount");
showPayment(annualRate, numYears, loanAmount);
}
}

While pursuing a Computer Science degree, I founded DaniWeb.com, an online community for developers and IT professionals. I coded the backend platform from the ground up and I also do all of the advertising sales and SEO. I'm a super-geeky programmer with a passion for Internet marketing.

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by diamonddog
0

Thank you so much for this sample code. I am a spatial learner and do well with examples. Thank you again

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.