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
54 changes: 47 additions & 7 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
import java.util.Arrays;

public class StringManipulation implements StringManipulationInterface {

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

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

@Override
public int count() {
return 0;
int countWords = myString.split("\\w+").length;
if (myString.charAt(0) == ' ') countWords --;
return countWords;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (n > myString.length()) {
throw new IndexOutOfBoundsException ("n is greater than the string length");
}
if (n <= 0) {
throw new IllegalArgumentException("n can not be less than or equal to zero");
}
String str = "";
for (int i = 0; i < myString.length(); i++) {
if (i%n != n-1) {
str += myString.charAt(i);
} else {
if (maintainSpacing) str += " ";
}
}
return str;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if (startWord <= 0 || endWord <= 0 || startWord > endWord) throw new IllegalArgumentException("invalid input for index");

Choose a reason for hiding this comment

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

I also did write the same things as you before I get a reviewer to say that it would be better for user if you can make the case startWord > endWord with a different if case so that you can throw a more specific error for that case.

if (count() < startWord || count() < endWord) throw new IndexOutOfBoundsException("index is out of bound");
String[] words = myString.split("\\s");
String[] substring;
substring = new String[endWord-startWord+1];
int cnt = 0;
for (int i = startWord-1; i < endWord; i++) {
substring[cnt] = words[i];
cnt++;
}
return substring;
}

@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices){
if (myString.length() != indices.length) throw new IllegalArgumentException("index values does not match the string");
String str = "";
for (int i = 0; i < indices.length; i++) {
if (indices[i] < 0 || indices[i] >= myString.length()) throw new IndexOutOfBoundsException("Invalid index input");
for (int j = 0; j < indices.length; j++) {
if (indices[j] == i) {
str += myString.charAt(j);
}
}
}
return str;
}


Expand Down
159 changes: 138 additions & 21 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,124 @@ public void tearDown() {
manipulatedstring = null;
}

// This test checks basic functionality for the count()
@Test
public void testCount1() {
manipulatedstring.setString("This is my string");
int length = manipulatedstring.count();
assertEquals(4, length);
}

// This test checks whether method can handle variation
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("String test with a word");
int length = manipulatedstring.count();
assertEquals(5, length);
}

// This test checks whether method can handle numbers
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("String test with a number 1");
int length = manipulatedstring.count();
assertEquals(6, length);
}

// This test checks whether space at the front and multiple spaces affects functionality
@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString(" String test with spaces ");
int length = manipulatedstring.count();
assertEquals(4, length);
}

// This test checks extreme cases with different spacings and multiple numbers
@Test
public void testCount5() {
manipulatedstring.setString(" String tests with numbers 1234 ");
int length = manipulatedstring.count();
assertEquals(5, length);
}

// This test checks empty string with spaces
@Test
public void testCount6() {
manipulatedstring.setString(" ");
int length = manipulatedstring.count();
assertEquals(0, length);
}

// This test checks one character
@Test
public void testCount7() {
manipulatedstring.setString(" a ");
int length = manipulatedstring.count();
assertEquals(1, length);
}

// This test checks the basic functionality
@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));
}

// This test checks the manipulatedstring
@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));
}

// This test checks different variable for n
@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("Testing to see the remove Nth Character");
assertEquals("Tsigt e h eoeNhCaatr", manipulatedstring.removeNthCharacter(2, false));
}

// This test checks whether method removeNthCharacter() suitably throws an IllegalArgumentException if n is less than or equal to zero
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("Testing to see the remove Nth Character");
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(0, false);
}, "n can not be less than or equal to zero");
}

// This test checks whether method removeNthCharacter() suitably throws an IllegalArgumentException if n is less than or equal to zero when maintainspacing is true
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("Testing to see the remove Nth Character");
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(0, true);
}, "n can not be less than or equal to zero");
}

// This test checks whether method removeNthCharacter() suitably throws an IndexOutOfBoundsException if n is greater than the string length
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("The length of this string is 31");
IndexOutOfBoundsException thrown =
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.removeNthCharacter(32, false);
}, "n is greater than the string length");
}

// This test checks whether method removeNthCharacter() suitably throws an IndexOutOfBoundsException if n is greater than the string length when maintainspacing is true
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("The length of this string is 31");
IndexOutOfBoundsException thrown =
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.removeNthCharacter(32, true);
}, "n is greater than the string length");
}

// This test checks the basic functionality of the method
@Test
public void testGeSubStrings1() {
manipulatedstring.setString("This is my string");
Expand All @@ -87,27 +146,60 @@ public void testGeSubStrings1() {
assertEquals(sStings[1], "string");
}

// This test checks the basic functionality of the method with different range
@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string with more words");
String [] sStings = manipulatedstring.getSubStrings(2, 6);

assertEquals(sStings[0], "is");
assertEquals(sStings[1], "my");
assertEquals(sStings[2], "string");
assertEquals(sStings[3], "with");
assertEquals(sStings[4], "more");
}

// This test checks whether method getSubStrings() suitably throws an IndexOutOfBoundsException when index is out of bound
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("This string contains 5 words");
IndexOutOfBoundsException thrown =
assertThrows(IndexOutOfBoundsException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(1, 6);
}, "index is out of bound");
}

// This test checks whether method getSubStrings() suitably throws an IndexOutOfBoundsException when index is out of bound
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("This string contains 5 words");
IndexOutOfBoundsException thrown =
assertThrows(IndexOutOfBoundsException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(6, 7);
}, "index is out of bound");
}

// This test checks whether method getSubStrings() suitably throws an IllegalArgumentException when invalid input for index
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("This string contains 5 words");
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(0, 4);
}, "invalid input for index");
}

// This test checks whether method getSubStrings() suitably throws an IllegalArgumentException when startWord value is larger then endWord
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("This string contains 5 words");
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
String [] sStings = manipulatedstring.getSubStrings(3, 2);
}, "invalid input for index");
}

// This test checks the basic functionality of the method
@Test
public void testRestoreString1()
{
Expand All @@ -118,32 +210,57 @@ public void testRestoreString1()
assertEquals(restoreString, "rat");
}

// This test checks the given example
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

manipulatedstring.setString("UnitTest");
int[] indicis = {4,5,6,7,0,2,1,3};
assertEquals("TsetUnit", manipulatedstring.restoreString(indicis));
}

// This test checks different arrangement
@Test
public void testRestoreString3()
{
fail("Not yet implemented");

manipulatedstring.setString("Shuffle Up");
int[] indicis = {1,6,2,0,5,7,3,4,8,9};
assertEquals("fSue fhlUp", manipulatedstring.restoreString(indicis));
}

// This test checks whether method restoreString() suitably throws an IllegalArgumentException when index values does not match the string
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

manipulatedstring.setString("The string length is 23");
int[] indicis = {0,1,2,3};
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
String Sting = manipulatedstring.restoreString(indicis);
}, "index values does not match the string");
}

// This test checks whether method restoreString() suitably throws an IllegalArgumentException when index values does not match the string
@Test
public void testRestoreString5()
{
fail("Not yet implemented");

manipulatedstring.setString("The string length is 23");
int[] indicis = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> {
String Sting = manipulatedstring.restoreString(indicis);
}, "index values does not match the string");
}

// This test checks whether method restoreString() suitably throws an IndexOutOfBoundsException when invalid index input
@Test
public void testRestoreString6()
{
manipulatedstring.setString("TEST");
int[] indicis = {0,-1, 2, 3};
IndexOutOfBoundsException thrown =
assertThrows(IndexOutOfBoundsException.class, () -> {
String Sting = manipulatedstring.restoreString(indicis);
}, "invalid index input");
}
}