public class SimpleRecursion { /* 1. This recursive procedure takes a non-negative integer value n * and prints a sequence of integers, "n, n-1, ..., 0" from n down to zero. * Here's the recursive logic that you must implement. Trust us: this works! * If n is zero, output zero and return: the job is done. Otherwise do two * things. First, solve a small part of the remaining problem by printing n. * Then call the procedure itself (from within itself!) to solve the rest of * the problem! A procedure that calls itself to solve a smaller instance of * the same kind of problem is said to be recursive. */ public static void nTimes(int n) { // If n is zero, print it out and return // Otherwise, print n, then use recursion to solve the smaller subproblem } public static void main(String[] args) { nTimes(3); int sum = add(3,4); } /* * 2. Set a breakpoint at the call in main to nTimes(3). Start the debugger, and * repeatedly use Step INTO to see what happens when this program executes. Take * your time to figure out what's happening. In particular, once your program is * nested several levels deep in the recursion, take some time to inspect the state * of the "stack". Notice in particular how calls to nTimes are nested inside of * other calls to nTimes; but notice too how the values passed to nested calls keep * decreasing. Can you convince yourself that the recursion will eventually stop? * What do you predict would happen if the code that checks for a "base case" were * somehow not functioning correctly? You may step OVER the call to add, for now. * That's the topic of the next exercise */ /* Use recusion to compute the sum of non-negative integers a and b. * The comments tell you what to write. In fact, we give you the first * line of code. All you need to do is to write the recursive call. Now * use the debugger to watch, in detail, at what happens when the call * from main is made to add(3,4). In this case, each nested recursive * call gets "closer" to the "base case." Can you convince yourselfe * that this procedure will always terminate, if given non-negative * integer inputs? Are there inputs that could cause the procedure * never to terminate? Go ahead and give it a try by changing the values * passed in from main. Be sure you remember how to terminate a debugging * thread! */ public static int add(int a, int b) { // if b is zero, the problem is solved; return the answer if (b==0) return a; // replace the following line of code with one that // uses recursion to compute and return the sum of a+1 and b-1 return 0; } } // SUBMIT your FunctionalAbstract.java and SimpleRecursion.java files.