From 31b8d6b41d5c113224e68432a045ec84bf016de7 Mon Sep 17 00:00:00 2001 From: YukiRivera Date: Thu, 15 Jun 2023 11:56:43 -0700 Subject: [PATCH 1/2] updated the skeleton for Code Review Activity --- src/main/java/StringManipulation.java | 114 +++++++++++++- src/test/java/StringManipulationTest.java | 182 ++++++++++++++++++---- 2 files changed, 260 insertions(+), 36 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..c83a64e 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,133 @@ + +import java.util.Arrays; + public class StringManipulation implements StringManipulationInterface { + private String stringToManipulate; + @Override public String getString() { - return null; + return stringToManipulate; } @Override public void setString(String string) { + stringToManipulate = string; } @Override public int count() { - return 0; + + // If the current stringToManipulate is null, throws an exception + if(stringToManipulate == null){ + throw new NullPointerException("The string is null."); + } + // If the stringToManipulate is an empty string, returns 0 + if(stringToManipulate.length()==0){ + return 0; + } + + // split the string by whitespace(s), period, comma, and other symbols + // and stores each word into a String array + String[] words = stringToManipulate.split("[\\s.(),:?!]+"); + + // returns the length of array as the word count + return words.length; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + + // If the current stringToManipulate is null, throws an exception + if(stringToManipulate == null){ + throw new NullPointerException("The string is null."); + } + // If the given n is smaller or equal to 0, throws IllegalArgumentException + if(n <= 0){ + throw new IllegalArgumentException("n must be greater than zero."); + } + // If the given n is larger than the length of the string, throws IndexOutOfBoundsException + if(n > stringToManipulate.length()){ + throw new IndexOutOfBoundsException("n must be less than or equal to the length of the string."); + } + + StringBuilder str = new StringBuilder(); + int length = stringToManipulate.length(); + + // Traverse the entire stringToManipulate + for(int i = 1; i <= length ;i++){ + // As long as the index of stringToManipulate is NOT divisible by n, append the + // corresponding character to the str + if(i % n != 0) { + str.append(stringToManipulate.charAt(i - 1)); + } + // if the index is divisible by n AND maintainSpacing is true, append a space + else if(maintainSpacing){ + str.append(' '); + } + } + + return str.toString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + + // If the current stringToManipulate is null, throws an exception + if(stringToManipulate == null){ + throw new NullPointerException("The string is null."); + } + // If the startWord or the endWord is zero or negative, OR the endWord is smaller than the startWord, + // throws an exception + if(startWord <= 0 || endWord <= 0 || startWord > endWord) + { + throw new IllegalArgumentException("Invalid argument(s) were entered."); + } + + // Gets the word count by using split() + String[] words = stringToManipulate.split("[\\s.,_:?!]+"); + + // If the endWord is larger than the length of string, throws an exception + if(endWord > words.length) + { + throw new IndexOutOfBoundsException("The endWord entered is out of bounds"); + } + + // copies the words in the specified range into a new array called substring + String[] substring = Arrays.copyOfRange(words, startWord-1, endWord); + + return substring; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + + // If the current stringToManipulate is null, throws an exception + if(stringToManipulate == null) + { + throw new NullPointerException("The string is null"); + } + // If the number of indices are not the same as the length of the string, + // throws an exception + if(indices.length != stringToManipulate.length()){ + throw new IllegalArgumentException("The number of indices must match the length of the string."); + } + + StringBuilder newString = new StringBuilder(); + + // Traverse the indices array to the end + for(int i = 0; i < indices.length; i++){ + // if the index is negative, or larger than equal to the length of the string, + // throws an exception + if(indices[i] < 0 || indices[i]>= stringToManipulate.length()){ + throw new IndexOutOfBoundsException("Each index must be between 0 and the length of the string."); + } + // otherwise append the character at the index to the new string + newString.append(stringToManipulate.charAt(indices[i])); + } + return newString.toString(); } } + diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..926a980 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,3 +1,4 @@ + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -26,58 +27,117 @@ public void testCount1() { assertEquals(4, length); } + // This test checks if the method returns zero as the count of words + // when the given string only contains spaces. @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // This test checks if the method properly returns the number of words + // when the string contains a question mark, a comma, and a period in addition to spaces. @Test public void testCount3() { - fail("Not yet implemented"); + + manipulatedstring.setString("How many words is this? It can count correctly, I hope."); + int length = manipulatedstring.count(); + assertEquals(11, length); } + // This test checks if the method properly returns the number of words + // when the string contains a colon and a space. @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("Item: 4"); + int length = manipulatedstring.count(); + assertEquals(2, length); + } + + // This test checks if method returns zero when the manipulatedstring is set to + // an empty string + @Test + public void testCount5() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // This test checks if the method throws NullPointerException if the count() is called + // on the string that has been set to null + @Test + public void testCount6() { + manipulatedstring.setString(null); + assertThrows(NullPointerException.class, ()->{manipulatedstring.count();}); + } + + @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)); } + @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 if the method throws NullPointerException if the string has been set to null. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + assertThrows(NullPointerException.class, ()->{manipulatedstring.removeNthCharacter(1, true);}); } + // This test checks if the method throws IllegalArgumentException if n is 0. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I'm a CS student."); + assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.removeNthCharacter(0, true);}); } - + //This test checks if the method throws IndexOutOfBoundsException if n is + // greater than the length of the string @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I live in Seattle."); + // nValue holds the length of the string plus one + int nValue = manipulatedstring.toString().length() + 1; + assertThrows(IndexOutOfBoundsException.class, () -> {manipulatedstring.removeNthCharacter(nValue, false);}); } - + // This test checks if the method removes all the characters without any spaces left + // if n is 1 and maintainSpacing is false @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("Remove all the characters."); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); } - + // This test checks if the method removes all the characters with the same number of + // spaces left if n is 1 and maintainSpacing is true @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("Leave all spaces."); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } + // This test checks if the method throws IndexOutOfBoundsException if .removeNthCharacter() + // is called when the string has been set to an empty string + @Test + public void testRemoveNthCharacter8() { + manipulatedstring.setString(""); + assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedstring.removeNthCharacter(1, true);}); + } + + // This test checks if the method removes the last character when n equals + // the length of the string + @Test + public void testRemoveNthCharacter9() { + manipulatedstring.setString("Remove the last character!"); + assertEquals("Remove the last character", manipulatedstring.removeNthCharacter(26, false)); + } @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); @@ -87,27 +147,55 @@ public void testGeSubStrings1() { assertEquals(sStings[1], "string"); } + // This test checks if the method returns a correct substring when startWord and + // endWord has the same integer @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("Just one word."); + String[] subString = manipulatedstring.getSubStrings(2,2); + assertEquals(subString[0], "one"); } + + // This test checks if the method throws NullPointerException when null has been set + // to the string @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + assertThrows(NullPointerException.class, ()->{manipulatedstring.getSubStrings(2, 4);}); } + + // This test checks if the method throws an IllegalArgumentException when startWord is + // zero @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a test."); + assertThrows(IllegalArgumentException.class, ()->{manipulatedstring.getSubStrings(0, 3);}); } + + // This test checks if the method throws an IllegalArgumentException when endWord is zero. @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("This is also a test."); + assertThrows(IllegalArgumentException.class, ()->{manipulatedstring.getSubStrings(3, 0);}); } + + // This test checks if the method throws an IllegalArgumentException when startWord is + // larger than the endWord @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("This is another test."); + assertThrows(IllegalArgumentException.class, ()->{manipulatedstring.getSubStrings(2, 1);}); } + // This test checks if the method throws an IndexOutOfBoundsException when endWord is + // larger than the length of the string + @Test + public void testGeSubStrings7() { + manipulatedstring.setString("This is another test."); + assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedstring.getSubStrings(3, 6);}); + } + + @Test public void testRestoreString1() { @@ -118,32 +206,68 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } + // This test checks if the IllegalArgumentException is thrown when the length of the indices[] + // is longer than the length of the given string @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + manipulatedstring.setString("heart"); + int[] array = new int[]{2, 3, 4, 0, 1, 5}; + assertThrows(IllegalArgumentException.class, ()->{ manipulatedstring.restoreString(array);}); } + // This test checks if the IllegalArgumentException is thrown when the length of the indices[] + // is shorter than the length of the given string @Test - public void testRestoreString3() - { - fail("Not yet implemented"); + public void testRestoreString3() { + manipulatedstring.setString("heart"); + int[] array = new int[]{2, 3, 4, 0}; + assertThrows(IllegalArgumentException.class, ()->{ manipulatedstring.restoreString(array);}); + } + // This test checks if the IndexOutOfBoundsException is thrown when any indices[i] is + // less than zero + @Test + public void testRestoreString4() { + manipulatedstring.setString("team"); + int[] array = new int[]{-1, 0, 1, 2}; + assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedstring.restoreString(array);}); } + // This test checks if the IndexOutOfBoundsException is thrown when an indices[i] is larger + // than or the equal to the length of the string @Test - public void testRestoreString4() - { - fail("Not yet implemented"); + public void testRestoreString5(){ + manipulatedstring.setString("group"); + int[] array = new int[]{5, 0, 1, 2, 3}; + + assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedstring.restoreString(array);}); } + // This test checks if the method throws NullPointerException if the string is null @Test - public void testRestoreString5() - { - fail("Not yet implemented"); + public void testRestoreString6() { + manipulatedstring.setString(null); + int[] array = new int[]{2,3,5,4,1,0}; + assertThrows(NullPointerException.class, ()->{manipulatedstring.restoreString(array);}); + } + // This test checks if the method returns the correct string regardless of the case + @Test + public void testRestoreString7() { + manipulatedstring.setString("MASTER"); + int[] array = new int[]{2,3,5,4,1,0}; + assertEquals("STREAM", manipulatedstring.restoreString(array)); } + // This is an additional test to check if getString() properly returns null when + // the manipulatedstring is null + @Test + public void testGetString(){ + manipulatedstring.setString(null); + assertNull(manipulatedstring.getString()); + } + + } + From b536d0d0600f15e6623b60fdef474ce4bd3626c5 Mon Sep 17 00:00:00 2001 From: YukiRivera Date: Fri, 16 Jun 2023 21:28:33 -0700 Subject: [PATCH 2/2] added a helper function to check if the string is null --- src/main/java/StringManipulation.java | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index c83a64e..a76d501 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -18,10 +18,9 @@ public void setString(String string) { @Override public int count() { - // If the current stringToManipulate is null, throws an exception - if(stringToManipulate == null){ - throw new NullPointerException("The string is null."); - } + // if the current stringToManipulate is null, throws a NullPointerException + checkForNullString(); + // If the stringToManipulate is an empty string, returns 0 if(stringToManipulate.length()==0){ return 0; @@ -37,11 +36,9 @@ public int count() { @Override public String removeNthCharacter(int n, boolean maintainSpacing) { + // if the current stringToManipulate is null, throws a NullPointerException + checkForNullString(); - // If the current stringToManipulate is null, throws an exception - if(stringToManipulate == null){ - throw new NullPointerException("The string is null."); - } // If the given n is smaller or equal to 0, throws IllegalArgumentException if(n <= 0){ throw new IllegalArgumentException("n must be greater than zero."); @@ -73,10 +70,9 @@ else if(maintainSpacing){ @Override public String[] getSubStrings(int startWord, int endWord){ - // If the current stringToManipulate is null, throws an exception - if(stringToManipulate == null){ - throw new NullPointerException("The string is null."); - } + // if the current stringToManipulate is null, throws a NullPointerException + checkForNullString(); + // If the startWord or the endWord is zero or negative, OR the endWord is smaller than the startWord, // throws an exception if(startWord <= 0 || endWord <= 0 || startWord > endWord) @@ -102,11 +98,9 @@ public String[] getSubStrings(int startWord, int endWord){ @Override public String restoreString(int[] indices){ - // If the current stringToManipulate is null, throws an exception - if(stringToManipulate == null) - { - throw new NullPointerException("The string is null"); - } + // if the current stringToManipulate is null, throws a NullPointerException + checkForNullString(); + // If the number of indices are not the same as the length of the string, // throws an exception if(indices.length != stringToManipulate.length()){ @@ -128,6 +122,13 @@ public String restoreString(int[] indices){ return newString.toString(); } + /* Helper Functions */ + // Helper function to check if the string is null. If so, throw an NullPointerException + private void checkForNullString(){ + if(stringToManipulate == null) { + throw new NullPointerException("The string is null."); + } + } }