diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 801ab56..cb77820 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,4 +1,4 @@ - +import java.util.Arrays; public class LoopFun { @@ -10,7 +10,14 @@ public class LoopFun * @return the factorial of the number */ public Integer factorial(Integer number){ - return null; + Integer factorialResult = 1; + for (int i = 1; i <= number; i++) { + factorialResult = factorialResult * i; + } + return factorialResult; + + //ternary operator would've worked too + //return (number ==1) || (number == 0) ? 1 : number * factorial(number -1); } /** @@ -21,7 +28,23 @@ public Integer factorial(Integer number){ * @return Upper case string of the first letter of each word */ public String acronym(String phrase) { - return null; + StringBuilder acronym = new StringBuilder(); + String input = phrase; + input = input.toUpperCase(); + String[] phraseArray = input.split(" "); + for (int i = 0; i < phraseArray.length; i++) { + acronym.append(phraseArray[i].charAt(0)); + } + String result = acronym.toString(); + return result; + + /*String [] sentence = phrase.split(" "); + String result = ""; + for (String s : sentence) { + result = result + s.charAt(0); + } + return result.toUpperCase(); + */ } /** @@ -37,6 +60,52 @@ public String acronym(String phrase) { * @return the encrypted string by shifting each character by three character */ public String encrypt(String word) { - return null; + //this code doesn't check for letters, and can encrypt special characters... + //Not the best implementation, but now the method only accepts strings composed of letters. How it re-enters input + //would have to be considered where the method is called. + for (int j = 0; j < word.length(); j++) { + if (!Character.isLetter(word.charAt(j))) { + return "Please only enter letters"; + } + } + //The actual, used code. The numeric values of the letters weren't matching unicode... I've got to find java's numeric values. + char[] charArray = new char[word.length()]; + + for (int i = 0; i < word.length(); i++) { + int numberValueOfCurrentChar = java.lang.Character.getNumericValue(word.charAt(i)); + if (numberValueOfCurrentChar >= 33) { + int encryptedValue = (word.charAt(i) - 23); + charArray[i] = (char) encryptedValue; + } else { + int encryptedValue = (word.charAt(i) + 3); + charArray[i] = (char) encryptedValue; + } + } + String encryptedWord = String.valueOf(charArray); + return encryptedWord; + + //second attempt +// int[] valueOfChars = new int[word.length()]; +// System.out.println(valueOfChars); +// for (int i = 0; i < word.length(); i++) { +// valueOfChars[i] = word.charAt(i) + 3; +// } +// System.out.println(valueOfChars); +// return null; + + //first attempt -_- +// char[] lettersToChange = new char[word.length()]; +// char currentLetter; +// for (int i = 0; i = 0; i--) { + reverser.append(valueToBeReversed.charAt(i)); + } + String result = reverser.toString(); + return result; + + // return new StringBuilder(valueToBeReversed).reverse().toString(); // a one line solution. } /** @@ -30,7 +38,14 @@ public String reverse(String valueToBeReversed) { * @return middle character of `word` */ public Character getMiddleCharacter(String word) { - return null; + if (word.length() % 2 == 0) { + throw new IllegalArgumentException("Word must contain odd number of letters"); + } + int characterIndexToRetrieve = word.length() / 2; + Character result = word.charAt(characterIndexToRetrieve); + return result; + + //return word.charAt(word.length()/2); // one line solution that does the same thing. } /** @@ -39,7 +54,7 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { - return null; + return value.replaceAll(charToRemove.toString(), ""); } /** @@ -47,6 +62,24 @@ public String removeCharacter(String value, Character charToRemove) { * @return last `word` in sentence */ public String getLastWord(String sentence) { - return null; + + //code directly below would extract all non letters and keep sentence structure w/ spaces. That way our last 'word' doesn't include punctuation. +// String updatedSentence = ""; +// for (int i = 0; i < sentence.length(); i++) { +// if (Character.isLetter(sentence.charAt(i)) || Character.isSpaceChar(sentence.charAt(i))) { +// updatedSentence = updatedSentence + sentence.charAt((i)); +// } +// } +// System.out.println(updatedSentence); + //this works but I don't like it. It meets the test cases, but still includes punctuation. may update method & test cases. + String[] input = sentence.split(" "); + return input[input.length -1]; + + //or + //ArrayList inputSentence = new ArrayList<>(sentence.split("")); + //return inputSentence.get(inputSentence.size()-1; + +// or + //could have also used a variable to spot the last space, store that index position. then use that with input.substring(); } } diff --git a/src/test/java/LoopFunEncryptTest.java b/src/test/java/LoopFunEncryptTest.java index 492f1dd..29e8513 100644 --- a/src/test/java/LoopFunEncryptTest.java +++ b/src/test/java/LoopFunEncryptTest.java @@ -41,4 +41,17 @@ public void testEncryptWithTheLastLetters() { //Then assertEquals(expected, actual); } + +// @Test +// public void testEncryptNoSpecialCharacters() { +// //Given +// String word = "quiz1!@#"; +// String expected = "Please only enter letters"; +// +// //When +// String actual = loop.encrypt(word); +// +// //Then +// assertEquals(expected, actual); +// } } diff --git a/src/test/java/LoopsFunAcronymTest.java b/src/test/java/LoopsFunAcronymTest.java index 61e2f5b..8ef11cb 100644 --- a/src/test/java/LoopsFunAcronymTest.java +++ b/src/test/java/LoopsFunAcronymTest.java @@ -51,6 +51,7 @@ public void test3() { assertEquals(expected, actual); } + @Test public void test6() { // given String phrase = "Center Disease Control Whistle Blower"; @@ -63,6 +64,7 @@ public void test6() { assertEquals(expected, actual); } + @Test public void test7() { // given String phrase = "Tuskegee Macon County, Alabama"; @@ -75,6 +77,7 @@ public void test7() { assertEquals(expected, actual); } + @Test public void test8() { // given String phrase = "John Charles Cutler"; @@ -100,6 +103,7 @@ public void test9() { assertEquals(expected, actual); } + @Test public void test4() { // given String phrase = "Tactical Reconnaissance Nuclear Powered Aerospace Platform"; diff --git a/src/test/java/StringUtilsGetLastWordTest.java b/src/test/java/StringUtilsGetLastWordTest.java index e03d1f9..dcb4b00 100644 --- a/src/test/java/StringUtilsGetLastWordTest.java +++ b/src/test/java/StringUtilsGetLastWordTest.java @@ -40,7 +40,7 @@ public void test2(){ } @Test - public void test4() { + public void test3() { // Given String sentence = "quantum information conservation"; String expected = "conservation"; @@ -53,7 +53,7 @@ public void test4() { } @Test - public void test5() { + public void test4() { // Given String sentence = "The universe yields geometries so elegant, they animate with intelligence."; String expected = "intelligence."; diff --git a/src/test/java/StringUtilsGetMiddleCharacterTest.java b/src/test/java/StringUtilsGetMiddleCharacterTest.java index 5447b19..359ac78 100644 --- a/src/test/java/StringUtilsGetMiddleCharacterTest.java +++ b/src/test/java/StringUtilsGetMiddleCharacterTest.java @@ -66,4 +66,18 @@ public void test4(){ //Then assertEquals(expected, actual); } + + //I don't think I did the exception correctly. +// @Test(expected = Exception.class) +// public void testStringWithEvenNoOfCharacters() { +// //Given +// String word = "pooppoop"; +// String expected = "Word must contain odd number of letters"; +// +// //When +// char actual = utilities.getMiddleCharacter(word); +// //Then +// assertEquals(expected, actual); +// +// } } diff --git a/src/test/java/TestSuite.java b/src/test/java/TestSuite.java new file mode 100644 index 0000000..b0b6aa5 --- /dev/null +++ b/src/test/java/TestSuite.java @@ -0,0 +1,24 @@ + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) + +@Suite.SuiteClasses({ + LoopFunEncryptTest.class, + LoopFunFactorialTest.class, + LoopsFunAcronymTest.class, + MathUtilitiesAddTest.class, + MathUtilitiesHalfTest.class, + MathUtilitiesIsOddTest.class, + MathUtilitiesSquareTest.class, + StringUtilsConcatenateTest.class, + StringUtilsGetLastWordTest.class, + StringUtilsGetMiddleCharacterTest.class, + StringUtilsRemoveCharacterTest.class, + StringUtilsReturnInputTest.class, + StringUtilsReverseTest.class, +}) + +public class TestSuite { +}