Conversation
There was a problem hiding this comment.
Good job with the code assignment! The majority of my comments are mostly in regards to small optimizations/readability improvements, so I don't consider them necessarily must-fix before approval. In the case of your unit testing, I believe that your tests largely cover the behavior cases that you expect from your methods, but I think that they could also be somewhat improved by refining their scope by splitting them up to test a single functionality per test.
-Nicholas C
| @Override | ||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String objString; |
There was a problem hiding this comment.
Good use of private access modifier to ensure encapsulation of class String member
| public int count() { | ||
| return 0; | ||
|
|
||
| String[] words = this.getString().split(" +"); |
There was a problem hiding this comment.
Good use of one-or-many whitespaces to split on, but because split() uses regex, perhaps you could go even a step farther and use "\s+" instead of " +" so that all potential characters that separate words (like newlines, whitespace, etc.) are split upon.
| } | ||
| this.setString(String.valueOf(objChars)); | ||
| } | ||
| else if(!maintainSpacing) |
There was a problem hiding this comment.
Could use just a simple else here since maintainSpacing only ever has two possibilities
| else | ||
| { | ||
| returnList = new String[endWord-startWord+1]; | ||
| int loopStart = startWord; |
There was a problem hiding this comment.
Can discard and use value of startWord in the following loop where loopStart is, since startWord is passed as an argument originally
| { | ||
| throw new IllegalArgumentException("upper bound greater than lower bound"); | ||
| } | ||
| else if(endWord > this.count()) |
There was a problem hiding this comment.
Since you've already done the work of splitting the String into words in line 66, you could compare the length of that array instead of needing to call the count() method for error checking, but good idea to use existing methods
| public String restoreString(int[] indices) { | ||
| return null; | ||
| public String restoreString(int[] indices){ | ||
| String restore = ""; |
There was a problem hiding this comment.
Really small optimization, but can throw the creation of the String to be returned after the error checking
| { | ||
| newChars[i] = this.getString().charAt(indices[i]); | ||
| } | ||
| restore = (String.valueOf(newChars)); |
There was a problem hiding this comment.
Using the String variable 'restore' is good for enhancing the readability of the code, but can also be left out since you could also return the 'newChars' array as a String with the same method you're using to assign it to 'restore'
| //blanks out string, maintainSpacing=false | ||
| manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); | ||
| assertEquals("", manipulatedstring.removeNthCharacter(1, false)); | ||
| } |
There was a problem hiding this comment.
6 and 7 are good tests, it is worth testing to verify a behavior that you'd expect like this when n = 1.
|
|
||
| assertEquals("Word position argument must be positive nonzero number.", thrown.getMessage()); | ||
| } | ||
|
|
There was a problem hiding this comment.
For getSubStrings() tests, because the behavior you're trying to verify is that the method throws an exception, the assertEquals statements may fall outside their scopes. The assertEquals() statements are best used in tests where you're testing the output, rather than the error checking.
TimSon1022
left a comment
There was a problem hiding this comment.
Overall the code looks well-structured and readable. There are some things you can change and edit but it's very small, so they are not necessarily prioritized changes. Great job though!
| for(int i = 0; i < endWord- startWord+1; i++) | ||
| { | ||
| returnList[i] = words[loopStart - 1 + i]; | ||
| System.out.println(returnList[i]); |
There was a problem hiding this comment.
Make sure to remove any debugging System.out.println statements that are not needed in the final code
| } | ||
| else if(n > this.getString().length()-1) | ||
| { | ||
| throw new IndexOutOfBoundsException("index out of bounds"); |
There was a problem hiding this comment.
The error message can be more descriptive here for if n is more than the string's length minus 1
| } | ||
| else if(endWord > this.count()) | ||
| { | ||
| throw new IndexOutOfBoundsException("upper bound or lower is greater than word count"); |
There was a problem hiding this comment.
In the error statement, you can add to the statement "or startWord is larger than the word count" to complete your error message.
| public int count() { | ||
| return 0; | ||
|
|
||
| String[] words = this.getString().split(" +"); |
There was a problem hiding this comment.
you can use \s+ instead of + so it can represent all whitespace characters
No description provided.