-
Notifications
You must be signed in to change notification settings - Fork 6
updated the skeleton project with my complete project. #20
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,125 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| private String str; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| } | ||
| @Override | ||
| public String getString() { | ||
| return this.str; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| } | ||
| @Override | ||
| public void setString(String string) { | ||
| this.str = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| } | ||
| @Override | ||
| public int count() { | ||
| if (str == null || str.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| String[] splitStr = str.split("\\s+"); | ||
| return splitStr.length; | ||
| /* | ||
| * int wordCount = 0; | ||
| boolean whitespace = false; | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| } | ||
| // Iterate through each character in the string | ||
| for (int i = 0; i < str.length(); i++) { | ||
| char c = str.charAt(i); | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| } | ||
| // Check if the character is a whitespace | ||
| if (Character.isWhitespace(c)) { | ||
| whitespace = true; | ||
|
|
||
| // Check if the character is a letter or digit | ||
| if (Character.isLetterOrDigit(str.charAt(i+1))) { | ||
| // If we are not inside a word, increment the word count | ||
| if (whitespace) { | ||
| wordCount++; | ||
| whitespace = false; | ||
| } | ||
| }*/ | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("'n' must be greater than zero."); | ||
| } | ||
|
|
||
| if (n > str.length()) { | ||
| throw new IndexOutOfBoundsException("'n' is greater than the string length."); | ||
| } | ||
|
|
||
| String result = new String(); | ||
| result = result.concat(str.charAt(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. This portion of the for loop in the removeNthCharacter() method seems to have some redundant code. Is there an advantage to starting your for loop at index = 1? |
||
| for (int i = 1; i < str.length(); i++) { | ||
| if((i+1) % n != 0) { | ||
| result = result.concat(str.charAt(i) + ""); | ||
|
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 is personal preference but I like using += when concatenating strings together. Personally I think that nesting methods inside one another makes code harder to read. |
||
| } | ||
| else { | ||
| if (maintainSpacing) { | ||
| result = result.concat(" "); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| // Check for invalid arguments | ||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IllegalArgumentException("Invalid startWord or endWord"); | ||
| } | ||
|
|
||
| // Split the sentence into words | ||
| String[] words = str.split("\\s+"); | ||
|
|
||
| // Check if the sentence has enough words | ||
| if (endWord > words.length) { | ||
| throw new IndexOutOfBoundsException("The String has less than " + endWord + " words"); | ||
| } | ||
|
|
||
| // Calculate the number of words to put in the array | ||
| int wordCount = endWord - startWord + 1; | ||
|
|
||
| // Create a new array to store the extracted words | ||
| String[] subStrArr = new String[wordCount]; | ||
|
|
||
| // Extract the words from the specified positions | ||
| for (int i = 0; i < wordCount; i++) { | ||
| subStrArr[i] = words[startWord - 1 + i]; | ||
| } | ||
|
|
||
| return subStrArr; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| //throws IllegalArgumentException if not s.length == indices.length == n | ||
| // throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length | ||
| if(str.length() != indices.length) { | ||
| throw new IllegalArgumentException("The indices array must be the same length as the string."); | ||
| } | ||
| char[] temp = str.toCharArray(); | ||
| char[] shuffled = new char[str.length()]; | ||
| String output = new String(); | ||
| for(int i = 0; i < str.length(); i++) { | ||
| if(i < indices.length) { | ||
| int pos = indices[i]; | ||
| if(pos < 0 || pos > str.length()) { | ||
| throw new IndexOutOfBoundsException("All values in the indices array must correspond to valid indexes in the string."); | ||
| } | ||
| shuffled[pos] = temp[i]; | ||
| } | ||
| else{ | ||
| shuffled[i] = temp[i]; | ||
| } | ||
| } | ||
| output = String.valueOf(shuffled); | ||
| return output; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,58 +28,89 @@ public void testCount1() { | |
|
|
||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Thisismystring"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("23 and me"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(3, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| public void testCount4() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount5() { | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, 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)); | ||
| String temp = manipulatedstring.removeNthCharacter(3, false); | ||
| assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", temp); | ||
| } | ||
|
|
||
| @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)); | ||
| String temp = manipulatedstring.removeNthCharacter(3, true); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", temp); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| String temp = manipulatedstring.removeNthCharacter(5, false); | ||
|
|
||
| assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", temp); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I'd 3tt3 put s0me d161 s in this 5tr1 6, r ght?", manipulatedstring.removeNthCharacter(5, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, 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. I believe you can use the assertThrows() method without having to set it equal to a Throwable object. The assertThrow() method checks to see whether the method you provided in the lambda function returns the same Throwable as the Throwable you claim it will throw. Your way of checking definitely works. |
||
| assertEquals("'n' is greater than the string length.", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); | ||
| assertEquals("'n' must be greater than zero.", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); | ||
| assertEquals("'n' must be greater than zero.", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| public void testRemoveNthCharacter8() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I'd b3tt3r put s0me d161ts in this 5tr1n6, right", manipulatedstring.removeNthCharacter(49, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
|
|
@@ -88,24 +119,49 @@ public void testGeSubStrings1() { | |
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings2() { | ||
| manipulatedstring.setString("Thisismystring"); | ||
| Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(3, 4)); | ||
| assertEquals("The String has less than 4 words", exception.getMessage()); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings3() { | ||
| manipulatedstring.setString(""); | ||
| Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(1, 4)); | ||
| assertEquals("The String has less than 4 words", exception.getMessage()); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings4() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 3); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings5() { | ||
| manipulatedstring.setString("This is my string"); | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1)); | ||
| assertEquals("Invalid startWord or endWord", exception.getMessage()); | ||
| } | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| public void testGetSubStrings6() { | ||
| manipulatedstring.setString("This is my string"); | ||
| Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); | ||
| assertEquals("The String has less than 100 words", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSubStrings7() { | ||
| manipulatedstring.setString("This is my string"); | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 3)); | ||
| assertEquals("Invalid startWord or endWord", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSubStrings8() { | ||
| manipulatedstring.setString("This is my string"); | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(1, -3)); | ||
| assertEquals("Invalid startWord or endWord", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -121,28 +177,44 @@ public void testRestoreString1() | |
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("This is my string"); | ||
|
|
||
| int [] array; | ||
| array=new int[]{1,0,2,5,3,4,7,6}; | ||
| Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array) ); | ||
| assertEquals("The indices array must be the same length as the string.", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("This is my string"); | ||
| int [] array; | ||
| array=new int[]{-1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,14,11}; | ||
| Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); | ||
| assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| int [] array; | ||
| array=new int[]{1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,100,11}; | ||
| Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); | ||
| assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("second test "); | ||
| int [] array; | ||
| array=new int[]{11,10,9,8,7,6,5,4,3,2,1,0}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, " tset dnoces"); | ||
|
|
||
| } | ||
|
|
||
|
|
||
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.
I really like how you used the split method with regex to get the appropriate count of words. I will definitely be implementing this into my own code. Thank you