LoopFun.java 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 result;
  11. if (number ==1)
  12. return 1;
  13. result = factorial(number-1)*number;
  14. return result;
  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. int x = phrase.indexOf(' ');
  25. return phrase.substring(0,1).toUpperCase() + phrase.substring(x+1,x+2).toUpperCase();
  26. }
  27. }
  28. /**
  29. * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
  30. * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
  31. * at the end of the alphabet, it will wraps around.
  32. * For example:
  33. * 'a' => 'd'
  34. * 'w' => 'z'
  35. * 'x' => 'a'
  36. * 'y' => 'b'
  37. * @param word
  38. * @return the encrypted string by shifting each character by three character
  39. */
  40. /*public String encrypt(String word) {
  41. return null;
  42. }
  43. }*/