Mortgage Payment Calculator Problem Description Creates a GUI program to calcula
ID: 3787251 • Letter: M
Question
Mortgage Payment Calculator
Problem Description
Creates a GUI program to calculate monthly Mortgage Payments.
Specifications
You will need to ask for a Loan Amount, Number of Years, and the Annual Interest Rate. The user will input these via text fields.
There will be a Calculate button that when pressed will calculate the payment and display it in a non-editable Text Box
The GUI should look something like this:
The Payment Amount should look like Currency.
The code for a Loan Class is included at the end of this document. You can use it for calculating the monthly payment.
public class Loan
{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
/** Default constructor */
public Loan() {
this(2.5, 1, 1000);
}
/** Construct a loan with specified annual interest rate,
number of years, and loan amount
*/
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
(1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
/** Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
Mortgage calculator Loan Amount Years al Interest Rate Payment CalculateExplanation / Answer
// Test.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Test extends JFrame
{
private JTextField rate = new JTextField();
private JTextField years = new JTextField();
private JTextField loanAMt = new JTextField();
private JTextField jtfTotalPayment = new JTextField();
private JButton compute = new JButton("CALCULATE");
public Test()
{
JPanel jpanel = new JPanel(new GridLayout(5, 2));
jpanel.add(new JLabel("Loan Amount"));
jpanel.add(loanAMt);
jpanel.add(new JLabel("Annual Interest Rate"));
jpanel.add(rate);
jpanel.add(new JLabel("Years"));
jpanel.add(years);
jpanel.add(new JLabel("Payment"));
jpanel.add(jtfTotalPayment);
jpanel.setBorder(new TitledBorder("Mortgage Payment Calculator"));
JPanel jpanel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
jpanel2.add(compute);
add(jpanel, BorderLayout.CENTER);
add(jpanel2, BorderLayout.SOUTH);
compute.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e )
{
double interestrate = Double.parseDouble(rate.getText());
int yr = Integer.parseInt(years.getText());
double loanAmt = Double.parseDouble(loanAMt.getText());
Loan l = new Loan(interestrate, yr, loanAmt);
jtfTotalPayment.setText(String.format("%.2f", l.getTotalPayment()));
}
}
public class Loan
{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
public Loan()
{
this(2.5, 1, 1000); //the default takes effect when other constructors are not used
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount)
{
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
}
public double getMonthlyPayment()
{
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1-(1 / Math.pow(1+monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment()
{
double totalPayment = getMonthlyPayment() * numberOfYears *12;
return totalPayment;
}
}
public static void main(String[] args)
{
Test test = new Test();
test.pack();
test.setTitle("Loan Calculator");
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.