LoopFun.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. {
  11. int i;
  12. int fact = 1;
  13. for(i=1;i<=number;i++)
  14. {
  15. fact=fact*i;
  16. }
  17. return fact;
  18. }
  19. /**
  20. * Given a phrase, get the acronym of that phrase. Acronym is the combination of
  21. * the first character of each word in upper case.
  22. * For example, given "Ruby on Rails", this method will return "ROR"
  23. * @param phrase
  24. * @return Upper case string of the first letter of each word
  25. */
  26. public String acronym(String phrase)
  27. {
  28. String res = "";
  29. res = phrase.replaceAll("[a-z ]", "" ).toUpperCase();
  30. return res;
  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. return null;
  46. }
  47. }