INTRODUCTION TO RECURSION: P E

advertisement
INTRODUCTION TO RECURSION:
PROGRAMMING EXERCISES
1. Create a program that takes a String from the user and then returns recursively a string where
adjacent characters that are the same have been reduced to a single char. So, for example, "yyzzza"
would return "yza".
Your output should look something like this:
Save the project as Remove Duplicates in your UNIT 4 folder.
2. Create a program that takes a number from the user and then returns recursively the number of
times 7 occurs. So, for example 717 would return 2.
HINT:
mod (%) by 10 gets you the rightmost digit (126 % 10 is 6), while dividing a number (/)
by 10 removes the rightmost digit (126 / 10 is 12).
Your output should look something like this:
Save the project as Count 7s in your UNIT 4 folder.
Recursion Exercises
Page 1 of 4
3. Let’s say that a female rabbit is mature 2 months after birth. And let’s say
that each mature female rabbit produces 1 baby female rabbit per month.
Create a Java application called Rabbit Population that applies a recursive
method to determine how many female rabbits you will have after a given
number of months.
The population of rabbits will grow in the same manner that the Fibonacci
series works. The series begins with 0 and 1, and each subsequent number is
the sum of the preceding two numbers in the series, as illustrated in the
following table:
RABBITS
0
1
1
2
3
5
8
13
21
34
55
89
MONTHS
0
1
2
3
4
5
6
7
8
9
10
11
The rabbit population can therefore be recursively defined as follows:
pop(0) = 0;
pop(1) = 1;
if index >= 2
pop(index) = pop(index – 2) + pop(index – 1)
The problem of determining pop(index) is reduced to calculating pop(index – 2) and pop(index
– 1), and you apply the idea recursively until index is reduced to 0 or 1.
The base case is index = 0 or index = 1. If you call the method with index = 0 or index = 1, it
immediately returns the result. If you call the method with an index value greater than or equal to 2,
it divides the problem into two sub problems for calculating pop(index – 1) and pop(index – 2)
using recursive calls.
Your program output should look something like this:
Recursion Exercises
Page 2 of 4
Save the project as Rabbit Population in your UNIT 4 folder.
4. Create a Handshakes application that uses a recursive method to determine
how many handshakes are exchanged at mass given the number of people in
attendance and each person shakes hands once with every other person. So, if
there are two people at mass, then that would result in one handshake. If a
third person enters the church, she must shake hands with each of the two
people already there. This would result in three handshakes: two handshakes
she makes in addition to the number of handshakes that were made when
there were only two people at mass.
If a fourth person enters the church, he must shake hands with each of the
three people present. This would result in a total of six handshakes: three
handshakes he makes in addition to three handshakes that were exchanged
when there were three people at mass.
So the pattern would look something like this:
HANDSHAKES
0
0
1
3
6
10
15
21
28
36
45
55
66
PEOPLE
0
1
2
3
4
5
6
7
8
9
10
11
12
Your program output should look something like this:
Recursion Exercises
Page 3 of 4
Save the project as Handshakes in your UNIT 4 folder.
Recursion Exercises
Page 4 of 4
Download