From b3c1256a8431097b7dd1f05e80b3adafdc961fa0 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 14:44:23 -0400 Subject: [PATCH 1/8] updated with my code --- src/main/java/StringManipulation.java | 106 ++++++++++-- src/test/java/StringManipulationTest.java | 192 +++++++++++++++------- 2 files changed, 225 insertions(+), 73 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..f86d8c7 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,117 @@ 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 + /** + * Counts the number of non-empty words in the string. + * @return The count of words. + */ public int count() { - return 0; + String[] words = string.split(" "); + int count = 0; + + for (String word : words) { + if (!word.isEmpty()) { + count++; + } + } + + return count; } - @Override + /** + * Removes the character at the nth position in the string. + * @param n The position of the character to be removed. + * @param maintainSpacing Determines whether to maintain spacing when removing characters. + * @return The string with the nth characters removed. + */ public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + validateCharacterIndex(n); + + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < string.length(); i++) { + if ((i + 1) % n == 0) { + if (maintainSpacing) { + stringBuilder.append(" "); + } + } else { + stringBuilder.append(string.charAt(i)); + } + } + + return stringBuilder.toString(); } - @Override + /** + * Retrieves a substring of words from the specified startWord to the endWord. + * @param startWord The starting index of the substring. + * @param endWord The ending index of the substring. + * @return An array of substrings. + */ public String[] getSubStrings(int startWord, int endWord) { - return null; + validateWordIndices(startWord, endWord); + + String[] words = string.split(" "); + String[] substrings = new String[endWord - startWord + 1]; + int wordCount = 1; + int substringsIndex = 0; + + for (String word : words) { + if (!word.isEmpty()) { + if (wordCount >= startWord && wordCount <= endWord) { + substrings[substringsIndex] = word; + substringsIndex++; + } + wordCount++; + } + } + + return substrings; } - @Override + /** + * Restores the characters from the given indices to form a string. + * @param indices The indices of the characters to be restored. + * @return The restored string. + */ public String restoreString(int[] indices) { - return null; + validateIndicesLength(indices); + + StringBuilder restoredString = new StringBuilder(); + + for (int index : indices) { + validateCharacterIndex(index); + restoredString.append(string.charAt(index)); + } + + return restoredString.toString(); + } + + // Private helper methods for input validation + + private void validateCharacterIndex(int index) { + if (index <= 0 || index > string.length()) { + throw new IndexOutOfBoundsException("Invalid character index"); + } } + private void validateWordIndices(int startWord, int endWord) { + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid word indices"); + } + } + private void validateIndicesLength(int[] indices) { + if (indices.length != string.length()) { + throw new IllegalArgumentException("Invalid indices length"); + } + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..88cb196 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -26,21 +26,49 @@ public void testCount1() { assertEquals(4, length); } + // Checks what happens if an empty string is passed in @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // This test checks that numbers are recognized as strings @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("132 444"); + int length = manipulatedstring.count(); + assertEquals(2, length); } + // This test ensures that the counter works on a long string @Test public void testCount4() { - fail("Not yet implemented"); + StringBuilder stringBuilder = new StringBuilder(); + String phrase = "the quick brown fox jumps over the lazy dog"; + int occurrences = 0; + + for (int i = 0; i < 100; i++) { + stringBuilder.append(phrase); + stringBuilder.append("\n"); + } + + String longString = stringBuilder.toString(); + int index = 0; + while (index < longString.length()) { + index = longString.indexOf(phrase, index); + if (index == -1) { + break; + } + occurrences++; + index += phrase.length(); + } + + assertEquals(100, occurrences); } + @Test public void testRemoveNthCharacter1() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); @@ -52,30 +80,35 @@ 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 public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, true)); } - + @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } - + @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); } - + @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); } - + @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); } @Test @@ -87,26 +120,47 @@ public void testGeSubStrings1() { assertEquals(sStings[1], "string"); } - @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); - } - @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); - } + // START + // This test tries to get substrings with a starting value less than 0 + @Test + public void testGetSubStrings2() { + manipulatedstring.setString("This is my string"); + // This test tries to get substrings with a negative starting value + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 4)); + } + + @Test + public void testGetSubStrings3() { + manipulatedstring.setString("This is my string"); + // This test tries to get substrings with the starting value greater than the ending one + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 1)); + } + + @Test + public void testGetSubStrings4() { + manipulatedstring.setString("This is my string"); + // This test tries to get substrings with both the starting and ending value negative + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-2, -1)); + } + + @Test + public void testGetSubStrings5() { + manipulatedstring.setString("This is my string"); + // This test tries to get substrings with an ending value larger than the string itself + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); + } + + @Test + public void testGetSubStrings6() { + manipulatedstring.setString("This is my string"); + // This test checks getting substrings when the starting and ending value are the same + String[] subStrings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(1, subStrings.length); + assertEquals("his", subStrings[0]); + } + +// END @Test public void testRestoreString1() @@ -118,32 +172,46 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } - @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - - } - -} + // Tests that case is maintained + @Test + public void testRestoreString2() { + manipulatedstring.setString("AaBbCc"); + int[] indices = {1, 0, 3, 2, 5, 4}; + // Tests restoring the string based on the given indices array + String restoreString = manipulatedstring.restoreString(indices); + assertEquals("aAbBcC", restoreString); + } + + @Test + public void testRestoreString3() { + manipulatedstring.setString("AaBbCc"); + int[] indices = {1}; + // Tests when the indices array size is smaller than the string length + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(indices)); + } + + @Test + public void testRestoreString4() { + manipulatedstring.setString("AaBbCc"); + int[] indices = {1, 2, 3, 4, 5, 6, 7}; + // Tests when the indices array size is larger than the string length + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(indices)); + } + + @Test + public void testRestoreString5() { + manipulatedstring.setString("AaBbCc"); + int[] indices = {1, 2, 3, 4, 5, -1}; + // Tests when an index in the indices array is less than zero + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices)); + } + + @Test + public void testRestoreString6() { + manipulatedstring.setString("AaBbCc"); + int[] indices = {1, 2, 3, 4, 5, 7}; + // Tests when an index in the indices array is larger than the string length + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(indices)); + } + +} \ No newline at end of file From 4bed1f1bc57ed316dee4f70006548fa8652d4577 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 14:52:12 -0400 Subject: [PATCH 2/8] fixed test cases --- src/test/java/StringManipulationTest.java | 40 +++++++++++------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 88cb196..b456298 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -86,7 +86,7 @@ public void testRemoveNthCharacter3() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, true)); } - + @Test public void testRemoveNthCharacter4() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); @@ -105,6 +105,7 @@ public void testRemoveNthCharacter6() { assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); } + // This test checks removing a negative Nth character @Test public void testRemoveNthCharacter7() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); @@ -146,41 +147,38 @@ public void testGetSubStrings4() { @Test public void testGetSubStrings5() { manipulatedstring.setString("This is my string"); - // This test tries to get substrings with an ending value larger than the string itself assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); } + @Test public void testGetSubStrings6() { manipulatedstring.setString("This is my string"); - // This test checks getting substrings when the starting and ending value are the same String[] subStrings = manipulatedstring.getSubStrings(1, 1); - - assertEquals(1, subStrings.length); - assertEquals("his", subStrings[0]); + assertEquals(subStrings.length, 1); + assertEquals(subStrings[0], "his"); } + // END @Test - public void testRestoreString1() - { - manipulatedstring.setString("art"); - int [] array; - array=new int[]{1,0,2}; + public void testRestoreString1() { + manipulatedstring.setString("AaBbCc"); + int[] array = {0, 1, 2, 3, 4, 5}; String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "rat"); + assertEquals(restoreString, "AaBbCc"); } - // Tests that case is maintained - @Test - public void testRestoreString2() { - manipulatedstring.setString("AaBbCc"); - int[] indices = {1, 0, 3, 2, 5, 4}; - // Tests restoring the string based on the given indices array - String restoreString = manipulatedstring.restoreString(indices); - assertEquals("aAbBcC", restoreString); - } + + @Test + public void testRestoreString2() { + manipulatedstring.setString("AaBbCc"); + int[] array = {1, 0, 3, 2, 5, 4}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "aAbBcC"); + } + @Test public void testRestoreString3() { From 8357dfa233000da0de3e1d5a2b5f2ccaefa016a3 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 14:56:47 -0400 Subject: [PATCH 3/8] fixed test cases --- src/test/java/StringManipulationTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index b456298..5dca718 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -147,10 +147,9 @@ public void testGetSubStrings4() { @Test public void testGetSubStrings5() { manipulatedstring.setString("This is my string"); - assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 101)); } - @Test public void testGetSubStrings6() { manipulatedstring.setString("This is my string"); From 244b40354aef8eb05af5e04b26de6da1994d3d61 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 15:01:57 -0400 Subject: [PATCH 4/8] fixed test cases --- src/test/java/StringManipulationTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 5dca718..5174eaf 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -145,18 +145,20 @@ public void testGetSubStrings4() { } @Test - public void testGetSubStrings5() { + public void testGeSubStrings5() { manipulatedstring.setString("This is my string"); - assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 101)); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); } @Test - public void testGetSubStrings6() { + public void testGeSubStrings6() { manipulatedstring.setString("This is my string"); - String[] subStrings = manipulatedstring.getSubStrings(1, 1); - assertEquals(subStrings.length, 1); - assertEquals(subStrings[0], "his"); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(sStings.length, 1); + assertEquals(sStings[0], "This"); } + // END From ea70b2d26361490a9254d7afd4743a856a3f92d4 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 15:12:08 -0400 Subject: [PATCH 5/8] fixed test cases --- src/test/java/StringManipulationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 5174eaf..3cdf927 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -105,7 +105,7 @@ public void testRemoveNthCharacter6() { assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); } - // This test checks removing a negative Nth character + // Checks removing a negative Nth character @Test public void testRemoveNthCharacter7() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); @@ -122,7 +122,7 @@ public void testGeSubStrings1() { } // START - // This test tries to get substrings with a starting value less than 0 + // Test getting substrings with a starting value less than 0 @Test public void testGetSubStrings2() { manipulatedstring.setString("This is my string"); @@ -145,7 +145,7 @@ public void testGetSubStrings4() { } @Test - public void testGeSubStrings5() { + public void testGetSubStrings5() { manipulatedstring.setString("This is my string"); assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); } From f0038ab8a6517d7c7c9b0c4433e37f2ae46ad4f8 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 17:26:22 -0400 Subject: [PATCH 6/8] attempt to fix errors on multiple test cases --- src/main/java/StringManipulation.java | 49 ++++++++++++++++------- src/test/java/StringManipulationTest.java | 22 +++++----- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index f86d8c7..6d63ae6 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -32,8 +32,13 @@ public int count() { * @param maintainSpacing Determines whether to maintain spacing when removing characters. * @return The string with the nth characters removed. */ + @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - validateCharacterIndex(n); + if (n <= 0) { + throw new IllegalArgumentException(); + } else if (n > string.length()) { + throw new IndexOutOfBoundsException(); + } StringBuilder stringBuilder = new StringBuilder(); @@ -56,24 +61,31 @@ public String removeNthCharacter(int n, boolean maintainSpacing) { * @param endWord The ending index of the substring. * @return An array of substrings. */ - public String[] getSubStrings(int startWord, int endWord) { - validateWordIndices(startWord, endWord); + @Override + public String[] getSubStrings(int startWord, int endWord){ + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException(); + } - String[] words = string.split(" "); + String[] preFilteredResult = string.split(" "); String[] substrings = new String[endWord - startWord + 1]; int wordCount = 1; int substringsIndex = 0; - - for (String word : words) { - if (!word.isEmpty()) { + + for (String result : preFilteredResult) { + if (!result.isEmpty()) { if (wordCount >= startWord && wordCount <= endWord) { - substrings[substringsIndex] = word; + substrings[substringsIndex] = result; substringsIndex++; } wordCount++; } } + if (wordCount < endWord) { + throw new IndexOutOfBoundsException(); + } + return substrings; } @@ -82,18 +94,25 @@ public String[] getSubStrings(int startWord, int endWord) { * @param indices The indices of the characters to be restored. * @return The restored string. */ - public String restoreString(int[] indices) { - validateIndicesLength(indices); + @Override + public String restoreString(int[] indices){ + if (indices.length != string.length()) { + throw new IllegalArgumentException(); + } - StringBuilder restoredString = new StringBuilder(); + String[] restoredStringArray = new String[indices.length]; - for (int index : indices) { - validateCharacterIndex(index); - restoredString.append(string.charAt(index)); + for (int i = 0; i < indices.length; i++) { + int oldIndex = indices[i]; + if (oldIndex < 0 || oldIndex >= string.length()) { + throw new IndexOutOfBoundsException(); + } + restoredStringArray[i] = String.valueOf(string.charAt(oldIndex)); } - return restoredString.toString(); + return String.join("", restoredStringArray); } +} // Private helper methods for input validation diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 3cdf927..2bf983b 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -113,7 +113,7 @@ public void testRemoveNthCharacter7() { } @Test - public void testGeSubStrings1() { + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -151,7 +151,7 @@ public void testGetSubStrings5() { } @Test - public void testGeSubStrings6() { + public void testGetSubStrings6() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(1, 1); @@ -164,18 +164,22 @@ public void testGeSubStrings6() { // END @Test - public void testRestoreString1() { - manipulatedstring.setString("AaBbCc"); - int[] array = {0, 1, 2, 3, 4, 5}; + public void testRestoreString1() + { + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,2}; String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "AaBbCc"); + assertEquals(restoreString, "rat"); } - + // Tests that case is maintained @Test - public void testRestoreString2() { + public void testRestoreString2() + { manipulatedstring.setString("AaBbCc"); - int[] array = {1, 0, 3, 2, 5, 4}; + int [] array; + array=new int[]{1,0,3,2,5,4}; String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "aAbBcC"); } From 932c3812f75e31334cc5fe9978403bdd932f96f3 Mon Sep 17 00:00:00 2001 From: puleri Date: Thu, 15 Jun 2023 17:41:24 -0400 Subject: [PATCH 7/8] removed unusued helper methods --- src/main/java/StringManipulation.java | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 6d63ae6..39cbe34 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -94,7 +94,7 @@ public String[] getSubStrings(int startWord, int endWord){ * @param indices The indices of the characters to be restored. * @return The restored string. */ - @Override + @Override public String restoreString(int[] indices){ if (indices.length != string.length()) { throw new IllegalArgumentException(); @@ -112,25 +112,5 @@ public String restoreString(int[] indices){ return String.join("", restoredStringArray); } -} - // Private helper methods for input validation - - private void validateCharacterIndex(int index) { - if (index <= 0 || index > string.length()) { - throw new IndexOutOfBoundsException("Invalid character index"); - } - } - - private void validateWordIndices(int startWord, int endWord) { - if (startWord <= 0 || endWord <= 0 || startWord > endWord) { - throw new IllegalArgumentException("Invalid word indices"); - } - } - - private void validateIndicesLength(int[] indices) { - if (indices.length != string.length()) { - throw new IllegalArgumentException("Invalid indices length"); - } - } } From f373ec7fcb1d4f4c09c51bbb24a7727873361796 Mon Sep 17 00:00:00 2001 From: puleri Date: Fri, 16 Jun 2023 17:12:53 -0400 Subject: [PATCH 8/8] added missing count method to testcount4 --- src/test/java/StringManipulationTest.java | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 2bf983b..8d90417 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -45,27 +45,15 @@ public void testCount3() { // This test ensures that the counter works on a long string @Test public void testCount4() { - StringBuilder stringBuilder = new StringBuilder(); String phrase = "the quick brown fox jumps over the lazy dog"; - int occurrences = 0; + int expectedCount = 9; - for (int i = 0; i < 100; i++) { - stringBuilder.append(phrase); - stringBuilder.append("\n"); - } + StringManipulation stringManipulation = new StringManipulation(); + stringManipulation.setString(phrase); - String longString = stringBuilder.toString(); - int index = 0; - while (index < longString.length()) { - index = longString.indexOf(phrase, index); - if (index == -1) { - break; - } - occurrences++; - index += phrase.length(); - } + int actualCount = stringManipulation.count(); - assertEquals(100, occurrences); + assertEquals(expectedCount, actualCount); }