• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Core Java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Some one please tell me , why the below program output is confusing.



output:

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou to both of you. I am closing with resolved status.
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic