LoopFun.java 1.7KB

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