Name __________________________                                                                      Date: 12/8/05

AP CS Java  - Mr. Merlis                                                                                             Block 4 ACE

 

Day 31 – WHAT’S GOING ON

TODAY’S AGENDA

 

-          Take 15 minutes to finish up Labs 2a and 3a.  Show them to me.

-          Page 98 of the review book on Recursion.

-          Powerpoint on Recursion.

-          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*)

 

 

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:  Google “Towers of Hanoi”. 

-          Read about what this is.

-          TYPE, on a piece of a paper, with your name and date on the top, a paragraph explaining how recursion applies to the Towers of Hanoi.

-          Find information that discusses the big “O” for the recursive solution to this problem.

-          Find a java applet that allows you to try the problem.  INDICATE THE WEBSITE on your typed paper.