From 7cd67fce73b16f4e0faab145bce1b07d9552bdcc Mon Sep 17 00:00:00 2001 From: pnara Date: Thu, 15 Jun 2023 13:38:48 -0700 Subject: [PATCH 1/2] updated the skeleton project with my complete project. --- src/main/java/CILab.java | 32 +++++++++++++++++++++++ src/main/java/CILabInterface.java | 27 ++++++++++++++++++++ src/test/java/CILabTest.java | 42 +++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/main/java/CILab.java create mode 100644 src/main/java/CILabInterface.java create mode 100644 src/test/java/CILabTest.java diff --git a/src/main/java/CILab.java b/src/main/java/CILab.java new file mode 100644 index 0000000..9b6f300 --- /dev/null +++ b/src/main/java/CILab.java @@ -0,0 +1,32 @@ +import javax.lang.model.type.NullType; + +public class CILab implements CILabInterface { + private String myString; + + @Override + public String getString() { + return myString; + } + + @Override + public void setString(String string) { + myString = string; + } + + @Override + public boolean detectCapitalUse() { + if(myString == null) { + return false; + } + if(myString.isEmpty()) { + return false; + } + for (char c : myString.toCharArray()) { + if (Character.isUpperCase(c)) { + return true; + } + } + return false; + } +} + diff --git a/src/main/java/CILabInterface.java b/src/main/java/CILabInterface.java new file mode 100644 index 0000000..43449af --- /dev/null +++ b/src/main/java/CILabInterface.java @@ -0,0 +1,27 @@ +public interface CILabInterface { + + /** + * Returns the current string. If the string is null, it should return null. + * + * @return Current string + */ + String getString(); + + /** + * Sets the value of the current string. + * + * @param string The value to be set + */ + void setString(String string); + + /** + * We define the usage of capitals in a word to be right when one of the following cases holds: + * All letters in this word are capitals, like "USA". + * All letters in this word are not capitals, like "leetcode". + * Only the first letter in this word is capital, like "Google". + * @return Given a string word, return true if the usage of capitals in it is right. + */ + + boolean detectCapitalUse(); + +} diff --git a/src/test/java/CILabTest.java b/src/test/java/CILabTest.java new file mode 100644 index 0000000..49fd16b --- /dev/null +++ b/src/test/java/CILabTest.java @@ -0,0 +1,42 @@ +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.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class CILabTest { + + private CILabInterface myString; + + @BeforeEach + public void setUp() { + myString = new CILab(); + } + + @AfterEach + public void tearDown() { + myString = null; + } + + @Test + public void detectCapitalUseTest1() { + myString.setString("hello world"); + assertEquals(false, myString.detectCapitalUse()); + } + @Test + public void detectCapitalUseTest2() { + myString.setString("Hello World"); + assertEquals(true, myString.detectCapitalUse()); + } + @Test + public void detectCapitalUseTest3() { + myString.setString(""); + assertEquals(false, myString.detectCapitalUse()); + } + @Test + public void detectCapitalUseTest4() { + assertEquals(false, myString.detectCapitalUse()); + } + +} From d5493694d8cc380ff653510302b4ae6abeb5f093 Mon Sep 17 00:00:00 2001 From: pnara Date: Thu, 15 Jun 2023 15:22:41 -0700 Subject: [PATCH 2/2] Added the correct files this time --- src/main/java/CILab.java | 32 ---- src/main/java/CILabInterface.java | 27 ---- src/main/java/StringManipulation.java | 138 +++++++++++++++--- .../java/StringManipulationInterface.java | 6 +- src/test/java/CILabTest.java | 42 ------ src/test/java/StringManipulationTest.java | 130 +++++++++++++---- 6 files changed, 219 insertions(+), 156 deletions(-) delete mode 100644 src/main/java/CILab.java delete mode 100644 src/main/java/CILabInterface.java delete mode 100644 src/test/java/CILabTest.java diff --git a/src/main/java/CILab.java b/src/main/java/CILab.java deleted file mode 100644 index 9b6f300..0000000 --- a/src/main/java/CILab.java +++ /dev/null @@ -1,32 +0,0 @@ -import javax.lang.model.type.NullType; - -public class CILab implements CILabInterface { - private String myString; - - @Override - public String getString() { - return myString; - } - - @Override - public void setString(String string) { - myString = string; - } - - @Override - public boolean detectCapitalUse() { - if(myString == null) { - return false; - } - if(myString.isEmpty()) { - return false; - } - for (char c : myString.toCharArray()) { - if (Character.isUpperCase(c)) { - return true; - } - } - return false; - } -} - diff --git a/src/main/java/CILabInterface.java b/src/main/java/CILabInterface.java deleted file mode 100644 index 43449af..0000000 --- a/src/main/java/CILabInterface.java +++ /dev/null @@ -1,27 +0,0 @@ -public interface CILabInterface { - - /** - * Returns the current string. If the string is null, it should return null. - * - * @return Current string - */ - String getString(); - - /** - * Sets the value of the current string. - * - * @param string The value to be set - */ - void setString(String string); - - /** - * We define the usage of capitals in a word to be right when one of the following cases holds: - * All letters in this word are capitals, like "USA". - * All letters in this word are not capitals, like "leetcode". - * Only the first letter in this word is capital, like "Google". - * @return Given a string word, return true if the usage of capitals in it is right. - */ - - boolean detectCapitalUse(); - -} diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..91ec691 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,33 +1,125 @@ public class StringManipulation implements StringManipulationInterface { + private String str; - @Override - public String getString() { - return null; - } + @Override + public String getString() { + return this.str; + } - @Override - public void setString(String string) { - } + @Override + public void setString(String string) { + this.str = string; + } - @Override - public int count() { - return 0; - } + @Override + public int count() { + if (str == null || str.isEmpty()) { + return 0; + } + + String[] splitStr = str.split("\\s+"); + return splitStr.length; + /* + * int wordCount = 0; + boolean whitespace = false; - @Override - public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; - } + // Iterate through each character in the string + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); - @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; - } + // Check if the character is a whitespace + if (Character.isWhitespace(c)) { + whitespace = true; + + // Check if the character is a letter or digit + if (Character.isLetterOrDigit(str.charAt(i+1))) { + // If we are not inside a word, increment the word count + if (whitespace) { + wordCount++; + whitespace = false; + } + }*/ + } - @Override - public String restoreString(int[] indices) { - return null; - } + @Override + public String removeNthCharacter(int n, boolean maintainSpacing) { + if (n <= 0) { + throw new IllegalArgumentException("'n' must be greater than zero."); + } + if (n > str.length()) { + throw new IndexOutOfBoundsException("'n' is greater than the string length."); + } + + String result = new String(); + result = result.concat(str.charAt(0) + ""); + for (int i = 1; i < str.length(); i++) { + if((i+1) % n != 0) { + result = result.concat(str.charAt(i) + ""); + } + else { + if (maintainSpacing) { + result = result.concat(" "); + } + } + } + + return result; + } + + @Override + public String[] getSubStrings(int startWord, int endWord) { + // Check for invalid arguments + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid startWord or endWord"); + } + + // Split the sentence into words + String[] words = str.split("\\s+"); + + // Check if the sentence has enough words + if (endWord > words.length) { + throw new IndexOutOfBoundsException("The String has less than " + endWord + " words"); + } + + // Calculate the number of words to put in the array + int wordCount = endWord - startWord + 1; + + // Create a new array to store the extracted words + String[] subStrArr = new String[wordCount]; + + // Extract the words from the specified positions + for (int i = 0; i < wordCount; i++) { + subStrArr[i] = words[startWord - 1 + i]; + } + + return subStrArr; + } + + @Override + public String restoreString(int[] indices) { + //throws IllegalArgumentException if not s.length == indices.length == n + // throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length + if(str.length() != indices.length) { + throw new IllegalArgumentException("The indices array must be the same length as the string."); + } + char[] temp = str.toCharArray(); + char[] shuffled = new char[str.length()]; + String output = new String(); + for(int i = 0; i < str.length(); i++) { + if(i < indices.length) { + int pos = indices[i]; + if(pos < 0 || pos > str.length()) { + throw new IndexOutOfBoundsException("All values in the indices array must correspond to valid indexes in the string."); + } + shuffled[pos] = temp[i]; + } + else{ + shuffled[i] = temp[i]; + } + } + output = String.valueOf(shuffled); + return output; + } } diff --git a/src/main/java/StringManipulationInterface.java b/src/main/java/StringManipulationInterface.java index 87127e7..cc81e80 100644 --- a/src/main/java/StringManipulationInterface.java +++ b/src/main/java/StringManipulationInterface.java @@ -79,10 +79,10 @@ public interface StringManipulationInterface { * The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. * Return the shuffled string. * example: - * Input: string = "UnitTest", indices = [4,5,6,7,0,1,2,3] + * Input: string = "UnitTest", indices = [4,5,6,7,0,2,1,3] * Output: "TestUnit" * Explanation: - * indices: 4 5 6 7 0 1 2 3 + * indices: 4 5 6 7 0 2 1 3 * String: U n i t T e s t * Actions to Shuffle: Shift U to 4th position, n to 5th position, i to 6th position ...... * Output: T e s t U n i t @@ -95,7 +95,7 @@ public interface StringManipulationInterface { * indices length is the same as the string length. * * throws IllegalArgumentException if not s.length == indices.length == n - * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length + * throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]> string length * * @param indices is an integer array for shuffled string new indices positions * the character at the ith position moves to indices[i] in the shuffled string. diff --git a/src/test/java/CILabTest.java b/src/test/java/CILabTest.java deleted file mode 100644 index 49fd16b..0000000 --- a/src/test/java/CILabTest.java +++ /dev/null @@ -1,42 +0,0 @@ -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.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -public class CILabTest { - - private CILabInterface myString; - - @BeforeEach - public void setUp() { - myString = new CILab(); - } - - @AfterEach - public void tearDown() { - myString = null; - } - - @Test - public void detectCapitalUseTest1() { - myString.setString("hello world"); - assertEquals(false, myString.detectCapitalUse()); - } - @Test - public void detectCapitalUseTest2() { - myString.setString("Hello World"); - assertEquals(true, myString.detectCapitalUse()); - } - @Test - public void detectCapitalUseTest3() { - myString.setString(""); - assertEquals(false, myString.detectCapitalUse()); - } - @Test - public void detectCapitalUseTest4() { - assertEquals(false, myString.detectCapitalUse()); - } - -} diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..c218b0e 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -28,58 +28,89 @@ public void testCount1() { @Test public void testCount2() { - fail("Not yet implemented"); + manipulatedstring.setString("Thisismystring"); + int length = manipulatedstring.count(); + assertEquals(1, length); } @Test public void testCount3() { - fail("Not yet implemented"); + manipulatedstring.setString("23 and me"); + int length = manipulatedstring.count(); + assertEquals(3, length); } @Test - public void testCount4() { - fail("Not yet implemented"); + public void testCount4() { + manipulatedstring.setString("This is my string"); + int length = manipulatedstring.count(); + assertEquals(4, length); + } + + @Test + public void testCount5() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @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)); + String temp = manipulatedstring.removeNthCharacter(3, false); + assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", temp); } @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)); + String temp = manipulatedstring.removeNthCharacter(3, true); + assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", temp); } @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + String temp = manipulatedstring.removeNthCharacter(5, false); + + assertEquals("I'd 3tt3 puts0med161s inthis5tr16, rght?", temp); } @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("I'd 3tt3 put s0me d161 s in this 5tr1 6, r ght?", manipulatedstring.removeNthCharacter(5, true)); } @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.removeNthCharacter(100, false)); + assertEquals("'n' is greater than the string length.", exception.getMessage()); } @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(0, false)); + assertEquals("'n' must be greater than zero.", exception.getMessage()); } - + @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.removeNthCharacter(-1, false)); + assertEquals("'n' must be greater than zero.", exception.getMessage()); } @Test - public void testGeSubStrings1() { + public void testRemoveNthCharacter8() { + manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); + assertEquals("I'd b3tt3r put s0me d161ts in this 5tr1n6, right", manipulatedstring.removeNthCharacter(49, false)); + } + + @Test + public void testGetSubStrings1() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -88,24 +119,49 @@ public void testGeSubStrings1() { } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2() { + manipulatedstring.setString("Thisismystring"); + Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(3, 4)); + assertEquals("The String has less than 4 words", exception.getMessage()); } @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3() { + manipulatedstring.setString(""); + Throwable exception = assertThrows(Exception.class, () -> manipulatedstring.getSubStrings(1, 4)); + assertEquals("The String has less than 4 words", exception.getMessage()); } @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4() { + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(3, 3); + + assertEquals(sStings[0], "my"); } @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(5, 1)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); } @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.getSubStrings(1, 100)); + assertEquals("The String has less than 100 words", exception.getMessage()); + } + + @Test + public void testGetSubStrings7() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(-1, 3)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); + } + + @Test + public void testGetSubStrings8() { + manipulatedstring.setString("This is my string"); + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.getSubStrings(1, -3)); + assertEquals("Invalid startWord or endWord", exception.getMessage()); } @Test @@ -121,28 +177,44 @@ public void testRestoreString1() @Test public void testRestoreString2() { - fail("Not yet implemented"); - + manipulatedstring.setString("This is my string"); + + int [] array; + array=new int[]{1,0,2,5,3,4,7,6}; + Throwable exception = assertThrows(IllegalArgumentException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("The indices array must be the same length as the string.", exception.getMessage()); } @Test public void testRestoreString3() { - fail("Not yet implemented"); - + manipulatedstring.setString("This is my string"); + int [] array; + array=new int[]{-1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,14,11}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); } @Test public void testRestoreString4() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + int [] array; + array=new int[]{1,0,2,5,3,4,7,6,13,15,16,9,10,12,8,100,11}; + Throwable exception = assertThrows(IndexOutOfBoundsException.class, () -> manipulatedstring.restoreString(array) ); + assertEquals("All values in the indices array must correspond to valid indexes in the string.", exception.getMessage()); + } @Test public void testRestoreString5() { - fail("Not yet implemented"); + manipulatedstring.setString("second test "); + int [] array; + array=new int[]{11,10,9,8,7,6,5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, " tset dnoces"); }