-
Notifications
You must be signed in to change notification settings - Fork 6
Update skeleton #3
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,89 @@ | ||
| ///////////////////////////////////////////////////////// | ||
| // | ||
| // Class: StringManipulation | ||
| // Author: Caleb Rector | ||
| // Date: 6/11/2023 | ||
| // | ||
| // Description: Implements methods in StringManipulationInterface | ||
| // | ||
| ///////////////////////////////////////////////////////// | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String s; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return s; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| s = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if(s.length() == 0) | ||
| return 0; | ||
|
|
||
| String[] arr = s.split("[\\s\\n]"); | ||
| return arr.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
|
|
||
| if (n > s.length()) | ||
| throw new IndexOutOfBoundsException(); | ||
| if (n <= 0) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| String s2 = ""; | ||
| int count = 1; | ||
| for (int i = 0; i < s.length(); i++) { | ||
| if (count == n) { | ||
| if (maintainSpacing) | ||
| s2 += " "; | ||
| count = 1; | ||
| } else { | ||
| s2 += s.charAt(i); | ||
| count++; | ||
| } | ||
| } | ||
| return s2; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
|
|
||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| String[] arr = s.split("[\\s\\n]"); | ||
| if (arr.length < endWord) | ||
| throw new IndexOutOfBoundsException(); | ||
|
|
||
| String[] ret = new String[endWord - startWord + 1]; | ||
| for (int i = 0; i < ret.length; i++) | ||
| ret[i] = arr[i + startWord - 1]; | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
|
|
||
| if (indices.length != s.length()) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| String ret = ""; | ||
| for (int i = 0; i < s.length(); i++) { | ||
| if (indices[i] >= s.length() || indices[i] < 0) | ||
| throw new IndexOutOfBoundsException(); | ||
| ret += s.charAt(indices[i]); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| ///////////////////////////////////////////////////////// | ||
| // | ||
| // Class: StringManipulationTest | ||
| // Author: Caleb Rector | ||
| // Date: 6/11/2023 | ||
| // | ||
| // Description: Provides test cases for methods found in StringManipulation class | ||
| // | ||
| ///////////////////////////////////////////////////////// | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
@@ -26,19 +35,28 @@ public void testCount1() { | |
| assertEquals(4, length); | ||
| } | ||
|
|
||
| // Tests single word string | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("OneWord"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| // Tests number of words in empty string | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| // Tests number of words in string with newline characters | ||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Including\nsome\nnewline\ncharacters"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, 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. Have you considered adding a test case that checks the null string case? |
||
| @Test | ||
|
|
@@ -50,100 +68,194 @@ public void testRemoveNthCharacter1() { | |
| @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)); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", | ||
| manipulatedstring.removeNthCharacter(3, true)); | ||
| } | ||
|
|
||
| // Tests string with newline characters | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Seeing what happens when\nnewline characters are introduced"); // Newline character counts as character | ||
| assertEquals("See ng hat hap ens whe \nne lin ch rac ers are int odu ed", manipulatedstring.removeNthCharacter(4, true)); | ||
| } | ||
|
|
||
| // Tests large value for n | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("What happens when n is a much larger value?"); | ||
| assertEquals("What happns when nis a muchlarger vaue?", manipulatedstring.removeNthCharacter(10, false)); | ||
| } | ||
|
|
||
| // Tests n = 1 (all characters should be replaced with a space) | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("What happens if n is 1?"); | ||
| assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); | ||
| } | ||
|
|
||
| // Tests whether method throws error when n = 0 | ||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("An exception should be thrown if n = 0"); | ||
| try { | ||
| manipulatedstring.removeNthCharacter(0, false); | ||
| fail("No exception thrown for n=0"); | ||
| } catch (IllegalArgumentException e) { | ||
| assert true; | ||
| } | ||
| } | ||
|
|
||
| // Tests whether method throws error when n > length | ||
| @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. I shared a link on line 160 that talks about using Lamba expressions when dealing with exceptions in JUnit. |
||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("An exception should ALSO be thrown if n > length"); | ||
| try { | ||
| manipulatedstring.removeNthCharacter(100, false); | ||
| fail("No exception thrown for n > length"); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| assert true; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
| String[] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Lets see what happens when we pass in a string with lots and lots of words"); | ||
| String[] substrings = manipulatedstring.getSubStrings(4, 10); | ||
| assertEquals(substrings[0], "happens"); | ||
| assertEquals(substrings[3], "pass"); | ||
| assertEquals(substrings[6], "string"); | ||
| } | ||
|
|
||
| // Tests a string with newline characters | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Trying out some\nnewline characters"); | ||
| String[] subStrings = manipulatedstring.getSubStrings(3, 4); | ||
| assertEquals(subStrings[0], "some"); | ||
| assertEquals(subStrings[1], "newline"); | ||
| } | ||
|
|
||
| // Tests a string with numbers and symbols | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring | ||
| .setString("Special characters! Punctuation, symbols !@#$ and numbers 91283... Will it matter?"); | ||
| String[] substrings = manipulatedstring.getSubStrings(2, 10); | ||
| assertEquals(substrings[0], "characters!"); | ||
| assertEquals(substrings[3], "!@#$"); | ||
| assertEquals(substrings[6], "91283..."); | ||
| } | ||
|
|
||
| // Test whether exceptions are thrown when startWord > endWord or when one of the variables is < 0 | ||
| @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. In Java 8+, you can use Lamba expressions with JUnit to handle exceptions. Here's a resource that shows how this is done: https://blog.codeleak.pl/2014/07/junit-testing-exception-with-java-8-and-lambda-expressions.html
Author
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. Thanks! I've been meaning to work more with lambda expressions. I'll be sure to check out your implementation as well. |
||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("SURELY nothing will go wrong if startWord is greater than endWord..."); | ||
|
|
||
| try { | ||
| manipulatedstring.getSubStrings(6, 2); | ||
| fail("No exception thrown"); | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
|
|
||
| try { | ||
| manipulatedstring.getSubStrings(-1, 2); | ||
| fail("No exception thrown"); | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
|
|
||
| try { | ||
| manipulatedstring.getSubStrings(0, -3); | ||
| fail("No exception thrown"); | ||
| } catch (IllegalArgumentException e) { | ||
| } | ||
| } | ||
|
|
||
| // Tests whether an exception is thrown when endWord is out of bounds | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
|
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. See line 160 comment |
||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Last test. Out of bounds?"); | ||
| try { | ||
| manipulatedstring.getSubStrings(0, 10); | ||
| fail("Out of bounds exception not thrown"); | ||
| } catch (IllegalArgumentException e) { | ||
| assert true; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| public void testRestoreString1() { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| int[] array; | ||
| array = new int[] { 1, 0, 2 }; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| // Tests a longer string | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString2() { | ||
| manipulatedstring.setString("calebrector"); | ||
| int[] array = new int[] {0, 5, 1, 4, 3, 2, 6, 7, 8, 9, 10}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "crabelector"); | ||
|
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. Lol, crabelector |
||
| } | ||
|
|
||
| // Tests another longer string | ||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString3() { | ||
| manipulatedstring.setString("clinteAstWOod"); | ||
| int[] array = new int[] {10, 1, 12, 9, 5, 7, 8, 6, 0, 8, 2, 11, 3}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "OldWestAction"); | ||
| } | ||
|
|
||
| // Tests whether capitalization is preserved | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString4() { | ||
| manipulatedstring.setString("CAPStest"); | ||
| int[] array = new int[] {4, 5, 6, 7, 0, 1, 2, 3}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "testCAPS"); | ||
| } | ||
|
|
||
| // Tests whether exceptions are thrown when an index is negative or out of bounds | ||
| @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. See line 160 comment. It would help to split this test case into several smaller test cases. |
||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
| public void testRestoreString5() { | ||
| manipulatedstring.setString("Exceptions"); | ||
|
|
||
| try | ||
| { | ||
| int[] array = new int[] {0, 2}; | ||
| manipulatedstring.restoreString(array); | ||
| fail("No exception thrown"); | ||
| } | ||
| catch (IllegalArgumentException e) {} | ||
|
|
||
| try | ||
| { | ||
| int[] array = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, -10}; | ||
| manipulatedstring.restoreString(array); | ||
| fail("No exception thrown"); | ||
| } | ||
| catch (IndexOutOfBoundsException e) {} | ||
| try | ||
| { | ||
| int[] array = new int[] {0, 1, 2, 3, 400, 5, 6, 7, 8, 9}; | ||
| manipulatedstring.restoreString(array); | ||
| fail("No exception thrown"); | ||
| } | ||
| catch (IndexOutOfBoundsException e) {} | ||
|
|
||
| assert true; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
This can be simplified as: String[] arr = s.split("\\s+")
There should be a '+' in case there are multiple spaces in between words