### Homework 11: Recursion

• Introduction

• This homework is intended to reinforce your recursion programming skills...

• Preparatiion

• Make directory and copy files:

 ``` mkdir ~/cs170/hw11 cp ~cs170002/share/hw11/*.java ~/cs170/hw11 cd ~/cs170/hw11 ```

• Part 1: palindromes

• We have written a isPalindrome(s) method is an earlier homework.

The isPalindrome(s) receives a String typed input parameter s and returns true if s is a palindrome and returns false otherwise.

In this homework, we will write a recursive algorithm to test if a string is a palindrome

• NOTE:

 Your algorithm must use recursion to get any credits BTW, it is easy to detect program that does not use recursion; so please do not try to turn in your previous program as answer.

• How to solve the "palindrome" problem using the solution of a smaller palindrome problem:

 ``` Example 1: Input: "abccbx" Check the first and the last letters: "abccbx" Not equal ===> not a palindrome (done) Example 2: Input: "abccxa" Check the first and the last letters: "abccxa" Equal ===> my answer depends on the solution of this smaller Palindrome problem: bccx Solve this smaller problem with recursion ```

• Base case:

 As discussed in class, every recursive method contains one or more if-statements to determine the bases cases of the problem. A base case is a problem where the solution to the problem can be easily determined

• The base cases in the Palindrome problem

 When the input string is "" the input is a palindrome How do you determine this base case ? (i.e., what if-statement do you use to check ?) And what should you return ? When the input string is a single character "?" the input is also a palindrome How do you determine this base case ? (i.e., what if-statement do you use to check ?) And what should you return ? Another base case (shown in Example 1 above) is: When the first and the last characters in the input string are not equal, the input is not a palindrome How do you determine this base case ? (i.e., what if-statement do you use to check ?) And what should you return ?

• Assignment for Part 1:

• Write the class method isPalindrome in the program file hw11.java

• You can test your isPalindrome method with the test program Palindrome.java as follows:

 ``` javac Palindrome.java java Palindrome ```

The Palindrome.java program will invoke the isPalindrome() method as hw11.isPalindrome(), so you don't need to make any changes to the Palindrome.java file.

Note: don't just use my test program blindly.... you should look inside my test programs also. They should you how your methods are invoked. Very informative....

• Part 2: fold String with a recursive algorithm

• We have also written a foldString(s) method in lab 8. The foldString(s) receives a String typed input parameter s and returns a String consisting of alternately the first and last characters of the input string.

• In lab 8, the fold operation on a string was defined as follows:

 ``` Original string: "c0c1c2.....cn-3cn-2cn-1" Folded string: "c0cn-1c1cn-2c2cn-3....." ```

In this homework, we will write a recursive foldString algorithm

• How to fold a string using the solution of a smaller fold string problem:

 ``` Suppose you want to fold this string: "c0c1c2.....cn-3cn-2cn-1" You can solve it as follows: 1. have someone solve this smaller problem: "c1c2.....cn-3cn-2" 2. using the solution of the above problem, your problem is solved by pre-pending c0cn-1 in front of the solution of the above problem. ```

There are 2 base cases that you need to take care of:

 The empty string (an input string with an even number of character will reduce to an empty string) The string with 1 character (an input string with an odd number of character will reduce to a singleton string)

• Assignment for Part 1:

• Write the class method foldString in the program file hw11.java

• You can test your foldString method with the test program FoldString.java as follows:

 ``` javac FoldString.java java FoldString ```

The FoldString.java program will invoke the foldString() method as hw11.foldString(), so you don't need to make any changes to the FoldString.java tes program file.

• Turnin

• Turn in your hw11.java program file (containing the methods isPalindrome() and foldString()) as follows:

 ``` cd ~/cs170/hw11 /home/cs170002/turnin-hw hw11.java hw11 ```