From a9a79b1ce427bb0e0422b3035cb34b2b8386263b Mon Sep 17 00:00:00 2001 From: brunochristensen Date: Wed, 14 Jun 2023 11:19:42 -0700 Subject: [PATCH] updated w/ final implementation and tests --- src/main/java/StringManipulation.java | 66 +++++++++++-- src/test/java/StringManipulationTest.java | 111 ++++++++++++++++------ 2 files changed, 141 insertions(+), 36 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..db576a6 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,87 @@ +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + public class StringManipulation implements StringManipulationInterface { + 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 == null || s.isEmpty()) + return 0; + return s.split("\\s+").length; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if (n <= 0) { + throw new IllegalArgumentException(); + } + if (n > s.length()) { + throw new IndexOutOfBoundsException(); + } + StringBuilder result = new StringBuilder(); + if(maintainSpacing){ + result.append(s); + for (int i = n - 1; i < s.length(); i += n) { + result.setCharAt(i, ' '); + } + } else { + int prev = 0; + for (int i = n - 1; i < s.length(); i += n) { + result.append(s, prev, i); + prev = i + 1; + } + result.append(s, prev, s.length()); + } + return result.toString(); } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + if(startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException(); + } + String[] words = s.split("\\s+"); + if(words.length < endWord) { + throw new IndexOutOfBoundsException(); + } + return Arrays.copyOfRange(words, startWord - 1, endWord); } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if(indices.length != s.length() || duplicates(indices)){ + throw new IllegalArgumentException(); + } + char[] ch = new char[s.length()]; + for(int i = 0; i < s.length(); i++) { + ch[indices[i]] = s.charAt(i); + } + return String.valueOf(ch); } + private boolean duplicates(int[] ints) + { + Set DT = new HashSet<>(); + for (int i : ints) + { + if (DT.contains(i)) + return true; + DT.add(i); + } + return false; + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..20fae37 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -26,19 +26,28 @@ public void testCount1() { assertEquals(4, length); } + //This test to check abnormal use of spaces when parsing for words. @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(" "); + int length = manipulatedstring.count(); + assertEquals(0, length); } + //This test to check abnormal use of spaces when parsing for words. @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("JustOneWord"); + int length = manipulatedstring.count(); + assertEquals(1, length); } + //This test handles a protential null argument. @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test @@ -53,59 +62,88 @@ public void testRemoveNthCharacter2() { assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); } + //This test checks n = 1 case maintains spacing. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); } + //This test checks n = 1 case removes all spacing. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); } + //This test check that an Indexoutofbounds exception is thrown appropriately. @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(50, false)); } + //This test check that an IllegalArgumentException exception is thrown appropriately. @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); } + //This test check that an IllegalArgumentException exception is thrown appropriately. @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); } @Test - public void testGeSubStrings1() { + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); - String [] sStings = manipulatedstring.getSubStrings(3, 4); + String [] sStrings = manipulatedstring.getSubStrings(3, 4); - assertEquals(sStings[0], "my"); - assertEquals(sStings[1], "string"); + assertEquals(sStrings[0], "my"); + assertEquals(sStrings[1], "string"); } + //This test handles larger given strings @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString("The Greatest String In The Universe"); + String [] sStrings = manipulatedstring.getSubStrings(3, 6); + + assertEquals(sStrings[0], "String"); + assertEquals(sStrings[1], "In"); + assertEquals(sStrings[2], "The"); + assertEquals(sStrings[3], "Universe"); } + + //This test handles IllegalArgumentException for improper start and end values @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(3, 2)); } + + //This test handles IllegalArgumentException for improper start and end values @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(2, 0)); } + + //This test handles IllegalArgumentException for improper start and end values @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedstring.setString("This is my string"); + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(0, 3)); } + + //This test handles IndexOutOfBoundsException for end values @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedstring.setString("This is my string"); + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(3, 5)); } @Test @@ -118,32 +156,45 @@ public void testRestoreString1() assertEquals(restoreString, "rat"); } + //This test handles empty input values @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString(""); + int [] array; + array=new int[]{}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, ""); } + //This test handles duplicate values in a given index @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("arty"); + int [] array; + array=new int[]{1,0,1,2}; + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); } + //This test handles illegal values in the given index @Test public void testRestoreString4() { - fail("Not yet implemented"); - + manipulatedstring.setString("arty"); + int [] array; + array=new int[]{-1,0,1,2}; + assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array)); } + //This test handles cases where s.length != indices.length @Test public void testRestoreString5() { - fail("Not yet implemented"); - + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,2,3}; + assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array)); } }