-
Notifications
You must be signed in to change notification settings - Fork 11
Added implementation for StringManipulation Files #14
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
Open
NadavH12
wants to merge
2
commits into
SWE-CS410:main
Choose a base branch
from
NadavH12:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,113 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| /* Nadav Horowitz 6/3/2023 StringManipulation.java | ||
| * This file contains various methods for manipulation of a String object. | ||
| * The methods implemented include count, removeNthCharacter, getSubStrings, and restoreString. | ||
| */ | ||
| import java.util.Arrays; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| public String string; | ||
|
|
||
|
|
||
| @Override | ||
| //Getter method for String field | ||
| public String getString() { | ||
| return null; | ||
| return this.string; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| //Setter method for String field | ||
| public void setString(String string) { | ||
| this.string = string; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| //Returns number of words present in String object | ||
| public int count() { | ||
| return 0; | ||
| //null case and empty case | ||
| if (string == null || string.length() == 0) | ||
| return 0; | ||
| //trim leading and ending whitespace | ||
| String copy = this.getString(); | ||
| copy = copy.trim(); | ||
| //Regex for matching words | ||
| String[] words = copy.split(" +"); | ||
| return words.length; | ||
| } | ||
|
|
||
|
|
||
|
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. This code shows good practice in terms of readability, by using specific and detailed comments before each method header in the class. |
||
| @Override | ||
| //Returns a string that consists of all characters in the original string except for the characters | ||
| //in positions n, 2n, 3n, and so on, either deleting those or replacing them with a white space. | ||
| //Throws IllegalArgumentException for n <= 0, throws IndexOutOfBoundsException for n > string length. | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if(n <= 0) | ||
| throw new IllegalArgumentException(); | ||
| if(n > string.length()) | ||
| throw new IndexOutOfBoundsException(); | ||
|
|
||
| String replacement = ""; | ||
| if(maintainSpacing) | ||
| replacement += " "; | ||
|
|
||
| String newString = ""; | ||
| for(int i = 1; i <= string.length(); i++){ | ||
| char next = string.charAt(i-1); | ||
| if(i % n == 0) | ||
| newString += replacement; | ||
| else | ||
| newString += next; | ||
| } | ||
| return newString; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| //Returns String[] containing words from position "startWord" to position "endWord" in String object, | ||
| //with 1 being the first Word in the String. | ||
| //Throws IllegalArgumentException If either "startWord" or "endWord" are invalid. | ||
| //Throws IndexOutOfBoundsException If the string has less than "endWord" words in it. | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| if(startWord <= 0 || endWord <= 0 || startWord > endWord) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| //trim leading and ending whitespace | ||
| String copy = this.getString(); | ||
| copy = copy.trim(); | ||
|
|
||
| //Regex for matching words | ||
| String[] words = copy.split(" +"); | ||
|
|
||
| if(endWord > words.length) | ||
| throw new IndexOutOfBoundsException(); | ||
|
|
||
| words = Arrays.copyOfRange(words,startWord - 1, endWord); | ||
| return words; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| //Given current String object and integer array "indices" of the same length, String will be shuffled such that | ||
| //the character at the ith position moves to indices[i] in the shuffled string. Returns the shuffled string. | ||
| //Throws IllegalArgumentException if string.length != indices.length | ||
| //Throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length. | ||
| public String restoreString(int[] indices){ | ||
| String copy = this.getString(); | ||
|
|
||
| if(copy.length() != indices.length) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| String newString = ""; | ||
| for(int i = 0; i < indices.length; i++) { | ||
| int stringIndex = indices[i]; | ||
|
|
||
| if(stringIndex < 0 || stringIndex > indices.length) | ||
| throw new IndexOutOfBoundsException(); | ||
|
|
||
| newString += copy.charAt(stringIndex); | ||
| } | ||
| return newString; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since the string is already a member of the class, there's no need to call a getter method of getString() to access it, nor is there any reason to copy the string every time -- none of these methods are mutators.
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.
I make a copy here because afterwards I call the String.trim() method, which removes leading and lagging whitespace. Just for the sake of not changing the original string.