-
Notifications
You must be signed in to change notification settings - Fork 11
Add final project implementation from Assignment 2 #8
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,82 @@ | ||
| 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) { | ||
| return 0; | ||
| } | ||
| String[] words = string.trim().split("\\s+"); | ||
| return words.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (string == null) { | ||
| return null; | ||
| } | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("n must be greater than zero"); | ||
| } | ||
| if (n > string.length()) { | ||
| throw new IndexOutOfBoundsException("n exceeds string length"); | ||
| } | ||
| StringBuilder result = new StringBuilder(); | ||
| char[] chars = string.toCharArray(); | ||
|
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. It doesn't look like you ever do anything with the chars variable. |
||
| int count = 0; | ||
| for (char c : string.toCharArray()) { | ||
| count++; | ||
| if (count % n != 0) { | ||
| result.append(c); | ||
| } else if (maintainSpacing) { | ||
| result.append(' '); | ||
| } | ||
| } | ||
| return result.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| if (string == null) { | ||
| return null; | ||
| } | ||
| String[] words = string.trim().split("\\s+"); | ||
| int numWords = words.length; | ||
|
|
||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
|
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 might be more of a style choice, but wouldn't it make more sense to throw exceptions before executing business logic? |
||
| throw new IllegalArgumentException("Invalid startWord or endWord values"); | ||
| } | ||
| if (endWord > numWords) { | ||
| throw new IndexOutOfBoundsException("The string has fewer than endWord words"); | ||
| } | ||
| return java.util.Arrays.copyOfRange(words, startWord - 1, endWord); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| if (string == null || indices == null || string.length() != indices.length) { | ||
| throw new IllegalArgumentException("Invalid arguments: string length must be equal to indices length"); | ||
| } | ||
| char[] chars = string.toCharArray(); | ||
| char[] restoredChars = new char[chars.length]; | ||
| for (int i = 0; i < indices.length; i++) { | ||
| int index = indices[i]; | ||
| if (index < 0 || index >= chars.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. Would it make more sense to verify the integrity of your data prior to when you start executing the logic of the method? |
||
| throw new IndexOutOfBoundsException("Invalid index: indices[i] must be within the string length and unique"); | ||
| } | ||
| restoredChars[i] = chars[index]; | ||
| } | ||
| return new String(restoredChars); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,142 +7,192 @@ | |
|
|
||
| public class StringManipulationTest { | ||
|
|
||
| private StringManipulationInterface manipulatedstring; | ||
| private StringManipulationInterface manipulatedString; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| manipulatedstring = new StringManipulation(); | ||
| manipulatedString = new StringManipulation(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| manipulatedstring = null; | ||
| manipulatedString = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| manipulatedString.setString("This is my string"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
|
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. I would consider adding more variety or some edge cases to your testing. Maybe add some sentences with unconventional spacing for characters. |
||
| manipulatedString.setString("I like video games"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("What"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Why there are so many test cases to create"); | ||
| int length = manipulatedString.count(); | ||
| assertEquals(9, 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)); | ||
| manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedString.removeNthCharacter(3, false)); | ||
| } | ||
|
|
||
| @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)); | ||
| 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 | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Holy mama"); | ||
| assertEquals("Hl aa", manipulatedString.removeNthCharacter(2, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Holy mama"); | ||
| assertEquals("H l a a", manipulatedString.removeNthCharacter(2, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Holy mama"); | ||
| assertEquals("Hol ma a", manipulatedString.removeNthCharacter(4, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Exception testing1"); | ||
| assertEquals("Don't mind this", manipulatedString.removeNthCharacter(0, true)); | ||
|
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. JUnit does provide a assertThrows() method to test exception handling. It would probably be better than relying on the exception methods returning string. |
||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Exception testing2"); | ||
| assertEquals("Don't mind this", manipulatedString.removeNthCharacter(19, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
| manipulatedString.setString("This is my string"); | ||
| String [] sStings = manipulatedString.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| @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"); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Why there are so many test cases to create"); | ||
| String [] sStings = manipulatedString.getSubStrings(1, 2); | ||
|
|
||
| assertEquals(sStings[0], "Why"); | ||
| assertEquals(sStings[1], "there"); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Why there are so many test cases to create"); | ||
| String [] sStings = manipulatedString.getSubStrings(1, 3); | ||
|
|
||
| assertEquals(sStings[0], "Why"); | ||
| assertEquals(sStings[1], "there"); | ||
| assertEquals(sStings[2], "are"); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Why there are so many test cases to create"); | ||
| String [] sStings = manipulatedString.getSubStrings(0, 6); | ||
|
|
||
|
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. How is this testing if an exception is thrown? What are the mechanics behind this? |
||
| assertEquals(sStings[0], "many"); | ||
| assertEquals(sStings[1], "test"); | ||
| // Exception1 | ||
| } | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("Why there are so many test cases to create"); | ||
| String [] sStings = manipulatedString.getSubStrings(5, 11); | ||
|
|
||
| assertEquals(sStings[0], "many"); | ||
| assertEquals(sStings[1], "test"); | ||
| assertEquals(sStings[2], "cases"); | ||
| // Exception2 | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| manipulatedstring.setString("art"); | ||
| manipulatedString.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| String restoreString = manipulatedString.restoreString(array); | ||
| assertEquals("rat", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedString.setString("art"); | ||
| int [] array; | ||
| array=new int[]{2,0,1}; | ||
| String restoreString = 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. What would happen if the array argument uses duplicate values? |
||
| assertEquals("tar", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("art"); | ||
| int [] array; | ||
| array=new int[]{2,1,0}; | ||
| String restoreString = manipulatedString.restoreString(array); | ||
| assertEquals("tra", restoreString); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2,3}; | ||
| String restoreString = manipulatedString.restoreString(array); | ||
| assertEquals("Exception1", restoreString); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedString.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,5,2}; | ||
| String restoreString = manipulatedString.restoreString(array); | ||
| assertEquals("Exception2", restoreString); | ||
|
|
||
| } | ||
|
|
||
|
|
||
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.
trim() is probably an unnecessary step to separate word by whitespace.