From f842d5c2eabc2bbeeee8a6d0eef6065a4a58dc2b Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 7 Mar 2024 12:24:51 -0500 Subject: [PATCH] Starting Assignment 3 --- src/Assignments/A3/InterpolationSearch.java | 144 ++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/Assignments/A3/InterpolationSearch.java diff --git a/src/Assignments/A3/InterpolationSearch.java b/src/Assignments/A3/InterpolationSearch.java new file mode 100644 index 0000000..1f813b2 --- /dev/null +++ b/src/Assignments/A3/InterpolationSearch.java @@ -0,0 +1,144 @@ +// Name: Jonathan Turner +// Class: CS 4306/01 +// Term: Spring 2024 +// Instructor: Dr. Haddad +// Assignment: 3 +// IDE Name: IntelliJ +package Assignments.A3; + +import java.util.Scanner; + +/** + * This class is used to perform the interpolation search algorithm and display the results. + * + * @author Jonathan Turner + * @version Spring 2024 + */ +public class InterpolationSearch { + + /** + * Starts the program. + * @param args the program arguments. + */ + public static void main(String[] args) { + InterpolationSearch program = new InterpolationSearch(); + 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(); + } + } + + /** + * Performs the action related to the option that was chosen. + * + * @precondition none + * @postcondition the action was performed. + * + * @param option the option that was chosen. + */ + private void executeOption(int option) { + + System.out.println(); + } + + /** + * Uses private fields to generate the number of subsets and comparisons. + * + * @precondition text != null && text != empty + * @postcondition the results are generated. + */ + private void performAlgorithm() { + + } + + /** + * 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; + } + + /** + * 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(); + } + + /** + * Gets a string and returns it. If the input is not valid it loops until valid. + * + * @precondition none + * @postcondition none + * @return a user-input text string. + */ + private String getString() { + System.out.println("Specify a text to check number of substrings."); + System.out.print("Text: "); + Scanner sc = new Scanner(System.in); + String input = sc.nextLine(); + + while (input == null || input.isEmpty()) { + System.out.println("Please specify a valid text to check substrings."); + System.out.print("Text: "); + input = sc.nextLine(); + } + + 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. + */ + private int getIntegerInput() { + Scanner sc = new Scanner(System.in); + try { + String textInput = sc.nextLine(); + return Integer.parseInt(textInput); + } catch (Exception e) { + return -1; + } + } +}