19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20.4 Problem Solving Using Recursion 745<br />

Recursion is everywhere. It is fun <strong>to</strong> think recursively. Consider drinking coffee. You may<br />

describe the procedure recursively as follows:<br />

think recursively<br />

public static void drinkCoffee(Cup cup) {<br />

if (!cup.isEmpty()) {<br />

cup.takeOneSip(); // Take one sip<br />

drinkCoffee(cup);<br />

}<br />

}<br />

Assume cup is an object for a cup of coffee with the instance methods isEmpty() and<br />

takeOneSip(). You can break the problem in<strong>to</strong> two subproblems: one is <strong>to</strong> drink one sip of<br />

coffee and the other is <strong>to</strong> drink the rest of the coffee in the cup. The second problem is the<br />

same as the original problem but smaller in size. The base case for the problem is when the<br />

cup is empty.<br />

Consider the problem of printing a message n times. You can break the problem in<strong>to</strong> two<br />

subproblems: one is <strong>to</strong> print the message one time and the other is <strong>to</strong> print it n - 1 times. The<br />

second problem is the same as the original problem but it is smaller in size. The base case for<br />

the problem is n == 0. You can solve this problem using recursion as follows:<br />

public static void nPrintln(String message, int times) {<br />

if (times >= 1) {<br />

System.out.println(message);<br />

nPrintln(message, times - 1);<br />

} // The base case is times == 0<br />

}<br />

Note that the fib method in the preceding section returns a value <strong>to</strong> its caller, but the<br />

drinkCoffee and nPrintln methods are void and they do not return a value.<br />

If you think recursively, you can use recursion <strong>to</strong> solve many of the problems presented in<br />

earlier chapters of this book. Consider the palindrome problem in Listing 9.1. Recall that a<br />

string is a palindrome if it reads the same from the left and from the right. For example,<br />

“mom” and “dad” are palindromes, but “uncle” and “aunt” are not. The problem of checking<br />

whether a string is a palindrome can be divided in<strong>to</strong> two subproblems:<br />

■ Check whether the first character and the last character of the string are equal.<br />

■ Ignore the two end characters and check whether the rest of the substring is a<br />

palindrome.<br />

The second subproblem is the same as the original problem but smaller in size. There are two<br />

base cases: (1) the two end characters are not the same, and (2) the string size is 0 or 1. In case<br />

1, the string is not a palindrome; in case 2, the string is a palindrome. The recursive method<br />

for this problem can be implemented as shown in Listing 20.3.<br />

recursive call<br />

think recursively<br />

LISTING 20.3<br />

RecursivePalindromeUsingSubstring.java<br />

1 public class RecursivePalindromeUsingSubstring {<br />

2<br />

3<br />

public static boolean isPalindrome(String s) {<br />

if (s.length()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!