programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# calculating interest using java

Greenhorn
Posts: 2
I'm days into my highschool course and i recently got an assignment to calculate interest, and I'm just completely confused.
hw.PNG

Greenhorn
Posts: 2

Piet Souris
Master Rancher
Posts: 2044
75

welcome to the Ranch!

Don't panic, it is not very difficult.

Say you have an interestperunage of i, meaning that when you start with €1, you have (1 + i) euros after one year, (1 + i)^2 euros after two years, et cetera, (this is what is called "compund interest"), then what equation do you get when your initial € 1 has doubled after n years?

And do you know how to solve that equation, for n = 10, 20 and 30?

Carey Brown
Saloon Keeper
Posts: 3323
46
I'm not sure how they are expecting you to solve this programmatically. You could pick a rate and run it through a compound interest formula then IF it is more than double, reduce the rate and try again. IF it is less than double then increase the rate. Another way is to algebraically take the compound interest formula and solve for rate (this is a paper and pencil exercise), then implement the new formula in Java.

amount = principle * Math.pow(1 + rate/12, 12*years)

If you know that amount is two times the principle and you know the number of years, solve for rate.

Does this help at all?

Carey Brown
Saloon Keeper
Posts: 3323
46
Google: "compound interest solve for rate"

Carey Brown
Saloon Keeper
Posts: 3323
46
Enough time has passed so I don't think posting the answer should be an issue.

Piet Souris
Master Rancher
Posts: 2044
75
Since OP did not respond anymore, I wouldn't have given a solution. But why are you using such a restrictive formula? Way back, we used to tease the students with interest based on a quarter year, but granted at the end of every fourth month, et cetera.

Campbell Ritchie
Marshal
Posts: 56562
172
Piet Souris wrote:. . . interest based on a quarter year, but granted at the end of every fourth month, et cetera.
So you worked in Iceland in 2008?

Piet Souris
Master Rancher
Posts: 2044
75
Hmm... if you earn 50,- per hour, and your salary is granted once every month, is your employer from Iceland?

Campbell Ritchie
Marshal
Posts: 56562
172
You have forgotten what happened in Iceland in 2008.

Knute Snortum
Sheriff
Posts: 4281
127
Piet Souris wrote:Since OP did not respond anymore, I wouldn't have given a solution.

I agree.  No complete solutions until the OP has posted their own.

Piet Souris
Master Rancher
Posts: 2044
75
Campbell Ritchie wrote:
Piet Souris wrote:. . . interest based on a quarter year, but granted at the end of every fourth month, et cetera.
So you worked in Iceland in 2008?

Campbell Ritchie wrote:You have forgotten what happened in Iceland in 2008.

No, I haven't forgotten. And if the Icelanders did what you suggest, I doubt if they would have gone bust.

The idea is of course to go from an interest rate per quarter to an interest rate per a period of four months, and then calculate with periods of four months. That is why I said that Carey's formula is a bit limited, although the idea is the same: recalculate the (what we call 'apparent') interest per year to an interest per month, and calculate in month periods. So it is easier to simply have an interest per some period, and leave the definition of what that might be to the market, or to the loan supplier, or to the one who gives the assignment.

Interest calculus is an incredibly interesting subject, with a range of questions that resembles OCAJP with ease and that one can make as simple or as complicated as one likes. With all the todays valuation according to say Solvency II, we have that every period has its own "yearly" interest rate. So a period of one month has an interest rate of i1 per year, a period of two months has an interest rate of i2 per year, ad infinitum so to speak, making interest calculations even more interesting than they already were.

But I better stop now, before loosing the one person reading this too.