From cf842d17ec3c1cf6a8e0402885308c5889466ffc Mon Sep 17 00:00:00 2001 From: Yusa-Kaya Date: Wed, 14 Jun 2023 11:09:41 -0700 Subject: [PATCH 1/2] Added StringManipulation.java and StringManipulationTest.java files --- src/main/java/StringManipulation.java | 72 ++++++++++-- src/test/java/StringManipulationTest.java | 137 ++++++++++++++++++---- 2 files changed, 176 insertions(+), 33 deletions(-) mode change 100644 => 100755 src/test/java/StringManipulationTest.java diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..c7273d5 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,89 @@ +import java.util.Arrays; + +// Description: This class implements various methods that manipulates strings. public class StringManipulation implements StringManipulationInterface { + private String str = null; @Override public String getString() { - return null; + return this.str; } @Override public void setString(String string) { + this.str = string; } @Override public int count() { - return 0; + if(this.str == null || this.str.isEmpty()){ + return 0; + } + String[] words = this.str.split("\\s+"); + return words.length; } - @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (n > str.length()) { + throw new IndexOutOfBoundsException("The argument is out of bound."); + } else if (n <= 0) { + throw new IllegalArgumentException("n index cannot be less than or equal to 0."); + } + + StringBuilder stringBuilder = new StringBuilder(str); + + // Loop through multiples of n + for (int i = 1; n * i <= str.length(); i++) { + int index = n * i - 1; + if (maintainSpacing) { + stringBuilder.setCharAt(index, ' '); + } else { + // Decrease index by i to account for previously deleted characters + stringBuilder.deleteCharAt(index - (i - 1)); + } + } + + return stringBuilder.toString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + // Validate the inputs + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("StartWord must be greater than 0 and less than or equal to EndWord."); + } + + // Split the string into words + String[] words = this.str.trim().split("\\s"); + + // Validate that endWord does not exceed the number of words + if (endWord > words.length) { + throw new IndexOutOfBoundsException("The positions must be within the bounds"); + } + + // Return the subarray of words + return Arrays.copyOfRange(words, startWord - 1, endWord); } @Override public String restoreString(int[] indices) { - return null; - } + // Check if the length of the string and indices array are the same + if (str.length() != indices.length) { + throw new IllegalArgumentException("Length of string and indices array must be equal."); + } + // Array of characters to rebuild the string. + char[] shuffledString = new char[str.length()]; + for (int i = 0; i < indices.length; i++) { + int index = indices[i]; + // Check if the given indice is within the bounds of the string + if (index < 0 || index >= str.length()) { + throw new IndexOutOfBoundsException("Index " + index + " is out of bounds."); + } + // Assign the character to the char array at the index position + shuffledString[index] = str.charAt(i); + } + return new String(shuffledString); + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java old mode 100644 new mode 100755 index 6692c2c..3cfb7e6 --- 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 { @@ -19,6 +18,13 @@ public void tearDown() { manipulatedstring = null; } + @Test + public void testgetString(){ + manipulatedstring.setString("Hello!"); + String expected = "Hello!"; + String actual = manipulatedstring.getString(); + assertEquals(expected, actual); + } @Test public void testCount1() { manipulatedstring.setString("This is my string"); @@ -26,56 +32,99 @@ public void testCount1() { assertEquals(4, length); } + // Test when string has multiple punctuations. @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("This isn't my string!"); + int length = manipulatedstring.count(); + assertEquals(4, length); } + // Test when string is empty. + // Expected return is 0 @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // Test when string is null + // Expected return is 0 @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // Test when string has only empty space character. + // Expected return is 0 + @Test + public void testCount5() { + manipulatedstring.setString(" "); + int length = manipulatedstring.count(); + assertEquals(0, length); } + + // Test if the method removes the right characters without maintaining spacing @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)); + String expected = "I' bttr uts0e 16tsinths trn6 rgh?"; + String actual = manipulatedstring.removeNthCharacter(3, false); + assertEquals(expected, actual); } + // Test if the method removes the right characters while maintaining spacing @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)); + String expected = "I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?"; + String actual = manipulatedstring.removeNthCharacter(3, true); + assertEquals(expected, actual); } + // Throw IllegalArgumentException if the given index is less than or equal to 0 @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("This is 28 character string."); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(29, true);}); } + // Throw IndexOutOfBoundException if the given index is exceeds the size of the string @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is 28 character string."); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(0, true);}); } + // Deleting all characters without maintaining spacing. @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("This is 28 character string."); + String expected = ""; + String actual = manipulatedstring.removeNthCharacter(1, false); + assertEquals(expected, actual); } + // Deleting each character while maintaining spacing. @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("This is 28 character string."); + String expected = " "; + String actual = manipulatedstring.removeNthCharacter(1, true); + assertEquals(expected, actual); } + // Deleting only the last character without adding a space. @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("This is 28 character string."); + String expected = "This is 28 character string"; + String actual = manipulatedstring.removeNthCharacter(28, false); + assertEquals(expected, actual); } @Test @@ -87,25 +136,51 @@ public void testGeSubStrings1() { assertEquals(sStings[1], "string"); } + // Test when the startWord index is less than or equal to 0 + // Throws IllegalArgumentException @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(0, 2);}); } + + // Test when the endWord index is less than or equal to 0 + // Throws IllegalArgumentException @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(1, 0);}); } + + // Test when the startWord index is greater than endWord index + // Throws IllegalArgumentException @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(2, 1);}); } + + // Test when the endWord index is greater than the word count of string + // Throws IndexOutOfBoundException @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(2, 5);}); } + + // Test when the startWord and endWord are equal @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(4, 4); + String expected = "string"; + String actual = sStings[0]; + + assertEquals(expected, actual); } @Test @@ -113,37 +188,49 @@ public void testRestoreString1() { manipulatedstring.setString("art"); int [] array; - array=new int[]{1,0,2}; + array = new int[]{1,0,2}; String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "rat"); } + // Throw IllegalArgumentException if the indices length is greater than the string length. {5,6,7,8,9,10,11,0,1,2,3,4,12}; @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("JUnitTesting!"); + int[] indices = {5,6,7,8,9,10,11}; + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(indices);}); } + // Throw IndexOutOfBoundsException if any indice is less than 0. @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("JUnitTesting!"); + int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,-8}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(indices);}); } + // Throw IndexOutOfBoundsException if any indice is greater than the string length @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("JUnitTesting!"); + int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,13}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(indices);}); } + // Test when each character is the same in the string @Test public void testRestoreString5() { - fail("Not yet implemented"); - + manipulatedstring.setString("aaa"); + int [] indices = {1,0,2}; + String restoreString = manipulatedstring.restoreString(indices); + assertEquals(restoreString, "aaa"); } } From a9481d54f31e24f377a45d25c8da4f377a4bd26a Mon Sep 17 00:00:00 2001 From: Yusa-Kaya Date: Wed, 14 Jun 2023 21:52:28 -0700 Subject: [PATCH 2/2] Addressed code review recommendations. Refactored count() method so that it considers punctuation and numerical values. Added 2 new test cases to test the newly added part. --- src/main/java/StringManipulation.java | 13 ++++++++-- src/test/java/StringManipulationTest.java | 30 ++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index c7273d5..7192501 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -19,8 +19,18 @@ public int count() { if(this.str == null || this.str.isEmpty()){ return 0; } + String[] words = this.str.split("\\s+"); - return words.length; + + int wordCount = 0; + for (String word : words) { + // Count words and numerical values as a word. + // Ignore punctuation. + if (word.matches(".*[a-zA-Z0-9].*")) { + wordCount++; + } + } + return wordCount; } public String removeNthCharacter(int n, boolean maintainSpacing) { @@ -85,5 +95,4 @@ public String restoreString(int[] indices) { } return new String(shuffledString); } - } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 3cfb7e6..eb8e9ba 100755 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,5 +1,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -25,6 +26,7 @@ public void testgetString(){ String actual = manipulatedstring.getString(); assertEquals(expected, actual); } + @Test public void testCount1() { manipulatedstring.setString("This is my string"); @@ -32,7 +34,7 @@ public void testCount1() { assertEquals(4, length); } - // Test when string has multiple punctuations. + //Test when string has multiple punctuations. @Test public void testCount2() { manipulatedstring.setString("This isn't my string!"); @@ -42,6 +44,7 @@ public void testCount2() { // Test when string is empty. // Expected return is 0 + @DisplayName("Count empty string") @Test public void testCount3() { manipulatedstring.setString(""); @@ -52,6 +55,7 @@ public void testCount3() { // Test when string is null // Expected return is 0 @Test + @DisplayName("Count null") public void testCount4() { manipulatedstring.setString(null); int length = manipulatedstring.count(); @@ -66,6 +70,26 @@ public void testCount5() { int length = manipulatedstring.count(); assertEquals(0, length); } + // Test when string has space separated punctuations. + // Expected return is 0 + @Test + @DisplayName("Count punctuation") + public void testCount6() { + manipulatedstring.setString("! ? ] "); + int length = manipulatedstring.count(); + assertEquals(0, length); + } + + // Test when string has numerical value. + // Expected return is 1 + @Test + @DisplayName("Count numeric") + public void testCount7() { + manipulatedstring.setString("! 123 "); + int length = manipulatedstring.count(); + assertEquals(1, length); + } + // Test if the method removes the right characters without maintaining spacing @Test public void testRemoveNthCharacter1() { @@ -84,7 +108,7 @@ public void testRemoveNthCharacter2() { assertEquals(expected, actual); } - // Throw IllegalArgumentException if the given index is less than or equal to 0 + // Throw IndexOutOfBoundException if the given index is exceeds the size of the string @Test public void testRemoveNthCharacter3() { manipulatedstring.setString("This is 28 character string."); @@ -92,7 +116,7 @@ public void testRemoveNthCharacter3() { manipulatedstring.removeNthCharacter(29, true);}); } - // Throw IndexOutOfBoundException if the given index is exceeds the size of the string + // Throw IndexOutOfBoundsException if the given index is less than or equal to 0 @Test public void testRemoveNthCharacter4() { manipulatedstring.setString("This is 28 character string.");