// Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 5, section 5.2, page 189 class sum { //Effects: If x null throw NullPointerException // else return the sum of the values in x public static int sum(int[] x) { int s = 0; for (int i=0; i < x.length; i++) { s = s + x[i]; // s = s - x[i]; } return s; } public static void main (String []argv) { // Driver method for sum // Read an array from standard input, call sum() int []inArr = new int [argv.length]; if (argv.length == 0) { System.out.println ("Usage: java numZero v1 [v2] [v3] ... "); return; } for (int i = 0; i< argv.length; i++) { try { inArr [i] = Integer.parseInt (argv[i]); } catch (NumberFormatException e) { System.out.println ("Entry must be a integer, using 1."); inArr [i] = 1; } } System.out.println ("The sum is: " + sum (inArr)); } }