Name __________________________                                                                      Date: 12/12/05

AP CS Java  - Mr. Merlis                                                                                             Block 4 ACE

 

Day 32 – WHAT’S GOING ON

TODAY’S AGENDA

 

-          EXAMINE TOWERS OF HANOI. - http://javascript.internet.com/games/hanoi.html

-          Continue work on recursively programming:

o        Calculate the factorial of an integer.                                   à public static long factorial(int number)

o        Calculate the sum of the first n digits.                                 à public static int sum(int n)

o        Print out (in order) the value stored in an ArrayList à public static void printForward(*AL*)

o        Print out (in reverse order) the values stored in an ArrayList à public static void printBackwards(*AL*)

o        Sum the values in an ArrayList                                           à public static int sum(*AL*)

 

 

THIS IS A RE-WRITE OF WHAT WAS ON THE CLASS #31 Agenda.

 

To do these five exercises, MAKE SURE YOU DO THE FOLLOWING:

 

Inside your “Labs” folder, make another folder called “Recursion”. 

Then open BlueJ and create a new project inside the “Recursion” folder called “RecursionPractice.”

Create a class called “RecursionTest” à This is an app program that will have the static methods.

 

 

 

 

You should also create a method with the following header:

 

public static ArrayList fill(ArrayList list, int index) // fills an ArrayList with index elements recursively.

 

 

 

There should be no class variables (keep the ArrayList object inside the main method.)

 

 

As always, work together, use the API, and lastly, don’t be afraid to think!  Use a pencil and paper to work your way through the problems.  No one method is very long.

 

For each method, you need to come up with a base case and the recursive call.  Make sure the recursive call is different than the call that it entered with or else you will have an infinite loop that never reaches the base case.

 

 

HOMEWORK: 

-          Go here: http://personal.vsnl.com/erwin/magic.htm

-          READ THAT PAGE IN ITS ENTIRETY.

-          Think about what the author is describing and the usefulness of recursion here and in other applications.

-          You don’t have to write anything, but be prepared to discuss it on Wednesday.