Why doesn't my program function correctly?
This is just a simple program that lets users input Principal, Duration and Rate and it will output Compound interest.
It has no errors but it doesn't work, when I put in numbers it just says 0.0 all the time for the result :confused:
Code:
public class CompoundInterestGUI extends javax.swing.JFrame {
double principal1;
double rate1;
double duration1;
double interest;
public CompoundInterestGUI() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
principal = new javax.swing.JLabel();
rate = new javax.swing.JLabel();
duration = new javax.swing.JLabel();
principalTextField = new javax.swing.JTextField();
rateTextField = new javax.swing.JTextField();
durationTextField = new javax.swing.JTextField();
result = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
principal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
principal.setText("Principal:");
rate.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
rate.setText("Rate:");
duration.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
duration.setText("Duration:");
principalTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
principalTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
principalTextFieldActionPerformed(evt);
}
});
rateTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
rateTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rateTextFieldActionPerformed(evt);
}
});
durationTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
durationTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
durationTextFieldActionPerformed(evt);
}
});
result.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton1.setText("Get Interest!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(principal)
.addGap(4, 4, 4)
.addComponent(principalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(rate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(rateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(duration)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(durationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {duration, principal, rate});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {durationTextField, principalTextField, rateTextField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(principalTextField, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(principal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(rate)
.addComponent(rateTextField))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(duration)
.addComponent(durationTextField))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}
private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
[b]principal1 = Double.parseDouble(principal.getText());
}
private void rateTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
rate1 = Double.parseDouble(rate.getText());
}
private void durationTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
duration1 = Double.parseDouble(duration.getText());
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
interest = principal1 * Math.pow(1 + rate1, duration1);
result.setText(interest + " $");
[/b]
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CompoundInterestGUI().setVisible(true);
}
});
}
private javax.swing.JLabel duration;
private javax.swing.JTextField durationTextField;
private javax.swing.JButton jButton1;
private javax.swing.JLabel principal;
private javax.swing.JTextField principalTextField;
private javax.swing.JLabel rate;
private javax.swing.JTextField rateTextField;
private javax.swing.JLabel result;
}
Mostly what I have bolded at the bottom is what I need help with!