-
Notifications
You must be signed in to change notification settings - Fork 11
Replace StringManipulation.java and StringManipulationTest.java with … #24
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,113 @@ | ||
| import java.util.*; | ||
| import java.lang.String; | ||
|
|
||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String manipulatedstring; | ||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return manipulatedstring; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| } | ||
| public void setString(String string) { manipulatedstring = string;} | ||
|
|
||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| } | ||
| int wordCount = 0; | ||
| if (manipulatedstring == "") { | ||
| return wordCount; | ||
| } | ||
| String trimmedString = manipulatedstring.trim(); | ||
|
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. Seems like trim is a popular tool for this method implementation, looks good to me at the moment. |
||
|
|
||
| wordCount++; | ||
|
|
||
| char[] charArray = trimmedString.toCharArray(); | ||
| for (char c : charArray) { | ||
| if (c == ' ') {wordCount++;} | ||
| } | ||
| return wordCount; | ||
| } // END public int count() -----------------------------------------------------------------------------------| | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| } | ||
|
|
||
| char[] charArray = manipulatedstring.toCharArray(); | ||
| char[] newCharArray = new char[charArray.length]; | ||
|
|
||
| if (n == 1 && maintainSpacing == false){return "";} | ||
|
|
||
| int numRemovals = 1; | ||
|
|
||
| for (int i = 1; i < charArray.length + 1; i++){ | ||
| if (i == (n*numRemovals)) { | ||
| if (maintainSpacing == true){ | ||
| charArray[i - 1] = ' '; | ||
| newCharArray[i - 1] = charArray[i - 1]; | ||
| } else{ | ||
| newCharArray[i - numRemovals] = charArray[i]; | ||
| i++; | ||
| } | ||
| numRemovals++; | ||
| } else{ | ||
| if (maintainSpacing == true){ | ||
| newCharArray[i-1] = charArray[i-1]; | ||
| } else{ | ||
| newCharArray[i-numRemovals] = charArray[i-1]; | ||
| } | ||
| } | ||
| } | ||
| String finalString = String.copyValueOf(newCharArray); | ||
|
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. Good organization to copy things over to one final string. |
||
| if (maintainSpacing == true){ | ||
| return finalString; | ||
| }else{ | ||
| return finalString.trim(); | ||
| } | ||
| } // END public String removeNthCharacter(int n, boolean maintainSpacing) -------------------------------------| | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| } | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| ArrayList<String> al = new ArrayList<String>(); | ||
| ArrayList<Character> cl = new ArrayList<Character>(); | ||
|
|
||
| for (int i = 0; i <manipulatedstring.length(); i++){ | ||
| for (int x = i; x <manipulatedstring.length(); x++) { | ||
|
|
||
| char c = manipulatedstring.charAt(x); | ||
| if ((c == ' ') || (c == '.') || (c == ',') || (c == ':') || (c == ';') || (c == '"')) { | ||
| break; | ||
| } | ||
| cl.add(c); | ||
| } | ||
| if (cl.size() == 0){continue;} | ||
| StringBuilder builder = new StringBuilder(cl.size()); | ||
| for (Character ch: cl){ | ||
| builder.append(ch); | ||
| } | ||
|
|
||
| al.add(builder.toString()); | ||
| i = i + cl.size(); | ||
| cl.clear(); | ||
| } | ||
| String [] requestedWords = new String[endWord-startWord+1]; | ||
| for (int i = 0; i < requestedWords.length; i++){ | ||
| requestedWords[i] = al.get(startWord-1+i); | ||
| } | ||
| return requestedWords; | ||
| } // END public String[] getSubStrings(int startWord, int endWord) --------------------------------------------| | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| public String restoreString(int[] indices){ | ||
| char[] initCharArray = manipulatedstring.toCharArray(); | ||
|
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. Prominent use of toCharArray, clever tool for solving these kinds of string character counting sort of problems. |
||
| char[] shuffledCharArray = new char[manipulatedstring.length()]; | ||
|
|
||
| for (int i = 0; i < indices.length; i++){ | ||
| shuffledCharArray[i] = initCharArray[indices[i]]; | ||
| } | ||
| String shuffledString = String.copyValueOf(shuffledCharArray); | ||
| return shuffledString; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,17 +28,23 @@ public void testCount1() { | |
|
|
||
| @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("Hello"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello "); // 3 spaces after "Hello" | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -55,27 +61,33 @@ public void testRemoveNthCharacter2() { | |
|
|
||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| // out of bounds error --> NOTE: this should fail (because n > 11) but it does not. Realized this too late | ||
| manipulatedstring.removeNthCharacter(20, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World is such an overused phrase:)"); | ||
| assertEquals("Hello Wor d is such an overus d phrase: ", manipulatedstring.removeNthCharacter(10, true)); | ||
|
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. Looks like multiples of 10, good work, good idea to test for bigger jumps. |
||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello --5blank spaces-- World"); | ||
| assertEquals("Helo -5ban sacs- ord", manipulatedstring.removeNthCharacter(3, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); | ||
|
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 a smart test to make, no issues, just seeing if retains the space that we told it to keep. |
||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| assertEquals("", manipulatedstring.removeNthCharacter(1, false)); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -89,23 +101,59 @@ public void testGeSubStrings1() { | |
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is a rather long, overblown, drawn-out, random sentence just used for test purposes."); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 14); | ||
|
|
||
| assertEquals(sStings[0], "This" ); | ||
| assertEquals(sStings[1], "is" ); | ||
| assertEquals(sStings[2], "a" ); | ||
| assertEquals(sStings[3], "rather" ); | ||
| assertEquals(sStings[4], "long" ); | ||
| assertEquals(sStings[5], "overblown" ); | ||
| assertEquals(sStings[6], "drawn-out" ); | ||
| assertEquals(sStings[7], "random" ); | ||
| assertEquals(sStings[8], "sentence" ); | ||
| assertEquals(sStings[9], "just" ); | ||
| assertEquals(sStings[10], "used" ); | ||
| assertEquals(sStings[11], "for" ); | ||
| assertEquals(sStings[12], "test" ); | ||
| assertEquals(sStings[13], "purposes" ); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings3(){ | ||
| manipulatedstring.setString("Hello World"); | ||
| assertThrows(java.lang.IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(0, 1)); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| assertThrows(java.lang.IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 4)); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello, World"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 2); | ||
| assertEquals(sStings[0], "Hello" ); | ||
|
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. Good work checking the shorter strings. Making sure to test all kinds of cases is important. |
||
| assertEquals(sStings[1], "World" ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is a rather long, overblown, drawn-out, random sentence just used for test purposes."); | ||
| String [] sStings = manipulatedstring.getSubStrings(11, 13); | ||
| assertEquals(sStings[0], "used" ); | ||
| assertEquals(sStings[1], "for" ); | ||
| assertEquals(sStings[2], "test" ); | ||
| } | ||
| @Test | ||
| public void testGeSubStringsExtra() { | ||
| manipulatedstring.setString("This is a rather"); //long, overblown, drawn-out, random sentence just used for test purposes."); | ||
| String [] sStings = manipulatedstring.getSubStrings(2, 4); | ||
| assertEquals(sStings[0], "is" ); | ||
| assertEquals(sStings[1], "a" ); | ||
| assertEquals(sStings[2], "rather" ); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -121,28 +169,39 @@ public void testRestoreString1() | |
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| int [] array; | ||
| array=new int[]{6,7,8,9,10,5,0,1,2,3,4}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "World Hello"); | ||
|
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. Looks like a correct rearrangement. There isn't really anything I would change about your test cases. |
||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| public void testRestoreString3() // too many elements in the int []. | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("Hello World"); | ||
| int [] array; | ||
| array=new int[]{6,7,8,9,10,5,0,1,2,3,4,4}; | ||
| assertThrows(ArrayIndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| public void testRestoreString4() // array[4] = 11 which is > 10 | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("Hello World"); | ||
| int [] array; | ||
| array=new int[]{6,7,8,9,11,5,0,1,2,3,4}; | ||
| assertThrows(ArrayIndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| public void testRestoreString5() // index [7] = -1 which is < 0 | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello World"); | ||
| int [] array; | ||
| array=new int[]{6,7,8,9,10,5,0,-1,2,3,4}; | ||
| assertThrows(ArrayIndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); | ||
|
|
||
| } | ||
|
|
||
|
|
||
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.
Ok I have noted that your data member for the string in question is "manipulatedstring" this is a good name and place to begin