Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 57 additions & 36 deletions src/main/java/LoopFun.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@


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();
StringBuilder initials = new StringBuilder();
for (String s : phrase.split(" ")){
initials.append(s.charAt(0));
}
return initials.toString().toUpperCase();
}

/**
* 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;
}
}
20 changes: 14 additions & 6 deletions src/main/java/MathUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -28,16 +29,23 @@ 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));
}

/**
* 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) {

boolean odd = false;
if (number % 2 == 0) {
odd = false;
} else {
odd = true;
}
return odd;
}


Expand All @@ -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;
}

}
21 changes: 15 additions & 6 deletions src/main/java/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class StringUtilities {
* @return `input`
*/
public String returnInput(String input) {
return null;
return input;
}

/**
Expand All @@ -14,23 +14,30 @@ public String returnInput(String input) {
* @return the concatenation of `baseValue` and `valueToBeAdded`
*/
public String concatenate(String baseValue, String valueToBeAdded) {
return null;
return baseValue + valueToBeAdded;
}

/**
* @param valueToBeReversed value to be reversed
* @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;
}

/**
* @param word word to get middle character of
* @return middle character of `word`
*/
public Character getMiddleCharacter(String word) {
return null;
int len = word.length();
int halfWord = len / 2;

return word.charAt(halfWord);
}

/**
Expand All @@ -39,14 +46,16 @@ 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;
}

/**
* @param sentence String delimited by spaces representative of a sentence
* @return last `word` in sentence
*/
public String getLastWord(String sentence) {
return null;
String lastWord = sentence.substring(sentence.lastIndexOf(" ")+ 1);
return lastWord;
}
}