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
111 changes: 101 additions & 10 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,124 @@
/*
Sean Fite
CS 410 Junit Project
This program implements logic for String Manipulation methods
Last Updated 6/7/23
*/
public class StringManipulation implements StringManipulationInterface {

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

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

// this method returns the number of words in the string passed in
@Override
public int count() {
return 0;
public int count()
{
String[] words = _string.split(" ");
int count = 0;
for (String sentance : words) {
if (!sentance.isEmpty()) {
count++;
}
}
return count;
}

// method to remove every nth character from string
@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
public String removeNthCharacter(int n, boolean maintainSpacing)
{
int index;
int nthValue = n;
int indexDeleted = 0;
String word = _string;
StringBuilder newWord = new StringBuilder(word); // using StringBuilder to manipulate string
if( n <= 0 || word == null) // if invalid input, throw exception
{
throw new IllegalArgumentException("Invalid Input");
}
if(n > word.length())
{
throw new IndexOutOfBoundsException("Index out of bounds");
}
for(int i = 0; i < word.length(); i++) // loop through entire string
{
if(i == n - 1) // if at nth place
{
if(maintainSpacing == true) // depending on if spacing is maintained
{
newWord.replace(i, i + 1, " "); // either replace letter with " "
}
else
{
newWord.deleteCharAt(i - indexDeleted); // or replace letter
}
indexDeleted++; // track changing string length
n += nthValue; // logic to track every nth value
}
}
return newWord.toString();
}

// method to return substrings
@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord)
{
int start = 0;
String word = _string;
String[] words = word.split("\\s+"); // split words using spaces
String[] wordsWithParams = new String[2]; // new array to return desired output
if(startWord > endWord || startWord <= 0 || endWord <= 0) // if input is not valid, throw exceptions
{
throw new IllegalArgumentException("Invalid input");
}
if(endWord > words.length)
{
throw new IndexOutOfBoundsException("endWord index out of bounds");
}
for(int i = startWord; i <= endWord; i++) // loop only from start to end params
{
if(i == startWord || i == endWord) // if we have a match
{
wordsWithParams[start] = words[i - 1]; // add to recently initiliaze array
start++;
}
}
return wordsWithParams;
}

// method to change string based on given indices
@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices)
{
String word = _string;
StringBuilder restoredString = new StringBuilder(word); // using StringBuilder to manipulate string
if(word.length() != indices.length) // if input is invalid, throw exception
{
throw new IllegalArgumentException("Input length does not match string");
}
for(int i = 0; i < word.length(); i++)
{
if(indices[i] < 0 || indices[i] > word.length()) // if indices values are out of range, throw exception
{
throw new IndexOutOfBoundsException("Indices value out of accepted range");
}
for(int j = 0; j < word.length(); j++) // start of nest loops to iterate for matches
{
if(i == indices[j]) // replace StringBuilder values using string word
{
restoredString.replace(j, j + 1, String.valueOf(word.charAt(i)));
}
}
}
return restoredString.toString();
}


}

135 changes: 108 additions & 27 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

import static org.junit.jupiter.api.Assertions.*;

/*
Sean Fite
CS 410 Junit Testing Project
This program runs Junit tests on StringManipulation.java
Last Edited 6/7/23
*/

public class StringManipulationTest {

private StringManipulationInterface manipulatedstring;
Expand All @@ -26,19 +33,29 @@ public void testCount1() {
assertEquals(4, length);
}

// test func when string input is just 1 word
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString("This");
int length = manipulatedstring.count();
assertEquals(1, length);
}

// test func when string input is empty
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("");
int length = manipulatedstring.count();
assertEquals(0, length);
}

// test func when string input has extra spaces
@Test
public void testCount4() {
fail("Not yet implemented");
public void testCount4()
{
manipulatedstring.setString("as I went to the store");
int length = manipulatedstring.count();
assertEquals(6, length);
}

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

// test case for deleting all the characters
@Test
public void testRemoveNthCharacter5()
{
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("", manipulatedstring.removeNthCharacter(1, false));
}

// test case for nth character out of bounds
@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("apple");
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.removeNthCharacter(10, true);
});
}

// test case for n < 0
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("coffee");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(-1, true);
});
}

// test case for n = 0
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
public void testRemoveNthCharacter7() {
manipulatedstring.setString("coffee");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(0, true);
});
}

// test case for empty string
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.removeNthCharacter(3, true);
});
}

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

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

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

// if start word > end word
@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(4, 2);
});
}

// if start word < 0
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(0, 2);
});
}

// if end word < 0
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.getSubStrings(2, 0);
});
}

// if end word > amount of words
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.getSubStrings(2, 6);
});
}

// if start and end indexes are the same
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 3);
assertEquals(sStings[0], "my");
assertEquals(sStings[1], null);
}

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

// testing for capital letter sensitivity
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

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

// testing if input string length matches indices length
@Test
public void testRestoreString3()
{
fail("Not yet implemented");
manipulatedstring.setString("Test Unit");
int [] array;
array=new int[]{1, 0, 6, 7, 5, 4, 2, 3};
assertThrows(IllegalArgumentException.class, () -> {
manipulatedstring.restoreString(array);
});

}

// testing if indices values > string input length
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

manipulatedstring.setString("TestUnit");
int [] array;
array=new int[]{1, 0, 6, 7, 9, 4, 2, 3};
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(array);
});
}

// testing if indices value < 0
@Test
public void testRestoreString5()
{
fail("Not yet implemented");

manipulatedstring.setString("TestUnit");
int [] array;
array=new int[]{1, 0, -6, 7, 9, 4, 2, 3};
assertThrows(IndexOutOfBoundsException.class, () -> {
manipulatedstring.restoreString(array);
});
}

}