Results 1 to 5 of 5
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Question how do i do recursion.....

    i think this sort of explains itself in the comments

    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 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
    Java 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:
    :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

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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:
    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;
                    }
    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:
                    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-

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    By the way, you would probably call the method in your main program with something like this:

    Java Code:
            boxWidth = adjustBoxWidth(numBoxes, boxWidth);
    -Gary-

  4. #4
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Thumbs up

    :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

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Just a couple more details:
    Java Code:
    		if( boxWidth < 10 ){
    			return MIN_BOX_WIDTH;
    		}
    I'm guessing MIN_BOX_WIDTH is 10? If so, then:
    Java 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,
    Java Code:
    		return adjustBoxWidth(numBoxes,boxWidth-5); // no
    		return adjustBoxWidth(numBoxes, boxWidth - 5); // Yes!
    If I don't nag you, who will? :)

    -Gary-

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  2. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 12:52 PM
  3. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 01:13 AM
  4. Recursion help
    By rjg_2186 in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 08:03 AM
  5. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 11:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •