1 / 13

Recursion

Learn about recursion and how to loop in a different way by calling a method repeatedly until you reach a stopper value. Explore the AddUp method to add up a given number and the StarString method to create a string of stars based on a given number.

awestlund
Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion Pepper

  2. Another way to loop Call 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)

  3. Start by trusting it will work Create 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 __________________; } }

  4. Then, write the stopping code When 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 __________________;} else { // normal work return numberToCount + addUp(numberToCount-1); } }

  5. 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); } } }

  6. Alternative: For public static int addUp(int numberToCount) { int sum = 0; for (int count =1; count <= numberToCount;count++) { sum = sum + count; } return sum; }

  7. Create a string of stars YOU 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)

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

  9. Work with shapes • Make a Rectangle keep getting smaller until it is size 5 wide. • The goal is:

  10. Work with shapes First code without recursion: • create a method that takes in the size of the square. Code it to return 1 slightly smaller square outline on a square. • Call that method from your main method to see one square on your canvas.

  11. Code of just the one square import javalib.worldimages.*; import java.awt.Color; public class Test7 { public static void main() { WorldImage myWorld = printInsideSquares ( 600); myWorld.show(); } public static WorldImage printInsideSquares( int size) { WorldImage mainSquare = AImage.makeRectangle(size,size,Mode.OUTLINED); WorldImage littleSquare = AImage.makeRectangle(size/10*9,size/10*9, Color.RED, Mode.OUTLINED); mainSquare = mainSquare.place( littleSquare, size/2, size/2); return mainSquare; } }

  12. Work with shapes Code the base case: • Add an if statement to return just one square of the given size when the size is < 5. • Code the recursion: • Instead of placing the slightly smaller square on the big square, place a call to your method with that slighly smaller size.

  13. Work with shapes import javalib.worldimages.*; import java.awt.Color; public class Test6 { public static void main() { WorldImage myWorld = printInsideSquares ( 600); myWorld.show(); } public static WorldImage printInsideSquares( int size) { if (size <= 5) { return AImage.makeRectangle(size,size,Mode.OUTLINED); } else { WorldImage mainSquare = AImage.makeRectangle(size,size,Mode.OUTLINED); // WorldImage littleSquare = AImage.makeRectangle(size/2,size/2,Color.RED, Mode.OUTLINED); mainSquare = mainSquare.place( printInsideSquares( size/10*9), size/2, size/2 ); return mainSquare; } } }

More Related