Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,87 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class StringManipulation implements StringManipulationInterface {

String s;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 8, the data field String s should be private. The business logic of the code needs to incorporate encapsulation to protect from external access.

@Override
public String getString() {
return null;
return s;
}

@Override
public void setString(String string) {
s = string;
}

@Override
public int count() {
return 0;
if (s == null || s.isEmpty())
return 0;
return s.split("\\s+").length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n <= 0) {
throw new IllegalArgumentException();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be helpful to provide am message pertaining to the exact reason illegalArgumentException is called. This may help clarify during testing to save some time.

}
if (n > s.length()) {
throw new IndexOutOfBoundsException();
}
StringBuilder result = new StringBuilder();
if(maintainSpacing){
result.append(s);
for (int i = n - 1; i < s.length(); i += n) {
result.setCharAt(i, ' ');
}
} else {
int prev = 0;
for (int i = n - 1; i < s.length(); i += n) {
result.append(s, prev, i);
prev = i + 1;
}
result.append(s, prev, s.length());
}
return result.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if(startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException();
}
String[] words = s.split("\\s+");
if(words.length < endWord) {
throw new IndexOutOfBoundsException();
}
return Arrays.copyOfRange(words, startWord - 1, endWord);
}

@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices){
if(indices.length != s.length() || duplicates(indices)){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilizing a set here to check for duplicates is nice way and optimal way to compare for duplicates. You helped me realize that I might not have done this for my own method. Well done.

throw new IllegalArgumentException();
}
char[] ch = new char[s.length()];
for(int i = 0; i < s.length(); i++) {
ch[indices[i]] = s.charAt(i);
}
return String.valueOf(ch);
}

private boolean duplicates(int[] ints)
{
Set<Integer> DT = new HashSet<>();
for (int i : ints)
{
if (DT.contains(i))
return true;
DT.add(i);
}
return false;
}

}
111 changes: 81 additions & 30 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,28 @@ public void testCount1() {
assertEquals(4, length);
}

//This test to check abnormal use of spaces when parsing for words.
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString(" ");
int length = manipulatedstring.count();
assertEquals(0, length);
}

//This test to check abnormal use of spaces when parsing for words.
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("JustOneWord");
int length = manipulatedstring.count();
assertEquals(1, length);
}

//This test handles a protential null argument.
@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString(null);
int length = manipulatedstring.count();
assertEquals(0, length);
}

@Test
Expand All @@ -53,59 +62,88 @@ public void testRemoveNthCharacter2() {
assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true));
}

//This test checks n = 1 case maintains spacing.
@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals(" ", manipulatedstring.removeNthCharacter(1, true));
}

//This test checks n = 1 case removes all spacing.
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("", manipulatedstring.removeNthCharacter(1, false));
}

//This test check that an Indexoutofbounds exception is thrown appropriately.
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(50, false));
}

//This test check that an IllegalArgumentException exception is thrown appropriately.
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false));
}

//This test check that an IllegalArgumentException exception is thrown appropriately.
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false));
}

@Test
public void testGeSubStrings1() {
public void testGetSubStrings1() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 4);
String [] sStrings = manipulatedstring.getSubStrings(3, 4);

assertEquals(sStings[0], "my");
assertEquals(sStings[1], "string");
assertEquals(sStrings[0], "my");
assertEquals(sStrings[1], "string");
}

//This test handles larger given strings
@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
public void testGetSubStrings2() {
manipulatedstring.setString("The Greatest String In The Universe");
String [] sStrings = manipulatedstring.getSubStrings(3, 6);

assertEquals(sStrings[0], "String");
assertEquals(sStrings[1], "In");
assertEquals(sStrings[2], "The");
assertEquals(sStrings[3], "Universe");
}

//This test handles IllegalArgumentException for improper start and end values
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(3, 2));
}

//This test handles IllegalArgumentException for improper start and end values
@Test

@will-eand will-eand Jun 14, 2023

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is redundant. The test above tests the exact same thing as below. you're Testing the situation where the startWord is greater than the endWord, but there is also the possibility the start word and end words are the same, OR that the parameter inputs a negative number on accident. These are other cases you can test.

public void testGeSubStrings4() {
fail("Not yet implemented");
public void testGetSubStrings4() {
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 0));
}

//This test handles IllegalArgumentException for improper start and end values
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testGetSubStrings5() {
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 3));
}

//This test handles IndexOutOfBoundsException for end values
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testGetSubStrings6() {
manipulatedstring.setString("This is my string");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(3, 5));
}

@Test
Expand All @@ -118,32 +156,45 @@ public void testRestoreString1()
assertEquals(restoreString, "rat");
}

//This test handles empty input values
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

manipulatedstring.setString("");
int [] array;
array=new int[]{};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "");
}

//This test handles duplicate values in a given index
@Test
public void testRestoreString3()
{
fail("Not yet implemented");

manipulatedstring.setString("arty");
int [] array;
array=new int[]{1,0,1,2};
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array));
}

//This test handles illegal values in the given index
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

manipulatedstring.setString("arty");
int [] array;
array=new int[]{-1,0,1,2};
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array));
}

//This test handles cases where s.length != indices.length
@Test
public void testRestoreString5()
{
fail("Not yet implemented");

manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2,3};
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code handles illegalArgumentException in two situations; the first is when there are duplicates, and the second when the size of the array is either too big or too small relative to the length of the string. I see you testing the duplicates well, but what about different length of arrays being inputted? Another testRestoreString6 could be here.

}