import java.util.Arrays; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application for testing instances of the SubSeqSumSolver class. ** ** Author: R. McCloskey ** Date: November 2022 */ public class SubSeqSumApp { public static void main(String[] args) throws FileNotFoundException { // From the file whose name was provided via command line argument, // read the elements of the sequence that will be the subject of // subsequent queries and place them into an an array. int[] ary = readArray(args[0]); // Display the array and create a new SubSeqSumSolver object having // it as its "resident" array. System.out.printf("Array is %s\n", Arrays.toString(ary)); SubSeqSumSolver ssss = new SubSeqSumSolver(ary, System.out); // Read inputs from the user, each of which is a target value to test. // An input of -1 signals the desire to quit. An input less than -1 // is a request to display the resident array. // For each target value entered, call the observer methods of the // SubSeqSumSolver object and report the results. Scanner input = new Scanner(System.in); boolean keepGoing = true; while (keepGoing) { System.out.print("\nEnter target value (-1 to quit): "); int target = input.nextInt(); if (target == -1) { keepGoing = false; } else if (target < 0) { System.out.println(Arrays.toString(ary)); } else { testExists(ssss, target); testNumberOf(ssss, target); testEnumerate(ssss, target); } } System.out.println("Goodbye."); } /* Invokes the exists() method on the given SubSeqSumSolver object to ** determine whether there are any ways to choose elements from its ** resident array to achieve a sum equal to 'target'. */ private static void testExists(SubSeqSumSolver solver, int target) { System.out.printf("\nexists(%d) yields %b\n", target, solver.exists(target)); } /* Invokes the numberOf() method on the given SubSeqSumSolver object to ** determine how many ways there are to choose elements from its resident ** array to achieve a sum equal to 'target'. */ private static void testNumberOf(SubSeqSumSolver solver, int target) { System.out.printf("\nnumberOf(%d) yields %d\n", target, solver.numberOf(target)); } /* Invokes the enumerate() method on the given SubSeqSumSolver object to ** print (a String representing) every one of the ways to choose elements ** from its resident array to achieve a sum equal to 'target'. */ private static void testEnumerate(SubSeqSumSolver solver, int target) { System.out.printf("\nenumerate(%d) yields:\n", target); solver.enumerate(target); } /* Returns a new array of type int[] containing the sequence of integers ** described in the file with the given name. The assumed format of data ** in that file is exemplified by "7 4 6 1 34 23 5 2". ** The first number, n, indicates the length of the sequence. ** The n numbers that follow are the elements in the sequence. */ private static int[] readArray(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); // the first input is the length of the sequence/array. final int N = input.nextInt(); int[] result = new int[N]; // The remaining inputs are the elements of the sequence. for (int i=0; i != N; i++) { result[i] = input.nextInt(); } return result; } }