From 45113726e9e4aa6bd208df6e5939e708c789cfb9 Mon Sep 17 00:00:00 2001 From: Hailey Mandal Date: Wed, 14 Jun 2023 11:13:46 -0700 Subject: [PATCH] added my implementation to skeleton --- src/main/java/StringManipulation.java | 108 ++++++++++- src/test/java/StringManipulationTest.java | 224 ++++++++++++++++++---- 2 files changed, 283 insertions(+), 49 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..7557042 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,4 +1,5 @@ public class StringManipulation implements StringManipulationInterface { + private String myString; @Override public String getString() { @@ -7,27 +8,120 @@ public String getString() { @Override public void setString(String string) { + this.myString = string; } @Override public int count() { - return 0; + if(myString == null){ + return 0; + } + + int countWords = 0; + if(myString.equals("")){ + return countWords; + } + + String removeSpace = myString.trim(); + for(String word : removeSpace.split("\\s+")){ + if(removeSpace.equals("")) + return 0; + countWords++; + } + + return countWords; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (myString.equals("")) { + return myString; + } else if(myString == null){ + throw new NullPointerException("String is null."); + } else if(n <= 0){ + throw new IllegalArgumentException("n cannot be less than or equal to zero"); + } else if(n > myString.length()){ + throw new IndexOutOfBoundsException("n cannot be greater than string length"); + } else if(n == 1 && maintainSpacing){ + return myString; + } + + StringBuilder result = new StringBuilder(); + int count = 0; + + for (int i = 0; i < myString.length(); i++) { + char c = myString.charAt(i); + + if (maintainSpacing) { + count++; + + if (count % n != 0) { + result.append(c); + } else { + result.append(' '); + } + } else { + count++; + if (count % n != 0) { + result.append(c); + } + } + } + return result.toString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + if(myString == null) { + throw new NullPointerException("String is null."); + } else if(startWord > endWord){ + throw new IllegalArgumentException("startWord cannot be greater than endWord."); + } else if(startWord <= 0){ + throw new IllegalArgumentException("startWord cannot be less than or equal to 0."); + } else if(endWord <= 0){ + throw new IllegalArgumentException("endWord cannot be less than or equal to 0."); + } + + //handles exception for endWord># of words in string + String[] words = myString.split("\\s+"); + if(endWord > words.length){ + throw new IndexOutOfBoundsException("endWord is greater than length of string."); + } + + int size = endWord - startWord + 1; + String[] result = new String[size]; + int index = 0; + + for (int i = startWord-1; i < endWord; i++) { + result[index] = words[i]; + index++; + } + + return result; } @Override - public String restoreString(int[] indices) { - return null; - } + public String restoreString(int[] indices){ + if(indices.length != myString.length()){ + throw new IllegalArgumentException("Indices length does not equal string's length"); + } else if(myString == null){ + throw new NullPointerException("String is null."); + } + StringBuilder result = new StringBuilder(myString.length()); + + for(int index : indices){ + + if(index < 0 || index > myString.length()){ + throw new IndexOutOfBoundsException("Indices length cannot be less than 0"); + } else if(myString.equals("")){ + return result.toString(); + } else { + result.append(myString.charAt(index)); + } + } + + return result.toString(); + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..d727ec4 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,8 +1,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - - import static org.junit.jupiter.api.Assertions.*; public class StringManipulationTest { @@ -20,96 +18,181 @@ public void tearDown() { } @Test - public void testCount1() { + //first test + public void count_CountingLength_ReturnCorrectLength() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); assertEquals(4, length); } @Test - public void testCount2() { - fail("Not yet implemented"); + //second test + public void count_EmptyString_return0() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); + } + + @Test + //third test + public void count_nullString_return0() { + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); + } + + @Test + //fourth test + public void count_noWords_return0() { + manipulatedstring.setString(" "); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test - public void testCount3() { - fail("Not yet implemented"); + //fifth test + public void count_trailingSpace_returnsCorrectCount(){ + manipulatedstring.setString("Hello World! "); + int length = manipulatedstring.count(); + assertEquals(2, length); } @Test - public void testCount4() { - fail("Not yet implemented"); + //sixth test + public void count_leadingSpace_returnsCorrectCount(){ + manipulatedstring.setString(" Hello World!"); + int length = manipulatedstring.count(); + assertEquals(2, length); } @Test - public void testRemoveNthCharacter1() { + //seventh test + public void removeNthCharacter_maintainSpacingFalse_returnsStringWithoutSpaces() { 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() { + //eighth test + public void removeNthCharacter_maintainSpacingTrue_returnsStringWithSpaces() { 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"); + //ninth test + public void removeNthCharacter_nLessThanOrEqual0_throwIllegalArgumentException() { + manipulatedstring.setString("Hello there"); + + assertThrows(IllegalArgumentException.class, ()-> + manipulatedstring.removeNthCharacter(0, false)); } @Test - public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + //tenth test + public void removeNthCharacter_nGreaterThanStringLength_throwIndexOutOfBoundsException() { + manipulatedstring.setString("Hello there"); + + assertThrows(IndexOutOfBoundsException.class, ()-> + manipulatedstring.removeNthCharacter(13, false)); } @Test - public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + //eleventh test + public void removeNthCharacter_emptyString_returnsEmptyString() { + manipulatedstring.setString(""); + String actual = manipulatedstring.removeNthCharacter(2, false); + assertEquals("", actual); } @Test - public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + //twelfth test + public void removeNthCharacter_nullString_returnNullPointerException() { + manipulatedstring.setString(null); + assertThrows(NullPointerException.class, ()-> + manipulatedstring.removeNthCharacter(4, false)); } @Test - public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + //thirteenth test + public void removeNthCharacter_noCharactersRemoved_returnSameInputString() { + manipulatedstring.setString("Same as input string"); + String actual = manipulatedstring.removeNthCharacter(1, true); + assertEquals("Same as input string", actual); } @Test - public void testGeSubStrings1() { + //fourteenth test + public void getSubStrings_rangeOfWords_returnsCorrectExpectedToActual() { manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); + String[] sStings = manipulatedstring.getSubStrings(3, 4); assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + //fifteenth test + public void getSubStrings_nullString_returnsNullPointerException() { + manipulatedstring.setString(null); + assertThrows(NullPointerException.class, ()-> + manipulatedstring.getSubStrings(3,5)); } + @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + //sixteenth test, endWord <=0 + public void getSubStrings_startWordGreaterThanEndWord_returnsIllegalArgumentException() { + manipulatedstring.setString("I enjoy sunny days"); + assertThrows(IllegalArgumentException.class, ()-> + manipulatedstring.getSubStrings(3,1)); } + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + //seventeenth test + public void getSubStrings_startWordLessThanEqualTo0_returnsIllegalArgumentException() { + manipulatedstring.setString("Summer is here"); + assertThrows(IllegalArgumentException.class, ()-> + manipulatedstring.getSubStrings(0,2)); } + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + //eighteenth test + public void getSubStrings_endWordLessThanEqualTo0_returnsIllegalArgumentException() { + manipulatedstring.setString("Summertime is sunny"); + assertThrows(IllegalArgumentException.class, ()-> + manipulatedstring.getSubStrings(1,0)); } + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + //nineteenth test + public void getSubStrings_endWordGreaterThanString_returnsIndexOutOfBoundsException() { + manipulatedstring.setString("Hello there"); + + assertThrows(IndexOutOfBoundsException.class, ()-> + manipulatedstring.getSubStrings(1,3)); } @Test - public void testRestoreString1() + //twentieth test + public void getSubStrings_oneWordString_returnsInputString() { + manipulatedstring.setString("Hello"); + String[] sStings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(sStings[0], "Hello"); + } + + @Test + //twenty-first test + public void getSubStrings_startWordEqualsEndWord_returnsOneWord() { + manipulatedstring.setString("JUnit testing is beneficial"); + String[] sStings = manipulatedstring.getSubStrings(4, 4); + + assertEquals(sStings[0], "beneficial"); + } + + @Test + //twenty-second test case + public void restoreString_lowerCaseLetters_returnsRestoredString() { manipulatedstring.setString("art"); int [] array; @@ -119,31 +202,88 @@ public void testRestoreString1() } @Test - public void testRestoreString2() + //twenty-third test case + public void restoreString_stringAndIndicesLengthDoNotEqual_returnsIllegalArgumentException() { - fail("Not yet implemented"); + manipulatedstring.setString("artistic"); + int [] array = new int[]{1,0,2,3}; + assertThrows(IllegalArgumentException.class, ()-> + manipulatedstring.restoreString(array)); } @Test - public void testRestoreString3() + //twenty-fourth test case + public void restoreString_indicesLessThan0_returnsIndexOutOfBoundsException() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] array = new int[]{2,-1,0}; + assertThrows(IndexOutOfBoundsException.class, ()-> + manipulatedstring.restoreString(array)); + } + @Test + //twenty-fifth test case + public void restoreString_indicesGreaterThanStringLength_returnsIndexOutOfBoundsException() + { + manipulatedstring.setString("art"); + int [] array = new int[]{1,0,4}; + assertThrows(IndexOutOfBoundsException.class, ()-> + manipulatedstring.restoreString(array)); } @Test - public void testRestoreString4() + //twenty-sixth test case + public void restoreString_nullString_returnNullPointerException() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int [] array = new int[]{}; + assertThrows(NullPointerException.class, ()-> + manipulatedstring.restoreString(array)); + } + + @Test + //twenty-seventh test case + public void restoreString_emptyString_returnsEmptyString() + { + manipulatedstring.setString(""); + int [] array; + array=new int[]{}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, ""); + } + @Test + //twenty-eighth test case + public void restoreString_upperCaseLetters_returnsRestoredString() + { + manipulatedstring.setString("ART"); + int [] array; + array=new int[]{1,0,2}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "RAT"); } @Test - public void testRestoreString5() + //twenty-ninth test case + public void restoreString_upperCaseAndLowerCaseLetters_returnsRestoredString() { - fail("Not yet implemented"); + manipulatedstring.setString("aRT"); + int [] array; + array=new int[]{1,0,2}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "RaT"); + } + @Test + //thirtieth test case + public void restoreString_repeatedLetters_returnsRestoredString() + { + manipulatedstring.setString("stressed"); + int [] array; + array=new int[]{7,6,0,4,3,2,1,5}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "desserts"); } }