diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..24a4d20 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,122 @@ 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 trimmedString = string.trim(); + if (trimmedString.isEmpty()) { + return 0; + } + + // Split the string by whitespace to get individual words + String[] words = trimmedString.split("\\s+"); + + return words.length; } + + @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; - } + if (n <= 0) { + throw new IllegalArgumentException("Error: n is less than or equal to 0"); + } + else if (string == null) { + throw new NullPointerException("Error: String has not been initialized"); + } + else if (n > string.length()) { + throw new IndexOutOfBoundsException("Error: n exceeds string length"); + } + + + StringBuilder result = new StringBuilder(); + int count = 0; + for (int i = 0; i < string.length(); i++) { + char currentChar = string.charAt(i); + count++; + if (count % n == 0 && maintainSpacing) { + result.append(' '); + } + if (count % n != 0) { + result.append(currentChar); + } + + + } + + return result.toString(); + } + + + + @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + if (string == null) { + throw new NullPointerException("Error: string is not yet initialized"); + } + String[] words = string.trim().split("\\s+"); + if(startWord <= 0 || endWord <= 0) { + throw new IllegalArgumentException("Error: start index and end index is less than 1"); + } + + else if (startWord > endWord) { + throw new IndexOutOfBoundsException("Error: start index is larger than end"); + } + + else if (endWord > words.length) { + throw new IndexOutOfBoundsException("Error: end index is larger than the string length"); + } + + int substringsCount = endWord - startWord + 1; + String[] substrings = new String[substringsCount]; + + int index = 0; + for (int i = startWord - 1; i < endWord; i++) { + substrings[index++] = words[i]; + } + + return substrings; } @Override public String restoreString(int[] indices) { - return null; - } + if (string == null) { + throw new NullPointerException("Error: string is not yet initialized"); + } - -} + if (indices.length != string.length()) { + throw new IllegalArgumentException("Error: string length is not equal to indices length"); + } + for (int i = 0; i < indices.length - 1; i++) { + for (int j = i + 1; j < indices.length; j++) { + if (indices[i] == indices[j]) { + throw new IllegalArgumentException("Error: some indexes are not unique"); + } + } + } + + char[] restored = new char[indices.length]; + for (int i = 0; i < indices.length; i++) { + + restored[i] = string.charAt(indices[i]); + } + return new String(restored); + } +} \ 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..274ab23 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -24,90 +24,159 @@ public void testCount1() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); assertEquals(4, length); + } + //this test checks whether method count throws an IllegalArgumentException if count is not 4 @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("hi"); + int length = manipulatedstring.count(); + assertEquals(1, length); } + //this test checks whether method count throws an IllegalArgumentException if count is not 1 @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } + //this test checks whether method count throws an IllegalArgumentException if count is not 0 @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(" Spaces between words "); + int length = manipulatedstring.count(); + assertEquals(3, length); } - + //this test checks whether method count throws an IllegalArgumentException if count is not 3 @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)); } - + // this test checks whether method removeNthCharacter prints out the right string, which is "I' bttr uts0e 16tsinths trn6 rgh?" @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)); } + + // this test checks whether method removeNthCharacter prints out the right string, which is "I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?" + @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello"); + IndexOutOfBoundsException testException = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(10, true); + }); + assertEquals("Error: n exceeds string length", testException.getMessage()); } + // this test checks whether method removeNthCharacter throws an IndexOutOfBoundsException if n exceeds string length @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("Testing123"); + assertEquals("Tetig13", manipulatedstring.removeNthCharacter(3, false)); } + + // this test checks whether method removeNthCharacter prints out the right string, which is "Tetig13" @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("Welcome to the jungle"); + assertEquals("W l o e t h u g e", manipulatedstring.removeNthCharacter(2, true)); } + + //this test checks whether method removeNthCharacter prints out the right string, which is "W l o e t h u g e" @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I am loving it!"); + IllegalArgumentException testException = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(-54, true); + }); + assertEquals("Error: n is less than or equal to 0", testException.getMessage()); } + + // this test checks whether method removeNthCharacter throws an IllegalArgumentException if n is less than or equal to 0 @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + NullPointerException testException = assertThrows(NullPointerException.class, () -> { + manipulatedstring.removeNthCharacter(5, true); + }); + assertEquals("Error: String has not been initialized", testException.getMessage()); } + // this test checks whether method removeNthCharacter throws an NullPointerException if string is null @Test - public void testGeSubStrings1() { + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } + // this test checks whether method getSubStrings returns the right substring, which is "my" and "string" @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString(null); + NullPointerException testException = assertThrows(NullPointerException.class, () -> { + manipulatedstring.getSubStrings(3, 4); + }); + assertEquals("Error: string is not yet initialized", testException.getMessage()); } + + // this test checks whether method getSubStrings throws an NullPointerException if the new sub string is null + @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedstring.setString("Hello World! How are you?"); + IndexOutOfBoundsException testException = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(4, 72); + }); + assertEquals("Error: end index is larger than the string length", testException.getMessage()); } + // this test checks whether method getSubStrings throws an IndexOutOfBoundsException if the end index is larger than the string length + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedstring.setString("Java is a popular programming language"); + IllegalArgumentException testException = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(4, -51); + }); + assertEquals("Error: start index and end index is less than 1", testException.getMessage()); } + // this test checks whether method getSubStrings throws an IllegalArgumentException if start index and end index is less than 1 + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedstring.setString("The quick brown fox jumps over the lazy dog"); + IndexOutOfBoundsException testException = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(23, 2); + }); + assertEquals("Error: start index is larger than end", testException.getMessage()); + } + // this test checks whether method getSubStrings throws an IllegalArgumentException if the new sub string does not contain "fox", "jumps", and "over" + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedstring.setString("I love coding in Java"); + String[] sStings = manipulatedstring.getSubStrings(2, 5); + + assertEquals( sStings[0], "love"); + assertEquals(sStings[1], "coding"); + assertEquals(sStings[2], "in"); + assertEquals( sStings[3], "Java"); } - + // this test checks whether method getSubStrings returns the right substring, which is "love", "coding", "in", and "Java" + @Test public void testRestoreString1() { @@ -117,33 +186,54 @@ public void testRestoreString1() String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "rat"); } + + // this test checks whether method restoreString returns the right restored string, which is "rat" @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + int[] array = new int[]{2,0,3,1,4}; + manipulatedstring.setString(null); + NullPointerException testException = assertThrows(NullPointerException.class, () -> { + manipulatedstring.restoreString(array); + }); + assertEquals("Error: string is not yet initialized", testException.getMessage()); } - + + // this test checks whether method count throws an IllegalArgumentException if the new restored string is not "sarudtay" @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + public void testRestoreString3() { + manipulatedstring.setString("abcd"); + int [] array; + array = new int[]{3, 2, 1, 3}; + IllegalArgumentException testException = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }); + assertEquals("Error: some indexes are not unique", testException.getMessage()); } + + // this test checks whether method restoreString throws an IllegalArgumentException if the indexes are not unique @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + public void testRestoreString4() { + int[] array = new int[]{2,1,3,0}; + manipulatedstring.setString("ih"); + IllegalArgumentException testException = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }); + assertEquals("Error: string length is not equal to indices length", testException.getMessage()); } + + // this test checks whether method restoreString throws an IllegalArgumentException if string length is not equal to indices length @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + public void testRestoreString5() { + manipulatedstring.setString("Programming is fun!"); + int [] array; + array=new int[]{3, 14, 9, 13, 6, 2, 1, 12, 4, 11, 7, 18, 8, 17, 16, 15, 10, 0, 5}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "g nsmorir m!inufgPa"); } + + // this test checks whether method restoreString returns the right restored string, which is "g nsmorir m!inufgPa" }