/**
* RecursionTest is a series of recursive static
* methods to help you learn to think recursively. No objects
* are involved. There are three methods to write. As
* you write each one, uncomment the call from main and
* test your method before going on. The second parameter
* is the index in each case. Do not change the parameters.
*
* NOTE: You will write a FOURTH method that is NOT recursive.
* It uses an Iterator to print the list.
*
* @author PUT YOUR NAME
* @version PUT THE DATE
*/
import java.util.ArrayList;
public class RecursionTest
{
public static void main(String [] args)
{
System.out.println("10! = " + factorial(10));
int listLength = 10;
ArrayList fives = new ArrayList();
fives = fill( fives, listLength);
printIteratively(fives);
/*Write and document the other recursive methods that
* are indicated by these calls:
*/
//printRecursivelyForward(fives,0);
//printRecursivelyBackward(fives, 0);
//System.out.println("\n\nThe sum of the numbers on the list is"
// + sum ( fives, fives.size()-1);
//
/*
* I would also like you to create a method
* that prints the list using an Iterator
*/
//printWithIterator(fives);
}
/**
* @param -- number to find the factorial of
* @return -- answer to number!
*
* long is a data type that will hold integers with
* many digits.
*/
public static long factorial(int number)
{
if (number == 1) return 1;
return number * factorial(number - 1);
}
/**
* @param --list to be filled
* -- index is the final index for that value
* @return -- the filled list
*/
public static ArrayList fill(ArrayList list, int index)
{
index--;
if (index < 0)return list;
list.add(0, new Integer(5*index));
list = fill(list, index);
return list;
}
/**
* prints the list using a loop
* @param -- the list to be printed
*/
public static void printIteratively(ArrayList list)
{
for (int i = 0; i < list.size(); i++)
{
System.out.print(list.get(i) + "\t");
}
}
}