-
Mortgage Calc
I am a student and was asked to write a mortgage calculator program using the GUI. I wrote it but it is giving me an error. I spent some time to debug it but I am not able to determine what's causing the problem.
Can you plese help to resolve my problem?
.java and . form are in the below directory.
Last edited by chadonline; 01-06-2007 at 07:08 PM.
-
Ok, I have gotten rid of the arrayIndex exception, I've moved some
variables up to class-level and fixed the logics in jButton1ActionPerformed.
I also put in a numeric validity check. I've had to replace your layout
with an XY-layout to make it compile on my system.
I noticed that you haven't added the radiobuttons to the buttongroup, but
I didn't do anything about it, since it seems to be within the realms of
your development tools auto generated code.
Btw, except for a thankyou, you owe me nada 
Code:
/*
* Ozdinc_Week_3.java
*
* Created on May 29, 2005, 2:53 AM
*/
import java.lang.*;
import java.math.*;
import java.text.*;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/* @author Chad Ozdinc
*/
public class Ozdinc_Week_3 extends JFrame {
// Decimal format
static DecimalFormat decmat = new DecimalFormat("$#,##0.00");
static double[] rate = new double[] {
.0535, .055, .0575};
static int[] term = new int[] {
7, 15, 30};
/** Creates new form Ozdinc_Week_3 */
public Ozdinc_Week_3() {
jbInit();
setSize(575, 575);
this.setTitle("POS/407 Chad's Mortgage Calculator");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void jbInit() { //GEN-BEGIN:initComponents
buttonGroup1 = new javax.swing.ButtonGroup();
Amount = new javax.swing.JTextField();
jTextPane1 = new javax.swing.JTextPane();
term1 = new javax.swing.JRadioButton();
term2 = new javax.swing.JRadioButton();
term3 = new javax.swing.JRadioButton();
jButton1 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
textOut = new javax.swing.JTextArea();
monthlyPay = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
btnClear = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
getContentPane().setLayout(xYLayout1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
xYLayout1.setWidth(553);
xYLayout1.setHeight(398);
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(30, 50, 130, -1));
jTextPane1.setBackground(new java.awt.Color(212, 208, 200));
jTextPane1.setText("Loan Amount");
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(30, 20, 80, -1));
term1.setText("7 years at 5.35%");
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(30, 100, 120, -1));
term2.setText("15 years at 5.5%");
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(360, 100, 110, -1));
term3.setText("30 years at 5.75%");
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(200, 100, 130, -1));
jButton1.setText("Calculate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(240, 50, -1, -1));
jScrollPane3.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane3.setVerticalScrollBarPolicy(javax.swing.JScrollPane.
VERTICAL_SCROLLBAR_ALWAYS);
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(0, 170, 510, 100));
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(180, 130, 280, 20));
jLabel3.setText("Your monthly payment is:");
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(10, 140, 130, -1));
btnClear.setText("Clear All");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
monthlyPay.setBorder(BorderFactory.createEtchedBorder());
monthlyPay.setHorizontalAlignment(SwingConstants.RIGHT);
this.getContentPane().add(jScrollPane3, new XYConstraints(48, 221, 303, 83));
this.getContentPane().add(btnClear, new XYConstraints(48, 317, 187, 27));
this.getContentPane().add(jLabel3, new XYConstraints(50, 185, 145, 25));
this.getContentPane().add(jButton1, new XYConstraints(48, 151, 147, 27));
this.getContentPane().add(term3, new XYConstraints(52, 115, -1, 27));
this.getContentPane().add(term1, new XYConstraints(52, 64, 123, 27));
this.getContentPane().add(term2, new XYConstraints(52, 90, 123, 27));
this.getContentPane().add(jTextPane1, new XYConstraints(44, 24, 99, 24));
this.getContentPane().add(Amount, new XYConstraints(146, 25, 179, 24));
this.getContentPane().add(monthlyPay, new XYConstraints(212, 185, 139, 25));
jScrollPane3.setViewportView(textOut);
//new org.netbeans.lib.awtextra.
//AbsoluteConstraints(230, 280, -1, -1));
jMenu1.setText("Menu");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
pack();
} //GEN-END:initComponents
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_btnClearActionPerformed
// Clear Button:
Amount.setText("");
textOut.setText("");
} //GEN-LAST:event_btnClearActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jButton1ActionPerformed
double beginingAmount = 0.0;
try {
beginingAmount = Double.parseDouble(Amount.getText().trim());
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,"Invalid numeric input","User error",
JOptionPane.ERROR_MESSAGE);
return;
}
int number = 0;
if (term1.isSelected()) {
number = 0;
} else if (term2.isSelected()) {
number = 1;
} else if (term3.isSelected()) {
number = 2;
}
double payment = (beginingAmount * rate[number]) /
(1.0d - Math.pow(1.0d / (1.0d + rate[number] / 12.0d), term[number] * 12.0d)) /
12.0d;
monthlyPay.setText(decmat.format(payment));
double payPri = 0;
int months = 1;
while (months <= term[number] * 12) {
double intrest1 = ( (rate[number] * beginingAmount) / 12.0d);
payPri = payment - intrest1;
beginingAmount = (beginingAmount - payPri);
textOut.append(months + "\t" + decmat.format(intrest1) + "\t" +
decmat.format(beginingAmount) + "\n");
months++;
}
} //GEN-LAST:event_jButton1ActionPerformed
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) { //GEN-FIRST:event_jMenu1ActionPerformed
// TODO add your handling code here:
this.dispose();
} //GEN-LAST:event_jMenu1ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ozdinc_Week_3().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField Amount;
private javax.swing.JButton btnClear;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JLabel monthlyPay;
private javax.swing.JRadioButton term1;
private javax.swing.JRadioButton term2;
private javax.swing.JRadioButton term3;
private javax.swing.JTextArea textOut;
XYLayout xYLayout1 = new XYLayout();
// End of variables declaration//GEN-END:variables
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|