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 int factorial(int number){ int fact = 1; for(int i = 1; i <= number; i++) { fact *= i; } return fact; } /** * 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[] strArr = phrase.split(" "); String acronym = ""; for(int i = 0; i < strArr.length; i++) { String tempStr = strArr[i].substring(0, 1); acronym = acronym.concat(tempStr.toUpperCase()); } return acronym; } /** * 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 wrap around. * For example: * 'a' => 'd' * 'w' => 'z' * 'x' => 'a' * 'y' => 'b' * @param word * @return the encrypted string by shifting each character by three character * ansi 97 ('a') to 122 ('z') */ public String encrypt(String word) { char[] charArr = word.toCharArray(); for(int i = 0; i < charArr.length; i++) { int tempInt = ((int)charArr[i]) + 3; if(tempInt > 122) { tempInt -= 26; } charArr[i] = (char)tempInt; } String returnStr = new String(charArr); return returnStr; } }