Can you help with Mortgage Calculator in Java?
Monday, February 1st, 2010Here’s my 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.
I’m still getting errors! (see below)
package MortCalc2.java ;
/**
*
* @author alesiab
*/
//Declaration of class
public class MortCalc2{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
//Declaration of class
/**
* @param args the command line arguments
*/
// Declaration of labels
import JPanel row1 = new JPanel(new GridLayout(1, 3));
import JLabel principal_label1 = new JLabel(”Net Amount $ :”, JLabel.LEFT);//Sets label text and alignment
JTextField principal_txt = new JTextField(10);
JPanel row2 = new JPanel(new GridLayout(1, 3));
JLabel term_label2 = new JLabel(”Term Yrs :”, JLabel.LEFT);
JTextField term_txt = new JTextField(10);
JPanel row3 = new JPanel(new GridLayout(1, 3));
JLabel rate_label3 = new JLabel(”Interest Rate %:”, JLabel.LEFT);
JTextField rate_txt = new JTextField(10);
JPanel row4 = new JPanel(new GridLayout(1, 3));
JLabel pay_label4 = new JLabel(”Monthly Payment $:”, JLabel.LEFT);
JTextField pay_txt = new JTextField(10);
JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER));
//Declaration of buttons and text shown on them
JButton rstButton = new JButton(”Reset”);
JButton extButton = new JButton(”Exit”);
JButton calcButton = new JButton(”Calculate”);
//Declaration for area to display user input
JTextArea displayArea = new JTextArea(10, 45);
// Declaration of number formats that will be output after calculation
DecimalFormat twodigits = new DecimalFormat(”#,###.00″);
//Declaration of variables
double rate = 0;//Sets rate as a double set to zero
double mpay = 0;//Sets monthly payment as a double set to zero
double principal = 0;//Sets principal as a double set to zero
int term = 0;//Sets interest as a integer set to zero
double interest = 0;//Sets interest as a double set to zero
//Declaration of constructor
public MortCalc2() {
super(”Mortgage Calculator: SR-mf-003 #4″);//Syntax used for calling constructor
setSize(400, 200);//sets size of the constructor
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//clears all info and closes window
Container pane = getContentPane();//returns the value for content
Border bdr = new EmptyBorder(2, 5, 2, 5);//provides borders for label
//Declaration of windows that will hold users input
pane.add(row1);
row1.add(principal_label1);
row1.add(principal_txt);
row1.setMaximumSize(new Dimension(300, 25));
row1.setBorder(bdr);
pane.add(row2);
row2.add(term_label2);
row2.add(term_txt);
row2.setMaximumSize(new Dimension(300, row2.getMinimumSize().height));
row2.setBorder(bdr);
pane.add(row3);
row3.add(rate_label3);
row3.add(rate_txt);
row3.setMaximumSize(new Dimension(300, row3.getMinimumSize().height));
row3.setBorder(bdr);
pane.add(row4);
row4.add(pay_label4);
row4.add(pay_txt);
row4.setMaximumSize(new Dimension(300, row4.getMinimumSize().height));
row4.setBorder(bdr);
button.add(calcButton);
button.add(rstButton);
button.add(extButton);
pane.add(button);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);
button.setMaximumSize(new Dimension(10000, button.getMinimumSize().height));
//Adds specified action listener to get action events from buttons
rstButton.addActionListener(this);
extButton.addActionListener(this);
calcButton.addActionListener(this);
}
//Declaration of calculation actions to take place
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
//Begin “if” function
if (source == calcButton) // calculates on hit
{
validateUserInput(principal_txt, rate_txt, term_txt); // validates input
//formula for calculation output
mpay = principal * (rate / (12 * 100)) / (1 - Math.pow(1 + (rate / (12 * 100)), -1 * (term * 12)));
pay_txt.setText(”" + twodigits.format(mpay));
}
if (source == rstButton) // Clears on hit
{
term_txt.setText(” “);
rate_txt.setText(” “);
principal_txt.setText(” “);
pay_txt.setText(” “);
displayArea.setText(” “);
}
if (source == extButton) // Exits on hit
{
System.exit(0);
}
}//end action
//Declaration of the main method
public static void main(String[] arguments) {
MortCalc2 mtg = new MortCalc2();
}
//Declaration of user input for each variable
public void validateUserInput(JTextField principal_txt, JTextField rate_txt, JTextField term_txt) {
//Begin “try/catch” function
try {
principal = Double.parseDouble(principal_txt.getText());
} catch (NumberFormatException e)//Action listener
{
/
Kristle
