-
Notifications
You must be signed in to change notification settings - Fork 6
Aravind's code #16
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?
Aravind's code #16
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "java.configuration.updateBuildConfiguration": "interactive" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,111 @@ | ||
| /* | ||
| Aravind Sripada | ||
| CS410 - Sara Farag | ||
| 6/13/2023 | ||
| This file contains the implementations for the string manipulations class. | ||
| */ | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| String current = null; | ||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return current; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| current = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if(current == null || current.isEmpty()) //empty or null string have a length of 0 | ||
|
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 may want to consider throwing a NullPointerException when count, or any of your other methods are called on a null string instead of returning something. |
||
| return 0; | ||
|
|
||
| Pattern pattern = Pattern.compile("\\w+"); | ||
| Matcher matcher = pattern.matcher(current); | ||
|
|
||
| int count = 0; | ||
| while(matcher.find()) { | ||
| count++; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if(current == null || current.isEmpty()) | ||
|
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. Great job checking for current == null before trying to call a method on the null string. |
||
| return current; | ||
|
|
||
| if(n <= 0 ) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if(n > current.length()) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
|
|
||
| int replacedDigit = n; | ||
| String replaced = ""; | ||
| for(int i = 0; i < current.length(); i++) { | ||
| if(i != replacedDigit - 1) { | ||
| replaced += current.charAt(i); | ||
| } else { | ||
| replacedDigit += n; | ||
| if(maintainSpacing) | ||
| replaced += " "; | ||
| } | ||
| } | ||
| return replaced; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| if(startWord <= 0 || endWord <= 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| int wordcount = count(); | ||
| if(startWord > wordcount || endWord > wordcount) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
|
|
||
| int start = startWord - 1; //the given ints are always 1 ahead since they start counting from 1 | ||
| int end = endWord - 1; | ||
|
|
||
| String[] words = current.trim().split(" "); //removes white space at the beginning and end of the string and splits by whitespace | ||
|
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 don't see any issue with using this method to break up the words in a String, but instead of instantiating another array and filling it, you could use a regex to extract and populate the output array directly. |
||
| String[] substring = new String[endWord - startWord + 1]; | ||
|
|
||
| for(int i = start; i <= end; i++) { | ||
| int substringindex = i - startWord + 1; | ||
| substring[substringindex] = words[i]; | ||
| } | ||
|
|
||
| return substring; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| public String restoreString(int[] indices){ | ||
| if(indices.length != current.length()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| String newStr = ""; | ||
| for(int i = 0; i < indices.length; i++) { | ||
| if(indices[i] < 0 || indices[i] >= current.length()) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| newStr += current.charAt(indices[i]); | ||
| } | ||
|
|
||
| return newStr; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| /* | ||
| Aravind Sripada | ||
| CS410 - Sara Farag | ||
| 6/13/2023 | ||
| This file contains the unit tests to ensure that the StringManipulation class works as intended. | ||
| */ | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -20,66 +27,100 @@ public void tearDown() { | |
| } | ||
|
|
||
|
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. Aravind, you're missing a requirement in your tests to verify that getString() is returning null when the string is null. |
||
| @Test | ||
| //this test checks for correct output | ||
| public void testGetString() { | ||
| String str = manipulatedstring.getString(); | ||
| assertEquals(null, str); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("!!!!!! .... ,,,,"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, 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. I don't see a test case for strings that look like "!!!!! !!!!". Your count method would return 2 words in this case and a group of exclamation points is not a word. |
||
| @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. You have quite a few of your test methods marked "throws Exception." This is unnecessary. You should remove the throws statement in the method declaration. 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. 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. As you can see from my Intellij, it doesn't recognize the method as throwing an exception. |
||
| //this test check for correct output | ||
| 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 | ||
| //this test check for correct output | ||
| 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)); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output by leaving the string with its default value | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| assertEquals(null, manipulatedstring.removeNthCharacter(3, false)); | ||
|
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. Instead of "assertEquals" you should use "assertNull" here. |
||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output by giving the string an empty string value | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| assertEquals("", manipulatedstring.removeNthCharacter(4, true)); | ||
| } | ||
|
|
||
| @Test | ||
| //this test checks to see if the IllegalArgumentException is thrown | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hi Sara!"); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.removeNthCharacter(-1, true); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| //this class checks to see if the IndexOutOfBoundsException is thrown | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Hi Sara!"); | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.removeNthCharacter(100, true); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check for correct output | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("CS4420"); | ||
| assertEquals("CS420", manipulatedstring.removeNthCharacter(4, false)); | ||
| } | ||
|
|
||
|
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. One note on the getSubstrings() method: |
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| //this test check for correct output | ||
| public void testGetSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
|
|
@@ -88,29 +129,59 @@ public void testGeSubStrings1() { | |
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| //this test check for correct output | ||
| public void testGetSubStrings2() throws Exception{ | ||
| manipulatedstring.setString("I need some sleep"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 3); | ||
|
|
||
| assertEquals(sStings[0], "I"); | ||
| assertEquals(sStings[1], "need"); | ||
| assertEquals(sStings[2], "some"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| //this test checks to see if the IllegalArgumentException is thrown | ||
| public void testGetSubStrings3() throws Exception{ | ||
| manipulatedstring.setString("I need some sleep"); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(1, 0); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| //this test checks to see if the IllegalArgumentException is thrown | ||
| public void testGetSubStrings4() throws Exception{ | ||
| manipulatedstring.setString("I need some sleep"); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(0, 1); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| //this test checks to see if the IndexOutOfBoundsException is thrown | ||
| public void testGetSubStrings5() { | ||
| manipulatedstring.setString("I need some sleep"); | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.getSubStrings(5, 3); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| //this test checks to see if the IndexOutOfBoundsException is thrown | ||
| public void testGetSubStrings6() throws Exception{ | ||
| manipulatedstring.setString("I need some sleep"); | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.getSubStrings(3, 5); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| //this test check for correct output | ||
| public void testRestoreString1() { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
|
|
@@ -119,31 +190,51 @@ public void testRestoreString1() | |
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
| //this test check to see if the IndexOutOfBoundsException is thrown | ||
| public void testRestoreString2() { | ||
| manipulatedstring.setString("acro"); | ||
| int[] array; | ||
| array = new int[] {3, 2, -1, 0}; | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(array); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check to see if the IndexOutOfBoundsException is thrown | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("acro"); | ||
| int[] array; | ||
| array = new int[] {3, 2, 1, 4}; | ||
|
|
||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(array); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| //this test check to see if the IllegalArgumentException is thrown | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("acro"); | ||
| int[] array; | ||
| array = new int[] {3, 2, 1}; | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.restoreString(array); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| //this test check for correct output | ||
| public void testRestoreString5() { | ||
| manipulatedstring.setString("UnitTest"); | ||
| int [] array; | ||
| array=new int[]{4, 5, 6, 7, 0, 1, 2, 3}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "TestUnit"); | ||
| } | ||
|
|
||
| } | ||

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.
No issues with the build on TravisCI.