/* * Kyle Pierce * CSE 143 * * Some more recursive methods */ import java.util.*; public class Recursion2 { public static void main(String[] args) { System.out.println("The stutter of 348 is " + stutter(348)); int[] arr = {3, 1, -4, 7, 2}; System.out.println("The sum of " + Arrays.toString(arr) + " is " + sum(arr)); } // post: returns the integer obtained by replacing every digit in n with // two of that digit. For example, stutter(348) returns 334488. public static int stutter(int n) { if (n < 0) { return -stutter(-n); } else if (n < 10) { return n * 11; } else { return stutter(n / 10) * 100 + stutter(n % 10); } } // post: returns the sum of the numbers in list public static int sum(int[] list) { return sum(list, 0); } // pre: 0 <= index <= list.length // post: returns the sum of list starting at the given index private static int sum(int[] list, int index) { if (index == list.length) { return 0; } else { return list[index] + sum(list, index + 1); } } }