LoopFun.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //Did not finish the last test
  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. int count;
  12. if(number==0 || number==1)
  13. {
  14. return 1;
  15. }
  16. count = factorial(number-1)*number;
  17. return count;
  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. String needed = phrase.replaceAll("\\B.|\\P{L}","").toUpperCase();
  28. return needed;
  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 alpha = "abcdefghijklmnopqrstuvwxyz";
  44. String table = alpha + alpha + alpha;
  45. int index= table.indexOf(word);
  46. if(index!=-1)
  47. {
  48. // word = table.charAt(index + word);
  49. }
  50. return word;
  51. }
  52. }