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 several errors after adding a chart

    I am trying to add a chart to my mortgage calculator having some errors. If anyone can help figure out my errors i be so grateful. The assignment is due at midnite.


    Code:
    /*
     * * @(#)Calculator.java
     *
     *
     * @author Mike Souza
     * @version 1.00 2011/18/10
    Change Request #7
    Requestor:  Ninfa Pendleton - Rapid City, SD
    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. 
     * Read the interest rates to fill the array from a sequential 
     * file. Display the mortgage payment amount followed by the loan 
     * balance and interest paid for each payment over the term of the 
     * loan. Add graphics in the form of a chart. 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. 
    
    
    
     */
    import java.awt.*;
    import java.text.NumberFormat;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Graphics2D;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.geom.Line2D;
    
       public class Calculator extends JFrame implements ActionListener
     {
    //Array
         int terms[] = { 7, 15, 30 };
         double rates[] = { 5.35, 5.5, 5.75 };
    
    //Labels Declared here
         JLabel AmountLabel = new JLabel("Amount of loan");
         JLabel PaymentLabel = new JLabel("Monthly Payment");
         JLabel InterestLabel = new JLabel("Interest Rate");
         JLabel TermLabel = new JLabel("Loan Term");
    
    //Text field
         JTextField mortgageAmount = new JTextField(7);
         JTextField Payment = new JTextField(7);
         JTextField InterestRate = new JTextField(3);
         JTextField Term = new JTextField(3);
    
    //Buttons here
         JButton Loan7 = new JButton("7 years at 5.35%");
         JButton Loan15 = new JButton("15 years at 5.50%");
         JButton Loan30 = new JButton("30 years at 5.75%");
         JButton ExitButton = new JButton("Exit");
         JButton ClearButton = new JButton("Clear All");
         JButton CalculateButton = new JButton("Calculate Loan");
    
    //text and scroll here
         JTextArea LoanPayments = new JTextArea(20, 50);
         JTextArea GraphArea = new JTextArea(19, 50);
         JScrollPane scroll = new JScrollPane(LoanPayments);
    
      public Calculator()
    {
    
    //panel and frame 
       super("Mortgage Calculator");
         setSize(700, 500);
         setLocation(200, 0);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JPanel pane = new JPanel(new GridLayout(2, 1));
    
    //content for container 
         Container grid = getContentPane();
         grid.setLayout(new GridLayout(4, 0, 4, 4));
         pane.add(grid);
         pane.add(scroll);
         grid.add(AmountLabel);
         grid.add(mortgageAmount);
         grid.add(InterestLabel);
         grid.add(InterestRate);
         grid.add(TermLabel);
         grid.add(Term);
         grid.add(PaymentLabel);
         grid.add(Payment);
         grid.add(Loan7);
         grid.add(Loan15);
         grid.add(Loan30);
         grid.add(CalculateButton);
         grid.add(ClearButton);
         grid.add(ExitButton);
         Payment.setEditable(false);
         setContentPane(pane);
         setContentPane(pane);
         setVisible(true);
    
    //Acton Listeners 
         ExitButton.addActionListener(this);
         ClearButton.addActionListener(this);
         Loan7.addActionListener(this);
         Loan15.addActionListener(this);
         Loan30.addActionListener(this);
         mortgageAmount.addActionListener(this);
         InterestRate.addActionListener(this);
         Term.addActionListener(this);
         Payment.addActionListener(this);
         CalculateButton.addActionListener(this);
    
    }
    
      public void actionPerformed(ActionEvent e) {
        Object command = e.getSource();
      if (command == ExitButton) {
        System.exit(0);
    }
      else if (command == Loan7) {
         calcLoan(terms[0], rates[0]);
    } 
      else if (command == Loan15) {
         calcLoan(terms[1], rates[1]);
    } 
      else if (command == Loan30) {
         calcLoan(terms[2], rates[2]);
    } 
      else if (command == CalculateButton) {
      double terms = 0;
      double rates = 0;
      try {
         terms = Double.parseDouble(Term.getText());
         rates = Double.parseDouble(InterestRate.getText());
    } 
      catch (Exception ex) {
         LoanPayments.setText("Invalid term or rate Amount");
      return;
    }
         calcLoan(terms, rates);
    } 
      else if (command == ClearButton) {
         mortgageAmount.setText("");
         Payment.setText("");
         InterestRate.setText("");
         Term.setText("");
         LoanPayments.setText("");
    }
    }
      private void calcLoan(double terms, double rates) {
         Term.setText(String.valueOf(terms));
         InterestRate.setText(String.valueOf(rates));
      double amount = 0;
      try {
         amount = Double.parseDouble(mortgageAmount.getText());
    } 
      catch (Exception ex) {
         LoanPayments.setText("Invalid mortgage Amount");
      return;
    }
      double interestRate = rates;
      
    //interest rate here 
      double intRate = (interestRate / 100) / 12;
      
    //Laon term calculaton for month 
      int months = (int) terms * 12;
    
    // coverts terms to month 
      double rate = (intRate / 12);
    
    //interest rate to monthly interest here 
      double payment = amount * intRate
        / (1 - (Math.pow(1 / (1 + intRate), months)));
    
      //Monthly payments here
      double remainingPrincipal = amount;
    
    //Balance
      double MonthlyInterest = 0;
    
    //interest payment
      double MonthlyAmt = 0;
    
    //loan payment
    //formats for output to table
        NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance();
         Payment.setText(CurrencyFormatter.format(payment));
         LoanPayments.setText("Month\tPrincipal\tInterest\tEnding Balance\n");
    //months
    
      int currentMonth = 0;
      while (currentMonth < months) {
    
    //details for mortgage text
         MonthlyInterest = (remainingPrincipal * intRate);
    
    //Payment Interest
         MonthlyAmt = (payment - MonthlyInterest);
    
    //Monthly Payment towards loan balance
         remainingPrincipal = (remainingPrincipal - MonthlyAmt);
    
    //loan Balance remaining
         LoanPayments.append((++currentMonth) + "\t"
        + CurrencyFormatter.format(MonthlyAmt) + "\t"
        + CurrencyFormatter.format(MonthlyInterest) + "\t"
        + CurrencyFormatter.format(remainingPrincipal) + "\n");
          GraphArea.append("" + remainingPrincipal);
    }
    } 
      public void paint(Graphics g)
    {
    double width = 900.00;
    int height= 400;
    double x=1.0;
    double y=0.0;
    int counter = 0;
    double xincrement = (width/(dblTerm*12));
    double yincrement = height/dblmortgageAmount;
    
    g.drawString("Payments", 65, 10);
    while (counter <= 10) {
    x = x + xincrement;
    g.drawString(Integer.toString((int)((((dblLoanDuration*12)/10)*counter))), (int)((width/10)*counter)+55, 20);
    counter++;
    drawn = true;
    }
    
    double amount=dblLoanAmount;
    double payment=amount/12;
    g.drawString("Amount", 0, 20);
    while (y < height) {
    y = y + (height/12);
    g.drawString(paymentFormat.format(amount), 0, (int)y);
    amount = amount-payment;
    }
    
    
        public static void main(String[] args) {
          Calculator frame = new Calculator();
    }
    
    }
    Last edited by WA; 03-29-2011 at 04:48 AM.

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    Utah
    Posts
    30
    Thanks
    0
    Thanked 6 Times in 6 Posts
    well for one it looks as if your main is in the paint method.
    It would help if you could post your errors as well


  •  

    Posting Permissions

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