Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate amount due  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Need to find out how long it would take to pay the due amount but for some reason, year is always 1 no mater what i do :|
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that "paid" is greater than zero from the start, so you never enter that loop. I'm not sure why that would be, but I do notice that your calculation is done with different variables from the ones you pass in as parameters. That's probably a source of some of the confusion.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "payment"? What is "interest"? Those are not defined in this method, so it's impossible to argue about it: PostRealCode
 
mike sin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should be fixed now and should add that return should be the amount of time it takes for paid in years
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think even if you go into that loop, you'll never get out.

paid never changes inside the loop.
 
mike sin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm Even If I do
total += (paid++);
(just an example) year is still 1 and always remains one :S
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, paid > 0, so you never enter your loop. I don't know the values you're passing into the method, or where "debt" comes from, but I'm sure that's true.

you should talk through in plain language what you are trying to do. It's always best to get your thoughts straight before coding. Are you computing how many years it would take to pay off a given debt? That would be when remaining debt is zero, not when the paid amount is more than zero. Think about how you'd solve the problem on paper first, then try to make a program to do it.
 
mike sin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, This is what I am basically trying to do
Debt - $1500
internet - 15%
payment - $100/mnth
end of year - (1500*0.15)+1500
payment at end of year = 100(12)
debt left(525) = end of year(1725) - payment at end of year(1200)
so it takes 1 year for all debt to pe paid.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i should tell you is....
until and unless you decrement the variable "paid",
you will never get into that loop...

So that if "paid" is positive at first iteration then it goes into the loop...
then year is will increment... and "paid" is getting decremented... still
condition is true.... It should work like this....
 
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 year? It looks like more than 1 year to me.
 
mike sin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
The calculation of debt is done at the end of the year so it should never reach 2nd year hence why it only takes one year.
I think the only trouble I have is this:
for(year = 1; paid>=0; year++) {
paid -= (year);
}
not sure how to do the for
Thanks!
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mike sin wrote:Hmm, This is what I am basically trying to do
Debt - $1500
internet - 15%
payment - $100/mnth
end of year - (1500*0.15)+1500
payment at end of year = 100(12)
debt left(525) = end of year(1725) - payment at end of year(1200)
so it takes 1 year for all debt to pe paid.



If it took 1 year to pay all the debt, the debt left would be zero I would think. Even with no interest, 100 a month won't pay off a 1500 debt. I'm having trouble understanding where you come up with 1 year for debt to be paid off?
 
Campbell Ritchie
Marshal
Posts: 58421
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need some very specialised pieces of hardware to work out your problem.
  • A pencil
  • A sheet of paper
  • An eraser. Preferably a large one
  •  
    mike sin
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ahhh okay
    i see it now,
    the end will be finished in the starting/middles of 1st year but the computer will find out its done after 2nd year so 2 years to pay the debt off as we are working with just years and not months and etc.
     
    mike sin
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Here's my try with do-while loop, year always returns 2 for some reason the loop only occurs once argh :/
     
    W. Joe Smith
    Ranch Hand
    Posts: 710
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    mike sin wrote:

    Here's my try with do-while loop, year always returns 2 for some reason the loop only occurs once argh :/



    I think your first problem is you mean while(debt>=0), since <0 would mean you have negative debt. Also, you aren't changing the amount of debt in your loop, so whatever debt you have going in is what will always be there.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!