-
Notifications
You must be signed in to change notification settings - Fork 11
Added my files to the skeleton #4
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,32 +1,121 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| import java.util.ArrayList; | ||
| import java.util.*; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| public String stored; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return stored; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| stored = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if(stored == null || stored.isEmpty()) { | ||
| return 0; | ||
| } | ||
| String[] words = stored.split("\\s+"); | ||
| return words.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| //throw cases | ||
| if(n > stored.length()) { | ||
| throw new IndexOutOfBoundsException("IndexOB"); | ||
| } else if(n <= 0) { | ||
| throw new IllegalArgumentException("IllegalArg"); | ||
| } | ||
|
|
||
| String result; | ||
|
|
||
|
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 used StringBuilder which is an efficient way of handling string manipulations especially when dealing with loops. I think you can just use a StringBuilder object to avoid conversions from string to char array to arraylist and back. 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. Here is how you can do that: |
||
| char[] broken = stored.toCharArray(); | ||
| ArrayList<Character> letters = new ArrayList<Character>(); | ||
| for(char c : broken) { | ||
| letters.add(c); | ||
| } | ||
|
|
||
| for(int i = n - 1; i < letters.size(); i += n) { | ||
| if(maintainSpacing) { | ||
| letters.set(i, ' '); | ||
| } else { | ||
| letters.remove(i); | ||
| i -= 1; //to accomodate removing a letter | ||
| } | ||
| } | ||
|
|
||
| broken = new char[letters.size()]; | ||
| for(int i = 0; i < letters.size(); i++) { | ||
| broken[i] = letters.get(i); | ||
| } | ||
| result = new String(broken); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| //throw cases | ||
| if(startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IndexOutOfBoundsException("IndexOB"); | ||
| } | ||
|
|
||
| String[] words = stored.split("\\s+"); | ||
|
|
||
| if(words.length < endWord) { | ||
| throw new IllegalArgumentException("IllegalArg"); | ||
| } | ||
|
|
||
| String[] searchedWords; | ||
| boolean inSelection = false; | ||
| ArrayList<String> returns = new ArrayList<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. since you already validated your interval (startWord to endword), and split the string by space character, I think you can simply return a copy of "words" array. How about "return Arrays.copyOfRange(words, startWord - 1, endWord);?" Do you think this would work? |
||
| for(int i = 0; i < words.length; i++) { | ||
| if(i == startWord - 1) { | ||
| returns.add(words[i]); | ||
| inSelection = true; | ||
| } else if(i == endWord - 1) { | ||
| returns.add(words[i]); | ||
| inSelection = false; | ||
| } else if(inSelection) { | ||
| returns.add(words[i]); | ||
| } | ||
| } | ||
| //searchedWords = returns.toArray(); //this probably dont work? | ||
| searchedWords = new String[returns.size()]; | ||
| for(int i = 0; i < returns.size(); i++) { | ||
| searchedWords[i] = returns.get(i); | ||
| } | ||
|
|
||
| return searchedWords; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| public String restoreString(int[] indices){ | ||
|
|
||
| //throw cases | ||
| if(stored.length() != indices.length) { | ||
| throw new IllegalArgumentException("IllegalArg"); | ||
| } | ||
|
|
||
| //make a char array of the stored string | ||
| char[] newString = new char[indices.length]; | ||
| for(int i = 0; i < indices.length; i++) { | ||
| if(indices[i] < 0 || indices[i] > stored.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. indices[i] should be greater than or equal to stored.length() (indices[i] >= stored.length()), so that you can avoid indexoutofbound exception. |
||
| throw new IndexOutOfBoundsException("IndexOB"); | ||
| } | ||
| newString[i] = stored.charAt(indices[i]); | ||
| } | ||
|
|
||
| //turn newString into an actual string | ||
| String result = new String(newString); | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,63 +19,105 @@ public void tearDown() { | |
| manipulatedstring = null; | ||
| } | ||
|
|
||
| //basic test of length | ||
| @Test | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| //nothing stored | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| //only numbers | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("56 78 9"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(3, length); | ||
| } | ||
|
|
||
| //large amounts of spaces | ||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("this and that "); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(3, length); | ||
| } | ||
|
|
||
| //test of false bool | ||
| @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)); | ||
| } | ||
|
|
||
| //test of true bool | ||
| @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)); | ||
| } | ||
|
|
||
| //test of making the string empty | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("n"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
|
|
||
| manipulatedstring.setString(manipulatedstring.removeNthCharacter(1, false)); | ||
| length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| //test of removal making more words | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("anger"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
|
|
||
| manipulatedstring.setString(manipulatedstring.removeNthCharacter(3, true)); | ||
| length = manipulatedstring.count(); | ||
| assertEquals(2, length); | ||
| } | ||
|
|
||
| //removal of space, merging two words | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("an er"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(2, length); | ||
|
|
||
| manipulatedstring.setString(manipulatedstring.removeNthCharacter(3, false)); | ||
| length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| //IndexOB 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. Maybe you can give more information about what these cases are actually testing. 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, you could rename test cases so that they have descriptive names indicating what they are testing. |
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.setString("anger"); | ||
| manipulatedstring.removeNthCharacter(6, false); | ||
| }); | ||
| } | ||
|
|
||
| //IllegalArg test | ||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.setString("anger"); | ||
| manipulatedstring.removeNthCharacter(0, false); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -87,25 +129,56 @@ public void testGeSubStrings1() { | |
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| //test for single string selection | ||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Success"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 1); | ||
|
|
||
| assertEquals(sStings[0], "Success"); | ||
| } | ||
|
|
||
| //test for many strings | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string and I hope that if I make it a lot longer everything will work out"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 10); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| assertEquals(sStings[2], "and"); | ||
| assertEquals(sStings[3], "I"); | ||
| assertEquals(sStings[4], "hope"); | ||
| assertEquals(sStings[5], "that"); | ||
| assertEquals(sStings[6], "if"); | ||
| assertEquals(sStings[7], "I"); | ||
| } | ||
|
|
||
| //test for punctuation | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Success!"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 1); | ||
|
|
||
| assertEquals(sStings[0], "Success!"); | ||
| } | ||
|
|
||
| //IndexOB test | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.setString("bad 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. Maybe you could have more descriptive assert messages? |
||
| String [] sStings = manipulatedstring.getSubStrings(2, 5); | ||
| }); | ||
| } | ||
|
|
||
| //IllegalArg test | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.setString("bad test!"); | ||
| String [] sStings = manipulatedstring.getSubStrings(0, 0); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -118,31 +191,52 @@ public void testRestoreString1() | |
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| //maintains capitalization test | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rAt"); | ||
|
|
||
| } | ||
|
|
||
| //maintains spaces test | ||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("r u"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, " ru"); | ||
|
|
||
| } | ||
|
|
||
| //IllegalArg test | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.setString("r u"); | ||
| int [] array; | ||
| array=new int[]{1,0,2,2,1,0}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| }); | ||
| } | ||
|
|
||
| //IndexOB test | ||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.setString("r u"); | ||
| int [] array; | ||
| array=new int[]{1,0,6}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
|
|
||
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.
It is better to have fields as "private" since you already have getString and setString methods.