| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- public class LoopFun
- {
-
- /**
- * Given a number, return the factorial of that number.
- * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
- * @param number
- * @return the factorial of the number
- */
- public int factorial(int number){
-
- int result = 1;
- for(int i = 2; i <= number; i++)
- result *= i;
- return result;
-
- }
-
- /**
- * Given a phrase, get the acronym of that phrase. Acronym is the combination of
- * the first character of each word in upper case.
- * For example, given "Ruby on Rails", this method will return "ROR"
- * @param phrase
- * @return Upper case string of the first letter of each word
- */
- public String acronym(String phrase) {
-
- String acronym = "";
- acronym += phrase.toUpperCase().charAt(0);
-
- for (int i = 1; i <= phrase.length() - 1; i++) {
- if (phrase.charAt(i - 1) == ' ' || phrase.charAt(i - 1) == '-') {
- acronym += phrase.toUpperCase().charAt(i);
- }
- }
-
- return acronym;
- }
- /*
-
- What can you learn from this solution?
-
- A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
-
- Here are some questions to help you reflect on this solution and learn the most from it.
-
- What compromises have been made?
- Are there new concepts here that I could read more about to develop my understanding?
- */
-
- /**
- * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
- * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
- * at the end of the alphabet, it will wraps around.
- * For example:
- * 'a' => 'd'
- * 'w' => 'z'
- * 'x' => 'a'
- * 'y' => 'b'
- * @param word
- * @return the encrypted string by shifting each character by three character
- */
- public String encrypt(String word) {
- char swap = ' ';
- char[] arr = word.toCharArray();
- swap(arr, 0, word.length + 3);
- int i = arr.length + 3 ;
- while(i > 1) {
- swap(arr, i, i-1);
- }
- return new swap;
- }
-
- }
-
|