diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..76d8ead 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,122 @@ -public class StringManipulation implements StringManipulationInterface { +//Khalil Coats +//CS 410 +//06/11/2023 +//JUnit Testing, StringManipulation implementation class - @Override +public class StringManipulation implements StringManipulationInterface { + + private String objString; + + @Override public String getString() { - return null; + return objString; } @Override public void setString(String string) { + objString = string; } - - @Override + + @Override public int count() { - return 0; + + String[] words = this.getString().split(" +"); + int wordCount = words.length; + return wordCount; } @Override - public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + public String removeNthCharacter(int n, boolean maintainSpacing) + { + if(n <= 0) + { + throw new IllegalArgumentException("n must be at least 1."); + } + else if(n > this.getString().length()-1) + { + throw new IndexOutOfBoundsException("index out of bounds"); + } + else + { + if(maintainSpacing) + { + char[] objChars = this.getString().toCharArray(); + for(int i = n; i <= this.getString().length(); i+=n) + { + objChars[i-1] = ' '; + } + this.setString(String.valueOf(objChars)); + } + else if(!maintainSpacing) + { + String newStr = ""; + for(int i = n-1; i < this.getString().length(); i+=(n-1)) + { + newStr = this.getString().substring(0, i) + this.getString().substring(i+1); + this.setString(newStr); + } + } + } + return this.getString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord) + { + String words[] = this.getString().split("\\W+"); + String returnList[]; + if(startWord <= 0 || endWord <= 0) + { + throw new IllegalArgumentException("Word position argument must be positive nonzero number."); + } + else if(startWord > endWord) + { + throw new IllegalArgumentException("upper bound greater than lower bound"); + } + else if(endWord > this.count()) + { + throw new IndexOutOfBoundsException("upper bound or lower is greater than word count"); + } + else + { + returnList = new String[endWord-startWord+1]; + int loopStart = startWord; + for(int i = 0; i < endWord- startWord+1; i++) + { + returnList[i] = words[loopStart - 1 + i]; + System.out.println(returnList[i]); + } + + } + return returnList; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + String restore = ""; + for(int i = 0; i < indices.length; i++) + { + if(indices[i] < 0 || indices[i] >= this.getString().length()) + { + throw new IndexOutOfBoundsException("one of the indices is out of array bounds"); + } + } + + if(indices.length != this.getString().length()) + { + throw new IllegalArgumentException("Illegal argument passed. Array argument length doesn't match string length"); + } + else + { + char[] newChars = new char[this.getString().length()]; + for(int i = 0; i { + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(-1, true)); + }, "n must be at least 1."); + assertEquals("n must be at least 1.", thrown.getMessage()); } @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + //checks if indexoutofboundsexception is thrown when n is greater than string length + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, () -> { + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(50, true)); + }, "index out of bounds"); + assertEquals("index out of bounds", thrown.getMessage()); } @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); - } + //checks if IllegalArgumentException is thrown when n is zero + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(0, true)); + }, "n must be at least 1."); + assertEquals("n must be at least 1.", thrown.getMessage()); + } @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + //blanks out string, maintainSpacing=true + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + //blanks out string, maintainSpacing=false + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); } @Test @@ -89,23 +122,102 @@ public void testGeSubStrings1() { @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(6, 4); + assertEquals(sStings[0], "the"); + assertEquals(sStings[1], "quick"); + assertEquals(sStings[2], "brown"); + assertEquals(sStings[3], "fox"); + assertEquals(sStings[4], "jumps"); + assertEquals(sStings[5], "over"); + assertEquals(sStings[6], "the"); + assertEquals(sStings[7], "lazy"); + assertEquals(sStings[8], "dog"); + }, "upper bound greater than lower bound"); + assertEquals("upper bound greater than lower bound", thrown.getMessage()); } + @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + //tests if IllegalArgumentException is thrown when startWord is negative + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(-2, 1); + assertEquals(sStings[0], "the"); + assertEquals(sStings[1], "quick"); + assertEquals(sStings[2], "brown"); + assertEquals(sStings[4], "fox"); + assertEquals(sStings[5], "jumps"); + assertEquals(sStings[6], "over"); + assertEquals(sStings[7], "the"); + assertEquals(sStings[8], "lazy"); + assertEquals(sStings[3], "dog"); + }, "Word position argument must be positive nonzero number."); + + assertEquals("Word position argument must be positive nonzero number.", thrown.getMessage()); } + @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + //tests if IllegalArgumentException is thrown when startWord is zero + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(0, 4); + assertEquals(sStings[0], "the"); + assertEquals(sStings[1], "quick"); + assertEquals(sStings[2], "brown"); + assertEquals(sStings[4], "fox"); + assertEquals(sStings[5], "jumps"); + assertEquals(sStings[6], "over"); + assertEquals(sStings[7], "the"); + assertEquals(sStings[8], "lazy"); + assertEquals(sStings[3], "dog"); + }, "Word position argument must be positive nonzero number."); + + assertEquals("Word position argument must be positive nonzero number.", thrown.getMessage()); } + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGeSubStrings5() + { + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + + ////tests if IndexOutOfBoundsException is thrown when arguments are greater than strings size + IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(10, 11); + assertEquals(sStings[0], "the"); + assertEquals(sStings[1], "quick"); + assertEquals(sStings[2], "brown"); + assertEquals(sStings[4], "fox"); + assertEquals(sStings[5], "jumps"); + assertEquals(sStings[6], "over"); + assertEquals(sStings[7], "the"); + assertEquals(sStings[8], "lazy"); + assertEquals(sStings[3], "dog"); + }, "upper bound or lower is greater than word count"); + + assertEquals("upper bound or lower is greater than word count", thrown.getMessage()); } + @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + //tests if IllegalArgumentException is thrown when endWord is negative + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(-3, -1); + assertEquals(sStings[0], "the"); + assertEquals(sStings[1], "quick"); + assertEquals(sStings[2], "brown"); + assertEquals(sStings[4], "fox"); + assertEquals(sStings[5], "jumps"); + assertEquals(sStings[6], "over"); + assertEquals(sStings[7], "the"); + assertEquals(sStings[8], "lazy"); + assertEquals(sStings[3], "dog"); + }, "Word position argument must be positive nonzero number."); + + assertEquals("Word position argument must be positive nonzero number.", thrown.getMessage()); } @Test @@ -121,28 +233,59 @@ public void testRestoreString1() @Test public void testRestoreString2() { - fail("Not yet implemented"); + //reverse string character order + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + int [] array; + array=new int[]{42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "god yzal eht revo spmuj xof nworb kciuq eht"); } @Test public void testRestoreString3() { - fail("Not yet implemented"); - + //tests if IndexOutOfBoundsException is thrown when one of index elements is greater than size of string + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + int [] array; + array=new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42, 43}; + IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, () -> { + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "the quick brown fox jumps over the lazy dog"); + }, "one of the indices is out of array bounds"); + + assertEquals("one of the indices is out of array bounds", thrown.getMessage()); } @Test public void testRestoreString4() { - fail("Not yet implemented"); + //tests if IndexOutOfBoundsException is thrown when one of index elements is negative + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + int [] array; + array=new int[]{-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41}; + IndexOutOfBoundsException thrown = assertThrows(IndexOutOfBoundsException.class, () -> { + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "the quick brown fox jumps over the lazy dog"); + }, "one of the indices is out of array bounds"); + + assertEquals("one of the indices is out of array bounds", thrown.getMessage()); } @Test public void testRestoreString5() { - fail("Not yet implemented"); + //tests if IllegalArgumentException is thrown when array length is less than string length + manipulatedstring.setString("the quick brown fox jumps over the lazy dog"); + int [] array; + array=new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41}; + IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> { + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "the quick brown fox jumps over the lazy dog"); + }, "Illegal argument passed. Array argument length doesn't match string length"); + + assertEquals("Illegal argument passed. Array argument length doesn't match string length", thrown.getMessage()); }