diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..825671a 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,113 @@ -public class StringManipulation implements StringManipulationInterface { +/* Nadav Horowitz 6/3/2023 StringManipulation.java + * This file contains various methods for manipulation of a String object. + * The methods implemented include count, removeNthCharacter, getSubStrings, and restoreString. + */ +import java.util.Arrays; +public class StringManipulation implements StringManipulationInterface { + + public String string; + + @Override + //Getter method for String field public String getString() { - return null; + return this.string; } + @Override + //Setter method for String field public void setString(String string) { + this.string = string; } + @Override + //Returns number of words present in String object public int count() { - return 0; + //null case and empty case + if (string == null || string.length() == 0) + return 0; + //trim leading and ending whitespace + String copy = this.getString(); + copy = copy.trim(); + //Regex for matching words + String[] words = copy.split(" +"); + return words.length; } + @Override + //Returns a string that consists of all characters in the original string except for the characters + //in positions n, 2n, 3n, and so on, either deleting those or replacing them with a white space. + //Throws IllegalArgumentException for n <= 0, throws IndexOutOfBoundsException for n > string length. public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if(n <= 0) + throw new IllegalArgumentException(); + if(n > string.length()) + throw new IndexOutOfBoundsException(); + + String replacement = ""; + if(maintainSpacing) + replacement += " "; + + String newString = ""; + for(int i = 1; i <= string.length(); i++){ + char next = string.charAt(i-1); + if(i % n == 0) + newString += replacement; + else + newString += next; + } + return newString; } + @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + //Returns String[] containing words from position "startWord" to position "endWord" in String object, + //with 1 being the first Word in the String. + //Throws IllegalArgumentException If either "startWord" or "endWord" are invalid. + //Throws IndexOutOfBoundsException If the string has less than "endWord" words in it. + public String[] getSubStrings(int startWord, int endWord){ + if(startWord <= 0 || endWord <= 0 || startWord > endWord) + throw new IllegalArgumentException(); + + //trim leading and ending whitespace + String copy = this.getString(); + copy = copy.trim(); + + //Regex for matching words + String[] words = copy.split(" +"); + + if(endWord > words.length) + throw new IndexOutOfBoundsException(); + + words = Arrays.copyOfRange(words,startWord - 1, endWord); + return words; } + @Override - public String restoreString(int[] indices) { - return null; + //Given current String object and integer array "indices" of the same length, String will be shuffled such that + //the character at the ith position moves to indices[i] in the shuffled string. Returns the shuffled string. + //Throws IllegalArgumentException if string.length != indices.length + //Throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length. + public String restoreString(int[] indices){ + String copy = this.getString(); + + if(copy.length() != indices.length) + throw new IllegalArgumentException(); + + String newString = ""; + for(int i = 0; i < indices.length; i++) { + int stringIndex = indices[i]; + + if(stringIndex < 0 || stringIndex > indices.length) + throw new IndexOutOfBoundsException(); + + newString += copy.charAt(stringIndex); + } + return newString; } - - -} +} \ No newline at end of file diff --git a/src/main/java/StringManipulationInterface.java b/src/main/java/StringManipulationInterface.java index 87127e7..cc81e80 100644 --- a/src/main/java/StringManipulationInterface.java +++ b/src/main/java/StringManipulationInterface.java @@ -79,10 +79,10 @@ public interface StringManipulationInterface { * The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. * Return the shuffled string. * example: - * Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3] + * Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3] * Output: "TestUnit" * Explanation: - * indices: 4 5 6 7 0 1 2 3 + * indices: 4 5 6 7 0 2 1 3 * String: U n i t T e s t * Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ...... * Output: T e s t U n i t @@ -95,7 +95,7 @@ public interface StringManipulationInterface { * indices length is the same as the string length. * * throws IllegalArgumentException if not s.length == indices.length == n - * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length + * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length * * @param indices is an integer array for shuffled string new indices positions * the character at the ith position moves to indices[i] in the shuffled string. diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..ebe1b0f 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,8 +1,11 @@ +/* Nadav Horowitz 6/3/2023 StringManipulationTest.java + * This file contains JUnit tests for the StringManipulation class. + * Tested methods include count, removeNthCharacter, getSubStrings, and restoreString. + * Tests either check for correct output or check that correct Exception is thrown. + */ 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 { @@ -10,16 +13,19 @@ public class StringManipulationTest { private StringManipulationInterface manipulatedstring; @BeforeEach + //setUp method initializes StringManipulation object with null String field before each test. public void setUp() { manipulatedstring = new StringManipulation(); } @AfterEach + //tearDown method ensures garbage collection of StringManipulation object after each test. public void tearDown() { manipulatedstring = null; } @Test + //testCount1 calls count() with a standard 4 word test String and ensures the method returns 4 as the number of words. public void testCount1() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); @@ -27,90 +33,165 @@ public void testCount1() { } @Test + //testCount2 calls count() with a null test String and ensures the method returns 0 as the number of words. public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + //testCount3 calls count() with an empty test String and ensures the method returns 0 as the number of words. public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test + //testCount3 calls count() with a test String containing lots of whitespace + //and ensures the method returns 5 as the number of words. public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(" a lot of white space "); + int length = manipulatedstring.count(); + assertEquals(5, length); } @Test + //testRemoveNthCharacter1 calls removeNthCharacter() with standard parameters and ensures the method + //removes the desired characters. 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)); + String NthCharRemoved = manipulatedstring.removeNthCharacter(3, false); + assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", NthCharRemoved); } @Test + //testRemoveNthCharacter2 calls removeNthCharacter() with standard parameters and ensures the method + //removes the desired characters and replaces them with spaces. 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)); + String NthCharRemoved = manipulatedstring.removeNthCharacter(3, true); + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", NthCharRemoved); } @Test + //testRemoveNthCharacter3 calls removeNthCharacter() with a standard String parameter + //and parameter n = 1 and ensures the method returns an empty String. public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a nice sentence."); + String NthCharRemoved = manipulatedstring.removeNthCharacter(1, false); + assertEquals("", NthCharRemoved); } @Test + //testRemoveNthCharacter4 calls removeNthCharacter with a standard String parameter + //and parameter n = stringLength and ensures the method removes only the last character. public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("Remove the last character only"); + int stringLength = manipulatedstring.getString().length(); + String NthCharRemoved = manipulatedstring.removeNthCharacter(stringLength, false); + assertEquals("Remove the last character onl", NthCharRemoved); } @Test + //testRemoveNthCharacter5 calls removeNthCharacter with a standard String parameter + //and parameter n = 2 and ensures the method removes and replaces every other character with a space. public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("This is a nice sentence."); + String NthCharRemoved = manipulatedstring.removeNthCharacter(2, true); + assertEquals("T i s a n c e t n e ", NthCharRemoved); } - + @Test + //testRemoveNthCharacter6 calls removeNthCharacter with n <= 0 and ensures the method throws an IllegalArgumentException public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + manipulatedstring.removeNthCharacter(-99, false); + }); } @Test + //testRemoveCharacter7 calls removeNthCharacter with n > stringLength and ensures the method + //throws an IndexOutOfBoundsException public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + assertThrows(IndexOutOfBoundsException.class, + ()->{ + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + manipulatedstring.removeNthCharacter(99, true); + }); } @Test + //testGeSubStrings1 calls testGeSubStrings with a standard String parameter and ensures the method + //returns the correct output public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); - assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } @Test + //testGeSubStrings2 calls testGeSubStrings with a negative startword parameter + //and ensures the method throws an IllegalArgumentException public void testGeSubStrings2() { - fail("Not yet implemented"); + assertThrows(IllegalArgumentException.class, + () ->{ + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(-1, 3); + }); } + @Test + //testGeSubStrings3 calls testGeSubStrings with a standard String parameter and an invalid startWord = 0 parameter + //and ensures the method throws an IllegalArgumentException public void testGeSubStrings3() { - fail("Not yet implemented"); + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.setString("This is my string"); + manipulatedstring.getSubStrings(0, 1); + }); } + @Test + //testGeSubStrings4 calls testGeSubStrings with a standard String parameter and invalid endword = 0 parameter + //and ensures the method throws an IllegalArgumentException public void testGeSubStrings4() { - fail("Not yet implemented"); + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.setString("This is my string"); + manipulatedstring.getSubStrings(3, 0); + }); } + @Test + //testGeSubStrings5 calls testGeSubStrings with a standard String parameter and invalid startWord > endWord parameters + //and ensures the method throws an IllegalArgumentException public void testGeSubStrings5() { - fail("Not yet implemented"); + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.setString("This is my string"); + manipulatedstring.getSubStrings(4, 1); + }); } + @Test + //testGeSubStrings6 calls testGeSubStrings with a standard String parameter and endWord > stringLength parameter + //and ensures the method throws an IndexOutOfBoundsException public void testGeSubStrings6() { - fail("Not yet implemented"); + assertThrows(IndexOutOfBoundsException.class, + ()->{ + manipulatedstring.setString("This is my string"); + manipulatedstring.getSubStrings(1, 10); + }); } - + @Test - public void testRestoreString1() - { + //testRestoreString1 calls testRestoreString with standard paramaters and ensures the method returns the correct output + public void testRestoreString1(){ manipulatedstring.setString("art"); int [] array; array=new int[]{1,0,2}; @@ -119,31 +200,54 @@ public void testRestoreString1() } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + //testRestoreString2 calls testRestoreString with an array of lesser length than the current String + //and ensures the method throws an IllegalArgumentException + public void testRestoreString2(){ + manipulatedstring.setString("0123456789"); + int [] array; + array=new int[] {5}; + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.restoreString(array); + }); + } + + @Test + //testRestoreString3 calls testRestoreString with an array of greater length than the current String + //and ensures the method throws an IllegalArgumentException + public void testRestoreString3(){ + manipulatedstring.setString("fart"); + int [] array; + array=new int[]{1,0,2}; + assertThrows(IllegalArgumentException.class, + ()->{ + manipulatedstring.restoreString(array); + }); } @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + //testRestoreString4 calls testRestoreString with indices array containing an element < 0 + //and ensures the method throws an IndexOutOfBoundsException + public void testRestoreString4(){ + manipulatedstring.setString("art"); + int [] array; + array=new int[]{-1,0,2}; + assertThrows(IndexOutOfBoundsException.class, + ()->{ + manipulatedstring.restoreString(array); + }); } @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - + //testRestoreString5 calls testRestoreString with indices array containing an element > stringLength + //and ensures the method throws an IndexOutOfBoundsException + public void testRestoreString5(){ + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,20}; + assertThrows(IndexOutOfBoundsException.class, + ()->{ + manipulatedstring.restoreString(array); + }); } - -} +} \ No newline at end of file