Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    having error messages with this code for a mortgage calculator

    I am just learning java and having a tough time figuring out this error.
    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 and the user's selection from a menu of available mortgage loans:

    - 7 years at 5.35%
    - 15 years at 5.5%
    - 30 years at 5.75%

    Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program.

    Listening on javadebug
    Not able to submit breakpoint LineBreakpoint Calculator.java : 59, reason: No source root found for URL 'file:/C:/Users/Jackfrost/Desktop/week%203/Calculator.java'. Verify the setup of project sources.
    Invalid LineBreakpoint Calculator.java : 59
    User program running
    User program finished


    //these packages need to be imported
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.text.DecimalFormat;
    //this is the calculator class
    public class Calculator extends JPanel implements ActionListener
    {

    protected JComboBox MortCombo;
    protected JLabel lblprincipal, lblinterestRate, lblTerm, lblmonthlyPayment;
    protected JButton calculate, Clear;
    protected JTextField principal, interestRate, Term, monthlyPayment;
    protected TextArea MortTextField;
    public Calculator()
    {

    String [] termString = {"7", "15", "30"};
    String [] rateString = {"0.535", "0.055", "0.0575"};
    //creates labels and fields
    lblprincipal=new JLabel("Principal Amount:");
    principal = new JTextField("",10);
    lblinterestRate=new JLabel("Interest Rate:");
    interestRate = new JTextField("",10);
    lblTerm=new JLabel("Term:");
    Term = new JTextField("",10);
    lblmonthlyPayment=new JLabel("Monthly Payment:");
    monthlyPayment = new JTextField("",10);
    MortCombo=new JComboBox(comboItems);
    MortTextField=new TextArea(10,120);
    //The buttons are created here
    calculate = new JButton("Calculate");
    calculate.setActionCommand("GO");
    Clear = new JButton("Clear");
    Clear.setActionCommand("Clear");
    //Added action for buttons here
    calculate.addActionListener(this);
    Clear.addActionListener(this);
    add(lblprincipal);
    add(principal);
    add(lblinterestRate);
    add(MortCombo);
    add(lblTerm);
    add(Term);
    add(lblmonthlyPayment);
    add(monthlyPayment);
    add(calculate);
    add(Clear);
    add(MortTextField);
    }
    public void actionPerformed(ActionEvent e)
    {

    if ("GO".equals(e.getActionCommand()))
    {

    CalculateMortgage();
    }
    else
    {

    principal.setText("");
    interestRate.setText("");
    Term.setText("");
    monthlyPayment.setText("");
    }
    }

    //This is where the interest rate reads and shows it in the principal field
    public void CalculateMortgage()
    {

    double dblprincipal=Double.parseDouble(principal.getText());
    double dblinterestRate=Double.parseDouble((String)MortCombo.getSelectedItem());
    int intTerm=Integer.parseInt(Term.getText());
    DecimalFormat money = new DecimalFormat("$0.00");
    double MonthlyPayment=0.0;
    double InterestPayment=0.0;
    double PrincipalBal=0.0;
    double MIntRate=dblinterestRate/1200;
    int MTerms=intTerm * 12;
    //The formula for monthly payment here
    MonthlyPayment=(dblprincipal * MIntRate) / (1-Math.pow((MIntRate+1),-MTerms));
    //This is where monthly payments gets converted to decimal format
    monthlyPayment.setText("" + (money.format(MonthlyPayment)));
    MortTextField.append("Month No.\t\tMonthly Payment\t\t\tLoan Balance\t\t\tInterest Payment\n");
    MortTextField.append("1\t\t\t" + MonthlyPayment + PrincipalBal + InterestPayment + "\n");

    for (int counter=1; counter < MTerms; counter++)
    {

    MortTextField.append((counter + 1) + "\t\t\t" + MonthlyPayment + PrincipalBal + InterestPayment + "\n");
    }
    }

    private static void createAndShowGui()
    {
    //Jframe here
    JFrame frame = new JFrame("Mortgage Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Calculator myCalculator = new Calculator();
    myCalculator.setOpaque(true);
    frame.setContentPane(myCalculator);
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {

    javax.swing.SwingUtilities.invokeLater(
    new Runnable()
    {

    public void run()
    {
    createAndShowGui();
    }
    }
    );
    }

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    I quickly put your code through my IDE and it says that comboItems is undefined (which it is) So you need to at least define that variable.

    After I commented out that particular line I was able to get a GUI to show.

    If that doesn't fix it you probably don't have your Java Environment set up correctly and are not pointing to your JDK.

    Or that would be my guess.

    Cheers.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •