1 Attachment(s)
Need some guidance with Java assignment
I'm a complete newbie, not just to Java, but programming in general so please have mercy. :D I have gotten this far but I am having trouble with other things. The assignment is to create a mortgage calculator with a GUI that gets loan amount, interest rate and the term of the loan in years from the user. The user presses the Calculate button and it should output the monthly mortgage payment that one would pay based on the loan amount, interest rate and number of years entered. For example if I borrowed $12 for 1 year at an interest rate of 0% then it should output 1.00 to pay back $1 per month for a year. The output is all kinds of messed up, as you can see from this screenshot. I am pretty sure I have the calculation correct. Also, I am supposed loop back so the user can enter new data or quit. I am having a lot of trouble figuring out where to start with that. Any guidance is appreciated, thank you for any help!
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/* Student Name: Jenna Garcia
Class: PRG421 - Week 2 SR-mf-003
Description: 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.
Date: 11/28/10
*/
public class Calculator extends JFrame implements ActionListener {
private JPanel panelAdder;
private JLabel labela;
private JLabel labelt;
private JLabel labelr;
private JTextField textFieldAmount;
private JTextField textFieldTerm;
private JTextField textFieldRate;
private JTextField textFieldResult;
private JButton buttonCalc;
public Calculator() {
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
// Add Listeners
buttonCalc.addActionListener(this);
}
public void initComponents() {
//Initialize Components
panelAdder = new JPanel();
labela = new JLabel("Amount");
textFieldAmount = new JTextField();
labelt = new JLabel("Term");
textFieldTerm = new JTextField();
labelr = new JLabel("Rate");
textFieldRate = new JTextField();
textFieldResult = new JTextField();
buttonCalc = new JButton("Calculate");
//Set Object Attributes
textFieldResult.setEditable(false);
textFieldResult.setColumns(10);
textFieldAmount.setColumns(10);
textFieldTerm.setColumns(5);
textFieldRate.setColumns(5);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//Lets add the components to the panel
panelAdder.add(labela);
panelAdder.add(textFieldAmount);
panelAdder.add(labelt);
panelAdder.add(textFieldTerm);
panelAdder.add(labelr);
panelAdder.add(textFieldRate);
panelAdder.add(buttonCalc);
panelAdder.add(textFieldResult);
contentPane.add(panelAdder);
}
public static void main(String[] args) {
Calculator frame = new Calculator();
}
private void setResultValue() {
double amount = Double.parseDouble(textFieldAmount.getText());
double term = Double.parseDouble(textFieldTerm.getText());
double rate = Double.parseDouble(textFieldRate.getText()) / 100;
double result = amount * (rate * Math.pow ((1 + rate), term))/(Math.pow((1 + rate), term) - 1);
textFieldResult.setText(Double.toString(result));
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == buttonCalc) {
setResultValue();
}
}
}