12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 fact =1;
  11. for(int i=1;i<=number;i++)
  12. fact *=i;
  13. return fact;
  14. }
  15. /**
  16. * Given a phrase, get the acronym of that phrase. Acronym is the combination of
  17. * the first character of each word in upper case.
  18. * For example, given "Ruby on Rails", this method will return "ROR"
  19. * @param phrase
  20. * @return Upper case string of the first letter of each word
  21. */
  22. public String acronym(String phrase) {
  23. String ar[] = phrase.split(" ");
  24. phrase ="";
  25. for(String p : ar) {
  26. phrase += p.substring(0,1).toUpperCase();
  27. }
  28. //System.out.println(phrase);
  29. return phrase;
  30. }
  31. /**
  32. * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
  33. * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
  34. * at the end of the alphabet, it will wraps around.
  35. * For example:
  36. * 'a' => 'd'
  37. * 'w' => 'z'
  38. * 'x' => 'a'
  39. * 'y' => 'b'
  40. * @param word
  41. * @return the encrypted string by shifting each character by three character
  42. */
  43. public String encrypt(String word) {
  44. String encrypted = "";
  45. for (int i = 0; i < word.length(); i++) {
  46. char ch = word.charAt(i);
  47. if (ch != ' ') {
  48. ch = (char)((ch - 'a' + 3) % 26 + 'a');
  49. }
  50. encrypted += ch;
  51. }
  52. return encrypted;
  53. }
  54. }