LoopFun.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 total = 0;
  11. for(int i = 1; i < number; i++ ){
  12. total = i * number;
  13. }
  14. return total;
  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. /* String x1="";
  25. String [] container = phrase.split(" ");
  26. for(int i= 0; i < container.length ; i++)
  27. {
  28. x1 = container.charAt(0,1);
  29. }
  30. String letters = x1;
  31. return letters.toUpperCase();*/
  32. //String result = phrase.replaceAll("\\B |\\P{L}","").toUpperCase();
  33. //return result;
  34. String [] container = phrase.split(" ");
  35. String a_c = "";
  36. for(int i= 0; i < container.length ; i++)
  37. {
  38. a_c +=Character.toString(container[i].charAt(0));
  39. }
  40. return a_c;
  41. }
  42. /**
  43. * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
  44. * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
  45. * at the end of the alphabet, it will wraps around.
  46. * For example:
  47. * 'a' => 'd'
  48. * 'w' => 'z'
  49. * 'x' => 'a'
  50. * 'y' => 'b'
  51. * @param word
  52. * @return the encrypted string by shifting each character by three character
  53. */
  54. public String encrypt(String word) {
  55. // char alph = (((alph - 'a' + 3) % 26) + 'a');
  56. return null;
  57. }
  58. }