Link to home
Start Free TrialLog in
Avatar of drivenit
drivenit

asked on

Java help

Hey all

Well I am still having trouble with my application
I have 3 fields that have to put out 6 calculations. the calculations are as fallows are stated here
p,loan amount or principal
n,number of payments = payments per year * number of years
i,interest rate per pay period = APR/payments per year = APR/12
t,interest paid = interest rate per pay period * previous principal balance
r,monthly payment amount = principal * interest per period / (1-(1+(interest per period)/100)^(number of payments-1)^2)
a, principle amount = monthly payment amount – interest paid
b, principle balance = previous balance – principle amount

I can't get it to except the apr because of it wont take a decimal i think this is due to integer value and not sure how get it to output more then one calculation Any help would be much appreciated here is the code..


/*      This is an example of a Java Swing Applet
 *      The first three import statements, java.awt.*,
 *      java.awt.event.*, and javax.swing.* must be included
 *      in the creation of any applet.  The remaining four
 *      are used for some of the math, input/output, language
 *      and utility functions that are used.  Not all of them
 *      are used in this applet,
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
import java.util.*;
import java.io.*;
import java.lang.*;

/*      Notice this is
 *      how the calculation gets started from the button
 */

public class IP5 extends JApplet implements ActionListener
      {
            // Variables declaration. For this applet, not all
            // declared labels and textfields are used
          private javax.swing.JPanel jPanel2;
            private javax.swing.JPanel jpanel3;
          private javax.swing.JLabel jLabel2;
          private javax.swing.JTextField jTextField1;
          private javax.swing.JLabel jLabel3;
          private javax.swing.JTextField jTextField2;            
          private javax.swing.JLabel jLabel4;
          private javax.swing.JTextField jTextField3;
          private javax.swing.JLabel jLabel5;
          private javax.swing.JTextField jTextField4;
          private javax.swing.JButton jButton1;
          private javax.swing.JButton jButton2;
          private javax.swing.JButton jButton3;
            private javax.swing.JTextArea resultArea;
          private Container container;
          // End of variables declaration
 
    /** Initialization method that will be called after the applet is loaded
     *  into the browser.  Change the setSize(x,y) values to resize the applet
       *  when using appletviewer
     */
    public void init () {
        initComponents();    
        setSize(800, 600);        
    }

    private void initComponents() {
       
        container = getContentPane();
            // The .setLayout creates the different layouts available.
            // This applet uses BorderLayout()
        container.setLayout( new BorderLayout() );
         
            // Creating instances of each item  
            jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        resultArea = new javax.swing.JTextArea();
            // End Creation
       
        // Set text on labels, preferred size can be optional on labels,
            // size should/must be used on text fields.
            // Then each individual item is added to a panel.
        jLabel2.setText("Loan Amount = ");
//      jLabel2.setPreferredSize(new java.awt.Dimension(75, 16));
        jPanel2.add(jLabel2);
       
        jTextField1.setText("");
        jTextField1.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField1);
       
        jLabel3.setText("APR amount =");
//            jLabel3.setPreferredSize(new java.awt.Dimension(75, 16));
        jPanel2.add(jLabel3);
       
        jTextField2.setText("");
        jTextField2.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField2);

        jLabel4.setText("How Many Payments = ");
//      jLabel4.setPreferredSize(new java.awt.Dimension(95, 16));
        jPanel2.add(jLabel4);
       
        jTextField3.setText("");
        jTextField3.setPreferredSize(new java.awt.Dimension(65, 20));
        jPanel2.add(jTextField3);
       
     
       
       
      // now the jPanel2 object is set in the borderlayout NORTH position.
      // you must use NORTH, SOUTH, EAST, WEST or CENTER.
      // In this case, only NORTH, SOUTH and CENTER are used.
        container.add( jPanel2, BorderLayout.NORTH);
        jButton1.setText("Click Here To Calculate");
            jButton1.addActionListener(this);
        container.add(resultArea, BorderLayout.CENTER);
        container.add(jButton1, BorderLayout.SOUTH);
    }
   
   public void actionPerformed(ActionEvent e)
      {
      try
            {
                  // The text fields pass strings, so you must create integers out of the field values.
                  int p=Integer.parseInt(jTextField1.getText()), i=Integer.parseInt(jTextField2.getText()),
                     n=Integer.parseInt(jTextField3.getText()), temp;

                  resultArea.setText("");
                  
                  // This applet should caculate the monthly payment, Interest per pay period, Number of pay periods
                        // Amortization shcedule .
                  if (p>i)
                        temp = ( p*i / (1-(1+(i)/100)^(n-1)^2));
                  else
                        temp = ( p*i / (1-(1+(i)/100)^(n-1)^2));
                        resultArea.setText("" + temp);
            }
            catch (Exception ex)
            {
                  // this gives an error when an exception accurs
                  resultArea.setText("Input properly please!");
            }      
      }
}
Here is the HTML to call the applet

<HTML>

<HEAD>

<TITLE>Unit 5 IP Add</TITLE>

</HEAD>

<BODY>

Here is the output of my program:
<APPLET CODE="IP5.class"WIDTH="600" HEIGHT="200">

</APPLET>

</BODY>

</HTML>
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial