Results 1 to 5 of 5
Thread: how do i do recursion.....
- 03-17-2010, 03:55 AM #1
how do i do recursion.....
i think this sort of explains itself in the comments
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.Java 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 did this
But this doesnt work either :confused:Java Code:private int checkBoxWidth(int boxes, int boxWidth) { if(boxes*boxWidth<getWidth())return boxWidth; boxWidth-=5; return checkBoxWidth(boxes,boxWidth); }:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-17-2010, 06:47 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
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.Java 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; }
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.Java Code:return adjustBoxWidth(numBoxes, boxWidth - 5); }
-Gary-
- 03-17-2010, 06:55 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
By the way, you would probably call the method in your main program with something like this:
-Gary-Java Code:boxWidth = adjustBoxWidth(numBoxes, boxWidth);
- 03-17-2010, 02:16 PM #4
: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:
Java 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); }:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 03-17-2010, 05:15 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Just a couple more details:
I'm guessing MIN_BOX_WIDTH is 10? If so, then:Java Code:if( boxWidth < 10 ){ 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.Java Code:if( boxWidth < MIN_BOX_WIDTH ){ return MIN_BOX_WIDTH; }
Also,
If I don't nag you, who will? :)Java Code:return adjustBoxWidth(numBoxes,boxWidth-5); // no return adjustBoxWidth(numBoxes, boxWidth - 5); // Yes!
-Gary-
Similar Threads
-
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion
By jachandru in forum New To JavaReplies: 1Last Post: 01-24-2009, 12:52 PM -
Recursion
By Mika in forum New To JavaReplies: 5Last Post: 01-04-2009, 01:13 AM -
Recursion help
By rjg_2186 in forum New To JavaReplies: 1Last Post: 01-02-2009, 08:03 AM -
Please help with recursion
By pheonix in forum New To JavaReplies: 9Last Post: 12-27-2008, 11:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks