Created default sentinal loop values and input menu options.
This commit is contained in:
parent
b935c1f10e
commit
a66a462ced
@ -37,16 +37,109 @@ Algorithm Design Block
|
|||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package Assignments.A2;
|
package Assignments.A2;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a brute force/exhaustive search for
|
* Performs a brute force/exhaustive search for
|
||||||
* how many substrings start with A and end with B.
|
* how many substrings start with A and end with B.
|
||||||
*
|
*
|
||||||
* @author Jonathan Turner
|
* @author Jonathan Turner
|
||||||
|
* @version Spring 2024
|
||||||
*/
|
*/
|
||||||
public class Substrings {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user