DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Applet / Array help (http://www.daniweb.com/forums/thread39798.html)

dmgs11 Feb 19th, 2006 2:45 am
Applet / Array help
 
I am looking to create a mortgage applet with an array for the 3 amounts. There is something I am missing here. Any help can be of use

/* GOAL Display the mortgage payment amount.  Then, list 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.  */

import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.lang.*;
import java.text.*;
import java.util.*;

public class Mortgage extends javax.swing.JApplet implements ActionListener {

  double[] annualInterests  = {5.35,5.5,5.75};
    int[] numOfYears    = {7,15,30};                       

    double monthlyPayment, monthlyInterest, denominator,  numOfMonths, getMortgageAmount;
        double principal;
        double interest;
        int term, i;
        float paymentdue;

    JLabel title = new JLabel("  Mortgage Calculator ");
    JLabel title = new JLabel("  Please enter Principal amount and select term with rate ");
        JTextField appPrincipal=new JTextField(10); 
        JTextField appTerm=new JTextField(2); 
        JTextField appInterest=new JTextField(5); 

    JTextField appMonthlyPayment=new JTextField(10);
        JLabel ErrorMsg=new JLabel("");

        Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

        public void init()
        {
                Container con = getContentPane();
                con.setLayout(new BorderLayout());

        JPanel TopLabel=new JPanel();
            TopLabel.setLayout(new FlowLayout());
                  title.setFont(bigFont);
                con.add("North",title);

                      setBackground(Color.blue);
                      setForeground(Color.yellow);

    JPanel MainForm=new JPanel();
    MainForm.setLayout(new GridLayout(0,3));
    JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT);
    MainForm.add(label1);
            JButton bttn = new JButton(" 7 Years at 5.35% ");
            bttn.addActionListener( new ActionListener() {                                                      }
                      public void actionPerformed(ActionEvent evt) {
                                    // This method will respond to the user's click entry for the mortgage term & interest
                                                i = 0;
                                        MainForm.add(i);

                                  }
        JButton bttn = new JButton(" 15 Years at 5.5% ");
                bttn.addActionListener( new ActionListener() {
                                    }
                                    public void actionPerformed(ActionEvent evt) {
                                      i = 1;
                                      MainForm.add(i);
                                }
        JButton bttn = new JButton(" 30 Years at 5.75% ");
                bttn.addActionListener( new ActionListener() {
                                    }
                                                        public void actionPerformed(ActionEvent evt) {
                                      i = 2;
                                      MainForm.add(i);
                                }



        JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT);
        MainForm.add(label2);

        MainForm.add(appPrincipal);

        JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT);
        MainForm.add(label3);

        MainForm.add(appInterest);

        JLabel label4=new JLabel("Result ",SwingConstants.RIGHT);
                MainForm.add(label4);

        MainForm.add(appMonthlyPayment);

                con.add("West",MainForm);

        JPanel ButtonPanel=new JPanel();
        ButtonPanel.setLayout(new BorderLayout());
        ButtonPanel.add("North",ErrorMsg);
        JButton Calculate=new JButton("Calculate");

        Calculate.addActionListener(this);

        ButtonPanel.add("Center",Calculate);
        con.add("South",ButtonPanel);
        appMonthlyPayment.setEditable(false);


        }

        public void start()
        {
                repaint();
        }

        public void actionPerformed(ActionEvent divide)
        {

            DecimalFormat decimalPlaces=new DecimalFormat("0.00");
            appMonthlyPayment.setText("");

            term = Integer.parseInt(appTerm.getText());
            interest = Double.parseDouble(appTerm.getText());
            principal = Double.parseDouble(appPrincipal.getText());

        monthlyInterest = annualInterests[i]/(12*100);
    int totalNumOfMonths = numOfYears[i]*12;

        int months= term*12;
        monthlyInterest = interest/(12 * 100); 
        denominator = Math.pow(1 + monthlyInterest, -(term*12));
        denominator = 1 - denominator;
        monthlyPayment = principal * (monthlyInterest / denominator);

  monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  return monthlyPayment;

  double getMonthlyPrincipal, monthlyPayment, remainingPrincipal;

  return monthlyPayment - (remainingPrincipal * monthlyInterest);

        appMonthlyPayment.setText("Payment = " + monthlyPayment);


        }

        }

server_crash Feb 19th, 2006 8:48 am
Re: Applet / Array help
 
What's the question? What problem are you having? Is there an error?

dmgs11 Feb 19th, 2006 1:00 pm
Re: Applet / Array help
 
\Mortgage.java:81: ')' expected
public void actionPerformed(ActionEvent evt) {
^
\Mortgage.java:90: ')' expected
public void actionPerformed(ActionEvent evt) {
^
\Mortgage.java:97: ')' expected
public void actionPerformed(ActionEvent evt) {
^
3 errors

Tool completed with exit code 1

dmgs11 Feb 19th, 2006 1:35 pm
Re: Applet / Array help
 
PLEASE DIS-REGARD THE PRIOR MESSAGE...
\Mortgage.java:79: ';' expected
bttn.addActionListener( new ActionListener()) { ^
\Mortgage.java:88: ';' expected
bttn.addActionListener( new ActionListener()) {
^
\Mortgage.java:95: ';' expected
bttn.addActionListener( new ActionListener()) {
^
3 errors

Tool completed with exit code 1

server_crash Feb 19th, 2006 1:53 pm
Re: Applet / Array help
 
Where's the closing parenthesis and semi-colon for the follow codes:
            bttn.addActionListener( new ActionListener() {                                                       }
                      public void actionPerformed(ActionEvent evt) {
                                    // This method will respond to the user's click entry for the mortgage term & interest
                                                i = 0;
                                        MainForm.add(i);

                                  }

dmgs11 Feb 19th, 2006 2:24 pm
Re: Applet / Array help
 
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.lang.*;
import java.text.*;
import java.util.*;

public class Mortgage extends javax.swing.JApplet implements ActionListener {


    /*Create Arrays*/
    double[] annualInterests  = {5.35,5.5,5.75}; //Interest Array
    int[] numOfYears    = {7,15,30};                        //Term Array


        // Declare variables
    double monthlyPayment, monthlyInterest, denominator, numOfMonths, getMortgageAmount;
        double principal;
        double interest;
        int term, i;
        float paymentdue; /*The variable used for user output for exact payment due information*/


    //Constructing Labels for GUI
    JLabel title = new JLabel("  Mortgage Calculator  ");
    JLabel title = new JLabel("  Please enter Principal amount and select term with rate ");
        JTextField appPrincipal=new JTextField(10);  // Principle Loan Amount
        JTextField appTerm=new JTextField(2);  // Term in Years "00"
        JTextField appInterest=new JTextField(5);  // Interest %

    JTextField appMonthlyPayment=new JTextField(10);
        JLabel ErrorMsg=new JLabel("");

        Font bigFont = new Font("Helvetica", Font.ITALIC, 24);

        //* Constructing Applet Container
        public void init()
        {
                Container con = getContentPane();
                con.setLayout(new BorderLayout());

        JPanel TopLabel=new JPanel();
            TopLabel.setLayout(new FlowLayout());
                  title.setFont(bigFont);
                con.add("North",title);

                                  // Initialize the applet by setting it to use blue
                  // and yellow as background and foreground colors.
                      setBackground(Color.blue);
                      setForeground(Color.yellow);

    JPanel MainForm=new JPanel();
    MainForm.setLayout(new GridLayout(0,3));
    JLabel label1=new JLabel(" Term ",SwingConstants.RIGHT);
    MainForm.add(label1);
                          JButton bttn = new JButton(" 7 Years at 5.35% ");
                      bttn.addActionListener( new ActionListener()) {            // Created an "action listener" for the button which is defined by this anonymous class.
                                                  }

                      public void actionPerformed(ActionEvent evt) {
                            // This method will respond to the user's click entry for the mortgage term & interest
                                                i = 0;
                                        MainForm.add(i);
                                            }

                                                JButton bttn = new JButton(" 15 Years at 5.5% ");
                                              bttn.addActionListener( new ActionListener()) {
                                        }

                                    public void actionPerformed(ActionEvent evt) {
                                      i = 1;
                                      MainForm.add(i);
                                                }

                                                JButton bttn = new JButton(" 30 Years at 5.75% ");
                                    bttn.addActionListener( new ActionListener()) {
                                          }

                                  public void actionPerformed(ActionEvent evt) {
                                      i = 2;
                                      MainForm.add(i);
                                      }


        JLabel label2=new JLabel(" Principle ",SwingConstants.RIGHT);
        MainForm.add(label2);

        MainForm.add(appPrincipal);

        JLabel label3=new JLabel(" Interest Rate ",SwingConstants.RIGHT);
        MainForm.add(label3);

        MainForm.add(appInterest);

        JLabel label4=new JLabel("Result ",SwingConstants.RIGHT);
                MainForm.add(label4);

        MainForm.add(appMonthlyPayment);

                con.add("West",MainForm);

        JPanel ButtonPanel=new JPanel();
        ButtonPanel.setLayout(new BorderLayout());
        ButtonPanel.add("North",ErrorMsg);
        JButton Calculate=new JButton("Calculate");

//add an action listener
        Calculate.addActionListener(this);

        ButtonPanel.add("Center",Calculate);
        con.add("South",ButtonPanel);
        appMonthlyPayment.setEditable(false);


        }

        public void start()
        {
                repaint();
        }

        //Ask For User Input And Divide Input
        public void actionPerformed(ActionEvent divide)
        {

            DecimalFormat decimalPlaces=new DecimalFormat("0.00");
            appMonthlyPayment.setText("");

        // The next 3 lines get the values from the text boxes.
            term = Integer.parseInt(appTerm.getText());
            interest = Double.parseDouble(appTerm.getText());
            principal = Double.parseDouble(appPrincipal.getText());


        //Calculate Monthly Interest
        monthlyInterest = annualInterests[i]/(12*100);
    int totalNumOfMonths = numOfYears[i]*12;

        int months= term*12; //The variable used for user input for exact month information
        monthlyInterest = interest/(12 * 100);  //calculate the monthly interst using simple interest.
        denominator = Math.pow(1 + monthlyInterest, -(term*12));
        denominator = 1 - denominator;
        monthlyPayment = principal * (monthlyInterest / denominator);


  // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
  // M = P * ( J / (1 - (1 + J) ** -N));
  // source: http://www.hughchou.org/calc/formula.html
  monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
  return monthlyPayment;



  //calculate the monthly interst using simple interest.
  double getMonthlyPrincipal, monthlyPayment, remainingPrincipal;

  return monthlyPayment - (remainingPrincipal * monthlyInterest);


    // Finally, set the text field to show the result
        appMonthlyPayment.setText("Payment = " + monthlyPayment);

  public void actionPerformed(ActionEvent e)
    {
      try
      { // Capture the Info the User Entered, remove 's and make it a double

        MortgageInfo.setInrestRatePercent(Double.parseDouble(IntrestBox.getText().replaceAll(",","")));
        MortgageInfo.setloanAmount(Double.parseDouble(MortgageBox.getText().replaceAll(",","")));
        MortgageInfo.setloanYears(Double.parseDouble(TermBox.getText().replaceAll(",","")));  // Get & Format the Results
        double test=MortgageInfo.getpaymentAmount();
        DecimalFormat myFormatter = new DecimalFormat("$###,###.00");
        MonthlyPayment.setText("<html><font color=blue>"+myFormatter.format(test)+"</font></html>");
              }
              catch(Exception except)
              {

      }

 public static void main(String[] args)
    {
          JFrame.setDefaultLookAndFeelDecorated(true); // Make it pretty
      JFrame frame=new briano_week2(); // Create a new Frame
      frame.setSize(250,180); // Set the Size of the Frame
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the User to Quit
      frame.setVisible(true);
    }

        }  //End Program

server_crash Feb 19th, 2006 5:32 pm
Re: Applet / Array help
 
You just made it worse:

                       bttn.addActionListener( new ActionListener()) {             // Created an "action listener" for the button which is defined by this anonymous class.
                                                  }

If you don't know how to add and use an actionlistener, then please read up on it.

dmgs11 Feb 19th, 2006 7:07 pm
Re: Applet / Array help
 
Thanks for the HELP!


All times are GMT -4. The time now is 9:18 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC