Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add...

10
Recursion Pepper

Transcript of Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add...

Page 1: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Recursion

Pepper

Page 2: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Another way to loopCall yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count-- Ouput – total of all those numbers added up

Logic:If the number I am counting is 0, just return 0. If the number I am counting is anything else, return the number I have

+ the total of all the rest (the number I have – 1)

Requires:• Trust the function will do its job• A stopper• A way to reach the stopper (decrement or increment)

Code the addUp method and call it to addUp(10)

Page 3: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Start by trusting it will workCreate a method that assumes it will do the job it says it will:

Return the number you have + everything else that needed to be done

public static int addUp(int numberToCount) { // something else will have to be put here return numberToCount + addUp(numberToCount-1); } }

Page 4: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Then, write the stopping codeWhen should it stop calling itself?What should it do in that last case?

public static int addUp(int numberToCount) { if (numberToCount == 1) // stop when you are down to #1 { // last called case work return numberToCount;} else {

// normal work return numberToCount + addUp(numberToCount-1); } }

Page 5: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

addUp Solution public class AddUp{ public static void main() { System.out.println(addUp(10)); } public static int addUp(int numberToCount) { if (numberToCount == 1) { return numberToCount;} else { return numberToCount + addUp(numberToCount-1); } }}

Page 6: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Alternative: For

public static int addUp(int numberToCount) { int sum = 0; for (int count =1; count <= numberToCount;count++) { sum = sum + count; } return sum; }

Page 7: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Create a string of starsYOU TRY:starString routine-- Input – number of stars needed-- Ouput – String of that many stars

Logic:If the number I am counting is 1, just return 1 star. If the number I am counting is anything else, return one star + all the rest of the

stars (the number I have – 1)

Requires:• Trust the function will do its job• A stopper• A way to reach the stopper (decrement or increment)

Code the starString method and call it to create starString(10)

Page 8: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

String of Stars

public static String starString (int numberStars) { if (numberStars == 0) { return(""); } else { return "*" + starString(numberStars-1); } }

Page 9: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Work with shapes

• Divide a rectangle up 4 levels deep– First print 1 big square– Then divide that one square – Test fully– Then create another method to

printInsideSquares with input: level, paintbrush, starting x,y and size.

• Stop at level 1 – just print the square with no division

• At every other level, print a big square and printInsideSquares for the smaller squares

Page 10: Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.

Work with ShapesFirst hint: Here is how to draw 1 level of 4 squares inside 1 square: public static void printOneInsideSquares(int level, Graphics g, int x, int y, int size) { g.drawRect(x,y,size,size); g.drawRect( x,y,size/2,size/2); // top left g.drawRect( x+(size/2), y,size/2,size/2); // top right g.drawRect( x,y+size/2,size/2,size/2); // bottom left g.drawRect( x+size/2,y+size/2,size/2,size/2); // bottom right }

Now – add the level logic:stopper: If level == 1, just draw the main rectangle do a job and call itself to do the rest: If level higher, draw main

rectangle and printInsideSquares of the small squares at the next level.