Programming help needed. Thanks.
I am working on a GUI that will calculate a mortgage payment when a user provides the amount, term, and interest of a loan. Any help will be very much appreciated.
Here is my work. The error when I compile is: operator / cannot be applied to javax.swing.JTextField,int payment = (amount*((interest/12)/(1-Math.pow((1+(interest/12)),-(term*12)))));
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass calculator{
JFrame frame;
JPanel contentPane;
JLabel prompt1, prompt2, prompt3, payment;
JTextField amount, term, interest;
JButton payButton;
public calculator(){
/* Create and set up the frame */
frame =new JFrame("calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a content pane with a GridLayout and empty borders */
contentPane =new JPanel();
contentPane.setLayout(new GridLayout(0, 2, 10, 5));
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
/* Create and add a prompt and then a text field */
prompt1 =new JLabel("Enter the Mortgage Amount: ");
contentPane.add(prompt1);
amount =new JTextField(10);
contentPane.add(amount);
/* Create and add a second prompt and then a text field */
prompt2 =new JLabel("Enter the Mortgage Term: ");
contentPane.add(term);
term =new JTextField(10);
contentPane.add(term);
/* Create and add a third prompt and then a text field */
prompt3 =new JLabel("Enter the Mortgage Interest: ");
contentPane.add(prompt3);
interest =new JTextField(10);
contentPane.add(interest);
/* Create and add button that will display the payment */
payButton =new JButton("Payment");
payButton.addActionListener(new PayListener());
contentPane.add(payButton);
/* Create and add a label that will display payment */
payment =new JLabel(" ");
payment.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
contentPane.add(payment);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
class PayListenerimplements ActionListener{
/**
* Handle mortgage button click event
* pre: none
* post: The mortgage payment has been calculated and displayed.
*/
publicvoid actionPerformed(ActionEvent event){
double payment;
String a1 = amount.getText();
String t2 = term.getText();
String i3 = interest.getText();
payment = (amount*((interest/12)/(1-Math.pow((1+(interest/12)),-(term*12)))));
payment.setText(Double.toString(payment));
}
}
/**
* Create and show the GUI.
*/
privatestaticvoid runcalculator(){
JFrame.setDefaultLookAndFeelDecorated(true);
calculator myPayment =new calculator();
}
publicstaticvoid main(String[] args){
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
runcalculator();
}
});
}
}
[5417 byte] By [
LMShaffera] at [2007-11-26 18:01:57]

As noted by someone above, your calculation contains the variables "amount", "interest" and "term", and these are actually JTextFields and not integers or doubles.
You actually did read the text from the text fileds correctly:
String a1 = amount.getText();
String t2 = term.getText();
String i3 = interest.getText();
Next, convert the above strings into Doubles:
double amount_D = Double.parseDouble(a1);
double term_D = Double.parseDouble(t2);
double interest_D = Double.parseDouble(i3);
Now, plug these into your calulation:
double payment = (amount_D*((interest_D/12) / (1-Math.pow((1+(interest_D/12)), -(term_D*12)))));
I also noticed that you had a global variable called:
JLabel payment
and you had a local variable in the PayListener.actionPerformed() method called:
double payment
You then got them confused, by storing the result of the calulation in "payment", followed in the next line by trying to set the text of "payment".
To avoid this, rename your label to:
JLabel paymentLabel;
Then the code would be:
double payment = // .... the long calculation
paymentLabel.setText( Double.toString(payment) );
You also have this error:
prompt2 = new JLabel("Enter the Mortgage Term: ");
contentPane.add(term);
The second line should add "prompt2", not "term":
With the above changed, I got the following code:
########################
########################
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator
{
JFrame frame;
JPanel contentPane;
JLabel prompt1, prompt2, prompt3, paymentLabel;
JTextField amount, term, interest;
JButton payButton;
public calculator()
{
/* Create and set up the frame */
frame = new JFrame("Mortgage Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Create a content pane with a GridLayout and empty borders */
contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 2, 10, 5));
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
/* Create and add a prompt and then a text field */
prompt1 = new JLabel("Enter the Mortgage Amount: ");
contentPane.add(prompt1);
amount = new JTextField(10);
contentPane.add(amount);
/* Create and add a second prompt and then a text field */
prompt2 = new JLabel("Enter the Mortgage Term: ");
contentPane.add(prompt2);
term = new JTextField(10);
contentPane.add(term);
/* Create and add a third prompt and then a text field */
prompt3 = new JLabel("Enter the Mortgage Interest: ");
contentPane.add(prompt3);
interest = new JTextField(10);
contentPane.add(interest);
/* Create and add button that will display the payment */
payButton = new JButton("Calulate the Payment");
payButton.addActionListener( new PayListener() );
contentPane.add(payButton);
/* Create and add a label that will display payment */
paymentLabel = new JLabel(" ");
paymentLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
contentPane.add(paymentLabel);
/* Add content pane to frame */
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
class PayListener implements ActionListener
{
/**
* Handle mortgage button click event
* pre: none
* post: The mortgage payment has been calculated and displayed.
*/
public void actionPerformed(ActionEvent event)
{
String a1 = amount.getText();
String t2 = term.getText();
String i3 = interest.getText();
// Now we need to convert the above Strings to numbers
// before doing the calulations below
double amount_D = Double.parseDouble(a1);
double term_D = Double.parseDouble(t2);
double interest_D = Double.parseDouble(i3);
/* I don't know whether calculation below is correct or not,
but I would suggest breaking it up into several smaller calculations */
double payment = (amount_D*((interest_D/12) / (1-Math.pow((1+(interest_D/12)), -(term_D*12)))));
paymentLabel.setText( Double.toString(payment) );
}
}
private static void runcalculator()
{
/* Create and show the GUI */
JFrame.setDefaultLookAndFeelDecorated(true);
calculator myPayment = new calculator();
}
public static void main(String[] args)
{
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
runcalculator();
}
});
}
} // end of class