From f04910cb73bebd5398e02f0a5ab6f8bc7c48044e Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 22 Mar 2024 23:24:44 -0400 Subject: [PATCH] Began the sentinel loop. --- src/Assignments/A3/InterpolationSearch.java | 3 +- .../A3/TestInterpolationSearch.java | 134 +++++++++++++++++- 2 files changed, 132 insertions(+), 5 deletions(-) diff --git a/src/Assignments/A3/InterpolationSearch.java b/src/Assignments/A3/InterpolationSearch.java index 7e07d04..c5727b5 100644 --- a/src/Assignments/A3/InterpolationSearch.java +++ b/src/Assignments/A3/InterpolationSearch.java @@ -48,8 +48,6 @@ public class InterpolationSearch { /* Checks if high is the same thing as low to prevent division by zero. */ if (high != low) { probe = low + (((key - array[low]) * (high - low)) / (array[high] - array[low])); - - /* This is here due to the fact that it is not always divided as there is only 1 element left. */ this.divisions++; } else { probe = high; @@ -59,6 +57,7 @@ public class InterpolationSearch { if (array[high] < key || array[low] > key) { break; } + /* Checks if the key is less than the probe's value */ if (array[probe] > key) { high = probe - 1; diff --git a/src/Assignments/A3/TestInterpolationSearch.java b/src/Assignments/A3/TestInterpolationSearch.java index 0f0dfba..9a83bc3 100644 --- a/src/Assignments/A3/TestInterpolationSearch.java +++ b/src/Assignments/A3/TestInterpolationSearch.java @@ -9,6 +9,128 @@ public class TestInterpolationSearch { /** Holds the size of the table to generate */ public int tableSize; + public static void main(String[] args) { + TestInterpolationSearch program = new TestInterpolationSearch(); + program.start(); + } + + /** + * Used as the main sentential loop and loops until the exit option is selected. + * + * @precondition none + * @postcondition none + */ + public void start() { + int option = this.getOption(); + while (option != 4) { + this.executeOption(option); + option = this.getOption(); + } + } + + /** + * Displays the menu options. + * + * @precondition none + * @postcondition the menu is displayed. + */ + private void displayMenu() { + System.out.println("----------------------MAIN MENU-------------------"); + System.out.println("1. Create, populate, and display array Values[]"); + System.out.println("2. Read output table size"); + System.out.println("3. Run interpolation search and display outputs"); + System.out.println("4. Exit program"); + System.out.println(); + } + + /** + * Prints out the menu of options, asks for an input, and if that input is invalid it prints an error and + * prompts the user again for the input. + * + * @precondition none + * @postcondition none + * + * @return the option selected + */ + private int getOption() { + this.displayMenu(); // Prints out the menu + + // Asks for the option and begins the loop until a valid option is gathered. + System.out.print("Enter option number: "); + int input = getIntegerInput(); + while (input < 1 || input > 4) { // Compares it to the valid options available. + System.out.println("\nPlease enter a valid input."); + System.out.print("Enter option number: "); + input = getIntegerInput(); + } + System.out.println(); + return input; + } + + /** + * Gets user input to a number. A prompt must be provided prior to running this method. + * If the input was not an integer/(unable to be autoboxed), returns -1. + * + * @precondition none + * @postcondition none + * + * @return the integer input, if invalid -1. + */ + protected int getIntegerInput() { + Scanner sc = new Scanner(System.in); + try { + String textInput = sc.nextLine(); + return Integer.parseInt(textInput); + } catch (Exception e) { + return -1; + } + } + + public void executeOption(int option) { + /* The option used for the values generation */ + if (option == 1) { + this.randomDistinct(); + System.out.println("The generated Values: "); + + /* Displays the generated values */ + for (int i = 0; i < this.values.length; i++) { + System.out.print(String.format("%-6s", this.values[i])); + /* Splits the rows at 30 columns */ + if ((i+1) % 30 == 0) { + System.out.println(); + } + } + System.out.println(); + } else if (option == 2) { /* Handles the table size */ + System.out.println("Enter the table size: "); + System.out.print("Option: "); + int size = -1; + size = getIntegerInput(); + + /* Used if the table input is invalid */ + while (size < 1) { + System.out.println("Enter a valid table size: (size > 0)"); + System.out.print("Option: "); + size = getIntegerInput(); + } + this.tableSize = size; + } else if (option == 3) { /* Generates and displays the table */ + if (this.values == null) { /* Used to check if the values have been generated */ + System.out.println("Please generate the values first."); + System.out.println(); + return; + } + if (this.tableSize < 1) { /* Used to check if the table size has been set */ + System.out.println("Please enter a table size."); + System.out.println(); + return; + } + /* Runs the search */ + this.runIntSearch(); + } + System.out.println(); + } + /** * Generates a list of 1024 random, non-repeating, integers and assigns them to the values field. */ @@ -37,15 +159,21 @@ public class TestInterpolationSearch { } public void runIntSearch() { - int totalDivisions; + System.out.println(this.values.length + " " + this.tableSize); + int totalDivisions = 0; Random rand = new Random(); - System.out.println(" Key\t\tFound\t\tIndex\t\tDivisions"); + System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", "Key", "Found", "Index", "Divisions")); System.out.println("----------------------------------------------"); for (int i = 0; i < this.tableSize; i++) { int key = rand.nextInt(9999) + 1; InterpolationSearch results = new InterpolationSearch(this.values, key); - System.out.println(" " + key + "\t\t" + results.isFound() + "\t\t" + results.getIndex() + "\t\t" + results.getDivisions()); + System.out.println(" " + String.format("%-10s%-10s%-10s%-10s", key, results.isFound(), results.getIndex(), results.getDivisions())); + totalDivisions += results.getDivisions(); } + double averageDivs = (double) totalDivisions / (double) this.tableSize; + System.out.println("\nDivisions average: " + averageDivs); + System.out.println("Difference: " + Math.abs(3.322 - averageDivs)); + System.out.println(); } }