This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Core Java

 
Kuyni Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Some one please tell me , why the below program output is confusing.



output:

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Can you please TellTheDetails? - Like what output you expected, what is the mistake in the output.

Also please UseCodeTags around your source code.
 
Kuyni Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thanks for responding immediately.

1. As per my code, It should iterate 3 times [as per the values of total_months = 3; starting_month = 1;]
It is does the same, It print the below sysout as a last statement.

System.out.println("After "+total_months+" you will get "+(total - (monthly_deposit_amt * total_months))+" for "+(monthly_deposit_amt * total_months));

Problem here is: The last Sysout is printed 4 times instead of 1 time. Please let me know still you need more info.
You can execute the program and observe the output.
 
Sreelatha Sankaranarayanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have modified your code to achieve what you want , please take a look and see if this what you want.




This is the output you get after running the modified program, which i think is what you want to achieve (I have formatted the output a little for clarity)

Month 1 : Amount Deposited = 2000.0
Interest = 19.2
Total = 2019.2
Month 2 : Amount Deposited = 2000.0
Interest = 38.58432
Total = 4057.78432
Month 3 : Amount Deposited = 2000.0
Interest = 58.15472947200001
Total = 6115.939049472
After 3 months you will get 115.93904947200008 for 6000.0
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As its a recursive call- the calculate() method. It prints the string mentioned by you more than once. So you would have to move that print statement out of the calculate() method. May be you can call it in the main method.
 
Kuyni Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohamed and Sreelatha

you are great, Thanks for solving this.
My final question, Any of you please eplain how JVM treating my previous code to print the sysout more than expected time?
 
Sreelatha Sankaranarayanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kuyni,

As Mohammad said it, in your prg you had the sys out in a method that was recursively called, and it will be called for total months+1 times which is 4 in your case, this was the reason why you had it printed 4 times when you expected it to be printed only once.
 
Kuyni Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my output is after the recursive call. Once the condition is false, Then it come out of the if condition [the recursive call is inside the if], Then it should exectued once.
If I am wrong, Is there any limitation, that "After recursive call we should not keep any statements"?
 
Sreelatha Sankaranarayanan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way recursion works is that, when a method calls itself recursively, the method invocation is kept on a stack each time the method is invoked (the parent method does not return as completed), once the recursive calls exit, then the method invocation completes itself from the stack , prior to the method completion or return the sysout gets printed, which is why you see the sysout printed the number of times the method was invoked.
 
Kuyni Kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou to both of you. I am closing with resolved status.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic