diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..0a9c1cd 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,111 @@ +/* +Aravind Sripada +CS410 - Sara Farag +6/13/2023 +This file contains the implementations for the string manipulations class. +*/ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class StringManipulation implements StringManipulationInterface { + String current = null; @Override public String getString() { - return null; + return current; } @Override public void setString(String string) { + current = string; } @Override public int count() { - return 0; + if(current == null || current.isEmpty()) //empty or null string have a length of 0 + return 0; + + Pattern pattern = Pattern.compile("\\w+"); + Matcher matcher = pattern.matcher(current); + + int count = 0; + while(matcher.find()) { + count++; + } + + return count; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if(current == null || current.isEmpty()) + return current; + + if(n <= 0 ) { + throw new IllegalArgumentException(); + } + + if(n > current.length()) { + throw new IndexOutOfBoundsException(); + } + + int replacedDigit = n; + String replaced = ""; + for(int i = 0; i < current.length(); i++) { + if(i != replacedDigit - 1) { + replaced += current.charAt(i); + } else { + replacedDigit += n; + if(maintainSpacing) + replaced += " "; + } + } + return replaced; } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + if(startWord <= 0 || endWord <= 0) { + throw new IllegalArgumentException(); + } + + int wordcount = count(); + if(startWord > wordcount || endWord > wordcount) { + throw new IndexOutOfBoundsException(); + } + + int start = startWord - 1; //the given ints are always 1 ahead since they start counting from 1 + int end = endWord - 1; + + String[] words = current.trim().split(" "); //removes white space at the beginning and end of the string and splits by whitespace + String[] substring = new String[endWord - startWord + 1]; + + for(int i = start; i <= end; i++) { + int substringindex = i - startWord + 1; + substring[substringindex] = words[i]; + } + + return substring; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if(indices.length != current.length()) { + throw new IllegalArgumentException(); + } + + + + String newStr = ""; + for(int i = 0; i < indices.length; i++) { + if(indices[i] < 0 || indices[i] >= current.length()) { + throw new IndexOutOfBoundsException(); + } + newStr += current.charAt(indices[i]); + } + + return newStr; } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..97467cc 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,3 +1,10 @@ +/* +Aravind Sripada +CS410 - Sara Farag +6/13/2023 +This file contains the unit tests to ensure that the StringManipulation class works as intended. +*/ + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,6 +27,14 @@ public void tearDown() { } @Test + //this test checks for correct output + public void testGetString() { + String str = manipulatedstring.getString(); + assertEquals(null, str); + } + + @Test + //this test check for correct output public void testCount1() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); @@ -27,59 +42,85 @@ public void testCount1() { } @Test + //this test check for correct output public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + //this test check for correct output public void testCount3() { - fail("Not yet implemented"); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + //this test check for correct output public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("!!!!!! .... ,,,,"); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + //this test check for correct output 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 + //this test check for correct output 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)); } @Test + //this test check for correct output by leaving the string with its default value public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + assertEquals(null, manipulatedstring.removeNthCharacter(3, false)); } @Test + //this test check for correct output by giving the string an empty string value public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + assertEquals("", manipulatedstring.removeNthCharacter(4, true)); } @Test + //this test checks to see if the IllegalArgumentException is thrown public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("Hi Sara!"); + + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(-1, true); + }); } @Test + //this class checks to see if the IndexOutOfBoundsException is thrown public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("Hi Sara!"); + + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(100, true); + }); } @Test + //this test check for correct output public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("CS4420"); + assertEquals("CS420", manipulatedstring.removeNthCharacter(4, false)); } @Test - public void testGeSubStrings1() { + //this test check for correct output + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -88,29 +129,59 @@ public void testGeSubStrings1() { } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + //this test check for correct output + public void testGetSubStrings2() throws Exception{ + manipulatedstring.setString("I need some sleep"); + String [] sStings = manipulatedstring.getSubStrings(1, 3); + + assertEquals(sStings[0], "I"); + assertEquals(sStings[1], "need"); + assertEquals(sStings[2], "some"); } + @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + //this test checks to see if the IllegalArgumentException is thrown + public void testGetSubStrings3() throws Exception{ + manipulatedstring.setString("I need some sleep"); + + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(1, 0); + }); } + @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + //this test checks to see if the IllegalArgumentException is thrown + public void testGetSubStrings4() throws Exception{ + manipulatedstring.setString("I need some sleep"); + + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(0, 1); + }); } + @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + //this test checks to see if the IndexOutOfBoundsException is thrown + public void testGetSubStrings5() { + manipulatedstring.setString("I need some sleep"); + + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(5, 3); + }); } + @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + //this test checks to see if the IndexOutOfBoundsException is thrown + public void testGetSubStrings6() throws Exception{ + manipulatedstring.setString("I need some sleep"); + + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(3, 5); + }); } @Test - public void testRestoreString1() - { + //this test check for correct output + public void testRestoreString1() { manipulatedstring.setString("art"); int [] array; array=new int[]{1,0,2}; @@ -119,31 +190,51 @@ public void testRestoreString1() } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); + //this test check to see if the IndexOutOfBoundsException is thrown + public void testRestoreString2() { + manipulatedstring.setString("acro"); + int[] array; + array = new int[] {3, 2, -1, 0}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }); } @Test + //this test check to see if the IndexOutOfBoundsException is thrown public void testRestoreString3() { - fail("Not yet implemented"); + manipulatedstring.setString("acro"); + int[] array; + array = new int[] {3, 2, 1, 4}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }); } @Test + //this test check to see if the IllegalArgumentException is thrown public void testRestoreString4() { - fail("Not yet implemented"); + manipulatedstring.setString("acro"); + int[] array; + array = new int[] {3, 2, 1}; + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }); } @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + //this test check for correct output + public void testRestoreString5() { + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4, 5, 6, 7, 0, 1, 2, 3}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "TestUnit"); } }