Code issue
Hi all,
I am having a problem with my code for my class. I'm new to Java programing. So, I am having difficulty trying to figure out why my program will not compile.
Here is the 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, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.
Here is my code:
/*
Title:Mortgage Calculator
Programmer: Mark Jackson
Date:Januaray 21, 2007
Filename:mortgageCalculation.java
Class:POS / 407
Instructor: Raouf Benhadj
Purpose:Write a program with a GUI. Have it calculate and display
mortgage payment amount from user input. Allow for user to
clear and input new data.
*/
import java.io.*;
import java.awt.*;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import java.text.DecimalFormat;
public class mortgageCalculator extends JFrame
{
final static DecimalFormat decimal = new DecimalFormat("$0.00");
// create new form
public mortgageCalculator()
{
initComponents();
}
private void initComponets()
{
jTextField1 = new JTextField();
jTextField2 = new JTextField();
jTextField3 = new JTextField();
jLabel1= new JLabel();
jLabel2= new JLabel();
jLabel3= new JLabel();
jLabel4= new JLabel();
jLabel5= new JLabel();
jButton1= new JButton();
jButton2= new JButton();
setDefaultCloseOperation(windowConstrants.EXIT_ON_CLOSE);
// make button1, the Calculate button clickable
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("0.00");
jLabel2.setText("Monthly Payment");
jLabel3.setText("Principal");
jLabel4.setText("Term");
jLabel5.setText("Interest");
// make button2, the Clear button clickable
jButton2.setText("Clear");
jButton2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jButton2ActionPerformed(evt);
}
});
// setting the layout
JPanel mortgagePanel = new JPanel(new GridLayout(3,2));
mortgagePanel.add(jLabel3);
mortgagePanel.add(jTextField1);
mortgagePanel.add(jLabel5);
mortgagePanel.add(jTextField2);
mortgagePanel.add(jLabel4);
mortgagePanel.add(jTextField3);
mortgagePanel.add(jlabel1);
mortgagePanel.add(jLabel2);
// setting button layout
JPanel buttonPanel = JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(jButton1);
buttonPanel.add(jButton2);
}
}
// setting the function on the Clear button
private void jButton2ActionPerformed(ActionEvent evt)
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jLabel1.setText("0.00");
}
// setting the function of the Calculate Button
private void jButton1ActionPerformed(ActionEvent evt)
{
double field1 = Double.parseDouble(jTextField1.getText());
double field2 = Double.parseDouble(jTextField.getText());
int field3= Integer.parseInt(jTextField3.getText());
double sum= calcPayment(field1, field2, field3);
String formatted = decimal.format(sum);
jLabel1.setText(formatted);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new mortgageCalculator().setVisible(true);
}
})
}
// calculation
public double calcPayment(double interestRate, double principal, int years)
{
int months = years * 12;
double annualInterest= interestRate / 100;
double periodicInterest = annualInterest / 12;
double interestBalance = principal * periodicInterest;
double monthlyPayment= interestBalance / (1-(1/Math.pow((1 + periodicInterest), months)));
double principalBalance = monthlyPayment - interestBalance;
double loanBalance= principal - principalBalance;
return monthlyPayment;
}
Any help would be greatly appreciated.
Thanks.

