From 046a125053262b5df1801f9daa9b8a44a8a8603d Mon Sep 17 00:00:00 2001 From: Kunga Date: Thu, 15 Jun 2023 19:23:19 -0700 Subject: [PATCH 1/3] code is ready for review --- src/main/java/StringManipulation.java | 80 ++++++++- src/test/java/StringManipulationTest.java | 209 +++++++++++++++++++--- 2 files changed, 253 insertions(+), 36 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..57c11c6 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,96 @@ -public class StringManipulation implements StringManipulationInterface { +/** + * junit Assignment + * kunga n ngochetsang + * date 6/12/2023 + * */ +public class StringManipulation implements StringManipulationInterface { + private String stringData; @Override public String getString() { - return null; + return stringData; } @Override public void setString(String string) { + stringData = string; } @Override public int count() { - return 0; + int wordCount = 0; + String[] stringArr; + + if(stringData!=null) { + stringArr = stringData.trim().split("\\W+"); + wordCount = stringArr.length; + + if(wordCount==1&&stringArr[0].isEmpty()){ + wordCount=0; + } + } + + return wordCount; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if(stringData==null){ + return null; + } + if (n < 1) { + throw new IllegalArgumentException("n must be greater than 0"); + } + if (n > stringData.length()) { + throw new IllegalArgumentException("n must be less than the length of the string"); + } + StringBuilder builtString = new StringBuilder(stringData); + for (int i = n - 1; i < stringData.length(); i += n) { + builtString.setCharAt(i, '~'); + } + if(maintainSpacing){ + return builtString.toString().replace('~',' '); + }else + return builtString.toString().replace("~",""); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + if (startWord < 1||endWord<1||startWord>endWord) { + throw new IllegalArgumentException("start word or end word is invalid"); + } + if (endWord > stringData.trim().split("\\W+").length) { + throw new IndexOutOfBoundsException("string has less than endWord words in it"); + } + String[] stringArr; + String[] stringArrSubString = new String[2]; + if(stringData!=null) { + stringArr = stringData.trim().split("\\W+"); + stringArrSubString[0] = stringArr[startWord-1]; + stringArrSubString[1] =stringArr[endWord-1]; + } + + return stringArrSubString; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if (stringData ==null){ + return null; + } + if (stringData.length() != indices.length) { + throw new IllegalArgumentException("not s.length == indices.length == n"); + } + for (int i = 0; i stringData.length()) { + throw new IndexOutOfBoundsException("indices[i]< 0 or indices[i]> string length"); + } + } + StringBuilder builtString = new StringBuilder(); + for (int i = 0;i { + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + manipulatedstring.removeNthCharacter(300, true); + }); + + String expectedMessage = "n must be less than the length of the string"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } + /** + *this is to test for IllegalArgumentException for negative n value + * */ @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); - } + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + manipulatedstring.removeNthCharacter(-300, true); + }); + String expectedMessage = "n must be greater than 0"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + + } + /** + *this is to test for IllegalArgumentException for 0 as for n value + * */ @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); - } + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + manipulatedstring.removeNthCharacter(0, true); + }); + + String expectedMessage = "n must be greater than 0"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + } + /** + * this is to test to handle null as just to return null of the string + * data hasn't been setup yet. i could retrn "" but i chose null to show + * null data type + * */ @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + assertEquals(null, manipulatedstring.removeNthCharacter(3, true)); } - + /** + * + * */ @Test public void testRemoveNthCharacter7() { fail("Not yet implemented"); } - + /** + * + * */ @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); @@ -86,28 +161,73 @@ public void testGeSubStrings1() { assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } - + /** + * this test is to check if the negative or 0 inputs produce + * the IllegalArgumentException + * + * */ @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(0, -4); + + }); + + String expectedMessage = "start word or end word is invalid"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); } + /** + *this test is to check if the endword is greater than the startword inputs produce + * the IllegalArgumentException + * */ @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(4, 3); + }); + + String expectedMessage = "start word or end word is invalid"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); } + /** + * this is to check if the string has less than "endWord" words in it + * */ @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 6); + }); + + String expectedMessage = "string has less than endWord words in it"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); } + /** + * + * */ @Test public void testGeSubStrings5() { fail("Not yet implemented"); } + /** + * + * */ @Test public void testGeSubStrings6() { fail("Not yet implemented"); } - + /** + * this is to test if the restoration works on a normal + * circumstance + * */ @Test public void testRestoreString1() { @@ -117,28 +237,61 @@ public void testRestoreString1() String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "rat"); } - + /** + * this test is to check the IllegalArgumentException if not s.length == indices.length == n + * */ @Test public void testRestoreString2() { - fail("Not yet implemented"); + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,2,3}; + String restoreString = manipulatedstring.restoreString(array); + }); - } + String expectedMessage = "not s.length == indices.length == n"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + } + /** + *this test is to check the IndexOutOfBoundsException if + * indices[i]< 0 or indices[i]> string length + * */ @Test public void testRestoreString3() { - fail("Not yet implemented"); + Exception exception = assertThrows(RuntimeException.class, () -> { + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,20}; + String restoreString = manipulatedstring.restoreString(array); + }); - } + String expectedMessage = "indices[i]< 0 or indices[i]> string length"; + String actualMessage = exception.getMessage(); + assertTrue(actualMessage.contains(expectedMessage)); + + } + /** + * To check for null pointer if the string has not been set yet + * or is null + * */ @Test public void testRestoreString4() { - fail("Not yet implemented"); + manipulatedstring.setString(null); + int [] array; + array=new int[]{1,0,2}; + String restoreString = manipulatedstring.restoreString(array); } - + /** + * + * */ @Test public void testRestoreString5() { From 240a8ecad95d9e5f0c74f5cf639afda45b731ce3 Mon Sep 17 00:00:00 2001 From: Kunga Date: Thu, 15 Jun 2023 19:57:52 -0700 Subject: [PATCH 2/3] code is ready for review --- src/test/java/StringManipulationTest.java | 32 +++-------------------- 1 file changed, 3 insertions(+), 29 deletions(-) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index f9906f9..820a272 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -143,13 +143,7 @@ public void testRemoveNthCharacter6() { manipulatedstring.setString(null); assertEquals(null, manipulatedstring.removeNthCharacter(3, true)); } - /** - * - * */ - @Test - public void testRemoveNthCharacter7() { - fail("Not yet implemented"); - } + /** * * */ @@ -210,20 +204,8 @@ public void testGeSubStrings4() { assertTrue(actualMessage.contains(expectedMessage)); } - /** - * - * */ - @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); - } - /** - * - * */ - @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); - } + + /** * this is to test if the restoration works on a normal * circumstance @@ -289,14 +271,6 @@ public void testRestoreString4() String restoreString = manipulatedstring.restoreString(array); } - /** - * - * */ - @Test - public void testRestoreString5() - { - fail("Not yet implemented"); - } } From 500e3ac6b816218eac16616e23c46cacda02aabe Mon Sep 17 00:00:00 2001 From: Kunga Date: Tue, 20 Jun 2023 00:41:50 -0700 Subject: [PATCH 3/3] upload after the codereview --- src/main/java/StringManipulation.java | 46 +++++++++----------- src/test/java/StringManipulationTest.java | 52 +++++++++++++++++------ 2 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 57c11c6..cbd5f85 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,11 +1,14 @@ - +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.MatchResult; /** * junit Assignment * kunga n ngochetsang * date 6/12/2023 * */ public class StringManipulation implements StringManipulationInterface { - private String stringData; + private String stringData =""; @Override public String getString() { return stringData; @@ -18,25 +21,25 @@ public void setString(String string) { @Override public int count() { + if(stringData == null) { + throw new NullPointerException("String is null."); + } int wordCount = 0; String[] stringArr; - if(stringData!=null) { - stringArr = stringData.trim().split("\\W+"); - wordCount = stringArr.length; + stringArr = stringData.trim().split("[^a-zA-Z0-9_'@&-]+"); + wordCount = stringArr.length; - if(wordCount==1&&stringArr[0].isEmpty()){ - wordCount=0; - } + if(wordCount==1&&stringArr[0].isEmpty()){ + wordCount=0; } - return wordCount; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { if(stringData==null){ - return null; + throw new NullPointerException("String is null."); } if (n < 1) { throw new IllegalArgumentException("n must be greater than 0"); @@ -46,12 +49,12 @@ public String removeNthCharacter(int n, boolean maintainSpacing) { } StringBuilder builtString = new StringBuilder(stringData); for (int i = n - 1; i < stringData.length(); i += n) { - builtString.setCharAt(i, '~'); + builtString.setCharAt(i, '|'); } if(maintainSpacing){ - return builtString.toString().replace('~',' '); + return builtString.toString().replace("|"," "); }else - return builtString.toString().replace("~",""); + return builtString.toString().replace("|",""); } @Override @@ -59,17 +62,12 @@ public String[] getSubStrings(int startWord, int endWord){ if (startWord < 1||endWord<1||startWord>endWord) { throw new IllegalArgumentException("start word or end word is invalid"); } - if (endWord > stringData.trim().split("\\W+").length) { + if (endWord > count()) { throw new IndexOutOfBoundsException("string has less than endWord words in it"); } String[] stringArr; - String[] stringArrSubString = new String[2]; - if(stringData!=null) { - stringArr = stringData.trim().split("\\W+"); - stringArrSubString[0] = stringArr[startWord-1]; - stringArrSubString[1] =stringArr[endWord-1]; - } - + stringArr = stringData.trim().split("[^a-zA-Z0-9_'@&]+"); + String[] stringArrSubString = Arrays.copyOfRange(stringArr,startWord-1,endWord); return stringArrSubString; } @@ -81,13 +79,11 @@ public String restoreString(int[] indices){ if (stringData.length() != indices.length) { throw new IllegalArgumentException("not s.length == indices.length == n"); } - for (int i = 0; i stringData.length()) { throw new IndexOutOfBoundsException("indices[i]< 0 or indices[i]> string length"); } - } - StringBuilder builtString = new StringBuilder(); - for (int i = 0;i { + manipulatedstring.setString(null); + manipulatedstring.count(); + }); + + String expectedMessage = "String is null."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); } /** @@ -66,7 +72,7 @@ public void testCount4() { * */ @Test public void testCount5() { - manipulatedstring.setString(" this is hello world !!!!! "); + manipulatedstring.setString(" This is hello world. !!!!! "); int length = manipulatedstring.count(); assertEquals(4, length); } @@ -107,7 +113,7 @@ public void testRemoveNthCharacter3() { * */ @Test public void testRemoveNthCharacter4() { - Exception exception = assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); manipulatedstring.removeNthCharacter(-300, true); }); @@ -140,8 +146,15 @@ public void testRemoveNthCharacter5() { * */ @Test public void testRemoveNthCharacter6() { - manipulatedstring.setString(null); - assertEquals(null, manipulatedstring.removeNthCharacter(3, true)); + Exception exception = assertThrows(NullPointerException.class, () -> { + manipulatedstring.setString(null); + manipulatedstring.removeNthCharacter(3, true); + }); + + String expectedMessage = "String is null."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); } /** @@ -151,18 +164,29 @@ public void testRemoveNthCharacter6() { public void testGeSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); - assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } + /** + * + * */ + @Test + public void testGeSubStrings2() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(2, 4); + + assertEquals(sStings[0], "is"); + assertEquals(sStings[1], "my"); + assertEquals(sStings[2], "string"); + } /** * this test is to check if the negative or 0 inputs produce * the IllegalArgumentException * * */ @Test - public void testGeSubStrings2() { - Exception exception = assertThrows(RuntimeException.class, () -> { + public void testGeSubStrings3() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(0, -4); @@ -178,7 +202,7 @@ public void testGeSubStrings2() { * the IllegalArgumentException * */ @Test - public void testGeSubStrings3() { + public void testGeSubStrings4() { Exception exception = assertThrows(RuntimeException.class, () -> { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(4, 3); @@ -193,7 +217,7 @@ public void testGeSubStrings3() { * this is to check if the string has less than "endWord" words in it * */ @Test - public void testGeSubStrings4() { + public void testGeSubStrings5() { Exception exception = assertThrows(RuntimeException.class, () -> { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 6); @@ -225,7 +249,7 @@ public void testRestoreString1() @Test public void testRestoreString2() { - Exception exception = assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { manipulatedstring.setString("art"); int [] array; array=new int[]{1,0,2,3}; @@ -245,7 +269,7 @@ public void testRestoreString2() @Test public void testRestoreString3() { - Exception exception = assertThrows(RuntimeException.class, () -> { + Exception exception = assertThrows(IndexOutOfBoundsException.class, () -> { manipulatedstring.setString("art"); int [] array; array=new int[]{1,0,20};