From 49ee79e153a49c7ca7b9db63dff879f78e0967dc Mon Sep 17 00:00:00 2001 From: carector Date: Thu, 15 Jun 2023 10:53:59 -0700 Subject: [PATCH 1/2] Update skeleton --- src/main/java/StringManipulation.java | 68 +++++++- src/test/java/StringManipulationTest.java | 185 +++++++++++++++++----- 2 files changed, 211 insertions(+), 42 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..d120d1d 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,89 @@ +///////////////////////////////////////////////////////// +// +// Class: StringManipulation +// Author: Caleb Rector +// Date: 6/11/2023 +// +// Description: Implements methods in StringManipulationInterface +// +///////////////////////////////////////////////////////// + public class StringManipulation implements StringManipulationInterface { + private String s; + @Override public String getString() { - return null; + return s; } @Override public void setString(String string) { + s = string; } @Override public int count() { - return 0; + if(s.length() == 0) + return 0; + + String[] arr = s.split("[\\s\\n]"); + return arr.length; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + + if (n > s.length()) + throw new IndexOutOfBoundsException(); + if (n <= 0) + throw new IllegalArgumentException(); + + String s2 = ""; + int count = 1; + for (int i = 0; i < s.length(); i++) { + if (count == n) { + if (maintainSpacing) + s2 += " "; + count = 1; + } else { + s2 += s.charAt(i); + count++; + } + } + return s2; } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + + if (startWord <= 0 || endWord <= 0 || startWord > endWord) + throw new IllegalArgumentException(); + + String[] arr = s.split("[\\s\\n]"); + if (arr.length < endWord) + throw new IndexOutOfBoundsException(); + + String[] ret = new String[endWord - startWord + 1]; + for (int i = 0; i < ret.length; i++) + ret[i] = arr[i + startWord - 1]; + + return ret; } @Override public String restoreString(int[] indices) { - return null; - } + if (indices.length != s.length()) + throw new IllegalArgumentException(); + + String ret = ""; + for (int i = 0; i < s.length(); i++) { + if (indices[i] >= s.length() || indices[i] < 0) + throw new IndexOutOfBoundsException(); + ret += s.charAt(indices[i]); + } + return ret; + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..5516952 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,9 +1,19 @@ +///////////////////////////////////////////////////////// +// +// Class: StringManipulationTest +// Author: Caleb Rector +// Date: 6/11/2023 +// +// Description: Provides test cases for methods found in StringManipulation class +// +///////////////////////////////////////////////////////// + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class StringManipulationTest { @@ -26,19 +36,28 @@ public void testCount1() { assertEquals(4, length); } + // Tests single word string @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("OneWord"); + int length = manipulatedstring.count(); + assertEquals(1, length); } + // Tests number of words in empty string @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } + // Tests number of words in string with newline characters @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("Including\nsome\nnewline\ncharacters"); + int length = manipulatedstring.count(); + assertEquals(4, length); } @Test @@ -50,38 +69,59 @@ 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)); } + // Tests string with newline characters @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("Seeing what happens when\nnewline characters are introduced"); // Newline character counts as character + assertEquals("See ng hat hap ens whe \nne lin ch rac ers are int odu ed", manipulatedstring.removeNthCharacter(4, true)); } + // Tests large value for n @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("What happens when n is a much larger value?"); + assertEquals("What happns when nis a muchlarger vaue?", manipulatedstring.removeNthCharacter(10, false)); } + // Tests n = 1 (all characters should be replaced with a space) @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("What happens if n is 1?"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } + // Tests whether method throws error when n = 0 @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("An exception should be thrown if n = 0"); + try { + manipulatedstring.removeNthCharacter(0, false); + fail("No exception thrown for n=0"); + } catch (IllegalArgumentException e) { + assert true; + } } + // Tests whether method throws error when n > length @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("An exception should ALSO be thrown if n > length"); + try { + manipulatedstring.removeNthCharacter(100, false); + fail("No exception thrown for n > length"); + } catch (IndexOutOfBoundsException e) { + assert true; + } } @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"); @@ -89,61 +129,134 @@ public void testGeSubStrings1() { @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("Lets see what happens when we pass in a string with lots and lots of words"); + String[] substrings = manipulatedstring.getSubStrings(4, 10); + assertEquals(substrings[0], "happens"); + assertEquals(substrings[3], "pass"); + assertEquals(substrings[6], "string"); } + + // Tests a string with newline characters @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("Trying out some\nnewline characters"); + String[] subStrings = manipulatedstring.getSubStrings(3, 4); + assertEquals(subStrings[0], "some"); + assertEquals(subStrings[1], "newline"); } + + // Tests a string with numbers and symbols @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring + .setString("Special characters! Punctuation, symbols !@#$ and numbers 91283... Will it matter?"); + String[] substrings = manipulatedstring.getSubStrings(2, 10); + assertEquals(substrings[0], "characters!"); + assertEquals(substrings[3], "!@#$"); + assertEquals(substrings[6], "91283..."); } + + // Test whether exceptions are thrown when startWord > endWord or when one of the variables is < 0 @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("SURELY nothing will go wrong if startWord is greater than endWord..."); + + try { + manipulatedstring.getSubStrings(6, 2); + fail("No exception thrown"); + } catch (IllegalArgumentException e) { + } + + try { + manipulatedstring.getSubStrings(-1, 2); + fail("No exception thrown"); + } catch (IllegalArgumentException e) { + } + + try { + manipulatedstring.getSubStrings(0, -3); + fail("No exception thrown"); + } catch (IllegalArgumentException e) { + } } + + // Tests whether an exception is thrown when endWord is out of bounds @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + manipulatedstring.setString("Last test. Out of bounds?"); + try { + manipulatedstring.getSubStrings(0, 10); + fail("Out of bounds exception not thrown"); + } catch (IllegalArgumentException e) { + assert true; + } } @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"); } + // Tests a longer string @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - + public void testRestoreString2() { + manipulatedstring.setString("calebrector"); + int[] array = new int[] {0, 5, 1, 4, 3, 2, 6, 7, 8, 9, 10}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "crabelector"); } + // Tests another longer string @Test - public void testRestoreString3() - { - fail("Not yet implemented"); - + public void testRestoreString3() { + manipulatedstring.setString("clinteAstWOod"); + int[] array = new int[] {10, 1, 12, 9, 5, 7, 8, 6, 0, 8, 2, 11, 3}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "OldWestAction"); } + // Tests whether capitalization is preserved @Test - public void testRestoreString4() - { - fail("Not yet implemented"); - + public void testRestoreString4() { + manipulatedstring.setString("CAPStest"); + int[] array = new int[] {4, 5, 6, 7, 0, 1, 2, 3}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "testCAPS"); } + // Tests whether exceptions are thrown when an index is negative or out of bounds @Test - public void testRestoreString5() - { - fail("Not yet implemented"); + public void testRestoreString5() { + manipulatedstring.setString("Exceptions"); + + try + { + int[] array = new int[] {0, 2}; + manipulatedstring.restoreString(array); + fail("No exception thrown"); + } + catch (IllegalArgumentException e) {} + try + { + int[] array = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, -10}; + manipulatedstring.restoreString(array); + fail("No exception thrown"); + } + catch (IndexOutOfBoundsException e) {} + try + { + int[] array = new int[] {0, 1, 2, 3, 400, 5, 6, 7, 8, 9}; + manipulatedstring.restoreString(array); + fail("No exception thrown"); + } + catch (IndexOutOfBoundsException e) {} + + assert true; } } From ab8770ccde26d96da512ece2f9f887e434991745 Mon Sep 17 00:00:00 2001 From: carector Date: Thu, 15 Jun 2023 11:14:05 -0700 Subject: [PATCH 2/2] Update StringManipulationTest import statement --- src/test/java/StringManipulationTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 5516952..91716a5 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -12,8 +12,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class StringManipulationTest {