Implemented Sentinel Loop.

This commit is contained in:
Jonathan Turner 2024-02-18 12:48:44 -05:00
parent da90939e5e
commit 456c003159

View File

@ -59,7 +59,9 @@ Algorithm Design Block
package Assignments.A2; package Assignments.A2;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Scanner;
/** /**
* Generates, if possible, two equal subsets of a given set of elements that are equal to each other. * Generates, if possible, two equal subsets of a given set of elements that are equal to each other.
@ -72,6 +74,155 @@ public class Partition {
private int[] set; private int[] set;
private List<List<Integer>> subsets; private List<List<Integer>> subsets;
public static void main(String[] args) {
Partition program = new Partition();
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) {
if (option == 1) {
System.out.println("Please specify the size of the set.");
System.out.print("Size: ");
int newSize = this.getIntegerInput();
while (newSize < 1) {
System.out.println("Please enter a valid set size.");
System.out.print("Size: ");
newSize = this.getIntegerInput();
}
this.set = new int[newSize];
} if (option == 2) {
if (this.set == null) {
System.out.println("Please enter a set size and values ");
System.out.println();
return;
}
this.set = this.getValues();
} else {
if (this.set == null || this.set[0] == 0) {
System.out.println("Please enter a set size and values before executing the algorithm.");
System.out.println();
return;
}
this.performAlgorithm();
}
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;
}
/**
* Displays the menu options.
*
* @precondition none
* @postcondition the menu is displayed.
*/
private void displayMenu() {
System.out.println("-----------------MAIN MENU--------------");
System.out.println("1. Read set size (number of integers)");
System.out.println("2. Read set elements (integer values)");
System.out.println("3. Run algorithm and display outputs");
System.out.println("4. 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.
*/
private int getIntegerInput() {
Scanner sc = new Scanner(System.in);
try {
String textInput = sc.nextLine();
return Integer.parseInt(textInput);
} catch (Exception e) {
return -1;
}
}
private int[] getValues() {
Scanner sc = new Scanner(System.in);
String input = null;
int[] values = new int[this.set.length];
System.out.println("Please enter a list of " + this.set.length + " positive values. (Ex. 34 35 21 23)");
while (values[0] == 0) {
System.out.print("Values: ");
input = sc.nextLine();
String[] stringRep = input.split(" ");
/* If the input values are not the same number as the set size */
if (stringRep.length != values.length || input.isEmpty() ) {
System.out.println("Please enter a valid list of " + this.set.length + " positive values. (Ex. 34 35 21 23)");
continue;
}
/* Attempts to convert each value from String to Integer. */
try {
for (int i = 0; i < values.length; i++) {
values[i] = Integer.parseInt(stringRep[i]);
if (values[i] < 1) {
System.out.println("Please enter a valid list of " + this.set.length + " positive values. (Ex. 34 35 21 23)");
Arrays.fill(values, 0);
break;
}
}
} catch (Exception e) {
System.out.println("Please enter a valid list of " + this.set.length + " positive values. (Ex. 34 35 21 23)");
}
}
return values;
}
/** /**
* Iterates through all the possible subsets generated by this.generateSubsets * Iterates through all the possible subsets generated by this.generateSubsets
* and if a subset is found that is exactly half the size it sets the firstset * and if a subset is found that is exactly half the size it sets the firstset
@ -92,6 +243,7 @@ public class Partition {
* Checks through every subset that was generated, * Checks through every subset that was generated,
* if a subset is found it sets the firstSubset to the found subset. * if a subset is found it sets the firstSubset to the found subset.
*/ */
generateSubsets();
for (List<Integer> subset : subsets) { for (List<Integer> subset : subsets) {
int subsetTotal = 0; int subsetTotal = 0;
for (int value : subset) { for (int value : subset) {
@ -134,12 +286,12 @@ public class Partition {
String first = "{"; String first = "{";
String second = "{"; String second = "{";
if (firstSubset != null) { if (firstSubset != null) {
for (int fvalues : firstSubset) { for (int fValues : firstSubset) {
first += fvalues + ","; first += fValues + ",";
} }
first = first.substring(0, first.length()-1) + "}"; first = first.substring(0, first.length()-1) + "}";
for (int svalues : secondSubset) { for (int sValues : secondSubset) {
second += svalues + ","; second += sValues + ",";
} }
second = second.substring(0, second.length()-1) + "}"; second = second.substring(0, second.length()-1) + "}";
} else { } else {
@ -147,7 +299,7 @@ public class Partition {
second = "of their elements found"; second = "of their elements found";
} }
System.out.println("Set size: " + setSize); System.out.println("Set size: " + setSize + " integers");
System.out.println("Integer values: " + original); System.out.println("Integer values: " + original);
System.out.println("Disjoint subsets with same sum: " + first); System.out.println("Disjoint subsets with same sum: " + first);
System.out.println(" " + second); System.out.println(" " + second);