Refactered into subfolders.
This commit is contained in:
parent
2879dd093c
commit
6b620cf0a1
@ -1,2 +0,0 @@
|
||||
package PACKAGE_NAME;public class AnagramsCheck {
|
||||
}
|
224
src/Assignment1/AnagramsCheck.java
Normal file
224
src/Assignment1/AnagramsCheck.java
Normal file
@ -0,0 +1,224 @@
|
||||
// Name: Jonathan Turner
|
||||
// Class: CS 4306/01
|
||||
// Term: Spring 2024
|
||||
// Instructor: Dr. Haddad
|
||||
// Assignment: 1
|
||||
// IDE Name: IntelliJ
|
||||
/*
|
||||
Algorithm Design Block
|
||||
Algorithm title: String Anagram Checker
|
||||
|
||||
|
||||
Logical steps:
|
||||
Step 1: Gather user input for the two separate values to check and save these as Strings.
|
||||
Step 2: Remove Cases and Spaces from the Strings by using replaceAll and toLowerCase.
|
||||
Step 3: Convert the strings to two separate Char Arrays using toChar (A1 and A2).
|
||||
Step 4: Check the length of those Arrays, if they are different they are not anagrams and stop algorithm.
|
||||
Step 5: Create an outer (i) and inner (j) for loop that goes to the length of the array lengths (n).
|
||||
Step 6: Compare the A1[i] to A2[j], if value is the same mark A2[j] empty (' ') and increment comparisons.
|
||||
Step 7: After the for loops, make a boolean value to check if it is an anagram and set it to true.
|
||||
Step 8: Loop through A2, if there is a non-null value then they are not anagrams.
|
||||
|
||||
|
||||
Pseudocode Syntax:
|
||||
word1_string <- input
|
||||
word2_string <- input
|
||||
|
||||
removeSpaces(word1_string)
|
||||
removeCases(word1_string)
|
||||
|
||||
removeSpaces(word2_string)
|
||||
removeCases(word2_string)
|
||||
|
||||
first_word <- word1_string.toChar
|
||||
second_word <- word2_string.toChar
|
||||
|
||||
if first_word.length != second_word.length
|
||||
return False
|
||||
|
||||
for i <- 1 to first_word
|
||||
for j <- 1 to second_word
|
||||
if first_word[i] = second_word[j] // Basic Operation
|
||||
second_word[j] <- ' ';
|
||||
break;
|
||||
end
|
||||
end
|
||||
|
||||
result <- True
|
||||
for i <- 1 to second_word
|
||||
if second_word[i] != ' '
|
||||
result <- false
|
||||
end
|
||||
|
||||
return result
|
||||
|
||||
*/
|
||||
package Assignment1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class AnagramsCheck {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AnagramsCheck program = new AnagramsCheck();
|
||||
program.start();
|
||||
}
|
||||
|
||||
private String first;
|
||||
private String second;
|
||||
private int comparisons;
|
||||
|
||||
public void start() {
|
||||
int option = getOption();
|
||||
while (option != 3) {
|
||||
executeOption(option);
|
||||
option = getOption();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private int getOption() {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private int getIntegerInput() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try {
|
||||
String textInput = sc.nextLine();
|
||||
return Integer.parseInt(textInput);
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays the menu options.
|
||||
*/
|
||||
private void displayMenu() {
|
||||
System.out.println("-----------------MAIN MENU--------------");
|
||||
System.out.println("1. Read input string1 and string2");
|
||||
System.out.println("2. Run algorithm and display output");
|
||||
System.out.println("3. Exit program");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executs the option that is provided in the parameters.
|
||||
*/
|
||||
private void executeOption(int option) {
|
||||
if (option == 1) { // Gets user input and sets the two strings.
|
||||
|
||||
// Gets the first and second string as input.
|
||||
first = getString("first");
|
||||
System.out.println();
|
||||
second = getString("second");
|
||||
|
||||
} else if (option == 2) { // Preforms the algorithm and displays the results.
|
||||
|
||||
// Checks if the strings have been set prior to performing the algorithm
|
||||
if (first == null || first.isEmpty() ||
|
||||
second == null || second.isEmpty()) {
|
||||
System.out.println("Please enter in the two strings prior to performing the algorithm.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Performs the algorithm and gathers results
|
||||
boolean result = performAlgorithm();
|
||||
String output = "Output: ";
|
||||
if (result) {
|
||||
output += "Strings are anagrams";
|
||||
} else {
|
||||
output += "Strings are not anagrams";
|
||||
}
|
||||
|
||||
// Prints results
|
||||
System.out.println("String 1: " + first);
|
||||
System.out.println("String 2: " + second);
|
||||
System.out.println(output);
|
||||
System.out.println("Comparisons: " + comparisons);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private String getString(String name) {
|
||||
System.out.println("Enter the " + name + " string.");
|
||||
System.out.print("String: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String input = sc.nextLine();
|
||||
|
||||
while (input == null || input.isEmpty()) {
|
||||
System.out.println("Please enter a valid string.");
|
||||
System.out.print("String: ");
|
||||
input = sc.nextLine();
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Performs the algorithm in checking if two strings are anagrams.
|
||||
* It accomplishes this by first removing casing and spacing from the strings.
|
||||
*
|
||||
* Next it will go through two nested loops to see if each character from the first string
|
||||
* is inside the second string, if it is it sets the value to null.
|
||||
*
|
||||
* Lastly it loops through the second string to see if there are any non-null values,
|
||||
* if there is it returns false.
|
||||
*/
|
||||
private boolean performAlgorithm() {
|
||||
|
||||
// Removes the casing and spaces and sets each word in a char array.
|
||||
char[] first_word = first.toLowerCase().replaceAll(" ", "").toCharArray();
|
||||
char[] second_word = second.toLowerCase().replaceAll(" ", "").toCharArray();
|
||||
|
||||
comparisons = 0;
|
||||
|
||||
/*
|
||||
Checks if these two strings had the same number of non-whitespace characters.
|
||||
If not, they're not anagrams.
|
||||
*/
|
||||
if (first_word.length != second_word.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Loops through all the characters in the first (i) and second (j) word.
|
||||
for (int i = 0; i < first_word.length; i++) {
|
||||
for (int j = 0; j < second_word.length; j++) {
|
||||
comparisons++; // Counts the number of basic operations
|
||||
if (first_word[i] == second_word[j]) { // Basic Operation
|
||||
second_word[j] = ' ';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if there is any characters that were not set to ' ', if so not all characters matched.
|
||||
boolean valid = true;
|
||||
for (int i = 0; i < second_word.length; i++) {
|
||||
if (second_word[i] != ' ') {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
}
|
219
src/Assignment1/LockerDoors.java
Normal file
219
src/Assignment1/LockerDoors.java
Normal file
@ -0,0 +1,219 @@
|
||||
// Name: Jonathan Turner
|
||||
// Class: CS 4306/01
|
||||
// Term: Spring 2024
|
||||
// Instructor: Dr. Haddad
|
||||
// Assignment: 1
|
||||
// IDE Name: IntelliJ
|
||||
/*
|
||||
Algorithm Design Block
|
||||
Algorithm title: Assignment1.Locker Doors Problem
|
||||
|
||||
Logical steps:
|
||||
Step 1: Declare two fields, An Array of Assignment1.Locker & A Comparisons Integer.
|
||||
Step 2: Gather user input for the number of lockers in the problem.
|
||||
Step 3: Start the locker swap. Check if the input has been gathered prior to execution, if not stop and revert to Step 2.
|
||||
Step 4: Set comparisons to 0.
|
||||
Step 5: Start the outer loop (i) that begins at 1 and ends at n, incremented by 1.
|
||||
Step 6: Start the inner loop (j) that begins at base and ends at n, incremented by the i. (j+=i)
|
||||
Step 7: Check lockers[j], if open close it, if closed open it.
|
||||
Step 8: Inside the j loop, increment comparisons by 1.
|
||||
Step 9: After running the locker swapping, check which lockers are open and closed.
|
||||
Step 10: Print out N, lockers that are open, lockers that are closed, and the comparisons.
|
||||
|
||||
Pseudocode Syntax:
|
||||
size <- input
|
||||
locker <- new locker[input]
|
||||
comparisons <- 0
|
||||
for i <- 1 to n
|
||||
for j <- i to n by j+=i
|
||||
if locker[j] is False // Basic Operation
|
||||
locker[j] <- True
|
||||
else
|
||||
locker[i] <- False
|
||||
comparisons++
|
||||
end
|
||||
end
|
||||
|
||||
open_lockers <- {}
|
||||
closed_lockers <- {}
|
||||
for i <- 1 to n
|
||||
if locker[i] is False
|
||||
closed_lockers.add(i)
|
||||
else
|
||||
open_lockers.add(i)
|
||||
end
|
||||
|
||||
print("Number of Lockers: " + size)
|
||||
print("Open lockers: " + open_lockers)
|
||||
print("Closed lockers: " + closed_lockers)
|
||||
print("Comparisons: " + comparisons)
|
||||
*/
|
||||
package Assignment1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class LockerDoors {
|
||||
/**
|
||||
* Starts the program and begins the sentinel loop.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
LockerDoors program = new LockerDoors();
|
||||
program.start();
|
||||
}
|
||||
|
||||
// The Field Variables
|
||||
private Locker[] lockers;
|
||||
private int comparisons;
|
||||
|
||||
/**
|
||||
* Controls the sentinal loop. Begins with getting an option, executing it, and repeating.
|
||||
* Only exits when option is 3.
|
||||
*/
|
||||
public void start() {
|
||||
int option = getOption();
|
||||
while (option != 3) {
|
||||
executeOption(option);
|
||||
option = getOption();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private int getOption() {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private int getIntegerInput() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
try {
|
||||
String textInput = sc.nextLine();
|
||||
return Integer.parseInt(textInput);
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method executes the option based on that which was provided in the parameters.
|
||||
* This method prevents any option from running (Ex. Option 2) until all preconditions have been met.
|
||||
* Such as setting a size of lockers prior to running the algorithm on the lockers.
|
||||
*/
|
||||
private void executeOption(int option) {
|
||||
if (option == 1) { // Sets the size of lockers based on user input.
|
||||
System.out.print("Enter the number of lockers: ");
|
||||
int input = getIntegerInput(); // Gets the user's input for the number of lockers
|
||||
|
||||
// If the input is non-positive then it loops until a positive value is presented.
|
||||
while (input < 1) {
|
||||
System.out.println("\nPlease enter a valid number of lockers.");
|
||||
System.out.print("Enter the number of lockers: ");
|
||||
input = getIntegerInput();
|
||||
}
|
||||
|
||||
// Initializes the lockers field to the provided input size.
|
||||
lockers = new Locker[input];
|
||||
} else if (option == 2) { // Runs the algorithm on the method and prints out the result.
|
||||
|
||||
// Checks if the number of lockers were set or not.
|
||||
if (lockers == null) {
|
||||
System.out.println("Please enter a number of lockers prior to running the algorithm.\n");
|
||||
return; // Stops the algorithm from running on a null array.
|
||||
}
|
||||
|
||||
// Sets all the lockers to closed and initializes all variables.
|
||||
for (int i = 0; i < lockers.length; i++) {
|
||||
lockers[i] = new Locker();
|
||||
}
|
||||
|
||||
performAlgorithm(); // Starts the locker problem algorithm.
|
||||
|
||||
// Calculates the statistics (Comparison, open/closed lockers)
|
||||
String openLockers = "";
|
||||
String closedLockers = "";
|
||||
|
||||
for (int i = 1; i <= lockers.length; i++) {
|
||||
if (lockers[i-1].isOpen) {
|
||||
openLockers += i + " ";
|
||||
} else {
|
||||
closedLockers += i + " ";
|
||||
}
|
||||
}
|
||||
|
||||
// Prints the output.
|
||||
System.out.println("Number of lockers: " + lockers.length);
|
||||
System.out.println("Open lockers: " + openLockers);
|
||||
System.out.println("Closed lockers: " + closedLockers);
|
||||
System.out.println("Comparisons: " + comparisons);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Displays the menu options.
|
||||
*/
|
||||
private void displayMenu() {
|
||||
System.out.println("-----------------MAIN MENU--------------");
|
||||
System.out.println("1. Read number pf lockers (integer value)");
|
||||
System.out.println("2. Run algorithm and display output");
|
||||
System.out.println("3. Exit program");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the lockers algorithm and begins opening and closing the respective doors.
|
||||
* After every toggle, the comparison operated is incremented as that is the basic operation.
|
||||
*/
|
||||
public void performAlgorithm() {
|
||||
if (lockers == null || lockers.length == 0) { // Prevents code from being run on a null array.
|
||||
return;
|
||||
}
|
||||
|
||||
comparisons = 0;
|
||||
// Runs the algorithm n times through the entire list.
|
||||
for (int base = 1; base <= lockers.length; base++) {
|
||||
// Controls which lockers to flip. Goes from the base index to n, incremented by base.
|
||||
for (int toggle = base-1; toggle < lockers.length; toggle=toggle+base) {
|
||||
// Flips the locker door.
|
||||
lockers[toggle].toggle(); // Basic Operation
|
||||
comparisons++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Made my locker list easier to follow by toggling.
|
||||
class Locker {
|
||||
public boolean isOpen;
|
||||
|
||||
Locker() { // Sets all new lockers to false.
|
||||
isOpen = false;
|
||||
}
|
||||
|
||||
// Allowed for simpler switching.
|
||||
public void toggle() {
|
||||
isOpen = !isOpen;
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
// Name: Jonathan Turner
|
||||
// Class: CS 4306/01
|
||||
// Term: Spring 2024
|
||||
// Instructor: Dr. Haddad
|
||||
// Assignment: 1
|
||||
// IDE Name: IntelliJ
|
||||
/*
|
||||
Algorithm Design Block
|
||||
Algorithm title: Find and print the smallest number in a list of numbers
|
||||
Logical steps:
|
||||
Step 1: Declare a variable to hold the smallest number.
|
||||
Step 2: Set the smallest number variable to the first number in the list.
|
||||
Step 2: Set a loop to check for a smaller number in the remainder of the list.
|
||||
Update the smallest number variable if a smaller value is found.
|
||||
Step 4: Print out the smallest number.
|
||||
Pseudocode syntax:
|
||||
Algorithm: Find and print smallest number in a list of numbers
|
||||
Input: Non-empty List (L) of numbers
|
||||
Output: Smallest number in list L
|
||||
Begin
|
||||
Smallest <- L[1];
|
||||
for i <- 2 to L.length do
|
||||
if (L[i] < Smallest)
|
||||
Smallest <- L[i];
|
||||
End Loop
|
||||
Print out Smallest;
|
||||
End;
|
||||
******************************************/
|
||||
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Locker {
|
||||
public boolean isOpen;
|
||||
|
||||
Locker() {
|
||||
isOpen = false;
|
||||
}
|
||||
|
||||
public void toggle() {
|
||||
isOpen = !isOpen;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user