-
Notifications
You must be signed in to change notification settings - Fork 6
Add previous implementation #25
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,79 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| private String myString = ""; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return myString; | ||
| } | ||
|
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's good to see the getString() method implemented, which returns the value of myString. It allows users to retrieve the current string. |
||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| myString = 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. Method effectively sets the value of myString to the provided string. |
||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| String[] spl = myString.split("\\s+"); | ||
| int i = 0; | ||
| for (String str : spl) { | ||
| if (!str.equals("")) { | ||
| i++; | ||
| } | ||
| } | ||
| return 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. Have you considered using the enhanced for-loop syntax for the count() method to improve readability? For example, To make the code even more readable, could you add a comment explaining what the purpose of the count() method is? |
||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (n > myString.length()) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } else if (n <= 0) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| char[] ar = myString.toCharArray(); | ||
| StringBuilder ret = new StringBuilder(); | ||
| for (int i = 1; i <= myString.length(); i++) { | ||
| if (i != 0 && i % n == 0) { | ||
| if (maintainSpacing) { | ||
| ret.append(" "); | ||
| } | ||
| } else { | ||
| ret.append(ar[i - 1]); | ||
| } | ||
| } | ||
| return ret.toString(); | ||
| } | ||
|
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 removeNthCharacter(int n, boolean maintainSpacing) method looks good. Once again have you consider commenting detailed documentation so that your code is easy to modify. Remember in the verification and validation phase of testing that we have to find bugs, ensure readability. Consider documenting everything. |
||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| if (startWord <= 0 || endWord <= 0 || endWord < startWord) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| String[] ret = new String[endWord - startWord + 1]; | ||
| String[] spl = myString.split("\\s+"); | ||
| if (spl.length < endWord) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| for (int i = 0; i < ret.length; i++) { | ||
| ret[i] = spl[startWord - 1 + i]; | ||
| } | ||
| return ret; | ||
| } | ||
|
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 validating input parameters in the getSubStrings(int startWord, int endWord) method. to ensure they met necessary conditions. |
||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| public String restoreString(int[] indices){ | ||
| if (indices.length != myString.length()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| StringBuilder ret = new StringBuilder(); | ||
| char[] ar = myString.toCharArray(); | ||
| for (int i = 0; i < indices.length; i++) { | ||
| if (indices[i] < 0 || indices[i] >= ar.length) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| ret.append(ar[indices[i]]); | ||
| } | ||
| return ret.toString(); | ||
| } | ||
|
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. Excellent job validating the indices array in the restoreString(int[] indices) method to ensure it has the same length as myString. |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.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. Great job on importing necessary libraries for your tests. It's clear that you've thought about what you need. |
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
@@ -19,65 +18,109 @@ public void tearDown() { | |
| manipulatedstring = null; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a simple string of space-separated words is counted correctly. | ||
| */ | ||
| @Test | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| 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. Good job on testing for an empty string in testCount2. This is a good edge case to consider. |
||
|
|
||
| /** | ||
| * Check whether an empty string is correctly counted as zero. | ||
| */ | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| assertEquals(0, manipulatedstring.count()); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a single letter string is correctly counted as one. | ||
| */ | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("A"); | ||
| assertEquals(1, manipulatedstring.count()); | ||
| } | ||
|
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 are testing a single character string. This is another good edge case. Have you considered testing a string with only one space. |
||
|
|
||
| /** | ||
| * Check whether a string of multiple spaces is correctly counted as zero. | ||
| */ | ||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(" "); | ||
| assertEquals(0, manipulatedstring.count()); | ||
| } | ||
|
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 do you think would happen if the string contained tabs or newline characters? |
||
|
|
||
| /** | ||
| * Check whether every third character is correctly removed (not maintaining spacing) in a simple scenario. | ||
| */ | ||
| @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)); | ||
| } | ||
|
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. testing the removal of the third character is a good test case. |
||
|
|
||
| /** | ||
| * Check whether every third character is correctly replaced with spaces in a simple scenario. | ||
| */ | ||
| @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)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an out of bounds exception is correctly thrown when n is over the string length. | ||
| */ | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("A b c d e"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(10, false)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown when n is negative. | ||
| */ | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("A b c d e"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether whitespace is removed correctly too. | ||
| */ | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("A\nb\tc\nd\te\n"); | ||
| assertEquals("Abcde", manipulatedstring.removeNthCharacter(2, false)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a single character is correctly replaced with a single space if n is one and spacing is maintained. | ||
| */ | ||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("a"); | ||
| assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown if n is zero AND the string is empty. | ||
| */ | ||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a simple string of words is correctly turned into substrings. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
|
|
@@ -87,27 +130,66 @@ public void testGeSubStrings1() { | |
| assertEquals(sStings[1], "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. you're testing the splitting of a string into substrings. This is a good test case. Have you considered what might happen if the string contains multiple spaces between words? |
||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown when one of the string indices is zero. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("a b c d"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 1)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown when the second index is lower than the first. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("a b c d"); | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 1)); | ||
| } | ||
| /** | ||
| * Check whether an out of bounds exception is correctly thrown when the right index is higher than the number of | ||
| * words. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("a b c d"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(2, 5)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a single string is correctly returned when the indices are the same and there is only | ||
| * one word. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("abc"); | ||
| assertEquals("abc", manipulatedstring.getSubStrings(1, 1)[0]); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an out of bounds exception is correctly thrown for a string of whitespace with nonzero indices. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(" "); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 1)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a simple string with words separated by multiple spaces is correctly converted to substrings. | ||
| */ | ||
| @Test | ||
| public void testGeSubStrings7() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "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. This is a good test case. Have you considered what might happen if the string contains other types of whitespace characters, like tabs or newlines? |
||
| /** | ||
| * Check whether a simple string can be rearranged correctly. | ||
| */ | ||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
|
|
@@ -118,32 +200,51 @@ public void testRestoreString1() | |
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a simple string can be correctly rearranged into all the same letter (this wasn't part of the spec | ||
| * but I am making the executive decision to include it). | ||
| */ | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("abc"); | ||
| int [] array = new int[]{2,2,2}; | ||
| assertEquals(manipulatedstring.restoreString(array), "ccc"); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown when the number of indices doesn't match | ||
| * the number of letters in the string. | ||
| */ | ||
| @Test | ||
| public void testRestoreString3() | ||
|
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, you're testing if your method can deal with a mismatch in the number of indices and the number of characters in the string. It's like trying to fit a square peg in a round hole - it's not gonna work, and your method should recognize that. Good on you for checking this! |
||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("abcd"); | ||
| int [] array = new int[]{1,2,3}; | ||
| assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether an illegal argument exception is correctly thrown when one of the indices is higher than the | ||
| * string length. | ||
| */ | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("abc"); | ||
| int [] array = new int[]{1,2,3}; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a string with non-alphanumeric characters can be successfully rearranged. (Also not part | ||
| * of the spec, but my implementation does it anyway and I wanted to test it.) | ||
| */ | ||
| @Test | ||
| public void testRestoreString5() | ||
|
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. And this one, you're throwing in non-alphanumeric characters into the mix. Real-world strings can be messy and full of all sorts of characters, so it's awesome that you're making sure your method can handle that |
||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("A 1"); | ||
| int [] array = new int[]{2,0,2}; | ||
| assertEquals(manipulatedstring.restoreString(array), "1A1"); | ||
| } | ||
|
|
||
| } | ||
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.
Great job creating the StringManipulation class and implementing the StringManipulation Interface.