but never terminates if n 2 public static int factint num int result if num 2

But never terminates if n 2 public static int factint

• University of Manitoba
• COMP 1020
• Notes
• tosypitarz
• 428
• 100% (20) 20 out of 20 people found this document helpful

This preview shows page 300 - 303 out of 428 pages.

Thus, the following method attempts to compute n! but never terminates if n 2. public static int fact(int num) { int result; if (num > 2) { result = 1; } else { result = num * fact(num - 1); } return result; }
Chapter 14 Recursion 293 14.4 Processing Strings java.lang.StackOverflowError at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) at Recursion.fact(Recursion.java:115) . . . Any time that a StackOverflowError is generated, it is almost always the result of an incorrectly specified recursive method. 14.4 Processing Strings String processing is a natural candidate for recursion since many string processing routines require that the same steps be repeated until there are no more characters remaining to be processed. 14.4.1 String Reverse The following iterative method accepts a String and returns the characters in the String in the reverse order (i.e. backwards). public static String stringReverse(String string) { String result; int count; result = ""; for (count=string.length()-1; count>=0; count--) { result += string.substring(count, count+1); } return result; } The following recursive method performs the same processing. public static String stringReverse(String string) { String result; if (string.equals("")) { result = ""; } else { result = string.substring(string.length()-1); result += stringReverse(string.substring(0, string.length()-1)); } return result; }
Chapter 14 Recursion 294 14.4 Processing Strings This method extracts the last character from the String, and then calls stringReverse again to extract the remaining characters (in reverse order) from the rest of the String. In this example, each call to stringReverse causes a new copy of all local variables (result in this case) be created. This will be discussed in more detail later in this chapter. The following recursive method generates the same result using a slightly different technique. public static String stringReverse(String string) { //Another recursive String reverse String result; if (string.equals("")) { result = ""; } else { result = string.substring(0,1); result = stringReverse(string.substring(1)) + result; } return result; } 14.4.2 Palindromes A palindrome is a collection of characters that read the same backwards as they do forwards. The following method illustrates how a String can be examined to determine whether or not it is a palindrome (this version is case sensitive). This method has two base cases. The first base case determines whether or not there are zero or one characters remaining in the String: if so, then the String is a palindrome. The next base case compares the first character in the String with the last character; if they are not equal, then the String is not a palindrome. Finally, the recursive case removes the first and last characters and calls the method again.

You've reached the end of your free preview.

Want to read all 428 pages?

• Fall '15