diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 801ab56..e0d4bbd 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,4 +1,4 @@ - +import java.util.Locale; public class LoopFun { @@ -10,8 +10,12 @@ public class LoopFun * @return the factorial of the number */ public Integer factorial(Integer number){ - return null; - } + int result = 1; + for (int i = number; i > 0; i--) { + result *= i; + } + return result; + } //passed /** * Given a phrase, get the acronym of that phrase. Acronym is the combination of @@ -21,8 +25,13 @@ public Integer factorial(Integer number){ * @return Upper case string of the first letter of each word */ public String acronym(String phrase) { - return null; - } + String result = ""; + for (String s : phrase.split("\\s")) { + result += s.charAt(0); + } + result = result.toUpperCase(); + return result; + } //passed /** * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its @@ -37,6 +46,20 @@ public String acronym(String phrase) { * @return the encrypted string by shifting each character by three character */ public String encrypt(String word) { - return null; - } + char[] ch = word.toCharArray(); + for (int i = 0; i < ch.length; i++) { + if (ch[i] == 'x') { + ch[i] -= 26; + } + if (ch[i] == 'y') { + ch[i] -= 26; + } + if (ch[i] == 'z') { + ch[i] -= 26; + } + ch[i] += 3; + } + String result = new String(ch); + return result; + } //passed } diff --git a/src/main/java/MathUtilities.java b/src/main/java/MathUtilities.java index e067c75..b0359d0 100644 --- a/src/main/java/MathUtilities.java +++ b/src/main/java/MathUtilities.java @@ -9,8 +9,9 @@ public class MathUtilities{ * @return the sum of the two numbers */ public Integer add(Integer baseValue, Integer valueToAdd){ - return null; - } + Integer result = baseValue + valueToAdd; + return result; + } //passed /** * Add two number together @@ -19,8 +20,9 @@ public Integer add(Integer baseValue, Integer valueToAdd){ * @return the sum of the two numbers */ public Double add(Double baseValue, Double valueToAdd){ - return null; - } + Double result = baseValue + valueToAdd; + return result; + } //passed /** * Get half the value of the number @@ -28,8 +30,9 @@ public Double add(Double baseValue, Double valueToAdd){ * @return the half of the number in double */ public Double half(Integer number) { - return null; - } + Double result = (number.doubleValue() / 2); + return result; + } //passed /** * Determine if the number is odd @@ -37,8 +40,11 @@ public Double half(Integer number) { * @return true if the number is odd, false if it is even */ public Boolean isOdd(Integer number){ - return null; - } + if (number % 2 != 0) { + return true; + } + return false; + } //passed /** @@ -47,7 +53,7 @@ public Boolean isOdd(Integer number){ * @return the result of the number multiply by itself */ public Integer square(Integer number) { - return null; - } + return number * number; + } //passed } diff --git a/src/main/java/StringUtilities.java b/src/main/java/StringUtilities.java index e3ea8a9..ea7bd16 100644 --- a/src/main/java/StringUtilities.java +++ b/src/main/java/StringUtilities.java @@ -1,12 +1,11 @@ - public class StringUtilities { /** * @param input value to be returned * @return `input` */ public String returnInput(String input) { - return null; - } + return input; + } //passed /** * @param baseValue value to be added to @@ -14,24 +13,32 @@ public String returnInput(String input) { * @return the concatenation of `baseValue` and `valueToBeAdded` */ public String concatenate(String baseValue, String valueToBeAdded) { - return null; - } + String result = baseValue.concat(valueToBeAdded); + return result; + } //passed /** * @param valueToBeReversed value to be reversed * @return identical string with characters in opposite order */ public String reverse(String valueToBeReversed) { - return null; - } + StringBuilder sb = new StringBuilder(); + for(int i=valueToBeReversed.length()-1;i>=0;i--) { + sb.append(valueToBeReversed.charAt(i)); + } + String result = sb.toString(); + return result; + } //passed /** * @param word word to get middle character of * @return middle character of `word` */ public Character getMiddleCharacter(String word) { - return null; - } + int length = word.length(); + int middle = length / 2; + return word.charAt(middle); + } //passed /** * @param value value to have character removed from @@ -39,14 +46,17 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { - return null; - } + String str = value.replace(charToRemove.toString(),""); + return str; + } //passed /** * @param sentence String delimited by spaces representative of a sentence * @return last `word` in sentence */ public String getLastWord(String sentence) { - return null; - } + String[] strArr = sentence.split("\\s"); + String result = strArr[strArr.length-1]; + return result; + } //passed }