This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 15121 FALL 2009 [CORTINA/REIDMILLER]HOMEWORK 7  due Thursday, November 5 by 11:59PMELECTRONIC HANDIN AVAILABLE SATURDAY BY 5PMPROBLEMS (10 pts)For the following problems that require recursion, your solution may not use iterative looping constructs like forand while. 1. (1 pt) a. Consider the following method, which correctly computes and returns the value of bn(for positive n). public static double power1(double b, int n){if (n == 0)return 1;else if (n % 2 == 1)return b * power1(b, n  1);elsereturn power1(b, n / 2) * power1(b, n / 2);}If we call power1(4, 7), how many times will power1(4, 0)be executed? What is the runtime complexity of power1, in terms of n? b. Now consider the following method, which also correctly computes and returns the value of bn. public static double power2(double b, int n) {if (n == 0)return 1;else if (n % 2 == 1)return b * power2(b, n  1);else {double result = power2(b, n / 2);return result * result;}}If we call power2(4, 7), how many times will power2(4, 0)be executed? What is the runtime complexity of power2, in terms of n? 2. (1 pt) a. Consider the following method: public static void printN(int n) {if (n == 0) {System.out.println();}else {printN(n1);System.out.print(n + " ");}}If we call printN(6), what will it print? b. Now consider the following method: public static void printN2(int n) {if (n == 0) {System.out.println();}else {System.out.print(n + " ");printN2(n1);}}If we call printN2(6), what will it print? 3. (2 pts) Use recursionto implement the method strBracketLengththat has two parameters, a string and a string pattern that finds the length of the longest substring of the first parameter that starts and ends with the pattern string. For example, strBracketLength("Tom15121Tom", "Tom")returns 11strBracketLength("Tom15121Tom", "1")returns 5strBracketLength("Tom15121Tom", "2")returns 1strBracketLength("Tom15121Tom", "t")returns Your method should throw an IllegalArgumentExceptionif the pattern string is empty. public static int strBracketLength(String text, String pattern) {}(Hint: Consider a string that is shorter at either end of the input string.)4. (2 pts) Use recursionto implement the method isPalindromethat returns true if the values in an integer array form a palindrome, and false otherwise. The array is a palindrome if the values read from the first to the last element in the array are the same as the values read from the last to the first element. For example, isPalindrome(new int{1, 2, 4, 4, 2, 1})returns trueisPalindrome(new int{1, 2, 1})returns trueisPalindrome(new int{1, 5, 1, 2, 1})returns falseisPalindrome(new int{1})returns trueisPalindrome(new int{})returns trueWhen should your method throw an IllegalArgumentException?...
View
Full
Document
 Spring '09
 ReidMiller
 Recursion

Click to edit the document details