From cbe0d5c849e44ab943bf24836eeac17d20f6598a Mon Sep 17 00:00:00 2001 From: jd Date: Wed, 29 Jun 2022 12:18:04 -0400 Subject: [PATCH 1/5] first failed quiz --- src/main/java/LoopFun.java | 74 ++++++++++++++++++++++-------- src/main/java/MathUtilities.java | 74 +++++++++++++++++------------- src/main/java/StringUtilities.java | 41 +++++++++++++++-- 3 files changed, 134 insertions(+), 55 deletions(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 801ab56..72ef4f1 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,27 +1,46 @@ - +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 + */ + + // 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 = 0; + for (int i = 1; i <= number; i++) { + sum = number *= i; + sum++; + } + + return sum; +// while(int i <= number){ +// number = number * i; +// i++ +// } +// return 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 + */ +// public String acronym(String phrase) { +// phrase.split(""); +// for (int i = 0; i <= phrase; i++){ +// System.out.println(i); +// } - /** - * 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; +} } /** @@ -37,6 +56,21 @@ public String acronym(String phrase) { * @return the encrypted string by shifting each character by three character */ public String encrypt(String word) { +// 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 alphaString = "abcdefghijklmnopqrstuvwxyz"; +// alphaString.split(""); +// for( int i = 0; i <= word; i++ ) +// if(int i = a){ +// } + + + + return null; } } 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..ef6c279 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,14 +24,33 @@ 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) { +// int position; +// int size; +// if (word.length() % 2 == 0){ +// position = word.length() / 2 - 1; +// size = 2; +// }else { +// position = word.length() / 2; +// size = 1; +// } +// return word.substring(position, position + size); return null; } @@ -39,6 +60,8 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { + String newStr = String.replace(charToRemove, " "); + return null; } @@ -47,6 +70,16 @@ public String removeCharacter(String value, Character charToRemove) { * @return last `word` in sentence */ public String getLastWord(String sentence) { - return null; + String[] word = sentence.split(" "); + for(int i = 0; i <= word.length; i++){ + System.out.println(word[i].length() - 1); + } +// sentence.split(""); +// for(int i = 0; i <= sentence; i++){ +// +// } + return ; + } + } From e73fee1ee37431f51b3f4bfeb6030f4666c3892d Mon Sep 17 00:00:00 2001 From: jd Date: Wed, 29 Jun 2022 18:16:57 -0400 Subject: [PATCH 2/5] finally got factorial complete close to completing Acronym --- src/main/java/LoopFun.java | 34 +++++++++++++++--------------- src/main/java/StringUtilities.java | 26 +++++++++++------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 72ef4f1..4dfd19e 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -1,3 +1,4 @@ +import java.util.Locale; import java.util.Scanner; public class LoopFun { @@ -12,20 +13,16 @@ public class LoopFun { // 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 = 0; - for (int i = 1; i <= number; i++) { - sum = number *= i; - sum++; + int sum = 1; + for (int i = number; i > 0; i--) { + sum = sum * i; + System.out.println(sum); } return sum; -// while(int i <= number){ -// number = number * i; -// i++ -// } -// return number; } + /** * Given a phrase, get the acronym of that phrase. Acronym is the combination of * the first character of each word in upper case. @@ -33,16 +30,19 @@ public Integer factorial(Integer number) { * @param phrase * @return Upper case string of the first letter of each word */ -// public String acronym(String phrase) { -// phrase.split(""); -// for (int i = 0; i <= phrase; i++){ -// System.out.println(i); -// } - - return null; -} + public String acronym(String phrase) { + String[] strList = phrase.split(" "); + String ans = ""; + for (int i = 0; i <= strList.length; i++){ + ans += strList[i].toUpperCase().charAt(0); + System.out.println(ans); + } + System.out.println(ans.toUpperCase()); + 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 diff --git a/src/main/java/StringUtilities.java b/src/main/java/StringUtilities.java index ef6c279..4fae8b9 100644 --- a/src/main/java/StringUtilities.java +++ b/src/main/java/StringUtilities.java @@ -41,16 +41,16 @@ public String reverse(String valueToBeReversed) { // 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) { -// int position; -// int size; -// if (word.length() % 2 == 0){ -// position = word.length() / 2 - 1; -// size = 2; -// }else { -// position = word.length() / 2; -// size = 1; -// } -// return word.substring(position, position + size); +//// int position; +//// int size; +//// if (word.length() % 2 == 0){ +//// position = word.length() / 2 - 1; +//// size = 2; +//// }else { +//// position = word.length() / 2; +//// size = 1; +//// } +//// return word.substring(position, position + size); return null; } @@ -60,7 +60,7 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { - String newStr = String.replace(charToRemove, " "); +// String newStr = String.replace(charToRemove, " "); return null; } @@ -72,13 +72,13 @@ public String removeCharacter(String value, Character charToRemove) { public String getLastWord(String sentence) { String[] word = sentence.split(" "); for(int i = 0; i <= word.length; i++){ - System.out.println(word[i].length() - 1); + System.out.println(word[i].toString().length() - 1); } // sentence.split(""); // for(int i = 0; i <= sentence; i++){ // // } - return ; + return null ; } From 64865537ed8d2ee2d03422d31e165c55ff76d060 Mon Sep 17 00:00:00 2001 From: jd Date: Thu, 30 Jun 2022 15:09:37 -0400 Subject: [PATCH 3/5] completed quiz after walk through --- src/main/java/LoopFun.java | 24 +++++++++++------------- src/main/java/StringUtilities.java | 26 +++++--------------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index 4dfd19e..fdfa42c 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -33,11 +33,9 @@ public Integer factorial(Integer number) { public String acronym(String phrase) { String[] strList = phrase.split(" "); String ans = ""; - for (int i = 0; i <= strList.length; i++){ + for (int i = 0; i <= strList.length -1; i++){ ans += strList[i].toUpperCase().charAt(0); - System.out.println(ans); } - System.out.println(ans.toUpperCase()); return ans; } @@ -61,16 +59,16 @@ public String encrypt(String word) { // 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 alphaString = "abcdefghijklmnopqrstuvwxyz"; -// alphaString.split(""); -// for( int i = 0; i <= word; i++ ) -// if(int i = a){ -// } +// 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 += 2; + idx = idx % 26; + encWord.append(alpha.charAt(idx)); + } + return encWord.toString(); - - - - return null; } } diff --git a/src/main/java/StringUtilities.java b/src/main/java/StringUtilities.java index 4fae8b9..951107c 100644 --- a/src/main/java/StringUtilities.java +++ b/src/main/java/StringUtilities.java @@ -41,17 +41,8 @@ public String reverse(String valueToBeReversed) { // 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) { -//// int position; -//// int size; -//// if (word.length() % 2 == 0){ -//// position = word.length() / 2 - 1; -//// size = 2; -//// }else { -//// position = word.length() / 2; -//// size = 1; -//// } -//// return word.substring(position, position + size); - return null; + return word.charAt(word.length() / 2); + } /** @@ -60,9 +51,9 @@ public Character getMiddleCharacter(String word) { * @return `value` with char of value `charToRemove` removed */ public String removeCharacter(String value, Character charToRemove) { -// String newStr = String.replace(charToRemove, " "); + String s = value.replaceAll(String.valueOf(charToRemove), ""); - return null; + return s; } /** @@ -71,14 +62,7 @@ public String removeCharacter(String value, Character charToRemove) { */ public String getLastWord(String sentence) { String[] word = sentence.split(" "); - for(int i = 0; i <= word.length; i++){ - System.out.println(word[i].toString().length() - 1); - } -// sentence.split(""); -// for(int i = 0; i <= sentence; i++){ -// -// } - return null ; + return word[word.length - 1]; } From c6d6abf032bab4dc067f1165f91c46c31e77b45f Mon Sep 17 00:00:00 2001 From: jd Date: Thu, 30 Jun 2022 19:40:28 -0400 Subject: [PATCH 4/5] completed final update --- src/main/java/LoopFun.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/LoopFun.java b/src/main/java/LoopFun.java index fdfa42c..88d5b35 100644 --- a/src/main/java/LoopFun.java +++ b/src/main/java/LoopFun.java @@ -64,7 +64,7 @@ public String encrypt(String word) { StringBuilder encWord = new StringBuilder(); for (int i = 0; i < word.length();i ++){ int idx = alpha.indexOf(word.charAt(i)); - idx += 2; + idx += 3; idx = idx % 26; encWord.append(alpha.charAt(idx)); } From 283c6c937629f2ce04597a65961da8aa43e9e302 Mon Sep 17 00:00:00 2001 From: jd Date: Fri, 1 Jul 2022 11:01:41 -0400 Subject: [PATCH 5/5] test change --- src/test/java/MathUtilitiesHalfTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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