123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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 factorial = 1;
- for (int i = number; i >0; i--){
- factorial = 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) {
- String str = phrase.substring(0,1);
- for (int i =0; i < phrase.length(); i++){
- if (phrase.charAt(i) == (' ')){
- str = str + phrase.charAt(i+1);
- }
- str = str.toUpperCase();
-
-
- } return str;
- }
-
- /**
- * 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) {
- StringBuilder sB = new StringBuilder();
- for (int i =0; i < word.length(); i++){
- if (word.charAt(i) == 'a'){
- sB = sB.append("d");
- }else if (word.charAt(i) == 'b'){
- sB = sB.append("e");
- }else if (word.charAt(i) == 'c'){
- sB = sB.append("f");
- } else if (word.charAt(i) == 'd'){
- sB = sB.append("g");
- } else if (word.charAt(i) == 'e'){
- sB = sB.append("h");
- } else if (word.charAt(i) == 'f'){
- sB = sB.append("i");
- } else if (word.charAt(i) == 'g'){
- sB = sB.append("j");
- } else if (word.charAt(i) == 'h'){
- sB = sB.append("k");
- } else if (word.charAt(i) == 'i'){
- sB = sB.append("l");
- } else if (word.charAt(i) == 'j'){
- sB = sB.append("m");
- } else if (word.charAt(i) == 'k'){
- sB = sB.append("n");
- } else if (word.charAt(i) == 'l'){
- sB = sB.append("o");
- } else if (word.charAt(i) == 'm'){
- sB = sB.append("p");
- } else if (word.charAt(i) == 'n'){
- sB = sB.append("q");
- } else if (word.charAt(i) == 'o'){
- sB = sB.append("r");
- } else if (word.charAt(i) == 'p'){
- sB = sB.append("s");
- } else if (word.charAt(i) == 'q'){
- sB = sB.append("t");
- } else if (word.charAt(i) == 'r'){
- sB = sB.append("u");
- } else if (word.charAt(i) == 's'){
- sB = sB.append("v");
- } else if (word.charAt(i) == 't'){
- sB = sB.append("w");
- } else if (word.charAt(i) == 'u'){
- sB = sB.append("x");
- } else if (word.charAt(i) == 'v'){
- sB = sB.append("y");
- } else if (word.charAt(i) == 'w'){
- sB = sB.append("z");
- } else if (word.charAt(i) == 'x'){
- sB = sB.append("a");
- } else if (word.charAt(i) == 'y'){
- sB = sB.append("b");
- } else if (word.charAt(i) == 'z'){
- sB = sB.append("c");
- }
-
- }return sB.toString();
- }
- }
|