diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..ab18763 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,135 @@ -public class StringManipulation implements StringManipulationInterface { + +public class StringManipulation implements StringManipulationInterface { + + private String myString; + @Override public String getString() { - return null; + return myString; } @Override public void setString(String string) { + myString = string; } @Override public int count() { - return 0; + if (myString == null) { + throw new NullPointerException("Cannot count uninitialized string."); + } + if (myString.isEmpty() || myString.isBlank()) { + return 0; + } + String[] arrOfStr = myString.split(" "); + int countOfWords = arrOfStr.length; + return countOfWords; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + char[] chars = myString.toCharArray(); + String str = null; + + if (n > chars.length) { + throw new IndexOutOfBoundsException("n cannot be greater than the length of the string"); + } + + if (n <= 0) { + throw new IllegalArgumentException("n must be greater than 0"); + } + + if(maintainSpacing) { + if (n==1) { + for (int i = 0; i=1) { + for (int i = 0; i<=chars.length; i=i+n) { + if (i == 0) { + continue; + } + int index = i-1; + chars[index] = ' '; + } + } + str = String.copyValueOf(chars); + } + else if (!maintainSpacing) + { + if (n==1) { + return null; + } + char[] temp = new char[chars.length-((int)Math.floor(chars.length/n))]; + if (n>=1) + { + for (int i = 0, k=0; i endWord || startWord <= 0 || endWord <= 0) { + throw new IllegalArgumentException("startWord must be greater than endWord. Both startWord and endWord must be greater than 0."); + } + + String[] substr = myString.split(" "); + + if (substr.length < endWord) { + throw new IndexOutOfBoundsException("endWord cannot be greater than length of substr"); + } + + String[] temp = new String[endWord-startWord+1]; + for (int i = startWord-1, j = 0; i myString.length() || indices[i] < 0) { + throw new IndexOutOfBoundsException("indices must be within length of the string"); + } + sumOfIndexes = sumOfIndexes + indices[i]; + k=k+i; + } + if (sumOfIndexes != k) { + throw new IllegalArgumentException("incorrect indexes provided"); + } + char[] chars = myString.toCharArray(); + char[] temp = new char[chars.length]; + for (int i = 0; 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..4a2b0e2 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,7 +1,9 @@ + + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - +import org.junit.jupiter.api.function.Executable; import static org.junit.jupiter.api.Assertions.*; @@ -20,6 +22,7 @@ public void tearDown() { } @Test + // Checks behavior for a string public void testCount1() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); @@ -27,58 +30,96 @@ public void testCount1() { } @Test + // Checks behavior for 1 word string public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("string"); + int length = manipulatedstring.count(); + assertEquals(1, length); } @Test + // Checks behavior for empty string public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + // Checks behavior for uninitialized string public void testCount4() { - fail("Not yet implemented"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.count(); + } + }; + NullPointerException exception = assertThrows(NullPointerException.class, executable); + assertEquals("Cannot count uninitialized string.", exception.getMessage()); } @Test + // Checks behavior for not preserving whitespace 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)); } @Test + // Checks behavior for preserving whitespace 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)); } @Test + // Checks behavior for preserving white space with n = 1 (all characters removed) public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } @Test + // Checks behavior for not preserving white space with n = 1 (all characters removed) public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + assertEquals(null, manipulatedstring.removeNthCharacter(1, false)); } @Test + // Checks behavior for removing interval of characters greater than string length public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.removeNthCharacter(20, false); + } + }; + IndexOutOfBoundsException exception = assertThrows(IndexOutOfBoundsException.class, executable); + assertEquals("n cannot be greater than the length of the string", exception.getMessage()); } @Test + // Checks behavior for removing interval of characters <0 public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.removeNthCharacter(-1, false); + } + }; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable); + assertEquals("n must be greater than 0", exception.getMessage()); } @Test + // Checks behavior for removing characters near end of string public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + assertEquals("I <3 string ", manipulatedstring.removeNthCharacter(12, true)); } @Test + // Checks behavior for substrings public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -88,27 +129,60 @@ public void testGeSubStrings1() { } @Test + // Checks behavior for start or end word = 0 public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.getSubStrings(0, 0); + } + }; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable); + assertEquals("startWord must be greater than endWord. Both startWord and endWord must be greater than 0.", exception.getMessage()); } @Test + // Checks behavior for start word > end word public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.getSubStrings(3, 2); + } + }; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable); + assertEquals("startWord must be greater than endWord. Both startWord and endWord must be greater than 0.", exception.getMessage()); } @Test + // Checks behavior for end word > than string length public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + Executable executable = new Executable() { + public void execute() { + manipulatedstring.getSubStrings(1, 5); + } + }; + IndexOutOfBoundsException exception = assertThrows(IndexOutOfBoundsException.class, executable); + assertEquals("endWord cannot be greater than length of substr", exception.getMessage()); } @Test + // Checks behavior for start word and end word being equal public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("I <3 strings"); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(sStings[0], "I"); } @Test + // Extra test? public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("I am not sure why there is a 6th test here"); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(sStings[0], "I"); } @Test + // Checks behavior for restoring string public void testRestoreString1() { manipulatedstring.setString("art"); @@ -119,31 +193,59 @@ public void testRestoreString1() } @Test + // Checks behavior for 1 index out of bounds in indices array public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + Executable executable = new Executable() { + public void execute() { + int [] array = new int[]{1,0,10}; + manipulatedstring.restoreString(array); + } + }; + IndexOutOfBoundsException exception = assertThrows(IndexOutOfBoundsException.class, executable); + assertEquals("indices must be within length of the string", exception.getMessage()); } @Test + // Checks behavior for too many indices given public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + Executable executable = new Executable() { + public void execute() { + int [] array = new int[]{0,1,2,3}; + manipulatedstring.restoreString(array); + } + }; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable); + assertEquals("the number of indices provides must match positions in the string", exception.getMessage()); } @Test + // Checks behavior for non-unique indices public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + Executable executable = new Executable() { + public void execute() { + int [] array = new int[]{2,2,2,}; + manipulatedstring.restoreString(array); + } + }; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, executable); + assertEquals("incorrect indexes provided", exception.getMessage()); } @Test + // additional test ? public void testRestoreString5() { - fail("Not yet implemented"); - + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4,5,6,7,0,1,2,3}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "TestUnit"); } }