import java.util.Scanner; import java.util.Arrays; import java.io.File; import java.io.FileNotFoundException; /* ArraySumRec.java ** Java application that includes a recursive method to compute the ** sum of the elements in a segment of an array of int values. ** Also includes a main() method, which is strictly for the purpose ** of testing the method that sums. ** ** Authors: R. McCloskey and < ... > ** Known deficiences: */ public class ArraySumRec { private static int depth; /* Returns the sum of the elements in the given array. */ public static int sumOf(int[] b) { return sumOfAux(b, 0, b.length); } /* Returns the sum of the elements in the array segment described ** by the parameters (namely b[low..high)). Employs recursion. */ public static int sumOfAux(int[] b, int low, int high) { depth = depth + 1; printMessage(String.format("Entering sumOfAux() to compute sum of b[%d..%d)\n", low, high)); int result; // CODE MISSING HERE!! printMessage(String.format("Leaving sumOfAux(); sum of b[%d..%d) found to be %d\n", low, high, result)); depth--; return result; } /* Prints the specified number of spaces. */ private static void printSpaces(int num) { for (int i=0; i != num; i++) { System.out.print(' '); } } /* Prints the given String, indented in accord with the current ** value of 'depth'. */ private static void printMessage(String s) { printSpaces(3*depth); System.out.print(s); } // Remaining methods are strictly for the purpose // of testing the sumOfAux() method above. // ---------------------------------------------------------- /* Reads numbers from an input source, places them into an array, ** and invokes the sumOf() method above to compute their sum. ** If command line arguments are provided, they are taken to be the ** numbers to be summed. Otherwise, the user is prompted to enter ** the name of a file containing input. The first number in that ** file is taken to describe how many numbers follow that are to be ** summed. */ public static void main(String[] args) throws FileNotFoundException { int[] a; if (args.length != 0) { // interpret command line args as being the elements of the // array to be summed a = strArrayToIntArray(args); } else { // In the absence of command line arguments, ask the user to // enter name of file in which is described an array of ints String fileName = getString("Enter name of input file: "); Scanner input = new Scanner(new File(fileName)); a = readArray(input); } System.out.println("The array is as follows:"); System.out.println(Arrays.toString(a)); System.out.println(); depth = -1; int sum = sumOf(a); System.out.printf("As computed by sumOf(), its elements sum to %d\n", sum); int actualSum = sumOfUsingLoop(a); if (sum != actualSum) { System.out.println("***WRONG!! Actual sum is " + actualSum); } } /* Given an array of Strings, each of which is an integer numeral ** (e.g., "368", "-21"), returns an array of type int[] containing ** the corresponding numeric values. */ private static int[] strArrayToIntArray(String[] ary) { int[] result = new int[ary.length]; for (int i=0; i != ary.length; i++) { result[i] = Integer.parseInt(ary[i]); } return result; } /* Prints the given prompt and returns the response entered at the keyboard. */ private static String getString(String prompt) { Scanner keyboard = new Scanner(System.in); System.out.print(prompt); return keyboard.nextLine(); } /* Returns a new array in which has been placed the numbers read by the ** given Scanner. The first number, N, indicates the intended length ** of the array. The N numbers that follow are placed into the array. */ private static int[] readArray(Scanner input) { int[] result; int N = input.nextInt(); // read length of intended array result = new int[N]; for (int i=0; i != N; i++) { // read N lements into the array result[i] = input.nextInt(); } return result; } /* Returns the sum of the elements in the given array ** using the obvious loop-based approach. */ private static int sumOfUsingLoop(int[] a) { int sumSoFar = 0; for (int i=0; i != a.length; i++) { sumSoFar = sumSoFar + a[i]; } return sumSoFar; } }