Help Please

I'm trying to make a program to compound interest using two classes. One with the compounding method and one as the main. I'm having trouble outputting the result from the compounding method through the main. The name of the variable I'm trying to output is "result." I'm required to use two classes and the output must derive from the main. Here's my code; I would appreciate any assistance.

PS. I know that the formula for interest is wrong, I'm just trying to get it working right now. Everything else seems to work fine when I take out the double result from the output line.

Thanks

Compound Class

import javax.swing.*;

publicclass SquiresCI{

publicdouble getFinal(double num1,double num2,double num3)

{

double result = num1 + num2;

return result;

}

}

MAIN CLASS

import javax.swing.*;

publicclass SquiresProject2

{

publicstaticvoid main(String[] args)//start of main method

{

String principalString, interestString, durationString;

double num1, num2, num3;

boolean repeat;

SquiresCI compound =new SquiresCI();

do{

int select;

principalString =

JOptionPane.showInputDialog("Please enter the amount of money you'd like to invest in dollars:");

num1 = Double.parseDouble(principalString);

durationString =

JOptionPane.showInputDialog("How many years will you be investing for?");

num2 = Double.parseDouble(durationString);

interestString =

JOptionPane.showInputDialog("What will the rate of interest be(%)?");

num3 = Double.parseDouble(interestString);

compound.getFinal(num1, num2, num3);

JOptionPane.showMessageDialog(null,"Your final amount will be: " + result);//THIS IS THE PROBLEMATIC LINE

//displays run again dialog -- presents two buttons, one for yes the other for no

select = JOptionPane.showConfirmDialog(null,"Run Again?","Run Again Dialog",JOptionPane.YES_NO_OPTION);

//if yes is selected "runagain" equates to true if no is selected "runagain" equates to false

repeat = (select == JOptionPane.YES_OPTION);

}while (repeat);

[3422 byte] By [dazemana] at [2007-11-27 9:43:52]
# 1
See what this doesJOptionPane.showMessageDialog(null, "Your final amount will be: " +compound.getFinal(num1,num2,num3));
pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 2
You must assign result to the return value of getFinal():result = compound.getFinal(num1, num2, num3);otherwise the result will never be assigned...
johek192a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 3

That worked great man. I tried something similar, but I guess I had it wrong. Now that I've got it working, can anyone help me with Java math please?

I have to use the formula :

startingAmount(1 + InterestRate/100) ^years

I'm not sure how to go about raising the equation to the number of years in java. I prompted the user for num1 num2 and num3 = principal, interest rate, and years of investment, but I don't know how to translate that into java. Any assistance would be appreciated. If you know of a good link for this info, that would work too

Thanks a lot

dazemana at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 4
tell me more about the equation, years is an exponent?
pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 5

Here's the formula:

P is the principal (the initial amount you borrow or deposit)

r is the annual rate of interest (percentage)

n is the number of years the amount is deposited or borrowed for.

A is the amount of money accumulated after n years, including interest.

When the interest is compounded once a year:

A = P(1 + r)n

However, if you borrow for 5 years the formula will look like:

A = P(1 + r)5

dazemana at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 6
but that is to the nth power correct? as in your example to the 5th power?
pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 7
yes
dazemana at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 8
A = P(1 + r)5 is it raised to the fifth power before being multiplied by P or does everything get raised to the fifth
pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 9

Good question; it's been awhile since I took Algebra. Can you tell me how to do one of the two and I'll experiment with it? I just don't know how to raise to powers. If I get that part, I should be able to play with it until I get the right answer.

I think I'm just too tired to do Algebra right now...hehe

dazemana at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 10

try this out. i put the comments in there cuz it helps me to see it while im writing the code. i think the exponent happens first.in the Math class it goes Math.pow(the value a,raised to the specific power)

public double getFinal(double num1, double num2, double num3)

{

/* P is the principal (the initial amount you borrow or deposit)

r is the annual rate of interest (percentage)

n is the number of years the amount is deposited or borrowed for.

A is the amount of money accumulated after n years, including interest.

When the interest is compounded once a year:

A = P(1 + r)n

However, if you borrow for 5 years the formula will look like:

A = P(1 + r)5 */

double a = (1+num3/100);

// double b = num2;

double b = Math.pow( a, num2);

double result = num1*b;

return result;

//startingAmount(1 + InterestRate/100) ^years

//num1 is starting amount

//num2 is years

//num3 is rate of interest

}

pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 11
> A = P(1 + r)5 > > is it raised to the fifth power before being> multiplied by P or does everything get raised to the> fifthP*(1+r)^5It's 1+r raised to fifth power and then multiplied by P.
aniseeda at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 12
let me know if it works and if it does gimme some dukes ok?
pberardi1a at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...
# 13
Works awesome...Thanks a lot for your help man
dazemana at 2007-7-12 23:49:49 > top of Java-index,Java Essentials,New To Java...