import java.util.Arrays; import java.util.Scanner; /* This Java application program is for the purpose of testing the ** ParallelArySummer class. ** ** Author: R. McCloskey ** Date: May 9, 2023 */ public class ParArySumTester { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int aryLen = getIntInput(keyboard, "Enter array length: "); int lowBound = getIntInput(keyboard, "Enter lower bound value: "); int upperBound = getIntInput(keyboard, "Enter upper bound value: "); int seed = getIntInput(keyboard, "Enter random seed: "); RandIntArrayMaker riam = new RandIntArrayMaker(); // Make an array filled with random integers in accord with // parameters entered by user. int[] ary = riam.randomIntArray(aryLen, lowBound, upperBound, seed); // Display the array System.out.printf("Array: %s\n", Arrays.toString(ary)); // Compute the sum of the array's elements using an object of the // ParallelArySummer class. ParallelArySummer pas = new ParallelArySummer(ary); pas.computeSum(); System.out.println("\nParallelArySummer reports a sum of " + pas.sumOf()); // Verify that the sum computed by the instance of ParallelArySummer // is correct. int correctSum = segmentSum(ary, 0, ary.length); if (pas.sumOf() != correctSum) { System.out.printf("***ERROR; correct sum is %d\n", correctSum); } } /* Returns the sum of the elements in b[floor..ceiling). ** pre: 0 <= floor <= ceiling <= b.length */ private static int segmentSum(int[] b, int floor, int ceiling) { int sumSoFar = 0; // loop invariant: // sumSoFar = sum of elements in b[floor..i) for (int i = floor; i != ceiling; i++) { sumSoFar = sumSoFar + b[i]; } return sumSoFar; } /* Displays the specified prompt and then returns the response read ** from the specified Scanner (which is assumed to be in a form that ** can be interpreted as an integer). */ private static int getIntInput(Scanner scanner, String prompt) { System.out.print(prompt); return scanner.nextInt(); } }