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
101 changes: 78 additions & 23 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,88 @@
public class StringManipulation implements StringManipulationInterface {
@Override

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You need to declare the string member of the StringManipulation class, since it isn't inherited from the interface.

public String getString() {
if (this.string != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Considering corner cases is good, but here the check for this.string is redundant and can be removed -- if this.string is null, then returning this.string will return null anyway (hence the check is unnecessary).

return this.string;
}

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

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

@Override
public int count() {
return 0;
}
@Override
public int count() {
String words = this.string.trim();
if (!words.isEmpty()) {
return words.split("\\s+").length;
}
return 0;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
}
@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The specification requires removing every n'th character (n, 2n, 3n, etc until end of string), this just removes the first one. Some ideas for this are recursive calls on removeNthCharacter with different n values, or a loop backwards, to make the multiple-of-n in case of removing characters instead of substituting them easier.

char letter = this.string.charAt(n);
if (maintainSpacing)
{
this.string = this.string.replace(letter, " ");
return this.string;
}
else
{
this.string = this.string.replace(letter, "");
return this.string;
}
return null;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
}
@Override
public String[] getSubStrings(int startWord, int endWord) {
String[] words = this.string.split(" ");

@Override
public String restoreString(int[] indices) {
return null;
}
if (startWord <= 0 || endWord <= 0 || startWord > endWord)
{
throw new IllegalArgumentException("Invalid startWord or endWord.");
}

if (endWord > words.length)
{
throw new IndexOutOfBoundsException("The string has less than endWord words.");
}

int numSubStrings = endWord - startWord + 1;
String[] subStrings = new String[numSubStrings];

for (int i = startWord - 1; i <= endWord - 1; i++)
{
subStrings[i - startWord + 1] = words[i];
}

return subStrings;
}

@Override
public String restoreString(int[] indices) {
if (this.string.length != indices.length)
{
throw new IllegalArgumentException("Indices lengths do not match");
}

char[] shuffledWords = new char[indices.length];

for (int i = 0; i < indices.length; i++)
{
int index = indices[i];
if (index < 0 || index >= indices.length)
{
throw new IndexOutOfBoundsException("Invalid index: " + index);
}
shuffledWords[index] = s.charAt(i);
}

return new String(shuffledWords);
}

}
87 changes: 65 additions & 22 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@ public void tearDown() {

@Test
public void testCount1() {
manipulatedstring.setString("This is my string");
manipulatedstring.setString("Work test string");
int length = manipulatedstring.count();
assertEquals(4, length);
}

@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("");
int length = manipulatedstring.count();
assertEquals(0, length);
}

@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("Testing");
int length = manipulatedstring.count();
assertEquals(1, length);
}

@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString("Method is tested for longer text now");
int length = manipulatedstring.count();
assertEquals(7, length);
}

@Test
Expand All @@ -55,27 +61,32 @@ public void testRemoveNthCharacter2() {

@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("Removing every multiple of four's position");
assertEquals("Remvin evry ultpleof ours psiton", manipulatedstring.removeNthCharacter(4, false));
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("Removing every multiple of four's position");
assertEquals("Rem vin ev ry ult ple of our s p sit on", manipulatedstring.removeNthCharacter(4, true));
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("a simpler phrase for a simple test");
assertEquals("a simpler phrase for a simple tes ", manipulatedstring.removeNthCharacter(34, true));
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("a simpler phrase for a simple test");
assertEquals("a simpler phrase for a simple tes", manipulatedstring.removeNthCharacter(34, false));
}

@Test
public void testRemoveNthCharacter7() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You need to test for exception handling for methods with exceptions in the specification. You can use assertThrows() for this.

fail("Not yet implemented");
manipulatedstring.setString("Arkansas is a big state perhaps");
assertEquals("", manipulatedstring.removeNthCharacter(1, false));
}

@Test
Expand All @@ -89,23 +100,43 @@ public void testGeSubStrings1() {

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("A blanket statement has been made");
String [] sStings = manipulatedstring.getSubStrings(1, 4);

assertEquals(sStings[0], "A");
assertEquals(sStings[1], "has");
}
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("there are a few words here");
String [] sStings = manipulatedstring.getSubStrings(1, 2);

assertEquals(sStings[0], "there");
assertEquals(sStings[1], "are");
}
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("batmanium is no real element");
String [] sStings = manipulatedstring.getSubStrings(3, 5);

assertEquals(sStings[0], "no");
assertEquals(sStings[1], "element");
}
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("no one should smoke, as smoking is bad");
String [] sStings = manipulatedstring.getSubStrings(1, 6);

assertEquals(sStings[0], "no");
assertEquals(sStings[1], "smoking");
}
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("Extract the main idea from this sentence");
String [] sStings = manipulatedstring.getSubStrings(1, 7);

assertEquals(sStings[0], "Extract");
assertEquals(sStings[1], "sentence");
}

@Test
Expand All @@ -121,29 +152,41 @@ public void testRestoreString1()
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

manipulatedstring.setString("Neil Armstrong");
int [] array;
array=new int[]{13,12,11,10,9,8,7,6,5,4,3,2,1,0};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "gnortsmrA lieN");
}

@Test
public void testRestoreString3()
{
fail("Not yet implemented");

manipulatedstring.setString("desperation");
int [] array;
array=new int[]{6,5,9,3,1,4,10,0,2,8,7};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "aropeendsit");
}

@Test
public void testRestoreString4()
{
fail("Not yet implemented");

manipulatedstring.setString("contradiction");
int [] array;
array=new int[]{5,0,8,1,4,6,2,11,9,10,12,7,3};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "accordnotinit");
}

@Test
public void testRestoreString5()
{
fail("Not yet implemented");

manipulatedstring.setString("easy");
int [] array;
array=new int[]{3,0,1,2};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "yeas");
}

}