LoopFun.java 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package QuizWeek1;
  2. public class LoopFun
  3. {
  4. /**
  5. * Given a number, return the factorial of that number.
  6. * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
  7. * @param number
  8. * @return the factorial of the number
  9. */
  10. public int factorial(int number){
  11. if(number==0){
  12. return 1;}
  13. else{
  14. return number*factorial(number-1);}
  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. //phrase= phrase.toLowerCase();
  25. //String[] ph =phrase.split(" "); */
  26. return phrase.replaceAll("[a-z ]", "").toUpperCase();
  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. char[] toEncode=word.toCharArray();
  42. for(int i=0;i<toEncode.length;i++)
  43. {
  44. }
  45. return null;
  46. }
  47. }