-
Notifications
You must be signed in to change notification settings - Fork 11
Added new versions of StringManipulation .java files #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,94 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String string; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return string; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| this.string = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if (string == null || string.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| String[] words = string.split("\\s+"); | ||
| return words.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (string == null || string.isEmpty()) { | ||
| return string; | ||
| } | ||
|
|
||
| if (n <= 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if (n > string.length()) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
|
|
||
| String result = ""; | ||
| int length = string.length(); | ||
| for (int i = 0; i < length; i++) { | ||
| char c = string.charAt(i); | ||
| if ((i + 1) % n != 0) { | ||
| result += c; | ||
| } else { | ||
| if (maintainSpacing) { | ||
| result += " "; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| String[] words = string.split("\\s+"); | ||
| if (endWord > words.length) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
|
|
||
| int substringCount = endWord - startWord + 1; | ||
| String[] substrings = new String[substringCount]; | ||
|
|
||
| for (int i = startWord - 1; i <= endWord - 1; i++) { | ||
| substrings[i - startWord + 1] = words[i]; | ||
| } | ||
|
|
||
| return substrings; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| int n = indices.length; | ||
| char[] shuffledChars = new char[n]; | ||
|
|
||
| if (!(string.length() == indices.length && indices.length == n)) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| if (indices[i] < 0 || indices[i] >= n) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| shuffledChars[indices[i]] = string.charAt(i); | ||
| } | ||
|
|
||
| } | ||
| return new String(shuffledChars); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend adding some test cases for setString() and getString() for sake of sanity. Be sure to add cases when this.string is null and when you try to set this.string to null as well as when string is empty. |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add cases in each method when string is null or empty. |
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
@@ -26,19 +25,29 @@ public void testCount1() { | |
| assertEquals(4, length); | ||
| } | ||
|
|
||
| // check to see that a null string will return a word count of 0. | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(null); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| // tests if numbers or special characters will mess with the word count | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is a string that the number $40.00 in the string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(11, length); | ||
| } | ||
|
|
||
| // another test, but this time we add quotation marks to see if it messes with | ||
| // word count | ||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This a string that can't fail just because \"can't\" is in the string"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test seems redundant with Test case #1. Can it be replaced with testing extra whitespace (EX: " abc") and the one where string has more than 1 whitespace between words (EX: "abc def")? |
||
| int length = manipulatedstring.count(); | ||
| assertEquals(13, length); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -50,100 +59,152 @@ public void testRemoveNthCharacter1() { | |
| @Test | ||
| 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)); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", | ||
| manipulatedstring.removeNthCharacter(3, true)); | ||
| } | ||
|
|
||
| // like the tests above, but we test it with a different value for n. | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", | ||
| manipulatedstring.removeNthCharacter(5, false)); | ||
| } | ||
|
|
||
| // we test for a smaller n. | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("let's try a smaller string"); | ||
| assertEquals("ltstyasalrsrn", | ||
| manipulatedstring.removeNthCharacter(2, false)); | ||
| } | ||
|
|
||
| // we test for a smaller n while maintaning the spacing in the string | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("let's try a smaller string"); | ||
| assertEquals("l t s t y a s a l r s r n ", | ||
| manipulatedstring.removeNthCharacter(2, true)); | ||
| } | ||
|
|
||
| // test to make sure it throws a IndexOutOfBoundsException when n > string | ||
| // length | ||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for n > string length"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); | ||
| } | ||
|
|
||
| // test to make sure it throws a IllegalArgumentException when n < 0 | ||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IllegalArgumentException for n < 0"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
| String[] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| // test to make sure that it can scale to more substrings | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend modifying this test case so that it calls getSubStrings(1,15) (i.e. get all the words in a string). |
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is a very long string that seems to go on and on and on"); | ||
| String[] sStings = manipulatedstring.getSubStrings(2, 7); | ||
|
|
||
| assertEquals(sStings[0], "is"); | ||
| assertEquals(sStings[1], "a"); | ||
| assertEquals(sStings[2], "very"); | ||
| assertEquals(sStings[3], "long"); | ||
| assertEquals(sStings[4], "string"); | ||
| assertEquals(sStings[5], "that"); | ||
| } | ||
|
|
||
| // tests thats startWord cannot be <= 0 and should throw a | ||
| // IllegalArgumentException | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord <= 0"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 5)); | ||
| } | ||
|
|
||
| // tests thats endWord cannot be <= 0 and should throw a | ||
| // IllegalArgumentException | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modify testGeSubStrings4 so that startWord is an integer greater than 0. This ensures that the exception is caused by the endWord <= 0, not because startWord is <= 0. |
||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IllegalArgumentException for endWord <= 0"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 0)); | ||
| } | ||
|
|
||
| // tests thats startWord < endWord and should throw a IllegalArgumentException | ||
| // if it isn't | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord > endWord"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1)); | ||
| } | ||
|
|
||
| // tests the case where string length is shorter than endWord and throws a | ||
| // IndexOutOfBoundsException if it is | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for endWord > string length"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test case for getSubStrings() when startWord = endWord. |
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| public void testRestoreString1() { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| int[] array; | ||
| array = new int[] { 1, 0, 2 }; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| // another test like above to check string length doesn't matter | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString2() { | ||
| manipulatedstring.setString("CatDog"); | ||
| int[] array; | ||
| array = new int[] { 3, 4, 5, 0, 1, 2 }; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "DogCat"); | ||
| } | ||
|
|
||
| // another test like above to check string length doesn't matter, even two of | ||
| // the same characters | ||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString3() { | ||
| manipulatedstring.setString("backwards"); | ||
| int[] array; | ||
| array = new int[] { 4, 2, 6, 7, 3, 5, 1, 0, 8 }; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "drawbacks"); | ||
| } | ||
|
|
||
| // tests the case where input array contains an invalid string index and throws | ||
| // a IndexOutOfBoundsException | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString4() { | ||
| manipulatedstring.setString("wrote"); | ||
| int[] array; | ||
| array = new int[] { -1, 0, 1, 2, 3 }; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
| // tests the case where input array is not the same length as the string and | ||
| // throws a IllegalArgumentException | ||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString5() { | ||
| manipulatedstring.setString("left"); | ||
| int[] array; | ||
| array = new int[] { 0, 1, 2, 3, 4, 5 }; | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add another test case for restoreString() for case when string contains more than 1 word, such as "Hello world". |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modify or add additional test cases in StringManipulationTest.java. If new test cases failed, modify this file so that it passes the said test cases.