// Tyler Mi // Program working through some more recursion methods, introducing the idea // of a public-private pair. The private helper method has parameters that can // keep track of more information. The public method just needs to start off the // recursion import java.util.*; public class Recursion2 { public static void main(String[] args) { System.out.println("The repeated version of -348 is " + repeat(-348)); int[] arr = {3, 1, -4, 7, 2}; System.out.println("The sum of " + Arrays.toString(arr) + " is " + sum(arr)); } // Returns a version of n, where each digit is repeated public static int repeat(int n) { if (n < 0) { return -1 * repeat(-1 * n); } else if (n < 10) { return 11 * n; } else { int lastDigit = n % 10; int leadDigits = n / 10; return repeat(leadDigits) * 100 + 11 * lastDigit; } } // Returns the sum of the contents of the array public static int sum(int[] arr) { return sum(arr, 0); } // Returns the sum of the contents of the array, // starting from the given index private static int sum(int[] arr, int index) { if (index == arr.length) { return 0; } else { return arr[index] + sum(arr, index + 1); } } // public static int sum(int[] array) { // int sum = 0; // for (int i = 0; i < array.length; i++) { // sum += array[i]; // } // return sum; // } }