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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
91 changes: 85 additions & 6 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,111 @@
/*
Aravind Sripada
CS410 - Sara Farag
6/13/2023
This file contains the implementations for the string manipulations class.
*/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringManipulation implements StringManipulationInterface {

Choose a reason for hiding this comment

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

No issues with the build on TravisCI.

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

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

@Override
public int count() {
return 0;
if(current == null || current.isEmpty()) //empty or null string have a length of 0

Choose a reason for hiding this comment

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

You may want to consider throwing a NullPointerException when count, or any of your other methods are called on a null string instead of returning something.

return 0;

Pattern pattern = Pattern.compile("\\w+");
Matcher matcher = pattern.matcher(current);

int count = 0;
while(matcher.find()) {
count++;
}

return count;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if(current == null || current.isEmpty())

Choose a reason for hiding this comment

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

Great job checking for current == null before trying to call a method on the null string.
It would error if you had a null string and current.isEmpty() came before current == null in your conditions.

return current;

if(n <= 0 ) {
throw new IllegalArgumentException();
}

if(n > current.length()) {
throw new IndexOutOfBoundsException();
}

int replacedDigit = n;
String replaced = "";
for(int i = 0; i < current.length(); i++) {
if(i != replacedDigit - 1) {
replaced += current.charAt(i);
} else {
replacedDigit += n;
if(maintainSpacing)
replaced += " ";
}
}
return replaced;
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
if(startWord <= 0 || endWord <= 0) {
throw new IllegalArgumentException();
}

int wordcount = count();
if(startWord > wordcount || endWord > wordcount) {
throw new IndexOutOfBoundsException();
}

int start = startWord - 1; //the given ints are always 1 ahead since they start counting from 1
int end = endWord - 1;

String[] words = current.trim().split(" "); //removes white space at the beginning and end of the string and splits by whitespace

Choose a reason for hiding this comment

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

I don't see any issue with using this method to break up the words in a String, but instead of instantiating another array and filling it, you could use a regex to extract and populate the output array directly.

String[] substring = new String[endWord - startWord + 1];

for(int i = start; i <= end; i++) {
int substringindex = i - startWord + 1;
substring[substringindex] = words[i];
}

return substring;
}

@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices){
if(indices.length != current.length()) {
throw new IllegalArgumentException();
}



String newStr = "";
for(int i = 0; i < indices.length; i++) {
if(indices[i] < 0 || indices[i] >= current.length()) {
throw new IndexOutOfBoundsException();
}
newStr += current.charAt(indices[i]);
}

return newStr;
}


Expand Down
151 changes: 121 additions & 30 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
Aravind Sripada
CS410 - Sara Farag
6/13/2023
This file contains the unit tests to ensure that the StringManipulation class works as intended.
*/

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -20,66 +27,100 @@ public void tearDown() {
}

Choose a reason for hiding this comment

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

Aravind, you're missing a requirement in your tests to verify that getString() is returning null when the string is null.

@Test
//this test checks for correct output
public void testGetString() {
String str = manipulatedstring.getString();
assertEquals(null, str);
}

@Test
//this test check for correct output
public void testCount1() {
manipulatedstring.setString("This is my string");
int length = manipulatedstring.count();
assertEquals(4, length);
}

@Test
//this test check for correct output
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("");
int length = manipulatedstring.count();
assertEquals(0, length);
}

@Test
//this test check for correct output
public void testCount3() {
fail("Not yet implemented");
int length = manipulatedstring.count();
assertEquals(0, length);
}

@Test
//this test check for correct output
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString("!!!!!! .... ,,,,");
int length = manipulatedstring.count();
assertEquals(0, length);
}

Choose a reason for hiding this comment

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

I don't see a test case for strings that look like "!!!!! !!!!". Your count method would return 2 words in this case and a group of exclamation points is not a word.

@Test

Choose a reason for hiding this comment

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

You have quite a few of your test methods marked "throws Exception." This is unnecessary. You should remove the throws statement in the method declaration.

Choose a reason for hiding this comment

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

image

Choose a reason for hiding this comment

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

As you can see from my Intellij, it doesn't recognize the method as throwing an exception.
The test cases will also run just fine without the throws Exception statement.

//this test check for correct output
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));
}

@Test
//this test check for correct output
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));
}

@Test
//this test check for correct output by leaving the string with its default value
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
assertEquals(null, manipulatedstring.removeNthCharacter(3, false));

Choose a reason for hiding this comment

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

Instead of "assertEquals" you should use "assertNull" here.
Another option is (as mentioned in the StringManipulation file) is set your methods to throw a NullPointerException when your string field is null. In this case you can assertThrows(NullPointerException.class, ....).

}

@Test
//this test check for correct output by giving the string an empty string value
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("");
assertEquals("", manipulatedstring.removeNthCharacter(4, true));
}

@Test
//this test checks to see if the IllegalArgumentException is thrown
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("Hi Sara!");

assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(-1, true);
});
}

@Test
//this class checks to see if the IndexOutOfBoundsException is thrown
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("Hi Sara!");

assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.removeNthCharacter(100, true);
});
}

@Test
//this test check for correct output
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("CS4420");
assertEquals("CS420", manipulatedstring.removeNthCharacter(4, false));
}

Choose a reason for hiding this comment

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

One note on the getSubstrings() method:
I notice that you don't have test cases that include punctuation, like "I need some sleep!". The reason I mention this is that I can tell by the way you split strings, the "!" will be included with the string. I'd recommend using a regex to extract your words, or additional processing on the temp array you use to hold strings to remove punctuations.

@Test
public void testGeSubStrings1() {
//this test check for correct output
public void testGetSubStrings1() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 4);

Expand All @@ -88,29 +129,59 @@ public void testGeSubStrings1() {
}

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
//this test check for correct output
public void testGetSubStrings2() throws Exception{
manipulatedstring.setString("I need some sleep");
String [] sStings = manipulatedstring.getSubStrings(1, 3);

assertEquals(sStings[0], "I");
assertEquals(sStings[1], "need");
assertEquals(sStings[2], "some");
}

@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
//this test checks to see if the IllegalArgumentException is thrown
public void testGetSubStrings3() throws Exception{
manipulatedstring.setString("I need some sleep");

assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(1, 0);
});
}

@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
//this test checks to see if the IllegalArgumentException is thrown
public void testGetSubStrings4() throws Exception{
manipulatedstring.setString("I need some sleep");

assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(0, 1);
});
}

@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
//this test checks to see if the IndexOutOfBoundsException is thrown
public void testGetSubStrings5() {
manipulatedstring.setString("I need some sleep");

assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.getSubStrings(5, 3);
});
}

@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
//this test checks to see if the IndexOutOfBoundsException is thrown
public void testGetSubStrings6() throws Exception{
manipulatedstring.setString("I need some sleep");

assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.getSubStrings(3, 5);
});
}

@Test
public void testRestoreString1()
{
//this test check for correct output
public void testRestoreString1() {
manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2};
Expand All @@ -119,31 +190,51 @@ public void testRestoreString1()
}

@Test
public void testRestoreString2()
{
fail("Not yet implemented");
//this test check to see if the IndexOutOfBoundsException is thrown
public void testRestoreString2() {
manipulatedstring.setString("acro");
int[] array;
array = new int[] {3, 2, -1, 0};

assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(array);
});
}

@Test
//this test check to see if the IndexOutOfBoundsException is thrown
public void testRestoreString3()
{
fail("Not yet implemented");
manipulatedstring.setString("acro");
int[] array;
array = new int[] {3, 2, 1, 4};

assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(array);
});
}

@Test
//this test check to see if the IllegalArgumentException is thrown
public void testRestoreString4()
{
fail("Not yet implemented");
manipulatedstring.setString("acro");
int[] array;
array = new int[] {3, 2, 1};

assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.restoreString(array);
});
}

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

//this test check for correct output
public void testRestoreString5() {
manipulatedstring.setString("UnitTest");
int [] array;
array=new int[]{4, 5, 6, 7, 0, 1, 2, 3};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "TestUnit");
}

}