These are videos on Recursion(Helpful take a look): https://youtu.be/coG4zDavl40 https://youtu.be/6adwO_ILBXw Remember, break your problems down into the
Question:
These are videos on Recursion(Helpful take a look):
https://youtu.be/coG4zDavl40
https://youtu.be/6adwO_ILBXw
Remember, break your problems down into the base case (simplest end case) and the recursive case (how to get to the next step).
Put all methods within one file called Recursion_Exercises_LastName.java.
Print three lines of output that look like the three examples for each problem, calling the relevant methods to get the value at the end of each line. (E.g. A line in main may look like: System.out.println("bunnyEars(0) ->" + bunnyEars(0)); to get "bunnyEars(0) -> 0" in the console.)
Regular Recursion Exercises
1. We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication). bunnyEars(0) 0 bunnyEars(1) 2 bunnyEars(2) 4
2. We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2, ... n (without loops or multiplication). bunnyEars2(0) 0 bunnyEars2(1) 2 bunnyEars2(2) 5
3. We have a triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows. triangle(0) 0 triangle(1) 1 triangle(2) 3
4. Given a non-negative int n, return the sum of its digits recursively (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 and converting to an integer removes the rightmost digit (126 / 10 is 12). sumDigits(126) 9 sumDigits(49) 13 sumDigits(12) 3
5. Given a non-negative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 and converting to an integer removes the rightmost digit (126 / 10 is 12). count7(717) 2 count7(7) 1 count7(123) 0
6. Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12). count8(8) 1 count8(818) 2 count8(8818) 4
7. Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared). powerN(3, 1) 3 powerN(3, 2) 9 powerN(3, 3) 27
8. Given a string, compute recursively (no loops) the number of lowercase 'x' chars in the string. countX("xxhixx") 4 countX("xhixhix") 3 countX("hi") 0
9. Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string. countHi("xxhixx") 1 countHi("xhixhix") 2 countHi("hi") 1
10. Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14". changePi("xpix") "x3.14x" changePi("pipi") "3.143.14" changePi("pip") "3.14p"