From 1898f20627c614ab980b90017ac707b191f27eb0 Mon Sep 17 00:00:00 2001 From: Troy Date: Wed, 29 Jun 2022 10:09:03 -0400 Subject: [PATCH 1/3] completed all test cases --- src/main/java/LoopFun.java | 89 ++++++++++++++++++------------ src/main/java/MathUtilities.java | 20 +++++-- src/main/java/StringUtilities.java | 21 +++++-- 3 files changed, 82 insertions(+), 48 deletions(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 801ab56..fae716f 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,42 +1,59 @@ -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 + */ + public Integer factorial(Integer number) { + int factorial = 1; + for (int i = number; i > 0; i--) { + factorial *= i; + } + return factorial; + } - /** - * 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; - } + /** + * 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) { + String result = phrase.replaceAll("\\B.|\\P{L}", "").toUpperCase(); + return result; + } - /** - * 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 - * at the end of the alphabet, it will wraps around. - * For example: - * 'a' => 'd' - * 'w' => 'z' - * 'x' => 'a' - * 'y' => 'b' - * @param word - * @return the encrypted string by shifting each character by three character - */ - public String encrypt(String word) { - return null; - } + /** + * 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 + * at the end of the alphabet, it will wraps around. + * For example: + * 'a' => 'd' + * 'w' => 'z' + * 'x' => 'a' + * 'y' => 'b' + * + * @param word + * @return the encrypted string by shifting each character by three character + */ + public String encrypt(String word) { + String s = ""; + int len = word.length(); + for (int i = 0; i < len; i++) { + char c = (char) (word.charAt(i) + 3); + if (c > 'z') + s += (char) (word.charAt(i) - (26 - 3)); + else { + s += (char) (word.charAt(i) + 3); + } + } + return s; + } } diff --git a/src/main/java/MathUtilities.java b/src/main/java/MathUtilities.java index e067c75..daee797 100644 --- a/src/main/java/MathUtilities.java +++ b/src/main/java/MathUtilities.java @@ -9,7 +9,8 @@ public class MathUtilities{ * @return the sum of the two numbers */ public Integer add(Integer baseValue, Integer valueToAdd){ - return null; + + return baseValue + valueToAdd; } /** @@ -19,7 +20,7 @@ public Integer add(Integer baseValue, Integer valueToAdd){ * @return the sum of the two numbers */ public Double add(Double baseValue, Double valueToAdd){ - return null; + return baseValue + valueToAdd; } /** @@ -28,7 +29,7 @@ public Double add(Double baseValue, Double valueToAdd){ * @return the half of the number in double */ public Double half(Integer number) { - return null; + return Double.valueOf((number / 2)); } /** @@ -36,8 +37,15 @@ public Double half(Integer number) { * @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) { + + boolean odd = false; + if (number % 2 == 0) { + odd = false; + } else { + odd = true; + } + return odd; } @@ -47,7 +55,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..81ae2ba 100644 --- a/src/main/java/StringUtilities.java +++ b/src/main/java/StringUtilities.java @@ -5,7 +5,7 @@ public class StringUtilities { * @return `input` */ public String returnInput(String input) { - return null; + return input; } /** @@ -14,7 +14,7 @@ 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,7 +22,11 @@ public String concatenate(String baseValue, String valueToBeAdded) { * @return identical string with characters in opposite order */ public String reverse(String valueToBeReversed) { - return null; + String r = ""; + for(int i = valueToBeReversed.length() - 1; i >= 0; i--){ + r = r + valueToBeReversed.charAt(i); + } + return r; } /** @@ -30,7 +34,10 @@ public String reverse(String valueToBeReversed) { * @return middle character of `word` */ public Character getMiddleCharacter(String word) { - return null; + int len = word.length(); + int halfWord = len / 2; + + return word.charAt(halfWord); } /** @@ -39,7 +46,8 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { - return null; + String strNew = value.replaceAll(String.valueOf(charToRemove), ""); + return strNew; } /** @@ -47,6 +55,7 @@ public String removeCharacter(String value, Character charToRemove) { * @return last `word` in sentence */ public String getLastWord(String sentence) { - return null; + String lastWord = sentence.substring(sentence.lastIndexOf(" ")+ 1); + return lastWord; } } From 0614e610d7985dddfdd0c27f5c79162f51d17c43 Mon Sep 17 00:00:00 2001 From: Troy Date: Thu, 30 Jun 2022 10:29:48 -0400 Subject: [PATCH 2/3] update --- src/main/java/LoopFun.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index fae716f..8995fc1 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -26,7 +26,8 @@ public Integer factorial(Integer number) { * @return Upper case string of the first letter of each word */ public String acronym(String phrase) { - String result = phrase.replaceAll("\\B.|\\P{L}", "").toUpperCase(); + //need to find real solution. + //String result = phrase.replaceAll("\\B.|\\P{L}", "").toUpperCase(); return result; } From 851f9e62f67577da079695a13cc36bc9301f925a Mon Sep 17 00:00:00 2001 From: Troy Date: Thu, 30 Jun 2022 13:26:10 -0400 Subject: [PATCH 3/3] finished acronym --- src/main/java/LoopFun.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 8995fc1..5fbc394 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -26,9 +26,12 @@ public Integer factorial(Integer number) { * @return Upper case string of the first letter of each word */ public String acronym(String phrase) { - //need to find real solution. //String result = phrase.replaceAll("\\B.|\\P{L}", "").toUpperCase(); - return result; + StringBuilder initials = new StringBuilder(); + for (String s : phrase.split(" ")){ + initials.append(s.charAt(0)); + } + return initials.toString().toUpperCase(); } /**