public class RecursiveProgramming { public static void main(String[] args) { int[] arr = {1, 2, 3}; System.out.println(sum(arr)); } // post: returns the sum of all the integers in the array public static int sum(int[] arr) { return sum(arr, 0); } // post: returns the sum of all the integers in the array // starting from the given index private static int sum(int[] arr, int index) { if (index == arr.length) { return 0; } else { int sum = arr[index] + sum(arr, index); return sum; } } // post: returns an integer where every digit of n // is replaced by two of that digit // Example: doubleUp(348) returns 334488 // Example: doubleUp(-348) returns -334488 public static int doubleUp(int n){ if (n < 0) { return -doubleUp(-n); } else if (n < 10) { return n * 11; } else { return 100 * doubleUp(n / 10) + doubleUp(n % 10); } } // post: returns a string where every character of str // is replaced by two of that character // Example: doubleUp("cat") returns "ccaatt" // Example: doubleUp("") returns "" public static String doubleUp(String str) { if (str.length() <= 1) { return str + str; } else { char c = str.charAt(0); return "" + c + c + doubleUp(str.substring(1)); } } }