[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Java help

Posted on 2011-04-24
Medium Priority
384 Views
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));

jTextField1.setText("");
jTextField1.setPreferredSize(new java.awt.Dimension(65, 20));

jLabel3.setText("APR amount =");
//            jLabel3.setPreferredSize(new java.awt.Dimension(75, 16));

jTextField2.setText("");
jTextField2.setPreferredSize(new java.awt.Dimension(65, 20));

jLabel4.setText("How Many Payments = ");
//      jLabel4.setPreferredSize(new java.awt.Dimension(95, 16));

jTextField3.setText("");
jTextField3.setPreferredSize(new java.awt.Dimension(65, 20));

// 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.
}

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
}
}
}
Here is the HTML to call the applet

<HTML>

<BODY>

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

</APPLET>

</BODY>

</HTML>
0
Question by:drivenit
1 Comment

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 35458936

I corrected your code - it  should work now.
I recommend that you first try in Appletviewer.
As in your HTML code you set small size for the applet - is should be about 800,600
as you set up in the code.
Anyway - wtith browser could be additional issues,
so better try in Applet viewer.

Don't understand what you mean about 6 calculations
but with the input in all three boxes

100000    5.5   30

(amount = 100000\$, APR 5.5% and 30 years)

you should get the result you see in the output box below

``````/*      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.*;
import java.text.DecimalFormat;

/*      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));

jTextField1.setText("");
jTextField1.setPreferredSize(new java.awt.Dimension(65, 20));

jLabel3.setText("APR amount, % =");
//            jLabel3.setPreferredSize(new java.awt.Dimension(75, 16));

jTextField2.setText("");
jTextField2.setPreferredSize(new java.awt.Dimension(65, 20));

jLabel4.setText("Term, years = ");
//      jLabel4.setPreferredSize(new java.awt.Dimension(95, 16));

jTextField3.setText("");
jTextField3.setPreferredSize(new java.awt.Dimension(65, 20));

// 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.
}

public void actionPerformed(ActionEvent e)
{
try
{
// The text fields pass strings, so you must create integers out of the field values.
double p=Double.parseDouble(jTextField1.getText());
double i=Double.parseDouble(jTextField2.getText());
double  n=Double.parseDouble(jTextField3.getText());
double 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.0-(1.0+(i)/100.0)^(n-1)^2));
else
temp = ( p*i / (1-(1+(i)/100)^(n-1)^2));
resultArea.setText("" + temp);
*/
DecimalFormat eachpayment = new DecimalFormat ("\$###,##0.00");

temp = ((p * i / 100 / 12) / (1 - Math.pow(1 + i / 100 / 12, - n *12)));
resultArea.setText("Monthly payment: " + eachpayment.format(temp));

}
catch (Exception ex)
{
System.out.println(ex.toString());
ex.printStackTrace();

// this gives an error when an exception accurs
}
}
}
``````

Output:
``````Monthly payment: \$567.79
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month17 days, 22 hours left to enroll