Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im having trouble with the following assignment...... Write the program in Java

ID: 3637653 • Letter: I

Question

Im having trouble with the following assignment......

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 and the user's selection from a menu of available mortgage loans:

- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%

Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program.


This is what i have so far.....




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.swing.event.*;
import javax.swing.text.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;

public class Main extends JPanel implements PropertyChangeListener
{
//sets initial values in the field boxes
private double balance = 200000;
private double rate = 7.5;
private int length = 30;

// field names
private JLabel balanceLabel;
private JLabel rateLabel;
private JLabel lengthLabel;

//sets the names of the labels to strings
private static String balanceString = "Enter Loan Amount: ";
private static String rateString = "Enter a number for the annual percentage rate : ";
private static String lengthString = "Enter the term of the loan in years ";
private static String monthlyPaymentString = "Monthly Payment: ";

//User input fields
private JFormattedTextField balanceField;
private JFormattedTextField rateField;
private JFormattedTextField lengthField;
private JFormattedTextField paymentField;

//Formats to format and parse numbers
private NumberFormat balanceFormat;
private NumberFormat percentFormat;
private NumberFormat monthlyPaymentFormat;

public void main ()
{
new BorderLayout();
setUpFormats();
double payment = computePayment(balance,
rate,
length);

//Create the labels.
balanceLabel = new JLabel(balanceString);
rateLabel = new JLabel(rateString);
lengthLabel = new JLabel(lengthString);
paymentLabel = new JLabel(monthlyPaymentString);

//Create the text fields and set them up.
balanceField = new JFormattedTextField( balanceFormat);
balanceField.setValue(new Double(balance));
balanceField.setColumns(10);
balanceField.addPropertyChangeListener("value", this);

rateField = new JFormattedTextField(percentFormat);
rateField.setValue(new Double(rate));
rateField.setColumns(10);
rateField.addPropertyChangeListener("value", this);

lengthField = new JFormattedTextField();
lengthField.setValue(new Integer(length));
lengthField.setColumns(10);
lengthField.addPropertyChangeListener("value", this);

paymentField = new JFormattedTextField(paymentFormat);
paymentField.setValue(new Double(payment));
paymentField.setColumns(10);
paymentField.setEditable(false);
paymentField.setForeground(Color.red);

//Tell accessibility tools about label and textfield pairs.
balanceLabel.setLabelFor(balanceField);
rateLabel.setLabelFor(rateField);
lengthLabel.setLabelFor(legnthField);
monthlyPaymentLabel.setLabelFor(monthlyPaymentField);

//Lay out the labels in a panel.
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(balanceLabel);
labelPane.add(rateLabel);
labelPane.add(lengthLabel);
labelPane.add(monthlyPaymentLabel);

//Layout the text fields in a panel.
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(balanceField);
fieldPane.add(rateField);
fieldPane.add(lengthField);
fieldPane.add(monthlyPaymentField);

//Put the panels in this panel, labels on left,
//text fields on right.
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
}

/** Called when a field's "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == balanceField) {
balance = ((Number)balanceField.getValue()).doubleValue();
} else if (source == rateField) {
rate = ((Number)rateField.getValue()).doubleValue();
} else if (source == lengthField) {
length = ((Number);engthField.getValue()).intValue();
}

double monthlyPayment = computePayment(balance, rate, length);
monthlyPaymentField.setValue(new Double(payment));
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add contents to the window.
frame.add (new FormattedTextFieldDemo());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}

//Compute the monthly payment based on the loan amount,
//APR, and length of loan.
double computePayment(double loanAmt, double rate, int length) {
double I, partial1, denominator, answer;

length *= 12; //get number of months
if (rate > 0.01) {
I = rate / 100.0 / 12.0; //get monthly rate from annual
partial1 = Math.pow((1 + I), (0.0 - length));
denominator = (1 - partial1) / I;
} else { //rate ~= 0
denominator = length;
}

answer = (-1 * loanAmt) / denominator;
return answer;
}

//Create and set up number formats. These objects also
//parse numbers input by user.
private void setUpFormats() {
balanceFormat = NumberFormat.getNumberInstance();

percentFormat = NumberFormat.getNumberInstance();
percentFormat.setMinimumFractionDigits(3);

monhlyPaymentFormat = NumberFormat.getCurrencyInstance();
}
}

Explanation / Answer

//these packages need to be imported import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JButton; import javax.swing.JTextField; import java.text.DecimalFormat; //this is the calculator class public class Calculator extends JPanel implements ActionListener { protected JComboBox MortCombo; protected JLabel lblprincipal, lblinterestRate, lblTerm, lblmonthlyPayment; protected JButton calculate, Clear; protected JTextField principal, interestRate, Term, monthlyPayment; protected TextArea MortTextField; public Calculator() { String [] termString = {"7", "15", "30"}; String [] rateString = {"0.535", "0.055", "0.0575"}; //creates labels and fields lblprincipal=new JLabel("Principal Amount:"); principal = new JTextField("",10); lblinterestRate=new JLabel("Interest Rate:"); interestRate = new JTextField("",10); lblTerm=new JLabel("Term:"); Term = new JTextField("",10); lblmonthlyPayment=new JLabel("Monthly Payment:"); monthlyPayment = new JTextField("",10); MortCombo=new JComboBox(comboItems); MortTextField=new TextArea(10,120); //The buttons are created here calculate = new JButton("Calculate"); calculate.setActionCommand("GO"); Clear = new JButton("Clear"); Clear.setActionCommand("Clear"); //Added action for buttons here calculate.addActionListener(this); Clear.addActionListener(this); add(lblprincipal); add(principal); add(lblinterestRate); add(MortCombo); add(lblTerm); add(Term); add(lblmonthlyPayment); add(monthlyPayment); add(calculate); add(Clear); add(MortTextField); } public void actionPerformed(ActionEvent e) { if ("GO".equals(e.getActionCommand())) { CalculateMortgage(); } else { principal.setText(""); interestRate.setText(""); Term.setText(""); monthlyPayment.setText(""); } } //This is where the interest rate reads and shows it in the principal field public void CalculateMortgage() { double dblprincipal=Double.parseDouble(principal.getText()); double dblinterestRate=Double.parseDouble((String)MortCombo.getSelectedItem()); int intTerm=Integer.parseInt(Term.getText()); DecimalFormat money = new DecimalFormat("$0.00"); double MonthlyPayment=0.0; double InterestPayment=0.0; double PrincipalBal=0.0; double MIntRate=dblinterestRate/1200; int MTerms=intTerm * 12; //The formula for monthly payment here MonthlyPayment=(dblprincipal * MIntRate) / (1-Math.pow((MIntRate+1),-MTerms)); //This is where monthly payments gets converted to decimal format monthlyPayment.setText("" + (money.format(MonthlyPayment))); MortTextField.append("Month No. Monthly Payment Loan Balance Interest Payment "); MortTextField.append("1 " + MonthlyPayment + PrincipalBal + InterestPayment + " "); for (int counter=1; counter