From 9d238f6266bd06b6905111d7e539fbc6dc9b7b98 Mon Sep 17 00:00:00 2001 From: Ema Ikeda Date: Wed, 14 Jun 2023 11:04:42 -0700 Subject: [PATCH] Initial Commit --- src/main/java/StringManipulation.java | 116 +++++++++- src/test/java/StringManipulationTest.java | 266 +++++++++++++++++++--- 2 files changed, 348 insertions(+), 34 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..da170bb 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,134 @@ +import java.util.ArrayList; +import java.util.Arrays; + public class StringManipulation implements StringManipulationInterface { + String string = null; + // Default constructor + StringManipulation() {} + + + // Parameterized constructor + StringManipulation(String string) { + this.string = string; + } @Override public String getString() { - return null; + return this.string; } @Override public void setString(String string) { + this.string = string; } + // todo: raise nullPointer Exception if string is null @Override public int count() { - return 0; + int count = 0; + String string = getString(); + char ch[]= new char[string.length()]; + for(int i=0;i 0) && (ch[i] != ' ') && (ch[i-1] == ' ')) || ((ch[0] != ' ') && (i == 0))) + count++; + } + return count; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + // throw IllegalArgument Exception if n <= 0 + if (n <= 0) { + throw new IllegalArgumentException("n has to be at least 1"); + } + + String string = this.getString(); + if (n >= string.length()) { + throw new IndexOutOfBoundsException("n cannot be greater than the length of a string"); + } + + String res = ""; // start with first substring from first char to the last char before nth char. + String subString = ""; + int startSub = n; // index for beginning of substring + int i = 2; + + if (maintainSpacing == false) { + res = string.substring(0,n-1); + // iterate while i*n is less than length. Increment by n + while (startSub < string.length()) { + if (string.length() % n != 0 && n*i > string.length()) { // add last substring if necessary + res += string.substring(startSub); + return res; + } + subString = string.substring(startSub, i*n-1); + res += subString; // add substring excluding nth char until the i*nth char + startSub = i*n; + i++; + } + + } else { + char whiteSpace = ' '; + res = string.substring(0,n-1) + whiteSpace; + // iterate while i*n is less than length. Increment by n + while (startSub < string.length()) { + if (string.length() % n != 0 && n*i > string.length()) { // add last substring if necessary + res += string.substring(startSub); + return res; + } + subString = string.substring(startSub, i*n-1)+whiteSpace; + res += subString; // add substring excluding nth char until the i*nth char + startSub = i*n; + i++; + } + } + return res; } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + String string = getString(); + int wordCount = count(); + if (endWord > wordCount) { + throw new IndexOutOfBoundsException("String has less than endWord words in it."); + } else if (endWord <= 0 || startWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid startWord or endWord."); + } + + String totalWords[] = string.split(" "); + String words[] = new String[endWord-startWord+1]; + int index = 0; + String subString = ""; + + // for corner case when startWord is the same as endWord + if (startWord == endWord) { + words[index] = totalWords[startWord-1]; + return words; + } + // Add matching totalWords[startWord-1] to totalWords[endWord-1] to words + for (int i = startWord-1; i { + manipulatedstring.count(); + }, "string cannot be null"); + } + + // Test case when string ends with whitespace. + @Test + public void testCount5() { + manipulatedstring.setString("This is my string "); + assertEquals(4, manipulatedstring.count()); + } + + // Test case when string contains 2 whitespaces. + @Test + public void testCount6() { + manipulatedstring.setString("This is my string"); + assertEquals(4, manipulatedstring.count()); } + // Test case when string is surrounded by whitespace. + @Test + public void testCount7() { + manipulatedstring.setString(" a "); + assertEquals(1, manipulatedstring.count()); + } + + // Default test case. @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)); } + // Default test case. @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)); } + // Throw IndexOutOfBoundsException if n is greater than length of a string. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("abc"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(10, false); + }, "n cannot be greater than the length of a string"); } + // Throws IllegalArgumentException if n < 0. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("abc"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(0, false); + }, "n has to be at least 1"); } + // Test case for when n is 1, meaning that all the characters in string will be removed without whitespace. @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("abc"); + assertEquals(manipulatedstring.removeNthCharacter(1, false), ""); } + // test case for when n is 1 so that all the characters in string will be replaced by whitespace. @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("abc"); + assertEquals(manipulatedstring.removeNthCharacter(1, true), " "); } + // Throws NullPointerException if string is null. @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + Throwable exception = assertThrows(NullPointerException.class, () -> { + manipulatedstring.removeNthCharacter(1, false); + }, "string cannot be null"); } + // Throws IllegalArgumentException, because startWord is <= 0. @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); - - assertEquals(sStings[0], "my"); - assertEquals(sStings[1], "string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(0,2); + }, "startWord has to be an integer greater than 0."); } + // Throws IllegalArgumentException, because endWord <= 0. @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(1, -1); + }, "endWord has to be an integer greater than 0."); } + + // Throws IndexOutOfBoundsException, because endWord is greater than the number of words in string. @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(1, 6); + }, "String has less than endWord words in it."); } + + // Throws IllegalArgumentException, because startWord > endWord. @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.getSubStrings(4, 1); + }, "startWord cannot be greater than endWord."); } + + // Throws IndexOutOfBoundsException, because string has less words than endWord. @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.getSubStrings(1, 5); + }, "String does not have enough words"); } + + + // Test case for when startWord > 1. @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 4); + + assertEquals(sStings[0], "my"); + assertEquals(sStings[1], "string"); + } + + // Test for corner case when startWord == endWord. + @Test + public void testGeSubStrings7() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 3); + + assertEquals(sStings[0], "my"); } + // Default test case. + @Test + public void testGeSubStrings8() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(1, 4); + + assertEquals(sStings[0], "This"); + assertEquals(sStings[1], "is"); + assertEquals(sStings[2], "my"); + assertEquals(sStings[3], "string"); + } + + // Throws IndexOutOfBoundsException, because one of the indices is greater than the length of string. @Test public void testRestoreString1() { manipulatedstring.setString("art"); int [] array; - array=new int[]{1,0,2}; - String restoreString = manipulatedstring.restoreString(array); - assertEquals(restoreString, "rat"); + array=new int[]{1,0,4}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indices cannot be larger than the length of the string-1."); } + // Throws IndexOutOfBoundsException, because one of the indices are negative. @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,-2}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indices cannot be negative."); } + // Throws IllegalArgumentException, because indices.length < string.length(). @Test public void testRestoreString3() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1}; + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indice array has to be the same length as string."); + } + // Test case when string is null. Throws NullPointerException. + @Test + public void testRestoreString7() + { + int [] array; + array = new int[] {1,0,2}; + Throwable exception = assertThrows(NullPointerException.class, () -> { + manipulatedstring.restoreString(array); + }, "String cannot be null"); } + // Default test case. @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,2}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "rat"); } + // Test case when string is separated by whitespace. @Test public void testRestoreString5() { - fail("Not yet implemented"); + manipulatedstring.setString("abc def"); + int [] array; + array=new int[]{6,5,4,3,0,1,2}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "fed abc"); + } + + // Corner case when string is an empty string and indices={0}. Throw IllegalArgumentException, because + // indices.length > string.length(). + @Test + public void testRestoreString6() + { + manipulatedstring.setString(""); + int [] array; + array=new int[]{0}; + Throwable exception = assertThrows(IllegalArgumentException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indices cannot be greater than length of a string"); + } + + // Test for corner case when string contains only one character. + @Test + public void testRestoreString8() + { + manipulatedstring.setString("a"); + int [] array; + array=new int[]{0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "a"); + } + // Test corner case when one of the elements in indices is equal to the length of a string. + @Test + public void testRestoreString9() + { + manipulatedstring.setString("art"); + int [] array; + array = new int[] {1,0,3}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indices[i] has to be less than the length of a string."); } + // Test corner case when one of the elements in indices is greater than the length of a string. + @Test + public void testRestoreString10() + { + manipulatedstring.setString("art"); + int [] array; + array = new int[] {1,0,10}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> { + manipulatedstring.restoreString(array); + }, "Indices[i] has to be less than the length of a string."); + } }