-
Notifications
You must be signed in to change notification settings - Fork 11
Pull request for updates/review #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,116 @@ | ||
| // Nick Cudd | ||
| // CS 410 Spring 2023 | ||
| // JUnit Testing Assignment | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String myString; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return myString; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| myString = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if (myString == null || myString.length() == 0) { | ||
| return 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Input Validation: The code correctly checks if the input string (myString) is null or empty. Returning 0 in these cases seems appropriate, assuming that an empty string should not be counted as a word. |
||
| } | ||
| int wordCount = 0; | ||
| // Use both whitespace and half thin-space characters to slice strings | ||
| String[] tokens = myString.split(" | "); | ||
| for (String i : tokens) { | ||
| if (Pattern.matches("^[A-Za-z]+$", i)) { | ||
| wordCount++; | ||
| } | ||
| } | ||
| return wordCount; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code iterates over the tokens and checks if each token consists only of alphabetical characters using a regular expression pattern. This pattern matches strings that contain only one or more uppercase or lowercase letters. This approach assumes that words are defined as sequences of alphabetic characters without any punctuation or numbers. If this definition aligns with your requirements, then the code is suitable. |
||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("n is invalid: n > 0"); | ||
| } else if (myString == null) { | ||
| throw new NullPointerException("String has not been initialized yet, or is null"); | ||
| } else if (n > myString.length()) { | ||
| throw new IndexOutOfBoundsException("n cannot exceed length of parameter String"); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throws appropriate exceptions for invalid cases. |
||
| ArrayList<Character> characters = new ArrayList<Character>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of new ArrayList(), you can use new ArrayList<>() to let the compiler infer the type. |
||
| for (char c : myString.toCharArray()) { | ||
| characters.add(c); | ||
| } | ||
|
|
||
| for (int position = n, k = 2; position <= myString.length(); position = k * n, k++) { | ||
| if (maintainSpacing) { | ||
| characters.set((position - 1), ' '); | ||
| } else | ||
| characters.set((position - 1), null); | ||
| } | ||
|
|
||
| String newString = ""; | ||
| for (Character c: characters) { | ||
| if (c != null) { | ||
| newString += c; | ||
| } | ||
| } | ||
| return newString; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| if (myString == null) { | ||
| throw new NullPointerException("String has not been initialized yet, or is null"); | ||
| } else if (startWord <= 0 || endWord <= 0) { | ||
| throw new IllegalArgumentException("startWord and endWord cannot be less than 1"); | ||
| } else if (endWord < startWord) { | ||
| throw new IndexOutOfBoundsException("index of endWord cannot be prior to index of startWord"); | ||
| } | ||
|
|
||
| int numberOfWords = (endWord - startWord) + 1; | ||
| String[] words = myString.split(" | "); | ||
| String[] subWords = new String[numberOfWords]; | ||
| for (int i = 0, j = startWord - 1; i < numberOfWords; i++, j++) { | ||
| subWords[i] = words[j]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using a fixed-size array subWords, consider using a List implementation, such as an ArrayList, to allow for dynamic resizing as needed. This eliminates the need to calculate the number of words beforehand. |
||
| } | ||
| return subWords; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| if (myString == null) { | ||
| throw new NullPointerException("String has not been initialized yet, or is null"); | ||
| } | ||
| // Examine passed indices array for validity | ||
| if (indices.length != myString.length()) { | ||
| throw new IllegalArgumentException("passed index array must be same length as current String"); | ||
| } | ||
| for (int i = 0; i < indices.length - 1; i++) { | ||
| if (indices[i] < 0 || indices[i] > myString.length()) { | ||
| throw new IndexOutOfBoundsException("Array of indices cannot contain negative integers or integers greater than the length of the current String"); | ||
| } | ||
| // Check for duplicate integers | ||
| for (int j = i + 1; j < indices.length; j++) { | ||
| if (indices[i] == indices[j]) { | ||
| throw new IllegalArgumentException("passed index array must contain unique integer values"); | ||
| } | ||
| } | ||
| } | ||
| char[] charString = myString.toCharArray(); | ||
| char[] referenceString = myString.toCharArray(); | ||
| for (int i = 0; i < charString.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code creates two separate character arrays, charString and referenceString, from myString.toCharArray(). However, you can directly modify charString in place without the need for the referenceString array. |
||
| charString[i] = referenceString[indices[i]]; | ||
| } | ||
| return new String(charString); | ||
| } | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.myString as it is a property of the class.