LoopFun.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. public class LoopFun
  2. {
  3. /**
  4. * Given a number, return the factorial of that number.
  5. * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
  6. * @param number
  7. * @return the factorial of the number
  8. */
  9. public int factorial(int number){
  10. int factorial = 1;
  11. for (int i = number; i >0; i--){
  12. factorial = factorial * i;
  13. }
  14. return factorial;
  15. }
  16. /**
  17. * Given a phrase, get the acronym of that phrase. Acronym is the combination of
  18. * the first character of each word in upper case.
  19. * For example, given "Ruby on Rails", this method will return "ROR"
  20. * @param phrase
  21. * @return Upper case string of the first letter of each word
  22. */
  23. public String acronym(String phrase) {
  24. String str = phrase.substring(0,1);
  25. for (int i =0; i < phrase.length(); i++){
  26. if (phrase.charAt(i) == (' ')){
  27. str = str + phrase.charAt(i+1);
  28. }
  29. str = str.toUpperCase();
  30. } return str;
  31. }
  32. /**
  33. * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
  34. * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
  35. * at the end of the alphabet, it will wraps around.
  36. * For example:
  37. * 'a' => 'd'
  38. * 'w' => 'z'
  39. * 'x' => 'a'
  40. * 'y' => 'b'
  41. * @param word
  42. * @return the encrypted string by shifting each character by three character
  43. */
  44. public String encrypt(String word) {
  45. StringBuilder sB = new StringBuilder();
  46. for (int i =0; i < word.length(); i++){
  47. if (word.charAt(i) == 'a'){
  48. sB = sB.append("d");
  49. }else if (word.charAt(i) == 'b'){
  50. sB = sB.append("e");
  51. }else if (word.charAt(i) == 'c'){
  52. sB = sB.append("f");
  53. } else if (word.charAt(i) == 'd'){
  54. sB = sB.append("g");
  55. } else if (word.charAt(i) == 'e'){
  56. sB = sB.append("h");
  57. } else if (word.charAt(i) == 'f'){
  58. sB = sB.append("i");
  59. } else if (word.charAt(i) == 'g'){
  60. sB = sB.append("j");
  61. } else if (word.charAt(i) == 'h'){
  62. sB = sB.append("k");
  63. } else if (word.charAt(i) == 'i'){
  64. sB = sB.append("l");
  65. } else if (word.charAt(i) == 'j'){
  66. sB = sB.append("m");
  67. } else if (word.charAt(i) == 'k'){
  68. sB = sB.append("n");
  69. } else if (word.charAt(i) == 'l'){
  70. sB = sB.append("o");
  71. } else if (word.charAt(i) == 'm'){
  72. sB = sB.append("p");
  73. } else if (word.charAt(i) == 'n'){
  74. sB = sB.append("q");
  75. } else if (word.charAt(i) == 'o'){
  76. sB = sB.append("r");
  77. } else if (word.charAt(i) == 'p'){
  78. sB = sB.append("s");
  79. } else if (word.charAt(i) == 'q'){
  80. sB = sB.append("t");
  81. } else if (word.charAt(i) == 'r'){
  82. sB = sB.append("u");
  83. } else if (word.charAt(i) == 's'){
  84. sB = sB.append("v");
  85. } else if (word.charAt(i) == 't'){
  86. sB = sB.append("w");
  87. } else if (word.charAt(i) == 'u'){
  88. sB = sB.append("x");
  89. } else if (word.charAt(i) == 'v'){
  90. sB = sB.append("y");
  91. } else if (word.charAt(i) == 'w'){
  92. sB = sB.append("z");
  93. } else if (word.charAt(i) == 'x'){
  94. sB = sB.append("a");
  95. } else if (word.charAt(i) == 'y'){
  96. sB = sB.append("b");
  97. } else if (word.charAt(i) == 'z'){
  98. sB = sB.append("c");
  99. }
  100. }return sB.toString();
  101. }
  102. }