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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Modify or add additional test cases in StringManipulationTest.java. If new test cases failed, modify this file so that it passes the said test cases.

private String string;

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

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

@Override
public int count() {
return 0;
if (string == null || string.isEmpty()) {
return 0;
}

String[] words = string.split("\\s+");
return words.length;
}

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

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

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

String result = "";
int length = string.length();
for (int i = 0; i < length; i++) {
char c = string.charAt(i);
if ((i + 1) % n != 0) {
result += c;
} else {
if (maintainSpacing) {
result += " ";
}
}

}
return result;
}

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

String[] words = string.split("\\s+");
if (endWord > words.length) {
throw new IndexOutOfBoundsException();
}

int substringCount = endWord - startWord + 1;
String[] substrings = new String[substringCount];

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

return substrings;
}

@Override
public String restoreString(int[] indices) {
return null;
}
int n = indices.length;
char[] shuffledChars = new char[n];

if (!(string.length() == indices.length && indices.length == n)) {
throw new IllegalArgumentException();
}

for (int i = 0; i < n; i++) {
if (indices[i] < 0 || indices[i] >= n) {
throw new IndexOutOfBoundsException();
}
shuffledChars[indices[i]] = string.charAt(i);
}

}
return new String(shuffledChars);
}
}
6 changes: 3 additions & 3 deletions src/main/java/StringManipulationInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public interface StringManipulationInterface {
* The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
* Return the shuffled string.
* example:
* Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3]
* Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3]
* Output: "TestUnit"
* Explanation:
* indices: 4 5 6 7 0 1 2 3
* indices: 4 5 6 7 0 2 1 3
* String: U n i t T e s t
* Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ......
* Output: T e s t U n i t
Expand All @@ -95,7 +95,7 @@ public interface StringManipulationInterface {
* indices length is the same as the string length.
*
* throws IllegalArgumentException if not s.length == indices.length == n
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length
* throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length
*
* @param indices is an integer array for shuffled string new indices positions
* the character at the ith position moves to indices[i] in the shuffled string.
Expand Down
133 changes: 97 additions & 36 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@Emc2Ikeda Emc2Ikeda 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.

Recommend adding some test cases for setString() and getString() for sake of sanity. Be sure to add cases when this.string is null and when you try to set this.string to null as well as when string is empty.


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also add cases in each method when string is null or empty.

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

public class StringManipulationTest {
Expand All @@ -26,19 +25,29 @@ public void testCount1() {
assertEquals(4, length);
}

// check to see that a null string will return a word count of 0.
@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedstring.setString(null);
int length = manipulatedstring.count();
assertEquals(0, length);
}

// tests if numbers or special characters will mess with the word count
@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedstring.setString("This is a string that the number $40.00 in the string");
int length = manipulatedstring.count();
assertEquals(11, length);
}

// another test, but this time we add quotation marks to see if it messes with
// word count
@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedstring.setString("This a string that can't fail just because \"can't\" is in the string");

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 seems redundant with Test case #1. Can it be replaced with testing extra whitespace (EX: " abc") and the one where string has more than 1 whitespace between words (EX: "abc def")?

int length = manipulatedstring.count();
assertEquals(13, length);
}

@Test
Expand All @@ -50,100 +59,152 @@ public void testRemoveNthCharacter1() {
@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));
assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?",
manipulatedstring.removeNthCharacter(3, true));
}

// like the tests above, but we test it with a different value for n.
@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?");
assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?",
manipulatedstring.removeNthCharacter(5, false));
}

// we test for a smaller n.
@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedstring.setString("let's try a smaller string");
assertEquals("ltstyasalrsrn",
manipulatedstring.removeNthCharacter(2, false));
}

// we test for a smaller n while maintaning the spacing in the string
@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedstring.setString("let's try a smaller string");
assertEquals("l t s t y a s a l r s r n ",
manipulatedstring.removeNthCharacter(2, true));
}

// test to make sure it throws a IndexOutOfBoundsException when n > string
// length
@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for n > string length");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false));
}

// test to make sure it throws a IllegalArgumentException when n < 0
@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IllegalArgumentException for n < 0");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false));
}

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

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

// test to make sure that it can scale to more substrings

@Emc2Ikeda Emc2Ikeda 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.

Recommend modifying this test case so that it calls getSubStrings(1,15) (i.e. get all the words in a string).

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedstring.setString("This is a very long string that seems to go on and on and on");
String[] sStings = manipulatedstring.getSubStrings(2, 7);

assertEquals(sStings[0], "is");
assertEquals(sStings[1], "a");
assertEquals(sStings[2], "very");
assertEquals(sStings[3], "long");
assertEquals(sStings[4], "string");
assertEquals(sStings[5], "that");
}

// tests thats startWord cannot be <= 0 and should throw a
// IllegalArgumentException
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord <= 0");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 5));
}

// tests thats endWord cannot be <= 0 and should throw a
// IllegalArgumentException

@Emc2Ikeda Emc2Ikeda 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.

Modify testGeSubStrings4 so that startWord is an integer greater than 0. This ensures that the exception is caused by the endWord <= 0, not because startWord is <= 0.

@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IllegalArgumentException for endWord <= 0");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 0));
}

// tests thats startWord < endWord and should throw a IllegalArgumentException
// if it isn't
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IllegalArgumentException for startWord > endWord");
assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1));
}

// tests the case where string length is shorter than endWord and throws a
// IndexOutOfBoundsException if it is
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedstring.setString("This Test should throw IndexOutOfBoundsException for endWord > string length");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add test case for getSubStrings() when startWord = endWord.

@Test
public void testRestoreString1()
{
public void testRestoreString1() {
manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2};
int[] array;
array = new int[] { 1, 0, 2 };
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "rat");
}

// another test like above to check string length doesn't matter
@Test
public void testRestoreString2()
{
fail("Not yet implemented");

public void testRestoreString2() {
manipulatedstring.setString("CatDog");
int[] array;
array = new int[] { 3, 4, 5, 0, 1, 2 };
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "DogCat");
}

// another test like above to check string length doesn't matter, even two of
// the same characters
@Test
public void testRestoreString3()
{
fail("Not yet implemented");

public void testRestoreString3() {
manipulatedstring.setString("backwards");
int[] array;
array = new int[] { 4, 2, 6, 7, 3, 5, 1, 0, 8 };
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "drawbacks");
}

// tests the case where input array contains an invalid string index and throws
// a IndexOutOfBoundsException
@Test
public void testRestoreString4()
{
fail("Not yet implemented");

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

// tests the case where input array is not the same length as the string and
// throws a IllegalArgumentException
@Test
public void testRestoreString5()
{
fail("Not yet implemented");

public void testRestoreString5() {
manipulatedstring.setString("left");
int[] array;
array = new int[] { 0, 1, 2, 3, 4, 5 };
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.

Add another test case for restoreString() for case when string contains more than 1 word, such as "Hello world".

}