CS1720

Program 11

Recursion

"Recursion captures the infinite in finite form"

 

Background: In this assignment you will be putting aside most of our object oriented concepts in order to practice writing recursive methods. Many of the methods will use recursion to solve problems that could just as easily, if not more easily, be solved using simple iteration, but you will use recursion for the practice. You will collect all of these methods in a single class, Recursive. Some methods that solve the problem recursively still have a normal loop.

 

 

The driver program will be provided for you to test the various methods, but beware, we may use a different driver program to actually test your program.

 

 

1.      Write a method that recursively determines if a String is a palindrome, a word spelled the same backwards and forwards such as deified, racecar, testset, and aibohphobia (the fear of palindromes.) The Base case would be a string of length 0 or 1. The recursive case would be to return true if the first and last letters are the same and the String remaining after stripping off the first and last letters is a palindrome. You only need to worry about strict palindromes. That is all characters should be considered, none should be skipped (not even blanks). This makes the method easier.

 

            The prototype is

public boolean isPali(String word)

 

 

2.      Suppose that you have somehow been transported back to 1777 and the Revolutionary War. You have been assigned a dangerous reconnaissance mission: evaluate the amount of ammunition available to the British for use with their large cannon which has been shelling the Revolutionary forces. Fortunately for you, the British, being neat and orderly, have stacked the cannonballs into a single pyramid-shaped stack with one cannonball at the top, sitting on top of a square composed of four cannonballs, sitting on top of a square composed of nine cannonballs, and so forth. Unfortunately, however, the Redcoats are also vigilant, and you only have time to count the number of layers in the  pyramid before you are able to escape back to your own troops. To make matters worse, computers will not be invented for at least 150 years, but you should not let that detail get in your way. Your mission is to write a recursive method cannonball that takes as its argument the height of the pyramid and returns the number of cannonballs therein.

 

public int cannonball(int levels)

 

3.      On the standard Touch-Tone™ telephone dial, the digits are mapped onto the alphabet (minus the letters Q and Z) as shown in the diagram below:

 

In order to make their phone numbers more memorable, service providers like to find numbers that spell out some word (called a mnemonic) appropriate to their business that makes that phone number easier to remember. For example, the phone number for a recorded time-of-day message in some localities is 637-8687 (NERVOUS). Imagine that you have just been hired by a local telephone company to write a method listMnemonics that will generate all possible letter combinations that correspond to a given number, represented as a string of digits. For example, if you call

listMnemonics("723") 

your method shall generate and print out (using recursion) the following 27 possible letter combinations that correspond to that prefix:

PAD PBD PCD RAD RBD RCD SAD SBD SCD

PAE PBE PCE RAE RBE RCE SAE SBE SCE

PAF PBF PCF RAF RBF RCF SAF SBF SCF

Note, the format of your output is not important. It is fine if there is one mnemonic per line.  The order you print the combinations and number of combinations per line is also unimportant.  You can use the following helper method:

 

public String digitLetters(char ch)
{
    switch (ch) 
    {  case '0': return ("0");
       case '1': return ("1");
       case '2': return ("ABC");
       case '3': return ("DEF");
       case '4': return ("GHI");
       case '5': return ("JKL");
       case '6': return ("MNO");
       case '7': return ("PRS");
       case '8': return ("TUV");
       case '9': return ("WXY");
       default: {System.out.println("Illegal digit"); 
                 System.exit(1);return"";}
    }
}

public void listMnemonics(String number)

You will probably find it useful to create a method that does most of the real work:

 

public void recursiveMnemonics(String mnemonicSoFar, String digitsLeft)

 

 

4.      Write a method to recursively print the binary equivalent of an int N.  Example the binary equivalent of 13 may be found by repeatedly dividing 13 by 2.  If there is a remainder a 1 gets printed otherwise a zero gets printed. 

13 / 2 = 6 r 1
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = 0 r 1

13 in base 2 is 1101.

//pre: N > 0
public void printBinary(int N)

Yes, you have to get the order of the digits correct! They cannot be reversed.

 

5.      Write a recursive method to determine the sum of the digits of an integer. 51624 = 5  + 1 + 6 + 2 + 4 = 18.

public int sumOfDigits(int N)

Hint: use the % operator.

 

6.      Write a program to determine the number of organisms in a matrix.  A matrix is defined as a 2D array of elements. Each cell can be occupied by a cell or empty. An organism consists of a cell and all the cells connected directly to that one via up, down, left, and right movements. Given the following matrix with occupied cells marked by a "*" how many organisms are there?

*

*

 

 

 

 

 

*

 

 

 

*

 

 

 

 

 

*

 

 

 

 

 

 

 

 

*

*

 

 

 

*

 

 

*

*

*

 

 

 

 

*

*

 

*

 

*

 

*

 

 

*

*

 

*

*

*

*

*

*

*

 

 

 

 

 

 

 

*

 

 

 

 

 

 

 

 

 

*

 

 

 

 

*

*

*

 

 

*

 

 

 

 

 

 

*

 

 

*

 

There are 5 organisms as shown below:

 

1

1

 

 

 

 

 

5

 

 

 

1

 

 

 

 

 

5

 

 

 

 

 

 

 

 

5

5

 

 

 

2

 

 

5

5

5

 

 

 

 

2

2

 

5

 

5

 

5

 

 

2

2

 

5

5

5

5

5

5

3

 

 

 

 

 

 

 

5

 

 

 

 

 

 

 

 

 

5

 

 

 

 

4

4

4

 

 

5

 

 

 

 

 

 

4

 

 

5

 

Write a method that accepts a 2D array of char ( “*” if an element is occupied and blank otherwise) and returns the number of organisms on the board. Write the method (or a helper method) so that it determines this recursively. You may not make changes to the world, such as blotting out organisms as you find them, so you probably need a local copy of the world to work with. Again, a helper method will be very useful.


public int numThings(char world[NUM][NUM])

 

 

7.      Write a recursive function which when given an array L of positive numbers and a positive number K will print all lists of elements from L whose sum adds up to K. For instance, if given the array [8,3,4,7,6,2,5] and the number 10, the answers to be provided should include 8 2,  3 7 and 3 5 2.