-
Notifications
You must be signed in to change notification settings - Fork 6
[Emily_Su_CodeReview] Implement StringManipulation Class and Corresponding Unit Tests #26
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,122 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| import java.util.Arrays; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String[] wordArray; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| if (wordArray == null) { | ||
| throw new NullPointerException("String has not been set for current StringManipulation object (hint: use setString())."); | ||
| } | ||
| return String.join(" ", wordArray).trim(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| if (string.isBlank()) { | ||
| wordArray = new String[] { string }; | ||
| } | ||
| else { | ||
| wordArray = string.split(" "); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if (wordArray == null) { | ||
| return -1; | ||
| } | ||
| if (getString().isBlank()) { | ||
| return 0; | ||
| } | ||
| return wordArray.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| String retVal = getString(); | ||
|
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. You have to convert to string here to do your manipulations which means code is somewhat coupled and dependent on itself. |
||
|
|
||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("First argument is less than or equal to zero."); | ||
| } | ||
| if (n > retVal.length()) { | ||
| throw new IndexOutOfBoundsException("Passed " + n + " as first argument. " + n + " is greater than length of current StringManipulation object: " + retVal.length()); | ||
| } | ||
| if (n == 1) { | ||
| return maintainSpacing ? generateSpaces(retVal.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. nice helper method |
||
| } | ||
| if (maintainSpacing) { | ||
| for (int i = n - 1; i < retVal.length(); i += n) { | ||
| retVal = retVal.substring(0, i) + " " + retVal.substring(i + 1); | ||
|
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 is clever use of .substring(x,y) |
||
| } | ||
| } | ||
| else { | ||
| for (int i = n - 1; i < retVal.length(); i += n) { | ||
| retVal = retVal.substring(0, i) + retVal.substring(i + 1); | ||
|
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. clever use of this method |
||
| i--; | ||
| } | ||
| } | ||
| return retVal; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a string consisting of n spaces | ||
| * | ||
| * Example | ||
| * Method Call: generateSpaces(5); | ||
| * Return: " " | ||
| * | ||
| * @param n Determines the number of spaces to return | ||
| * @return String consisting of n spaces | ||
| */ | ||
| public String generateSpaces(int n) { | ||
| if (n < 0) { | ||
| throw new IllegalArgumentException("Cannot generate negative number of spaces."); | ||
| } | ||
| String retVal = ""; | ||
| for (int i = 0; i < n; i++) { | ||
| retVal += " "; | ||
| } | ||
| return retVal; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| if (wordArray == null) { | ||
| throw new NullPointerException("String has not been set for current StringManipulation object (hint: use setString())."); | ||
| } | ||
| if (startWord <= 0) { | ||
| throw new IllegalArgumentException("Index for startWord (first argument) must be greater than or equal to 1."); | ||
| } | ||
| if (endWord <= 0) { | ||
| throw new IllegalArgumentException("Index for endWord (second argument) must be greater than or equal to 1."); | ||
| } | ||
| if (startWord > endWord) { | ||
| throw new IllegalArgumentException("Index for startWord (first argument) must be less than index for endWord (second argument)."); | ||
| } | ||
| if (endWord > count()) { | ||
| throw new IndexOutOfBoundsException("Index of endWord is greater than word count for current StringManipulation object."); | ||
| } | ||
| return Arrays.copyOfRange(wordArray, startWord - 1, endWord); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| String currentString = getString(); | ||
|
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. You had to perform conversion here again to make manipulations |
||
| if (currentString.length() != indices.length) { | ||
| throw new IllegalArgumentException("Length mismatch between number of words in current StringManipulation object and length of given indices array."); | ||
| } | ||
| String[] retVal = new String[currentString.length()]; | ||
| for (int i = 0; i < indices.length; i++) { | ||
| if (indices[i] < 0) { | ||
| throw new IndexOutOfBoundsException("Invalid index: Element at index " + i + " in passed array is less than zero."); | ||
| } | ||
| if (indices[i] >= currentString.length()) { | ||
| throw new IndexOutOfBoundsException("Invalid index: Element at index " + i + " in passed array is GTE the length of the current StringManipulation object."); | ||
| } | ||
| retVal[indices[i]] = String.valueOf(currentString.charAt(i)); | ||
| } | ||
| return String.join("", retVal); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,148 +2,177 @@ | |
| import org.junit.jupiter.api.BeforeEach; | ||
|
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. tests look good |
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
||
| private StringManipulationInterface manipulatedstring; | ||
| private StringManipulationInterface manipulatedString; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| manipulatedstring = new StringManipulation(); | ||
| manipulatedString = new StringManipulation(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| manipulatedstring = null; | ||
| manipulatedString = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(-1, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString(""); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Thisismystring"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("This is my string"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter1() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedstring.removeNthCharacter(3, false)); | ||
| Exception e = assertThrows(NullPointerException.class, () -> { manipulatedString.removeNthCharacter(3, false); }); | ||
| assertEquals(e.getMessage(), "String has not been set for current StringManipulation object (hint: use setString())."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter2() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| Exception e = assertThrows(IllegalArgumentException.class, () -> { manipulatedString.removeNthCharacter(0, false); }); | ||
| assertEquals(e.getMessage(), "First argument is less than or equal to zero."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| int n = 50; | ||
| Exception e = assertThrows(IndexOutOfBoundsException.class, () -> { manipulatedString.removeNthCharacter(n, false); }); | ||
| assertEquals(e.getMessage(), "Passed " + n + " as first argument. " + n + " is greater than length of current StringManipulation object: " + manipulatedString.getString().length()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("", manipulatedString.removeNthCharacter(1, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals(" ", manipulatedString.removeNthCharacter(1, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedString.removeNthCharacter(3, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedString.removeNthCharacter(3, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| public void testGetSubStrings1() { | ||
| Exception e = assertThrows(NullPointerException.class, () -> { manipulatedString.getSubStrings(3, 4); }); | ||
| assertEquals(e.getMessage(), "String has not been set for current StringManipulation object (hint: use setString())."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings2() { | ||
| manipulatedString.setString("This is my string"); | ||
| Exception e = assertThrows(IllegalArgumentException.class, () -> { manipulatedString.getSubStrings(0, 4); }); | ||
| assertEquals(e.getMessage(), "Index for startWord (first argument) must be greater than or equal to 1."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings3() { | ||
| manipulatedString.setString("This is my string"); | ||
| Exception e = assertThrows(IllegalArgumentException.class, () -> { manipulatedString.getSubStrings(3, 0); }); | ||
| assertEquals(e.getMessage(), "Index for endWord (second argument) must be greater than or equal to 1."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings4() { | ||
| manipulatedString.setString("This is my string"); | ||
| Exception e = assertThrows(IllegalArgumentException.class, () -> { manipulatedString.getSubStrings(4, 3); }); | ||
| assertEquals(e.getMessage(), "Index for startWord (first argument) must be less than index for endWord (second argument)."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings5() { | ||
| manipulatedString.setString("This is my string"); | ||
| Exception e = assertThrows(IndexOutOfBoundsException.class, () -> { manipulatedString.getSubStrings(3, 5); }); | ||
| assertEquals(e.getMessage(), "Index of endWord is greater than word count for current StringManipulation object."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings6() { | ||
| manipulatedString.setString("This is my string"); | ||
| String[] substrings = manipulatedString.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(substrings[0], "my"); | ||
| assertEquals(substrings[1], "string"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| public void testRestoreString1() { | ||
| int[] array = { 1, 0, 2 }; | ||
| Exception e = assertThrows(NullPointerException.class, () -> { manipulatedString.restoreString(array); }); | ||
| assertEquals(e.getMessage(), "String has not been set for current StringManipulation object (hint: use setString())."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString2() { | ||
| manipulatedString.setString("art"); | ||
| int[] array = { 1, 0, 2, 3 }; | ||
| Exception e = assertThrows(IllegalArgumentException.class, () -> { manipulatedString.restoreString(array); }); | ||
| assertEquals(e.getMessage(), "Length mismatch between number of words in current StringManipulation object and length of given indices array."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString3() { | ||
| manipulatedString.setString("art"); | ||
| int[] array = { 1, 0, 2 }; | ||
| int targetIdx = 0; | ||
| array[targetIdx] = -1; | ||
| Exception e = assertThrows(IndexOutOfBoundsException.class, () -> { manipulatedString.restoreString(array); }); | ||
| assertEquals(e.getMessage(), "Invalid index: Element at index " + targetIdx + " in passed array is less than zero."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString4() { | ||
| manipulatedString.setString("art"); | ||
| int[] array = { 1, 0, 2 }; | ||
| int targetIdx = 0; | ||
| array[targetIdx] = 3; | ||
| Exception e = assertThrows(IndexOutOfBoundsException.class, () -> { manipulatedString.restoreString(array); }); | ||
| assertEquals(e.getMessage(), "Invalid index: Element at index " + targetIdx + " in passed array is GTE the length of the current StringManipulation object."); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString5() { | ||
| manipulatedString.setString("art"); | ||
| int[] array = { 1, 0, 2 }; | ||
| String restoredString = manipulatedString.restoreString(array); | ||
| assertEquals(restoredString, "rat"); | ||
| } | ||
|
|
||
| } | ||
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 is more my opinion but after reviewing all of your code I think you might have done yourself a slight disservice by using an array of Strings as your private field. Throughout your code you are converting between the array to the String quite often and the state of your variable changes depending on the requirements of the method. Several of the methods have to call one another to achieve basic output. For example, your return removeNthCharacter method has to convert the string to an array and then perform manipulations on it. The only method which really saved you time by using the array of Strings field was the get substrings method but depending on the final implementation this time savings may or may not be worth it. Given the generic nature of the assignment and the number of methods we had to implement it might have been easier to work with a simple string and convert it to an array when necessary.