Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Simple Loan Calculator

Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Hey all,

I'm pretty fresh at this Java stuff, so I'm trying to work through the resources at: http://introcs.cs.princeton.edu/java/home/

It seems to have some good info, and has lots of exercises, which I'm pretty pumped about. The problem is, I got stuck relatively quickly.

I'm in Chapter 1.2, looking at the Creative Exercises, #24. I have a program that will do something, but it doesn't seem right.

So you don't have to go check the exercise out on your own, it says: Loan payments. Write a program that calculates the monthly payments you would have to make over a given number of years to pay off a loan at a given interest rate compounded continuously, taking the number of years t, the principal P, and the annual interest rate r as command-line arguments. The desired value is given by the formula Pe^(rt). Use Math.exp().

Well, I couldn't get Math.exp to work for me, so some research made me use Math.pow.

Anywho, what am I doing wrong?

Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Thank you for quoting the question and its source.
When they said Math#exp, they meant Math#exp. What you have is P to the power of rt and what you are supposed to produce is P times e to the power of rt. What went wrong when you tried exp?

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Thanks for the quick reply! I think my first confusion was e. I didn't know what was meant by that, so I thought it was just Period. Where does e come from? They don't really go out of their way to explain how to 'use' Math.exp.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Are you sure they gave you the right formula? I looked up loan calculating and the correct name for the process is loan amortisation. You will find formulae here and here. You will also find those are exactly the same formula, even using the same abbreviations

Remember that 6% is 0.06 and 6% is 0.005/month.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Ah, well I'm not really sure of anything in this crazy, mixed up world, but I'll try it with another formula. I assumed (which I'm sure is part of the problem) that the formula given made the translation into months, but I did manage to remember that 6% is 0.06.

After looking at the link you provided for Math.exp, I still wasn't sure how to actually apply it here. I'm sure I'm just being dense.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
It means Euler's number, which is the base of natural logarithms. You can calculate eⁿ by
n⁰ ÷ 0! + n¹ ÷ 1! + n² ÷ 2! + n³ ÷ 3! + n⁴ ÷ 4! + n⁵ ÷ 5! + n⁶ ÷ 6! + …
When 0! = 1, and 1! = 1 and n⁰ = 1, and n¹ = n. If n = 1, then you get e¹ [= e] = 2.718…

There will probably be a million‑page long article about it on Wikipedia.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Math.exp(r * t)

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Alright, so for this program, would I have to calculate e on my own to get it in there, or is there a reserved way to use it?

lowercase baba
Posts: 13063
67
• Number of slices to send:
Optional 'thank-you' note:

Scott Mac Donald wrote:Alright, so for this program, would I have to calculate e on my own to get it in there, or is there a reserved way to use it?

if you look at the first link Campbell posted, it takes you right to the Math.exp() method's definition:

Returns Euler's number e raised to the power of a double value.

you can also explicity get E or Pi by referring to them using Math.E or Math.PI (I think I have those right...)

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Math.exp(r * t) will give you the exponential expression which is mentioned in that formula, but I suspect you have got the wrong formula, since it is so different from what I found on the other two links I posted earlier.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Ok, I think I get what you're saying Fred. So now I have this.

When I use 10 (for years) 10000 (for Principal) and 0.06 (for rate) it tells me 18221, which I can only guess is the total I would pay after the term, but doesn't give me a per month. Would I have to take that and divide it by months in the term?

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Ok Campbell, I'll take a look at those and just see what I can come out with. If this equation is not what it should be, I suppose I'll never get what I expect!

Thanks guys!

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Use println or printf in preference to print.
Try Note I am used to a different indentation convention and I have used println and added line 9. Otherwise it is what you posted.

campbell@campbellsComputer:~/java\$ java LoanPayment 60 20000 0.00625
29099.828292364025
484.99713820606706

In one of the links I gave earlier they said you would pay \$400.76. Are you given any expected results on the website you quoted?

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Bwahahaha!

So I did a bit of a rebuild. Keeping in mind that I'm doing my best for the math stuff, I checked out the Amortization Calculations link you posted earlier, Campbell, and translated it as best I could into a line of code.

where
A = payment Amount per period
P = initial Principal (loan amount)
r = interest rate per period
n = total number of payments or periods

It compiled on the first try, which really surprised me!

Mind you, when I used the same args as last time, I came out with -9400 a payment, so that seemed odd.

Have a peek:

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:

Are you given any expected results on the website you quoted?

One of the only problems I have with the site is that they don't give solutions to the exercises.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
I would not expect solutions on such a website, but they might have done well to provide example outputs, as I did earlier. I suspect you have simply got the wrong formula. Look at the links I posted earlier.

By the way: this is more complicated arithmetic than about 99% of programming ever uses.

Saloon Keeper
Posts: 9419
79
• Number of slices to send:
Optional 'thank-you' note:
I read through this thread and one of the things that makes it more difficult to follow is the use of single character variable names.

Could be more clearly written as

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
Agree about single‑letter variables. But how do you know the term is in years? Unfortunately, the link shown above uses t P and r in the question, so that will entice people into using single letter.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Actually the question does say:

taking the number of years t,

but I agree, it was confusing with the single letter variables. I'll try to move on and see if I can make some progress on another exercise. Any idea what's wrong with my second attempt based on the actual amortization formula?

Marshal
Posts: 27285
87
• Number of slices to send:
Optional 'thank-you' note:
I don't understand why you divide t by 12 to get the "periods" variable. There are two problems here: (1) I don't understand what "t" is supposed to mean; (2) I don't understand the magic number 12; (3) okay, three problems, I don't know what the user keyed in there.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
No prob Paul.

T is meant to be the Term in years. I divide the years by 12 to get the ...... Oh. Yeah, that should be multiplied. Ok, so that was dumb. I was trying to get the total number of payments.

I'll fix that and re-run to see what happens!

The user is going to enter 10 10000 0.06

-=hrms=- Still gives me -9400. I'm going to try to do the math by hand and see what happens.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
This is where Carey Brown's comments come in. Replace all your variables with something like principal annualRate years months and monthlyRate. I think you are using the wrong rate in the formula.
Also change the print statement so it shows the three basic values:
If you borrow \$1234567.89 over 97 years at 32.1% it will cost \$3456.78.

Paul Clapham
Marshal
Posts: 27285
87
• Number of slices to send:
Optional 'thank-you' note:
You don't have enough brackets in your crazy equation, so the calculation isn't done in the same order as in the equation you posted.

Scott Mac Donald
Ranch Hand
Posts: 31
• 1
• Number of slices to send:
Optional 'thank-you' note:
Ok, so I cleaned up my variables, pounded my head against my super equation for a while and decided to break it down. Here is what I ended up with:

I used the print statements to check the numbers to see if everything was working correctly. After breaking it down and printing it out, I found an error in my smaller equations that resulted in success!

I still don't know how to write it all in one line, but in a case like this, I don't imagine I'm adding any real extra processing time.

Thanks for the help everyone!

Paul Clapham
Marshal
Posts: 27285
87
• Number of slices to send:
Optional 'thank-you' note:
That's a good piece of work, Scott. Most people think that programmers write code, but (as you're finding out) we mostly spend our time trying to figure out why our code doesn't work.

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
And also, if you can't get the formula to work on one line, how much easier it is to separate it into several lines. And how useful intermediate print statements are. And it produced 400.76. Well done

You should break the print statement into several lines; that is easy enough if you insert a new line and some additional indentation adjacent to some of the + operators.Even if the comma looks peculiar out on its own. Also put spaces around binary operators. Not r*t but r * t (old variable names). Don't double‑space code. Use blank lines to divide it into sections instead.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
Well thanks everyone for all the help! It turns out that the second exercise was much easier! Campbell, what's the theory behind using multiple lines for the print statement?

fred rosenberger
lowercase baba
Posts: 13063
67
• Number of slices to send:
Optional 'thank-you' note:

Scott Mac Donald wrote: Campbell, what's the theory behind using multiple lines for the print statement?

Because one really long line doesn't render well on this site. Do you see how in your post, there is a scroll bar? (you may not depending on your screen size and resolution) And because many people find it easier to read in general.

Scott Mac Donald
Ranch Hand
Posts: 31
• Number of slices to send:
Optional 'thank-you' note:
So less a Java reason and more a site posting reason? I suppose it's a pain in the arse to scroll sideways any time you don't need to, so maybe I should just do it that way anyway....

Campbell Ritchie
Marshal
Posts: 76079
362
• Number of slices to send:
Optional 'thank-you' note:
It becomes a Java® reason if you try to print your program on paper and the long line wraps awkwa
rdly at the edge of the sheet and you get half a word on one line and the other half on ano
ther line

Paul Clapham
Marshal
Posts: 27285
87
• Number of slices to send:
Optional 'thank-you' note:

Scott Mac Donald wrote:So less a Java reason and more a site posting reason? I suppose it's a pain in the arse to scroll sideways any time you don't need to, so maybe I should just do it that way anyway....

Yes, readability is always an important thing. We already went through the reason for meaningful variable names in this thread, and making your code not too wide is another readability issue. As you say, it's a pain to scroll back and forth to see the whole line. And even if you're the only one who is going to read the code it should be readable, because when you come back to the code six months later you're going to be in the same position as somebody else who never saw it before.

I know, these are homework problems and you're never going to look at them again after they're turned in. Just another example of how standard teaching techniques lead people into bad habits which have to be unlearned later.

 Consider Paul's rocket mass heater.