aspose file tools*
The moose likes Java in General and the fly likes Core Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Core Java" Watch "Core Java" New topic
Author

Core Java

Kuyni Kumar
Greenhorn

Joined: Jan 18, 2011
Posts: 14
Hi Some one please tell me , why the below program output is confusing.



output:

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
Kuyni Kumar
Greenhorn

Joined: Jan 18, 2011
Posts: 14
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

Joined: Dec 10, 2010
Posts: 9
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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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

Joined: Jan 18, 2011
Posts: 14
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

Joined: Dec 10, 2010
Posts: 9
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

Joined: Jan 18, 2011
Posts: 14
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

Joined: Dec 10, 2010
Posts: 9
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

Joined: Jan 18, 2011
Posts: 14
Thankyou to both of you. I am closing with resolved status.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java