In: Computer Science
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. You need to include Calculate, Reset, and Exit buttons on your GUI. Please insert comments in the program to document the program. Allow the user to enter the values of the loan, term, and rate. Do not add any additional touches, keep it simple.
Hii, I have answered similar questions many times before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// MortgageCalculator.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MortgageCalculator extends JFrame implements ActionListener {
// declaring needed UI components
private JTextField loanAmtTxt, intRateTxt, yearsTxt, resultTxt;
private JButton calculate, reset, exit;
// constructor
public MortgageCalculator() {
// using a grid layout with any number of rows and 2 columns
setLayout(new GridLayout(0, 2));
// adding labels and text fields in proper order
add(new JLabel("Enter loan amount: "));
loanAmtTxt = new JTextField();
add(loanAmtTxt);
add(new JLabel("Enter interest rate (%): "));
intRateTxt = new JTextField();
add(intRateTxt);
add(new JLabel("Enter number of years: "));
yearsTxt = new JTextField();
add(yearsTxt);
add(new JLabel("Monthly payment: "));
resultTxt = new JTextField();
resultTxt.setEditable(false);
add(resultTxt);
//creating buttons and adding action listeners
calculate = new JButton("Calculate");
calculate.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
exit = new JButton("Exit");
exit.addActionListener(this);
//creating a Panel and adding buttons to the panel
JPanel buttons = new JPanel();
buttons.add(calculate);
buttons.add(reset);
buttons.add(exit);
//adding panel to frame
add(buttons);
//compact size
pack();
//exit on close button click
setDefaultCloseOperation(EXIT_ON_CLOSE);
//making window visible
setVisible(true);
}
public static void main(String[] args) {
//initializing GUI
new MortgageCalculator();
}
@Override
public void actionPerformed(ActionEvent e) {
//finding source of action
if (e.getSource().equals(calculate)) {
try {
//extracting values
double loan = Double.parseDouble(loanAmtTxt.getText());
double interestRate = Double.parseDouble(intRateTxt.getText());
int years = Integer.parseInt(yearsTxt.getText());
//finding monthly interest rate and then monthly payment
double monthlyInterestRate = getMonthlyInterest(interestRate);
double monthlyPayment = getMonthlyPayment(loan, years,
monthlyInterestRate);
//updating result text
resultTxt.setText(String.format("$%.2f", monthlyPayment));
} catch (Exception ex) {
//error occurred
resultTxt.setText("Invalid input");
}
} else if (e.getSource().equals(reset)) {
//clearing all text fields
loanAmtTxt.setText("");
yearsTxt.setText("");
intRateTxt.setText("");
resultTxt.setText("");
} else if (e.getSource().equals(exit)) {
//exit
System.exit(0);
}
}
/**
* method to calculate the monthly interest, by passing annual interest rate
*
* @param interestRate
* - rate of interest in percentage
*/
private double getMonthlyInterest(double interestRate) {
double monthlyInterest = ((interestRate / 100) / 12);
return monthlyInterest;
}
/**
* method to find the monthly payment rate, by passing loan amount, total
* number of years and monthly interest rate
*/
private double getMonthlyPayment(double loanAmt, int numYears,
double monthlyInterest) {
/**
* Applying the formula to find the monthly payment rate
*
* P = L[c(1 +c)^n]/[(1 + c)^n - 1]
*
* (fixed monthly payment (P) required to fully amortize a loan of L
* dollars over a term of n months at a monthly interest rate of c)
*/
int numMonths = numYears * 12;
double monthlyPaymentRate = loanAmt * monthlyInterest
* (Math.pow(1 + monthlyInterest, numMonths))
/ (Math.pow(1 + monthlyInterest, numMonths) - 1);
return monthlyPaymentRate;
}
}
/*OUTPUT*/