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. for(int i=1; i<= number; i++){
  11. return number * i;
  12. }
  13. return number;
  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 s ="";
  24. String []acronym = phrase.split(" ");
  25. for (int i = 0; i < acronym.length; i++) {
  26. s = acronym[i];
  27. s.charAt(0);
  28. }
  29. return s.toUpperCase();
  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. char [] encodeWord= word.toCharArray();
  45. for (int i= 0; i <encodeWord.length; i++){
  46. if (Character.isLetter(encodeWord[i])){
  47. encodeWord[i]+=3;}
  48. }
  49. return encodeWord.toString();
  50. }
  51. }