/** Lab 07. Writing recursive methods */ public class Recursive { /** Yields: number of times c occurs in s. */ public static int numberc(char c, String s) { return 0; // Stub return. Replace this. } /** Yields: number of chars in s that are not c. */ public static int numberNotc(char c, String s) { return 0; // Stub return. Replace this. } /** Yields: a copy of s but with all occurrences of c replaced by d. * Example: replace("abeabe", 'e', '$') = "ab$ab$". * For lab purposes, do NOT use the pre-existing String function replace */ public static String replace(String s, char c, char d) { return ""; // Stub return. Replace this. } /** Yields: a copy of s with adjacent duplicates removed. * Example: for s = "abbcccdeaaa", the answer is "abcdea".*/ public static String removeDups(String s) { return ""; // Stub return. Replace this. } /** Yields: a copy of s but with the FIRST occurrence of c removed (if present). * Note: This can be done easily using indexOf. Don't do that. * Do it recursively.*/ public static String removeFirst(String s, char c) { return ""; // Stub return. Replace this. } /** Yields: the reverse of s. e.g. rev("abc") is "cba". * Do this one using this idea: The reverse of s of * at least one char is the reverse of s[1..] catenated * with s[0]. */ public static String reverse1(String s) { return ""; // Stub return. Replace this. } /** Yields: the reverse of s. e.g. rev("abc") is "cba". * Do this one using this idea. To reverse a string * that contains at least 2 characters, switch the * first and last ones and reverse the middle. */ public static String reverse2(String s) { return ""; // Stub return. Replace this. } /** Yields: the sum of the integers in s. * Precondition: s contains at least one integer and * has the form <integer>:<integer>:...:<integer>, * and all the integers are positive (no - signs). * Note that s contains no blanks * e.g. sumInts("34") = 34. * e.g. sumInts("7:34:1:2:2") is 46. */ public static int sumInts(String s) { return 0; // Stub return. Replace this. } /** Yields: sum of numbers 1 to n. * e.g. sumTo(3) = 1+2+3 = 6, * sumTo(5) = 1+2+3+4+5 = 15 * Precondition: n >= 1. */ public static int sumTo(int n) { return 0; // Stub return. Replace this. } /** Yields: number of the digits in the decimal representation of n. * e.g. numDigits(0) = 1, numDigits(3) = 1, numDigits(34) = 2. * numDigits(1356) = 4. * Precondition: n >= 0. */ public static int numDigits(int n) { return 0; // Stub return. Replace this. } /** = sum of the digits in the decimal representation of n. e.g. sumDigits(0) = 0, sumDigits(3) = 3, sumDigits(34) = 7, sumDigits(345) = 12. Precondition: n >= 0. */ public static int sumDigits(int n) { return 0; // Stub return. Replace this. } /** Yields: the number of 2's in the decimal representation of n. * e.g. number2(0) = 0, number2(2) = 1, number2(234252) = 3. * Precondition: n >= 0. */ public static int number2(int n) { return 0; // Stub return. Replace this. } /** Yields: The number of times that c divides n, * e.g. into(5,3) = 0 because 3 does not divide 5. * e.g. into(3*3*3*3*7,3) = 4. * Precondition: n >= 1 and c > 1 */ public static int into(int n, int c) { return 0; // Stub return. Replace this. } /** Yields b ** c, i.e. b multiplied by itself c times. * Also called b "to the power" c. * Precondition: c � 0 * Hints: b ** 0 = 1. * if c is even, b**c == (b*b) ** (c/2) * if c > 0, b**c = b * (b ** (c-1)). */ public static int exp(int b, int c) { return 0; } public static long number= 0; /** You know that each execution of a method call causes a frame to be created to hold * information about the call --its parameters, local variables, the method name, the scope * box, etc. On your computer, how many frames can be present at one time? To find out, hit * the Reset button and then type these lines in the Interactions pane, one at a time: * * Recursive.number= 0; * Recursive.test(); * Recursive.number * * Look at the body of test. From it, you can see that the number in static * variable number is the number of calls made, and thus frames created, until * there was "stack overflow". Write down that number somewhere. * * Then hit the reset button and perform this test: * * Recursive.number= 0; * Recursive.test(5); * Recursive.number * * Here, method test(int), which has one parameter but many local variables, is called many times. * Compare the number of frames created here with the number created when calling method test(). * Why is one bigger than another? * * Finally hit the reset button and perform this test: * * Rec.number= 0; * Rec.test(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); * Rec.number * * Here, a method with 10 parameters is called many times. Compare the number of frames created * this time to the number of frames created the other times. Do the numbers make any sense to you? */ public static void test() { number= number + 1; test(); } /** Same as test(), but now has a single parameter and 10 local variables. */ public static void test(int b) { int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9; number= number + 1; test(b); } /** Same as test(), but now has 10 parameters */ public static void test(int b0, int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8, int b9) { number= number + 1; test(b0, b1, b2, b3, b4, b5, b6, b7, b8, b9); } }