# how do i do recursion.....

• 03-17-2010, 04:55 AM
sonny
how do i do recursion.....
i think this sort of explains itself in the comments

Code:

```[COLOR="YellowGreen"]/* checks the number of boxes multiplied by WIDTH is greater than screen width         * if false the box width remains unchanged,         * if true the box width is reduced by 5 pixels and the method         * is called recursively.         */[/COLOR]         private int checkBoxWidth(int boxes, int boxWidth) {                 if(boxes*boxWidth>getWidth()){                         boxWidth-=5;                         checkBoxWidth(boxes,boxWidth);                 }                 return boxWidth;                         }```
so i figured out what is wrong here (i think) I ran this in console and it goes through the motions but, i'm simply returning the initial value i passed,, i only altered the value inside the braces.

so i did this
Code:

```private int checkBoxWidth(int boxes, int boxWidth) {                 if(boxes*boxWidth<getWidth())return boxWidth;                 boxWidth-=5;                 return checkBoxWidth(boxes,boxWidth);         }```
But this doesnt work either :confused:
• 03-17-2010, 07:47 AM
gcalvin
Let me talk a bit about style, first. Code is not just for the compiler to read. It's for you and other programmers who will be looking at your code as well. That's why spacing and indentation is so important for readability. IfItypelikethis,youcanprobablyfigureoutwhatIamsayi ng,butitisn'tveryeasy,right? Same goes for code.

checkBoxWidth() is not a great name for this method, as it's doing more than checking. Consider something like adjustBoxWidth() instead.

The key to recursion is that there is always a "base case" -- a condition where you know you're done, and you don't want to recurse anymore. Then if your condition doesn't satisfy the base case, you do the easy thing and pass off the hard thing to another version of yourself. So in this case, you know you want to return a boxWidth that will allow you to fit numBoxes (a better name than boxes -- boxes is a name for a collection of Box objects) of boxes on the screen. If you already have that, then you just return what you have:
Code:

```        /* checks the number of boxes multiplied by WIDTH is greater than screen width         * if false the box width remains unchanged,         * if true the box width is reduced by 5 pixels and the method         * is called recursively.         */         private int adjustBoxWidth(int numBoxes, int boxWidth) {                 if (numBoxes * boxWidth <= getWidth()) {                         return boxWidth;                 }```
Otherwise, you don't know exactly how small to make boxWidth, so you say, "well, it'll be at least five pixels smaller" and then pass the hard work to another version of yourself.
Code:

```                return adjustBoxWidth(numBoxes, boxWidth - 5);         }```
The key to understanding this is to realize that each invocation of the method gets its own copy of boxWidth. Eventually one of them will be small enough to satisfy the base condition, and then its value will be returned all the way up the stack to the code that called the original method.

-Gary-
• 03-17-2010, 07:55 AM
gcalvin
By the way, you would probably call the method in your main program with something like this:

Code:

`        boxWidth = adjustBoxWidth(numBoxes, boxWidth);`
-Gary-
• 03-17-2010, 03:16 PM
sonny
:eek: I realised that it may not have worked because i forgot to pass the adjusted boxwidth to consrtuct the box :o

but the style points are well noted , many thanks,
and now i have this which works a treat:

Code:

```private int adjustBoxWidth(int numBoxes, int boxWidth) {                 if( boxWidth < 10 ){                         return MIN_BOX_WIDTH;                 }                 if( numBoxes * boxWidth < getWidth()){                         return boxWidth;                 }                 return adjustBoxWidth(numBoxes,boxWidth-5);         }```
• 03-17-2010, 06:15 PM
gcalvin
Just a couple more details:
Code:

```                if( boxWidth < 10 ){                         return MIN_BOX_WIDTH;                 }```
I'm guessing MIN_BOX_WIDTH is 10? If so, then:
Code:

```                if( boxWidth < MIN_BOX_WIDTH ){                         return MIN_BOX_WIDTH;                 }```
...or else someday when you change MIN_BOX_WIDTH you'll have a subtle little bug that will drive you insane.

Also,
Code:

```                return adjustBoxWidth(numBoxes,boxWidth-5); // no                 return adjustBoxWidth(numBoxes, boxWidth - 5); // Yes!```
If I don't nag you, who will? :)

-Gary-