-
Notifications
You must be signed in to change notification settings - Fork 6
Vanessa Code Review Files #28
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,78 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| public class StringManipulation { | ||
|
|
||
| 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.trim().split("\\s+"); | ||
| return words.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("Invalid value for n"); | ||
| } | ||
| if (string == null || string.isEmpty() || n > string.length()) { | ||
| throw new IndexOutOfBoundsException("n is greater than the string length"); | ||
| } | ||
|
|
||
| StringBuilder result = new StringBuilder(); | ||
| for (int i = 0; i < string.length(); i++) { | ||
| if ((i + 1) % n != 0) { | ||
| result.append(string.charAt(i)); | ||
| } else if (maintainSpacing) { | ||
| result.append(' '); | ||
| } | ||
| } | ||
| return result.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| } | ||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IllegalArgumentException("Invalid startWord or endWord values"); | ||
| } | ||
| if (string == null || string.isEmpty()) { | ||
| throw new IndexOutOfBoundsException("String is empty"); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| String[] words = string.trim().split("\\s+"); | ||
| if (endWord > words.length) { | ||
| throw new IndexOutOfBoundsException("String has fewer words than endWord"); | ||
| } | ||
|
|
||
| String[] substrings = new String[endWord - startWord + 1]; | ||
| int index = 0; | ||
| for (int i = startWord - 1; i < endWord; i++) { | ||
| substrings[index] = words[i]; | ||
| index++; | ||
| } | ||
| return substrings; | ||
| } | ||
|
|
||
| public String restoreString(int[] indices) { | ||
| if (string == null || indices == null || string.length() != indices.length) { | ||
|
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. Java already throws a runtime error if you try to call |
||
| throw new IllegalArgumentException("Invalid input: string length does not match indices length"); | ||
| } | ||
|
|
||
| char[] charArray = string.toCharArray(); | ||
| char[] restoredArray = new char[charArray.length]; | ||
| for (int i = 0; i < charArray.length; i++) { | ||
| int index = indices[i]; | ||
| if (index < 0 || index >= charArray.length) { | ||
| throw new IndexOutOfBoundsException("Invalid index value"); | ||
| } | ||
| restoredArray[index] = charArray[i]; | ||
| } | ||
| return new String(restoredArray); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,14 @@ | |
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
||
| private StringManipulationInterface manipulatedstring; | ||
| private StringManipulation manipulatedstring; | ||
ness-miewald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
|
|
@@ -28,19 +30,28 @@ public void testCount1() { | |
|
|
||
| @Test | ||
| public void testCount2() { | ||
ness-miewald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is another string"); | ||
| int count = manipulatedstring.count(); | ||
| assertEquals(4, count); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Count multiple words"); | ||
| int count = manipulatedstring.count(); | ||
| assertEquals(3, count); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int count = manipulatedstring.count(); | ||
| assertEquals(0, count); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter1() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
|
|
@@ -55,95 +66,140 @@ public void testRemoveNthCharacter2() { | |
|
|
||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
|
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. The following test cases (testRemoveNthCharacter 3-7) are all variations of the first two test cases. Please revise these test cases so that they're testing for different kinds of input (e.g., |
||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello, World!"); | ||
| String result = manipulatedstring.removeNthCharacter(2, false); | ||
| assertEquals("Hlo ol!", result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello, World!"); | ||
| String result = manipulatedstring.removeNthCharacter(5, true); | ||
| assertEquals("Hell , Wo ld!", result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello, World!"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.removeNthCharacter(0, false); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hello, World!"); | ||
| String result = manipulatedstring.removeNthCharacter(3, false); | ||
| assertEquals("Helo Wrl!", result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("world"); | ||
| String result = manipulatedstring.removeNthCharacter(2, true); | ||
| assertEquals("w r d", result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| public void testGetSubStrings1() { | ||
|
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. You need to add unit tests for all the if statements in |
||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| String[] substrings = manipulatedstring.getSubStrings(3, 4); | ||
| assertEquals("my", substrings[0]); | ||
| assertEquals("string", substrings[1]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings2() { | ||
| manipulatedstring.setString("This is a test string"); | ||
| String[] result = manipulatedstring.getSubStrings(2, 4); | ||
| assertArrayEquals(new String[]{"is", "a", "test"}, result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings3() { | ||
| manipulatedstring.setString("Hello, World!"); | ||
| String[] result = manipulatedstring.getSubStrings(1, 2); | ||
| assertArrayEquals(new String[]{"Hello,", "World!"}, result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings4() { | ||
| manipulatedstring.setString("mochi is the best dog"); | ||
| String[] result = manipulatedstring.getSubStrings(2, 4); | ||
| assertArrayEquals(new String[]{"is", "the", "best"}, result); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings5() { | ||
| manipulatedstring.setString("This is a test"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.getSubStrings(4, 6); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings6() { | ||
| manipulatedstring.setString("This is a test"); | ||
| String[] result = manipulatedstring.getSubStrings(1, 3); | ||
| assertArrayEquals(new String[]{"This", "is", "a"}, result); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| public void testRestoreString1() { | ||
|
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. Need more unique test cases for testing |
||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| int[] array = {1, 0, 2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| assertEquals("rat", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString2() { | ||
| manipulatedstring.setString("opna"); | ||
| int[] array = {3, 0, 2, 1}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("pano", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| @Test | ||
| public void testRestoreString3() { | ||
| manipulatedstring.setString("abcdefg"); | ||
| int[] array = {6, 5, 4, 3, 2, 1, 0}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("gfedcba", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| @Test | ||
| public void testRestoreString4() { | ||
| manipulatedstring.setString("Hello"); | ||
| int[] array = {0, 1, 2, 3, 4}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("Hello", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| @Test | ||
| public void testRestoreString5() { | ||
| manipulatedstring.setString("abc"); | ||
| int[] array = {1, 2, 0}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("cab", restoreString); | ||
| } | ||
|
|
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.