123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- package QuizWeek1;
-
- 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;
- if (number == 0){
- result = 1;
- } else {
- for (int i = 1; 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 newPhrase = phrase.toUpperCase();
-
- char[] charArray = newPhrase.toCharArray();
- String firstLet = Character.toString(charArray[0]);
- //System.out.println(firstLet);
-
- for (int i = 1; i < charArray.length; i++){
- if (charArray[i] == ' '){
- System.out.println(charArray[i]);
- firstLet += (charArray[i]+1);
- System.out.println(firstLet);
- }
- }
-
-
- return firstLet;
- }
-
- /**
- * 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) {
- // ASCII Table - a = 97, z = 122
-
- char[] charArray = word.toCharArray();
- for (int i = 0; i < charArray.length; i++){
- (int)charArray[i] += 3;
- if ((int)charArray[i] > 119){
- (int)charArray[i] -= 23;
- }
- }
-
- return null;
- }
- }
|