// Name: Jonathan Turner // Class: CS 4306/01 // Term: Spring 2024 // Instructor: Dr. Haddad // Assignment: 2 // IDE Name: IntelliJ /* Algorithm Design Block Algorithm title: Finding Substrings Logical steps: Step 1: Gather input for the text and store them in a string. Step 2: Create a counter that tracks how many substrings are in the text. Step 3: Iterate through a for loop (i) that goes from 1->n. Step 4: If the ith letter in the text is equal to A, then move to Step 5. Otherwise, continue loop. Step 5: Iterate through a for loop (j) which is inside loop (i) and goes from i+1->n. Step 6: If the jth letter in the text is equal to B, increment counter. Step 7: At the end of the loop (i), return the counter to show the number of substrings. Pseudocode Syntax: text <- input counter <- 0 comps <- 0 for i <- 1 to n if text[i] = A for j <- i+1 to n if text[j] = B counter + 1 comps + 1 comps + 1 return counter, comps Big-O Analysis: (Based on Implementation) */ package Assignments.A2; import java.util.Scanner; /** * Performs a brute force/exhaustive search for * how many substrings start with A and end with B. * * @author Jonathan Turner * @version Spring 2024 */ public class Substrings { /** * 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 != 3) { 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) { performAlgorithm(); } /** * 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 > 3) { // 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. Read input string"); System.out.println("2. Run algorithm and display outputs"); System.out.println("3. Exit program"); System.out.println(); } /** * 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; } } }