diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..3deaf9a 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,126 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + public class StringManipulation implements StringManipulationInterface { + private String message; @Override public String getString() { - return null; + return message; } @Override public void setString(String string) { + message = string; } @Override public int count() { - return 0; + int counting = 0; + for (int i = 0; i < message.length(); i++) { + if (message.charAt(i) == ' ') { + if (counting == 0) { + if (i > 0 && message.charAt(i - 1) != ' ') { + counting = 1; + } + } + if (i + 1 < message.length() && message.charAt(i + 1) != ' ') { + counting++; + } + } + } + return counting; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + + if (n > message.length()) { + throw new IndexOutOfBoundsException("N is greater than string length!"); + } + if (n <= 0) { + throw new IllegalArgumentException("N is less than or equal to 0"); + } + + List array = new ArrayList<>(message.length() + n - 1); + + for (int start = 0; start < message.length(); start += n) { + array.add(message.substring(start, Math.min(message.length(), start + n))); + } + + for (int i = 0; i < array.size(); i++) { + if (array.get(i).length() == n) { + String temp = array.get(i); + temp = temp.substring(0, n - 1); + if (maintainSpacing) { + temp = temp + " "; + } + array.set(i, temp); + } + } + String a = ""; + for (int i = 0; i < array.size(); i++) { + a = a + array.get(i); + } + return a; } + @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + + if (startWord <= 0 ||endWord <= 0 || startWord >endWord) { + throw new IllegalArgumentException("Invalid endWord/startWord"); + } + + if (count() < endWord) { + throw new IndexOutOfBoundsException("String has less words than endWords amount in it!"); + } + + String after = message.trim().replaceAll(" +", " "); + + List wordArrayList = new ArrayList(); + for (String word : after.split(" ")) { + wordArrayList.add(word); + } + int test = wordArrayList.size(); + String[] tempStringArray = new String[test]; + + for (int i = 0; i < test; i++) { + tempStringArray[i] = wordArrayList.get(i); + } + String[] finalStringArray = new String[endWord - startWord + 1]; + int count = 0; + for (int i = startWord - 1; i < endWord; i++) { + finalStringArray[count++] = tempStringArray[i]; + } + return finalStringArray; } @Override public String restoreString(int[] indices) { - return null; - } + if (message.length()!=indices.length){ + throw new IllegalArgumentException("Cannot have indices array of different length than message size!"); + } -} + //This turns message into a character array piece by piece. + char [] charArray = message.toCharArray(); + + //HashMap contains index in key, and associated value from message. + HashMap map = new HashMap<>(); + + for (int i = 0; i=message.length()){ + throw new IndexOutOfBoundsException("Cannot have negative index values for indices array"); + } + finalString = finalString + map.get(indices[i]); + } + return finalString; + } +} \ 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 old mode 100644 new mode 100755 index 6692c2c..984f745 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -25,26 +25,40 @@ public void testCount1() { int length = manipulatedstring.count(); assertEquals(4, length); } - + //This tests spaces AFTER ONLY and many spaces. @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("T "); + int length = manipulatedstring.count(); + assertEquals(1, length); } - + //This tests count with many space before after and inbetween. @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(" This is my string "); + int length = manipulatedstring.count(); + assertEquals(4, length); } - + //This tests count with no string. @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); + } + //This tests count with spaces before ONLY. + @Test + public void testCount5() { + manipulatedstring.setString(" This"); + int length = manipulatedstring.count(); + assertEquals(1, length); } @Test 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 @@ -53,31 +67,44 @@ public void testRemoveNthCharacter2() { assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); } + //This tests n at 5 with an number of characters not evenly divisible by 5. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("abcdefghijklmnopqrstuvwxyz"); + assertEquals("abcd fghi klmn pqrs uvwx z", manipulatedstring.removeNthCharacter(5, true)); } + //This tests the illegalArgument exception when n is inputted as a negative number. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("abcdefghijklmnopqrstuvwxyz"); + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(-5, true); + }); } + //This tests the IndexOfOutBoundsExceptions when the Nth number is larger than characters in string @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("abcdef"); + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(9, true); + }); } - + //This tests edge case when there are only spaces inputted into string with NO space inputted. @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + assertEquals(" ", manipulatedstring.removeNthCharacter(2, false)); } - + //This tests edge case when there are only spaces inputted into string with YES space inputted. @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + assertEquals(" ", manipulatedstring.removeNthCharacter(2, true)); } + //This method was given in the skeleton provided. @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); @@ -86,28 +113,57 @@ public void testGeSubStrings1() { assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } - + //This method tests spaces before the string and after the string. And tests the first two words. @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString(" This is my string "); + String [] sStings = manipulatedstring.getSubStrings(1, 2); + + assertEquals(sStings[0], "This"); + assertEquals(sStings[1], "is"); } + //This tests multiple spaces before and after and between. @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString(" This is my string "); + String [] sStings = manipulatedstring.getSubStrings(1, 4); + + assertEquals(sStings[0], "This"); + assertEquals(sStings[1], "is"); + assertEquals(sStings[2], "my"); + assertEquals(sStings[3], "string"); } + //This tests the IllegalArgumentException when the startWord is greater than the endWord. @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString(" This is my string "); + String [] sStings = manipulatedstring.getSubStrings(1, 3); + + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(2,1); + }); } + //This tests the IndexOutOfBoundsException class when the endWord is greater than the amount of words in the string. @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString(" This is my string "); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(2,5); + }); + } + //This tests single word containing multiple spaces. @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString(" I A C "); + String [] sStings = manipulatedstring.getSubStrings(1, 1); + + assertEquals(sStings[0], "I"); } + //This tests with the skeleton provided. Basic mixing of letters based on array. @Test public void testRestoreString1() { @@ -118,32 +174,51 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } + //This tests IllegalArgumentException when there are 7 items in array and 8 letters in the string. @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4,5,6,7,0,1,2}; + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }); } + //This tests IllegalArgumentException there are 9 numbers in the array and 8 letters in the string. @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4,5,6,7,0,1,2,3,8}; + assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }); } - + //This tests IndexOfOutBounds exception [THERE IS -1 in array] when the array contains a number less than 0 in the array. @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4,5,-1,2,0,6,7,3}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }); } + //This tests IndexOfOutBounds exception [THERE IS 10 in array] when the array contains a number larger than message length! @Test public void testRestoreString5() { - fail("Not yet implemented"); - + manipulatedstring.setString("UnitTest"); + int [] array; + array=new int[]{4,5,10,2,0,6,7,3}; + assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }); } -} +} \ No newline at end of file