homework7_001

homework7_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 7 - due...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [CORTINA]HOMEWORK 7 - due Friday, November 6 by 11:59PMELECTRONIC HANDIN AVAILABLE BY MONDAY 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(n-1);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(n-1);}}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 given in the second parameter. 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 isArrayPalindromethat 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, isArrayPalindrome(new int{1, 2, 4, 4, 2, 1})returns trueisArrayPalindrome(new int{1, 2, 1})returns trueisArrayPalindrome(new int{1, 5, 1, 2, 1})returns falseisArrayPalindrome(new int{1})returns trueisArrayPalindrome(new int{})returns trueWhen should your method throw an IllegalArgumentException?...
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Page1 / 7

homework7_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 7 - due...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online