import java.util.Scanner; import java.util.Arrays; import java.util.Random; /* Java application having as its purpose to test the methods of one ** or more classes that implement the MaxSegSum interface. */ public class MaxSegSumTester { public static void main(String[] args) { int aryLen; // length of array to test int lowerBound; // array elements no less than this int upperBound; // array elements no greater than this int randSeed; // seed for Random object if (args.length == 0) { // User must provide inputs Scanner keyboard = new Scanner(System.in); aryLen = getInt(keyboard, "Enter desired array length:"); lowerBound = getInt(keyboard, "Enter element lower bound:"); upperBound = getInt(keyboard, "Enter element upper bound:"); randSeed = getInt(keyboard, "Enter seed for random # generation:"); } else { // interpret the command-line arguments as the inputs aryLen = Integer.parseInt(args[0]); lowerBound = Integer.parseInt(args[1]); upperBound = Integer.parseInt(args[2]); randSeed = Integer.parseInt(args[3]); System.out.println("Inputs:"); System.out.printf("array length = %d\n", aryLen); System.out.printf("lower bound = %d\n", lowerBound); System.out.printf("upper bound = %d\n", upperBound); System.out.printf("randomization seed = %d\n", randSeed); } // Create an array containing pseudorandom values in the range // specified by the user. Random rand = new Random(randSeed); RandomIntAryGenerator riag = new RandomIntAryGenerator(rand); int[] a = riag.randomArray(aryLen, lowerBound, upperBound); System.out.println("\nArray generated:"); printWithLineBreaks(Arrays.toString(a), ',', 80); //System.out.println(Arrays.toString(a)); System.out.println(); System.out.println(); // Now compute the maximum segment sum of that array, using objects // of two different classes that implement the MaxSegSum interface // Report differences in the results. MaxSegSum mssPref = new MaxSegSumViaPrefixSums(); compute(mssPref, a, "Using prefix sums: "); MaxSegSum mssRec = new MaxSegSumViaRec(); compute(mssRec, a, "Using recursion: "); if (mssRec.maxSegSumVal() != mssPref.maxSegSumVal()) { System.out.println("**WARNING: DISAGREEMENT"); } } /* Applies the maxSegSum() method of the given MaxSegSum object to ** the given array and reports the results. */ private static void compute(MaxSegSum mss, int[] b, String message) { System.out.print(message); int result = mss.maxSegSum(b); System.out.printf("Max segment sum is %d; work measure is %d\n", mss.maxSegSumVal(), mss.measureOfWork()); if (result != mss.maxSegSumVal()) { System.out.println("**ERROR: maxSegSumVal() and maxSegSum() " + "yield different values, namely " + mss.maxSegSumVal() + " and " + result); } } /* Prints the given prompt and returns the user's response, interpreted ** to be an integer. */ private static int getInt(Scanner scanner, String prompt) { System.out.print(prompt); return scanner.nextInt(); } /* Prints the given string, splitting it so that no printed line ** exceeds the specified # of characters and so that each line ** ends with the specified separator (which is assumed to appear ** frequently in the string). */ private static void printWithLineBreaks(String s, char separator, int lineWidth) { int lineStart = 0; while (lineStart + lineWidth < s.length()) { int stop = s.lastIndexOf(separator, lineStart + lineWidth-1); if (stop < lineStart) { // no occurrences of separator printSubstring(s, lineStart, lineStart + lineWidth); lineStart = lineStart + lineWidth; } else { printSubstring(s, lineStart, stop+1); lineStart = stop + 1; } System.out.println(); } printSubstring(s, lineStart, s.length()); } /* Prints s[low..high). */ private static void printSubstring(String s, int low, int high) { high = Math.min(high, s.length()); while (low != high) { System.out.print(s.charAt(low)); low = low + 1; } } }