This week's book giveaway is in the Android forum. We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line! See this thread for details.

Lets take the 5 as an example: bunnyEars(5) returns 2 + bunnyEars(4) bunnyEars(4) returns 2 + bunnyEars(3) bunnyEars(3) returns 2 + bunnyEars(2) bunnyEars(2) returns 2 + bunnyEars(1) bunnyEars(1) returns 2 + bunnyEars(0) bunnyEars(0) returns 0 because this is specified as such

So if you put this together: bunnyEars(5) returns 2 + (2 + (2 + (2 + (2 + 0))))

Although they're not needed, some additional braces and an "else" might make this more clear...

So when the number of bunnies reaches zero, then return zero. Otherwise:

Reduce the number of bunnies by 1. (So -- assuming that the initial int value is non-negative -- this will eventually get to zero so the method will stop making recursive calls).

Pass this reduced value back into the method (recursive call).

Add 2 to the value returned by the method.

Return this sum.

In other words, for each bunny (from the initial number of bunnies down to zero), add 2 ears to a cummulative sum. When you're done counting down, return this sum.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

First you give a value for the int variable bunnies when the bunnyEars() method is called. Then inside the method it checks if the value of bunnies is zero. If it is zero, then returns the values zero, giving the number of bunnyEars as zero (Obviously if the number of bunnies is zero, how can there be any ears). With this the method returns.

If the bunnies is not equals to zero, return the value corresponding to the following statement; 2 + bunnyEars(bunnies-1)

Let's analyze this with an example. Lets say the number of bunnies=5. Then the method does not return zero, instead it returns the above value.

2 + bunnyEars(bunnies-1) can be written as 2 + bunnyEars(4) since initially bunnies=5. Then bunnyEars() method is called again within the same method itself for the value bunnies=4. This goes on as follows;

When you substitute [B], [C], [D], [E] in to [A], you get the value 10 for number of ears. The process can be analyzed as above for any number of bunnies.

What happens here is, the method calls itself while reducing the number of bunnies of which the ears need to be counted and adding 2 for each bunny reduced for the number of ears.

Since here number of bunnies cannot be less than zero, it's better if you replace if (bunnies==0) with if (bunnies<=0).

Put a reply if any further explanation is needed.

"Simplicity is the ultimate sophistication..." ~Leonardo da Vinci~
SCJP 1.4

Calling bunnyEars with a negative value will end: - the bunny parameter will be decreased until it becomes Integer.MIN_VALUE - another decrease will make it Integer.MAX_VALUE - it will be decreased further until it becomes 0

However, the check could be better. Perhaps as follows:

All depending on what you actually want bunnyEars to calculate