Recursion II

advertisement
Recursion II
Some important points on recursion
• Always be careful of memory overflow when using recursion.
• Any recursive method can be implemented using iteration and vice
versa.
• Constructors cannot call themselves.
• Be sure to have terminating conditions to avoid infinite recursion.
Exercise 1
Use Recursion to create the following triangle.
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
3
4
5
6
7
8
9
4
5
6
7
8
9
5
6
7
8
9
6
7
8
9
7
8
9
8
9
9
Exercise 2
Create an integer array of size 20 and use recursion to assign random
values between 0-100 to all fields.
Now use recursion to display this array.
No loops of any kind please.
Exercise 3
Use recursion to build a 2D integer array of size 10 x 10. Assign random
values between 0 – 100 to each position.
Once done, use recursion to display this array.
Exercise 4
Declare a class called Numbers. Numbers has three fields:
int a;
int b;
int c;
You will recursively create an array list of this class. Everytime a new
object is created, generate random values between 0-100 for each 3.
Recursion stops when all three numbers added together is divisible by
10. Output values as you go.
Download