LoopFun.java 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. int factorial = 0;
  12. int i = 0;
  13. while (i <= number){
  14. i*=i;
  15. i++;
  16. }
  17. return factorial;
  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. int spaceIndex = phrase.indexOf(" ");
  28. char acro = phrase.charAt(spaceIndex+1);
  29. for (int i = 1; i < phrase.length(); i++){
  30. acro = Character.toUpperCase(phrase.charAt(spaceIndex+1));
  31. }
  32. return Character.toUpperCase(phrase.charAt(0)) + Character.toString(acro);
  33. }
  34. /**
  35. * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
  36. * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
  37. * at the end of the alphabet, it will wraps around.
  38. * For example:
  39. * 'a' => 'd'
  40. * 'w' => 'z'
  41. * 'x' => 'a'
  42. * 'y' => 'b'
  43. * @param word
  44. * @return the encrypted string by shifting each character by three character
  45. */
  46. public String encrypt(String word) {
  47. return null;
  48. }
  49. }