{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework7 - 15-121 FALL 2009[CORTINA/REID-MILLER HOMEWORK 7...

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

View Full Document Right Arrow Icon
15-121 FALL 2009 [CORTINA/REID-MILLER] HOMEWORK 7 - due Thursday, November 5 by 11:59PM ELECTRONIC HANDIN AVAILABLE SATURDAY BY 5PM PROBLEMS (10 pts) For the following problems that require recursion, your solution may not use iterative looping constructs like for and while . 1. (1 pt) a. Consider the following method, which correctly computes and returns the value of b n (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); else return 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 b n . 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 + " ");
Image of page 1

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

View Full Document Right Arrow Icon
} } 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 recursion to implement the method strBracketLength that 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 11 strBracketLength("Tom15121Tom", "1") returns 5 strBracketLength("Tom15121Tom", "2") returns 1 strBracketLength("Tom15121Tom", "t") returns 0 Your method should throw an IllegalArgumentException if 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 recursion to implement the method isPalindrome that 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 true isPalindrome(new int[]{1, 2, 1}) returns true isPalindrome(new int[]{1, 5, 1, 2, 1}) returns false isPalindrome(new int[]{1}) returns true isPalindrome(new int[]{}) returns true When should your method throw an IllegalArgumentException ?
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern