Lesson 07 In-Class Exercises

Before starting any specific exercise, you may wish to download, extract, and open up the provided starter project for this lesson so that you'll have it ready when you need/want it.

Part 1: Recursion: Predicting Output From An Existing Program

Find a partner (or, if you have to, 2 partners)  to work with, then open up your solution to the Lesson 06 PCE: Recursion By hand: Warm-up #2.  If you haven't finished this exercise yet then you are encouraged to pair up with someone who has.

You should first check your answer against your partner's, and make sure that you understand what gets printed out and (more importantly) why it gets printed out.
(If you didn't do this exercise then you should double-check your partner's work, asking questions about anything that you don't understand, or anything you think is off).

Once you're done you should then modify the program listed in the "Recursion By hand: Warm-up #2" document, and re-trace the program together.

Part 2: Recursion: Writing A Recursive Function: PrintToZero

Within the provided starter project there's a class named WriteARecursiveFunction.  Your job is to implement the PrintToZero method, along with it's two private helpers, PrintToZero_Positives and PrintToZero_Negatives.

PrintToZero will take a signed integer as a parameter, and print all whole numbers between the parameter and zero (including bot the parameter, and zero). PrintToZero will only do this for numbers in the range of [-100, 100] - any parameters outside that range will produce an error message.

 In order to do this PrintToZero will first check to see if the parameter is valid (if it's in the range [-100, 100], and then call either PrintToZero_Positives or PrintToZero_Negatives, which will handle actually printing out the numbers.  Because these two methods are private, they do NOT need to check that the input parameters are valid.

Note: Clearly, this is a contrived exercise.  Be able to use the trick of having a public validation method and a private recursive method is useful, and so using that pattern here is good practice, even if you could get this whole thing done just fine with a single, public method.

Future Ideas

Fix 'comparing two pows' so that the numbers calculated are always a power of 2

One Minute Paper

"A “one-minute paper” may be defined as a very short, in-class writing activity (taking one-minute or less to complete) in response to an instructor-posed question, which prompts students to reflect on the day’s lesson and provides the instructor with useful feedback." (from http://www.oncourseworkshop.com/Awareness012.htm).

Once you're done with this lesson please head on over to the One-Minute Paper and fill it out: