posted 2 years ago

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

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?

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?

posted 2 years ago

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

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

Greenhorn

Posts: 24

Campbell Ritchie

Marshal

Posts: 56608

172

posted 2 years ago

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.

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

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

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.

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: 56608

172

posted 2 years ago

It means Euler's number, which is the base of natural logarithms. You can calculate

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

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

*eⁿ*byn⁰ ÷ 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.

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

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...)

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...)

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie

Marshal

Posts: 56608

172

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

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?

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

Greenhorn

Posts: 24

Campbell Ritchie

Marshal

Posts: 56608

172

posted 2 years ago

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.

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.

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?campbell@campbellsComputer:~/java$ java LoanPayment 60 20000 0.00625

29099.828292364025

484.99713820606706

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

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)

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:

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 periodn = 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

Greenhorn

Posts: 24

Campbell Ritchie

Marshal

Posts: 56608

172

posted 2 years ago

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.

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

posted 2 years ago

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

Could be more clearly written as

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Campbell Ritchie

Marshal

Posts: 56608

172

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

Actually the question does say:

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?

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?

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

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.

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: 56608

172

posted 2 years ago

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.

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.

Scott Mac Donald

Greenhorn

Posts: 24

posted 2 years ago

- 1

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!

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!

Campbell Ritchie

Marshal

Posts: 56608

172

posted 2 years ago

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.

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

Greenhorn

Posts: 24

posted 2 years ago

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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

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

Greenhorn

Posts: 24

Campbell Ritchie

Marshal

Posts: 56608

172

posted 2 years ago

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

rdly at the edge of the sheet and you get half a word on one line and the other half on ano

ther line

posted 2 years ago

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.

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.

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |