From 726f29c2d92259c24d067d909e81d955aa1443fc Mon Sep 17 00:00:00 2001 From: forthelion Date: Thu, 15 Jun 2023 11:27:01 -0700 Subject: [PATCH 1/3] Added my code of StringManipulation.Java and StringManipulationtest --- src/main/java/StringManipulation.java | 103 +++++++++++++- src/test/java/StringManipulationTest.java | 160 ++++++++++++++++++---- 2 files changed, 227 insertions(+), 36 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..3eb1f4e 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,124 @@ public class StringManipulation implements StringManipulationInterface { + private String _string; + @Override public String getString() { - return null; + return _string; } @Override public void setString(String string) { + _string = string; } @Override public int count() { - return 0; + String string = getString(); + String[] words = string.split("\\s+"); + return words.length; + } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + String string = getString(); + StringBuilder result = new StringBuilder(); + + // exception throw IndexOutOfBoundsException + if (n > string.length()) { + throw new IndexOutOfBoundsException("n is more then string "); + } + // exception throw IllegalArgumentException + if (n <= 0) { + throw new IllegalArgumentException("n is less then or equal to zero"); + } + + int target_n = n; + for (int i = 0; i < string.length(); i++) { + + // gets character + char c = string.charAt(i); + + // if i is not equal to n then it add charter + if (i != target_n-1) { + result.append(c); + + } + else { + if(maintainSpacing) { + + result.append(' '); + + } + target_n += n; + } + + + } + return result.toString(); } @Override public String[] getSubStrings(int startWord, int endWord) { - return null; + // get number of words + int numberOfWords = count(); + String string = getString(); + String[] words = string.split("\\s+"); + + + // exception throw IndexOutOfBoundsException + + if (numberOfWords < endWord) { + throw new IndexOutOfBoundsException("string has less then endword "); + } + // exception throw IllegalArgumentException + + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("startword or endword is invalid "); + } + + String[] substrings = new String[endWord - startWord + 1]; + + int count = 0; + for (int i = startWord-1; i <= endWord-1;i++){ + substrings[count] = words[i]; + count++; + } + return substrings; } @Override public String restoreString(int[] indices) { - return null; - } + String string = getString(); + int stringSize = string.length(); + + System.out.println("The count is: " + stringSize+ "indices.length = "+indices.length); + + + // exception throw IllegalArgumentException + + if (stringSize != indices.length ) { + throw new IllegalArgumentException("length of indices does not match string length "); + } + + char[] stringOutPut = new char[indices.length]; + + for (int i = 0; i < stringSize; i++) { + int index = indices[i]; + + + // exception throw IndexOutOfBoundsException + //indices.length + if (index < 0 || index >= stringSize) { + throw new IndexOutOfBoundsException("string has less then endword "); + } + + stringOutPut[i] = string.charAt(index); + + } + return new String(stringOutPut); + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..4127ec4 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -19,63 +19,114 @@ public void tearDown() { manipulatedstring = null; } + + // testcount1() test that it should behave as expected and print out 4 items @Test public void testCount1() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); assertEquals(4, length); } - + // testcount()2 test if there is nothing there and should return a 1 @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(1, length); } + // testcount()3 test to see tab and new line it should print 0 @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString(" \t \n"); + int length = manipulatedstring.count(); + assertEquals(0, length); } - + // testcount()4 test to see tab and new line with words it should print 4 @Test public void testCount4() { - fail("Not yet implemented"); + manipulatedstring.setString("This\tthis\nmy string"); + int length = manipulatedstring.count(); + assertEquals(4, length); } - + // test to see if funcation delete every third characters:false @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)); } - + // test to see if funcation replace every third character with spacing:true @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)); } + // test an exception of throws IndexOutOfBoundsException If n is greater than the string length with deleting n characters. IndexOutOfBoundsException If n is greater than the string length. @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); - } + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertThrows( + IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(100, false); + } + ); + + + }; + + + + // test an exception of throws IndexOutOfBoundsException If n is greater than the string length with replacing every n character with spacing . IndexOutOfBoundsException If n is greater than the string length. @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); - } + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + + assertThrows( + IndexOutOfBoundsException.class, () -> { + manipulatedstring.removeNthCharacter(100, true); + } + ); + } + // test an exception of throws IIllegalArgumentException If n is equal to zero deleting n characters. IndexOutOfBoundsException @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + + assertThrows( + IllegalArgumentException.class, () -> { + manipulatedstring.removeNthCharacter(0, false); + } + ); } + + // test an exception of throws IIllegalArgumentException If n is equal to zero replacing every n character with spacing. IndexOutOfBoundsException @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, true); + } + ); } + + //test boundry that it would stop if n=3 and there 5 characters "hey i" should be equal to "he i" replacing every 3n character with spacing @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("hey i"); + assertEquals("he i", manipulatedstring.removeNthCharacter(3, true)); + } + //test boundry that it would stop if n=3 and there 5 characters "hey i" should be equal to "he i" deleting every 3n character + @Test + public void testRemoveNthCharacter8() { + manipulatedstring.setString("hey i"); + assertEquals("he i", manipulatedstring.removeNthCharacter(3, false)); } @Test @@ -86,28 +137,59 @@ public void testGeSubStrings1() { assertEquals(sStings[0], "my"); assertEquals(sStings[1], "string"); } - + // throw the exception of illegalArgumentException if start word is less or equal to sero @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString("this is my string "); + + assertThrows( + IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(0, 4); + } + ); } + // throw the exception of illegalArgumentException if end word is less or equal to zero or less then start word @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + manipulatedstring.setString("this is my string "); + + assertThrows( + IllegalArgumentException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(4, 3); + } + ); } + // throw IndexOutOfBoundsException has less then end word with it @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + manipulatedstring.setString("this is my string "); + + assertThrows( + IndexOutOfBoundsException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(0, 5); + } + ); } + // testing if start word and end word is the same thing @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + manipulatedstring.setString("fairies wear boots "); + String [] sStings = manipulatedstring.getSubStrings(3, 3); + + assertEquals( "boots", sStings[0]); } + // testing if start word and end world are greater then total words @Test public void testGeSubStrings6() { - fail("Not yet implemented"); - } + manipulatedstring.setString("blue oyster cult"); + assertThrows( + IndexOutOfBoundsException.class, () -> { + String [] sStings = manipulatedstring.getSubStrings(6, 8); + } + ); } + + // test funcationality @Test public void testRestoreString1() { @@ -117,32 +199,50 @@ public void testRestoreString1() String restoreString = manipulatedstring.restoreString(array); assertEquals(restoreString, "rat"); } - + // test IndexOutOfBoundsException by not matchling length of string @Test public void testRestoreString2() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] indices = new int[]{1,0,3}; + assertThrows( + IndexOutOfBoundsException.class, () -> { + String restoreString= manipulatedstring.restoreString( indices ); + } + ); } - + // test illegalArgumentException by not matching indexs of being outbounds @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("part"); + int [] indices = new int[]{1,0,2}; + assertThrows( + IllegalArgumentException.class, () -> { + String restoreString= manipulatedstring.restoreString( indices ); + } + ); } + // how well it handles the empty the string and empty index @Test public void testRestoreString4() { - fail("Not yet implemented"); + manipulatedstring.setString(""); + int [] indices = new int[]{}; + String restoreString = manipulatedstring.restoreString(indices ); + assertEquals(restoreString, ""); } - + // testing with special character @Test public void testRestoreString5() { - fail("Not yet implemented"); + manipulatedstring.setString("a@b#c$d"); + int[] indices = new int[]{6, 5, 4, 3, 2, 1, 0}; + String restoreString = manipulatedstring.restoreString(indices); + assertEquals("d$c#b@a", restoreString); } From 4c588158878aea5af105b16d0914b894db658b5b Mon Sep 17 00:00:00 2001 From: forthelion Date: Sat, 17 Jun 2023 09:29:19 -0700 Subject: [PATCH 2/3] Did first comment and added is blank to it and fixed tests --- src/main/java/StringManipulation.java | 4 ++++ src/test/java/StringManipulationTest.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 3eb1f4e..ec9626e 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -14,7 +14,11 @@ public void setString(String string) { @Override public int count() { + String string = getString(); + if (string.isBlank()){ + return 0; + } String[] words = string.split("\\s+"); return words.length; diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 4127ec4..bdd6966 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -32,7 +32,7 @@ public void testCount1() { public void testCount2() { manipulatedstring.setString(""); int length = manipulatedstring.count(); - assertEquals(1, length); + assertEquals(0, length); } // testcount()3 test to see tab and new line it should print 0 From c35af55efbca740742b5f284bfd07184299018e0 Mon Sep 17 00:00:00 2001 From: forthelion Date: Sat, 17 Jun 2023 09:41:42 -0700 Subject: [PATCH 3/3] Added testRemoveNthCharacters9 and 10 on n =1 --- src/test/java/StringManipulationTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index bdd6966..6242f23 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -129,6 +129,20 @@ public void testRemoveNthCharacter8() { assertEquals("he i", manipulatedstring.removeNthCharacter(3, false)); } + // tested if 1 = n and false should leave string empty + @Test + public void testRemoveNthCharacter9() { + manipulatedstring.setString("hey i"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); + } + + // tested if 1 = n and true should leave string 5 empty spaces + @Test + public void testRemoveNthCharacter10() { + manipulatedstring.setString("hey i"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); + } + @Test public void testGeSubStrings1() { manipulatedstring.setString("This is my string");