diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..374c740 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,94 @@ public class StringManipulation implements StringManipulationInterface { + private String string; + @Override public String getString() { - return null; + return string; } @Override public void setString(String string) { + this.string = string; } @Override public int count() { - return 0; + if (string == null || string.isEmpty()) { + return 0; + } + + String[] words = string.split("\\s+"); + return words.length; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (string == null || string.isEmpty()) { + return string; + } + + if (n <= 0) { + throw new IllegalArgumentException(); + } + + if (n > string.length()) { + throw new IndexOutOfBoundsException(); + } + + String result = ""; + int length = string.length(); + for (int i = 0; i < length; i++) { + char c = string.charAt(i); + if ((i + 1) % n != 0) { + result += c; + } else { + if (maintainSpacing) { + result += " "; + } + } + + } + return result; } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException(); + } + + String[] words = string.split("\\s+"); + if (endWord > words.length) { + throw new IndexOutOfBoundsException(); + } + + int substringCount = endWord - startWord + 1; + String[] substrings = new String[substringCount]; + + for (int i = startWord - 1; i <= endWord - 1; i++) { + substrings[i - startWord + 1] = words[i]; + } + + return substrings; } @Override public String restoreString(int[] indices) { - return null; - } + int n = indices.length; + char[] shuffledChars = new char[n]; + + if (!(string.length() == indices.length && indices.length == n)) { + throw new IllegalArgumentException(); + } + for (int i = 0; i < n; i++) { + if (indices[i] < 0 || indices[i] >= n) { + throw new IndexOutOfBoundsException(); + } + shuffledChars[indices[i]] = string.charAt(i); + } -} + return new String(shuffledChars); + } +} \ No newline at end of file diff --git a/src/main/java/StringManipulationInterface.java b/src/main/java/StringManipulationInterface.java index 87127e7..cc81e80 100644 --- a/src/main/java/StringManipulationInterface.java +++ b/src/main/java/StringManipulationInterface.java @@ -79,10 +79,10 @@ public interface StringManipulationInterface { * The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. * Return the shuffled string. * example: - * Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3] + * Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3] * Output: "TestUnit" * Explanation: - * indices: 4 5 6 7 0 1 2 3 + * indices: 4 5 6 7 0 2 1 3 * String: U n i t T e s t * Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ...... * Output: T e s t U n i t @@ -95,7 +95,7 @@ public interface StringManipulationInterface { * indices length is the same as the string length. * * throws IllegalArgumentException if not s.length == indices.length == n - * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length + * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length * * @param indices is an integer array for shuffled string new indices positions * the character at the ith position moves to indices[i] in the shuffled string. diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..44fb00a 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.*; public class StringManipulationTest { @@ -26,19 +25,29 @@ public void testCount1() { assertEquals(4, length); } + // check to see that a null string will return a word count of 0. @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // tests if numbers or special characters will mess with the word count @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a string that the number $40.00 in the string"); + int length = manipulatedstring.count(); + assertEquals(11, length); } + // another test, but this time we add quotation marks to see if it messes with + // word count @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("This a string that can't fail just because \"can't\" is in the string"); + int length = manipulatedstring.count(); + assertEquals(13, length); } @Test @@ -50,100 +59,152 @@ public void testRemoveNthCharacter1() { @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)); + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", + manipulatedstring.removeNthCharacter(3, true)); } + // like the tests above, but we test it with a different value for n. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", + manipulatedstring.removeNthCharacter(5, false)); } + // we test for a smaller n. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("let's try a smaller string"); + assertEquals("ltstyasalrsrn", + manipulatedstring.removeNthCharacter(2, false)); } + // we test for a smaller n while maintaning the spacing in the string @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("let's try a smaller string"); + assertEquals("l t s t y a s a l r s r n ", + manipulatedstring.removeNthCharacter(2, true)); } + // test to make sure it throws a IndexOutOfBoundsException when n > string + // length @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for n > string length"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); } + // test to make sure it throws a IllegalArgumentException when n < 0 @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IllegalArgumentException for n < 0"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); } @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); + String[] sStings = manipulatedstring.getSubStrings(3, 4); assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } + // test to make sure that it can scale to more substrings @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a very long string that seems to go on and on and on"); + String[] sStings = manipulatedstring.getSubStrings(2, 7); + + assertEquals(sStings[0], "is"); + assertEquals(sStings[1], "a"); + assertEquals(sStings[2], "very"); + assertEquals(sStings[3], "long"); + assertEquals(sStings[4], "string"); + assertEquals(sStings[5], "that"); } + + // tests thats startWord cannot be <= 0 and should throw a + // IllegalArgumentException @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord <= 0"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 5)); } + + // tests thats endWord cannot be <= 0 and should throw a + // IllegalArgumentException @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IllegalArgumentException for endWord <= 0"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 0)); } + + // tests thats startWord < endWord and should throw a IllegalArgumentException + // if it isn't @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord > endWord"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1)); } + + // tests the case where string length is shorter than endWord and throws a + // IndexOutOfBoundsException if it is @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for endWord > string length"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); } @Test - public void testRestoreString1() - { + public void testRestoreString1() { manipulatedstring.setString("art"); - int [] array; - array=new int[]{1,0,2}; + int[] array; + array = new int[] { 1, 0, 2 }; String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "rat"); } + // another test like above to check string length doesn't matter @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + manipulatedstring.setString("CatDog"); + int[] array; + array = new int[] { 3, 4, 5, 0, 1, 2 }; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "DogCat"); } + // another test like above to check string length doesn't matter, even two of + // the same characters @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + public void testRestoreString3() { + manipulatedstring.setString("backwards"); + int[] array; + array = new int[] { 4, 2, 6, 7, 3, 5, 1, 0, 8 }; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "drawbacks"); } + // tests the case where input array contains an invalid string index and throws + // a IndexOutOfBoundsException @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + public void testRestoreString4() { + manipulatedstring.setString("wrote"); + int[] array; + array = new int[] { -1, 0, 1, 2, 3 }; + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); } + // tests the case where input array is not the same length as the string and + // throws a IllegalArgumentException @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + public void testRestoreString5() { + manipulatedstring.setString("left"); + int[] array; + array = new int[] { 0, 1, 2, 3, 4, 5 }; + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); } }