|
@@ -8,7 +8,11 @@ public class LoopFun
|
8
|
8
|
* @return the factorial of the number
|
9
|
9
|
*/
|
10
|
10
|
public int factorial(int number){
|
11
|
|
- return -1;
|
|
11
|
+ int factorial = 1;
|
|
12
|
+ for(int i = 1; i <= number; i++) {
|
|
13
|
+ factorial *= i;
|
|
14
|
+ }
|
|
15
|
+ return factorial;
|
12
|
16
|
}
|
13
|
17
|
|
14
|
18
|
/**
|
|
@@ -19,7 +23,14 @@ public class LoopFun
|
19
|
23
|
* @return Upper case string of the first letter of each word
|
20
|
24
|
*/
|
21
|
25
|
public String acronym(String phrase) {
|
22
|
|
- return null;
|
|
26
|
+ StringBuilder acronym = new StringBuilder();
|
|
27
|
+ acronym.append(phrase.charAt(0));
|
|
28
|
+ for(int i = 0; i < phrase.length(); i++){
|
|
29
|
+ if(Character.isWhitespace(phrase.charAt(i))) {
|
|
30
|
+ acronym.append(phrase.charAt(i + 1));
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+ return acronym.toString().toUpperCase();
|
23
|
34
|
}
|
24
|
35
|
|
25
|
36
|
/**
|
|
@@ -35,6 +46,19 @@ public class LoopFun
|
35
|
46
|
* @return the encrypted string by shifting each character by three character
|
36
|
47
|
*/
|
37
|
48
|
public String encrypt(String word) {
|
38
|
|
- return null;
|
|
49
|
+ String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
50
|
+
|
|
51
|
+ StringBuilder encryptedWord = new StringBuilder();
|
|
52
|
+ for(int i = 0; i < word.length(); i++) {
|
|
53
|
+ char letter = word.charAt(i);
|
|
54
|
+ int index = 0;
|
|
55
|
+ if(alphabet.indexOf(letter + 3) == -1) {
|
|
56
|
+ index = ((alphabet.indexOf(letter) - 26) + 3);
|
|
57
|
+ } else {
|
|
58
|
+ index = alphabet.indexOf(letter + 3);
|
|
59
|
+ }
|
|
60
|
+ encryptedWord.append(alphabet.charAt(index));
|
|
61
|
+ }
|
|
62
|
+ return encryptedWord.toString();
|
39
|
63
|
}
|
40
|
64
|
}
|