import java.util.Arrays; import java.util.Scanner; /* This Java application program is for the purpose of testing the ** ParallelMinMax class. ** ** Author: R. McCloskey ** Date: May 9, 2023 */ public class ParMinMaxTester { 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); System.out.printf("Array: %s\n", Arrays.toString(ary)); ParallelMinMax pmm = new ParallelMinMax(ary); pmm.computeMinMax(); System.out.print("\nParallelMinMax reports that the min and max are "); System.out.printf("%d and %d\n", pmm.minimumOf(), pmm.maximumOf()); int minVal = ary[indexOfMin(ary,0,ary.length)]; if (pmm.minimumOf() != minVal) { System.out.printf("***ERROR; actual minimum is %d\n", minVal); } int maxVal = ary[indexOfMax(ary,0,ary.length)]; if (pmm.maximumOf() != maxVal) { System.out.printf("***ERROR; actual maximum is %d\n", minVal); } } /* Returns the location, within b[floor..ceiling), of the minimum ** value in that array segment. ** pre: 0 <= floor < ceiling <= b.length */ private static int indexOfMin(int[] b, int floor, int ceiling) { int locOfMinSoFar = floor; // loop invariant: // the minimum value in b[floor..i) is at location locOfMinSoFar for (int i = floor+1; i != ceiling; i++) { if (b[i] < b[locOfMinSoFar]) { locOfMinSoFar = i; } } return locOfMinSoFar; } /* Returns the location, within b[floor..ceiling), of the maximum ** value in that array segment. ** pre: 0 <= floor < ceiling <= b.length */ private static int indexOfMax(int[] b, int floor, int ceiling) { int locOfMaxSoFar = floor; // loop invariant: // the maximum value in b[floor..i) is at location locOfMaxSoFar for (int i = floor+1; i != ceiling; i++) { if (b[i] > b[locOfMaxSoFar]) { locOfMaxSoFar = i; } } return locOfMaxSoFar; } /* 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(); } }