diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..5c7295c 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,119 @@ public class StringManipulation implements StringManipulationInterface { - + String str = null; @Override public String getString() { - return null; + return str; } @Override public void setString(String string) { + str = string; } @Override public int count() { - return 0; + //checks for null or 'empty' one char strings, anything more is a bit beyond scope of project + if( str == null || str == " ") { + return 0; + } + int n = 1; + for(int i = 1; i < str.length()-1; i++) { + //adds a word count at a space, but doesn't count up for multiple spaces in a row, or a space at the end of a string. + if(str.charAt(i) == ' ' && str.charAt(i-1) != ' ') { + n++; + } + } + return n; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + //exception check + String output = ""; + if(n <= 0) { + throw new IllegalArgumentException("n is less than or equal to zero"); + } + if( n > str.length()) { + throw new IndexOutOfBoundsException("n is greater than string length"); + } + //iterates over string in increments of n, replacing those words, using substring function of String to grab the other snippets. + for(int i = n; i <= str.length(); i+=n) { + output += str.substring(i-n, i-1); + if(maintainSpacing) { + output += " "; + } + } + //Grabs the remaining portion of the string after the last removed character. + int remain = str.length()%n; + if(remain != 0) { + output += str.substring(str.length() - remain); + } + return output; } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + //Exceptions for invalid inputs + if(endWord > count() ) { + throw new IndexOutOfBoundsException("String has less words than endWord"); + } + + if(startWord <= 0) { + throw new IllegalArgumentException("startWord must be greater than zero"); + } + if(endWord <= 0) { + throw new IllegalArgumentException("endWord must be greater than zero"); + } + if(endWord < startWord) { + throw new IllegalArgumentException("endWord must not be less than startWord"); + } + //Creates the string array, sets it to blank so there aren't any null issues. + String[] result = new String[endWord-startWord + 1]; + for(int i = 0; i < result.length; i++) { + result[i] = ""; + } + int words = 0; + int x = 0; + //Iterates over the whole string, only counts where words would be, copying it into the result + for(int i = 0; i < endWord; i++) { + while( x < str.length() && str.charAt(x) != ' ') { + if(i >= startWord-1) { + result[words] += str.charAt(x); + } + x++; + } + if(i >= startWord-1) { + words++; + } + x++; + } + return result; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + //Exception for incorrect int array + if(indices.length != str.length()) { + throw new IllegalArgumentException("Number of Indices must equal length of string"); + } + //Create char array of same length of string. + char[] result = new char[str.length()]; + int i = 0; + //Iterates over the indices and the string, assigning the string chars based on index values + for(int x : indices) { + //Exception handling for invalid indices. + if(x < 0) { + throw new IndexOutOfBoundsException("index must be above 0"); + } + if( x >= str.length() ) { + throw new IndexOutOfBoundsException("Index must be under string length"); + } + result[x] = str.charAt(i); + i++; + } + //Use str constructor to convert from char array. + String stringResult = new String(result); + return stringResult; } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..720863b 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,4 +1,5 @@ import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -25,20 +26,26 @@ public void testCount1() { int length = manipulatedstring.count(); assertEquals(4, length); } - + //Checks to see if default value for non-null/empty strings is 1 @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello"); + int length = manipulatedstring.count(); + assertEquals(1, length); + } - + //Checks to ensure that it properly measures null strings as 0 words @Test public void testCount3() { - fail("Not yet implemented"); + int length = manipulatedstring.count(); + assertEquals(0, length); } - + //Checks to see if it's counting multiple spaces as multiple words. @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("This checks for long spaces"); + int length = manipulatedstring.count(); + assertEquals(5, length); } @Test @@ -53,29 +60,38 @@ public void testRemoveNthCharacter2() { assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); } + //checks to see if it throws exception when the index is larger than the number of chars @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); - } + + manipulatedstring.setString("Hello"); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {manipulatedstring.removeNthCharacter(12, true); }); + } + //Checks for exception when -1, an illegal argument, is passed. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); - } + manipulatedstring.setString("Hello"); + Assertions.assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.removeNthCharacter(-1, true); }); + } + //Testing to see if it will properly remove all chars when value is 1. @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } - + //testing to see if it properly removes the last character of the string when the input number is a factor of the string length @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("Ten Chars!"); + assertEquals("Ten hars ", manipulatedstring.removeNthCharacter(5,true)); } - + //Testing to check whole string iteration, like test 5 without maintaining spacing. @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("Hello"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); } @Test @@ -86,26 +102,50 @@ public void testGeSubStrings1() { assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } - + //Checks for index out of bound exception being thrown @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {manipulatedstring.getSubStrings(1, 5);}); } + //checks for startWord being <= 0 throwing an exception @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("test with many words for exceptions"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.getSubStrings(0, 1);}); + String expected = "startWord must be greater than zero"; + String actual = exception.getMessage(); + assertTrue(actual == expected); } + //checks for endWord being <= 0 throwing an exception @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("test with many words for exceptions"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.getSubStrings(1, 0);}); + String expected = "endWord must be greater than zero"; + String actual = exception.getMessage(); + assertTrue(actual == expected); } + //Checks for endWord being less than startWord throwing an exception @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("test with many words for exceptions"); + Exception exception = assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.getSubStrings(4, 2);}); + String expected = "endWord must not be less than startWord"; + String actual = exception.getMessage(); + assertTrue(actual == expected); } + //Checks to see how well it works on a string with many words. @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a long string"); + String [] sStrings = manipulatedstring.getSubStrings(1, 5); + + assertEquals(sStrings[0], "This"); + assertEquals(sStrings[1], "is"); + assertEquals(sStrings[2], "a"); + assertEquals(sStrings[3], "long"); + assertEquals(sStrings[4], "string"); } @Test @@ -118,31 +158,41 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } + //checks to see if it throws an exception when an array that is shorter than the string size is passed @Test public void testRestoreString2() { - fail("Not yet implemented"); + manipulatedstring.setString("short"); + int[] array = {1, 2, 3}; + Assertions.assertThrows(IllegalArgumentException.class, () -> {manipulatedstring.restoreString(array);}); } - + //checks to see if an exception is thrown when an index over the string length is passed @Test public void testRestoreString3() { - fail("Not yet implemented"); + manipulatedstring.setString("short"); + int[] array = {1, 2, 3, 4, 5}; + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {manipulatedstring.restoreString(array);}); } - + //checks to see if exception is thrown when index below 0 is thrown. @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("short"); + int[] array = {1, 2, 3, 4, -1}; + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {manipulatedstring.restoreString(array);}); } - + //test for longer, more complex usage of restore string, to check if method works (actually did catch that it wasn't quite right, so it worked.) @Test public void testRestoreString5() { - fail("Not yet implemented"); + manipulatedstring.setString("ixemdpusllyiingstr "); + int [] array; + array=new int[]{1, 2, 3, 0, 4, 7, 6, 9, 11, 12, 13, 10, 18, 19, 20, 15, 16, 17, 5, 8, 14}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "mixed up silly string"); }