public class SimpleRecursion { /* * method for recursively computing n! * @param n * precondition: n>=0 * postcondition: returns n! */ public int factorial(int n){ if (n==0) { System.out.println("BASE CASE: factorial(0) = 1"); return 1; } else { int result = factorial(n-1) * n; System.out.println("current n: " + n + " current result: " + result); return result; } } public int max(int [] list){ return recursiveMax(list, 0); } /* * recursive method for computing the maximum element in part of an array * that starts at a given index * @param list - an array of integers * @param start - the starting index * precondition: 0 <= start < list.length * postcondition: returns the maximum in list[start],...,list[list.length - 1] */ public int recursiveMax(int [] list, int start){ if (start == list.length - 1) { return list[start]; } else { return Math.max(list[start], recursiveMax(list, start + 1)); } } public static void main(String[] args) { SimpleRecursion rec = new SimpleRecursion(); for(int i=1; i<8; i++){ System.out.println("computing factorial("+ i +")"); System.out.println("factorial("+i+")=" + rec.factorial(i)); } int [] list = { 1, 5, 2, 3}; System.out.println("maximum in the list: " + rec.max(list)); } }