import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application having as its purpose to test the methods of the ** MaxHeapOfInt class. ** ** Author: R. McCloskey ** Date: May 2022 */ public class HeapTester { private static Scanner input; private static boolean echo; private static MaxHeapOfInt heap; public static void main(String[] args) throws FileNotFoundException { // Establish whether input comes from the keyboard or from a file // whose name is specified by the first command line argument. if (args.length == 0) { input = new Scanner(System.in); echo = false; } else { input = new Scanner(new File(args[0])); echo = true; } System.out.println("Welcome to the Heap Tester program."); doTesting(); System.out.println("Goodbye from the Heap Tester program."); } /* Performs testing upon a heap. */ private static void doTesting() { heap = new MaxHeapOfInt(); // initialize heap to being empty boolean keepGoing = true; while (keepGoing) { System.out.println(); printMenu(); int choice = getInt("> "); if (choice == 1) { heap = new MaxHeapOfInt(); heap.isValid(true); } else if (choice == 2) { heap = loadHeap(); heap.isValid(true); } else if (choice == 3) { insert(); heap.isValid(true); } else if (choice == 4) { System.out.println(heap.getMax()); } else if (choice == 5) { heap.deleteMax(); heap.isValid(true); } else if (choice == 6) { heap.print(); } else if (choice == 7) { keepGoing = false; } else { System.out.println("** Invalid choice **"); } } } private static void printMenu() { System.out.println("(1) Create new empty heap"); System.out.println("(2) Load some values into a new heap"); System.out.println("(3) Insert value into heap"); System.out.println("(4) Show max value in heap"); System.out.println("(5) Delete max value from heap"); System.out.println("(6) Display heap"); System.out.println("(7) Quit"); } /* Prompts the user to enter an integer and returns the response. If the ** response entered cannot be translated to an int by Integer.parseInt(), ** the prompt it repeated. This continues until a valid response is given. */ private static int getInt(String prompt) { int result = -99; boolean keepTrying = true; while (keepTrying) { try { System.out.print(prompt); result = Integer.parseInt(input.nextLine().trim()); if (echo) { System.out.printf("%d\n", result); } keepTrying = false; } catch (Exception e) { System.out.println("** Invalid response; expecting an integer! **"); } } return result; } /* Prompts user to enter values to be loaded into a new heap. */ private static MaxHeapOfInt loadHeap() { System.out.print("Enter (one or more) values to load into a new heap: "); String response = input.nextLine().trim(); if (response.length() == 0) { // Return an empty heap. return new MaxHeapOfInt(); } else { int[] ary = stringToIntAry(response); return new MaxHeapOfInt(ary); } } /* Returns an array of type int[] containing the translations, into int ** values, of tokens of the given string. */ private static int[] stringToIntAry(String s) { Scanner valScanner = new Scanner(s); // Count how many int tokens are in the String int cntr = 0; while (valScanner.hasNextInt()) { valScanner.nextInt(); cntr = cntr + 1; } int[] result = new int[cntr]; valScanner = new Scanner(s); for (int i=0; i != cntr; i++) { result[i] = valScanner.nextInt(); } return result; } /* Prompts user to enter a number to insert into the heap, and ** does so. */ private static void insert() { int val = getInt("Enter a number to insert: "); heap.insert(val); } }