diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 801ab56..88d5b35 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,29 +1,46 @@ - +import java.util.Locale; +import java.util.Scanner; -public class LoopFun -{ +public class LoopFun { - /** - * Given a number, return the factorial of that number. - * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120. - * @param number - * @return the factorial of the number - */ - public Integer factorial(Integer number){ - return null; - } + /** + * Given a number, return the factorial of that number. + * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120. + * + * @param number + * @return the factorial of the number + */ - /** - * Given a phrase, get the acronym of that phrase. Acronym is the combination of - * the first character of each word in upper case. - * For example, given "Ruby on Rails", this method will return "ROR" - * @param phrase - * @return Upper case string of the first letter of each word - */ + // Getting first test to pass, I know I'm on the right track but must move on to other questions + public Integer factorial(Integer number) { + int sum = 1; + for (int i = number; i > 0; i--) { + sum = sum * i; + System.out.println(sum); + } + + return sum; + } + + + /** + * Given a phrase, get the acronym of that phrase. Acronym is the combination of + * the first character of each word in upper case. + * For example, given "Ruby on Rails", this method will return "ROR" + * @param phrase + * @return Upper case string of the first letter of each word + */ public String acronym(String phrase) { - return null; + String[] strList = phrase.split(" "); + String ans = ""; + for (int i = 0; i <= strList.length -1; i++){ + ans += strList[i].toUpperCase().charAt(0); + } + return ans; } + + /** * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is @@ -37,6 +54,21 @@ public String acronym(String phrase) { * @return the encrypted string by shifting each character by three character */ public String encrypt(String word) { - return null; +// thinking process LONG VERSION......create string of alphabet +// split the alphabet string +// run for loop on "words" String +// for each character of "words" string compare it to its position in alphabet string + 3 +// return the new element +// Not really sure how to accomplish this + String alpha = "abcdefghijklmnopqrstuvwxyz"; + StringBuilder encWord = new StringBuilder(); + for (int i = 0; i < word.length();i ++){ + int idx = alpha.indexOf(word.charAt(i)); + idx += 3; + idx = idx % 26; + encWord.append(alpha.charAt(idx)); + } + return encWord.toString(); + } } diff --git a/src/main/java/MathUtilities.java b/src/main/java/MathUtilities.java index e067c75..ecfb655 100644 --- a/src/main/java/MathUtilities.java +++ b/src/main/java/MathUtilities.java @@ -1,43 +1,55 @@ -public class MathUtilities{ - - /** - * Add two number together - * @param baseValue first number - * @param valueToAdd number - * @return the sum of the two numbers - */ - public Integer add(Integer baseValue, Integer valueToAdd){ - return null; - } +public class MathUtilities { + + /** + * Add two number together + * + * @param baseValue first number + * @param valueToAdd number + * @return the sum of the two numbers + */ + public Integer add(Integer baseValue, Integer valueToAdd) { + + return baseValue + valueToAdd; + } + + /** + * Add two number together + * + * @param baseValue first number + * @param valueToAdd second number + * @return the sum of the two numbers + */ + public Double add(Double baseValue, Double valueToAdd) { + return baseValue + valueToAdd; + } + + /** + * Get half the value of the number + * + * @param number the number given + * @return the half of the number in double + */ + public Double half(Integer number) { + double ans = (number / 2); + return ans; + } - /** - * Add two number together - * @param baseValue first number - * @param valueToAdd second number - * @return the sum of the two numbers - */ - public Double add(Double baseValue, Double valueToAdd){ - return null; - } - /** - * Get half the value of the number - * @param number the number given - * @return the half of the number in double - */ - public Double half(Integer number) { - return null; - } /** * Determine if the number is odd * @param number the number given * @return true if the number is odd, false if it is even */ - public Boolean isOdd(Integer number){ - return null; + public Boolean isOdd(Integer number) { + if (number % 2 == 1) { + return true; + } else { + + return false; + } } @@ -47,7 +59,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; } } diff --git a/src/main/java/StringUtilities.java b/src/main/java/StringUtilities.java index e3ea8a9..951107c 100644 --- a/src/main/java/StringUtilities.java +++ b/src/main/java/StringUtilities.java @@ -5,7 +5,8 @@ public class StringUtilities { * @return `input` */ public String returnInput(String input) { - return null; + + return input; } /** @@ -14,7 +15,8 @@ public String returnInput(String input) { * @return the concatenation of `baseValue` and `valueToBeAdded` */ public String concatenate(String baseValue, String valueToBeAdded) { - return null; + + return (baseValue + valueToBeAdded); } /** @@ -22,15 +24,25 @@ public String concatenate(String baseValue, String valueToBeAdded) { * @return identical string with characters in opposite order */ public String reverse(String valueToBeReversed) { - return null; + String results = ""; + + for (int i = valueToBeReversed.length() - 1; i >= 0; i--){ + results = results + valueToBeReversed.charAt(i); + + } + return results; + } /** * @param word word to get middle character of * @return middle character of `word` */ + +// I know im close, still a beginner and trying to figure it all out, had to comment out "getMiddlechar" to run other test public Character getMiddleCharacter(String word) { - return null; + return word.charAt(word.length() / 2); + } /** @@ -39,7 +51,9 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { - return null; + String s = value.replaceAll(String.valueOf(charToRemove), ""); + + return s; } /** @@ -47,6 +61,9 @@ public String removeCharacter(String value, Character charToRemove) { * @return last `word` in sentence */ public String getLastWord(String sentence) { - return null; + String[] word = sentence.split(" "); + return word[word.length - 1]; + } + } diff --git a/src/test/java/MathUtilitiesHalfTest.java b/src/test/java/MathUtilitiesHalfTest.java index 586afc8..d110492 100644 --- a/src/test/java/MathUtilitiesHalfTest.java +++ b/src/test/java/MathUtilitiesHalfTest.java @@ -47,7 +47,7 @@ public void test3(){ //Given double expected = 8; - //When + //When ?tests double actual = utilities.half(16); //Then