CS4306-Algorithm_Analysis/src/Assignments/A2/Substrings.java

200 lines
5.9 KiB
Java
Raw Normal View History

// 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 {
2024-02-15 18:41:33 +00:00
public static void main(String[] args) {
Substrings program = new Substrings();
program.start();
}
private String text;
private int subStringCount;
private int comparisons;
/**
* 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) {
2024-02-15 18:41:33 +00:00
if (option == 1) {
this.text = this.getString();
} else {
if (this.text == null) {
System.out.println("Please specify a text to perform the algorithm.");
return;
}
this.performAlgorithm();
System.out.println("Input string: " + this.text);
System.out.println("# of substrings: " + this.subStringCount);
System.out.println("# of comparisons: " + this.comparisons);
}
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() {
2024-02-15 18:41:33 +00:00
this.subStringCount = 0;
this.comparisons = 0;
char[] values = this.text.toLowerCase().toCharArray();
for (int i = 0; i < this.text.length(); i++) {
if (values[i] == 'a') {
for (int j = i+1; j < this.text.length(); j++) {
if (values[j] == 'b') {
subStringCount++;
}
comparisons++;
}
}
comparisons++;
}
}
/**
* 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();
}
2024-02-15 18:41:33 +00:00
/**
* 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.
*/
2024-02-15 18:41:33 +00:00
private int getIntegerInput() {
Scanner sc = new Scanner(System.in);
try {
String textInput = sc.nextLine();
return Integer.parseInt(textInput);
} catch (Exception e) {
return -1;
}
}
}