|
@@ -1,40 +1,56 @@
|
1
|
1
|
public class LoopFun
|
2
|
2
|
{
|
3
|
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
|
|
- return -1;
|
12
|
|
- }
|
|
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 = 1;
|
|
12
|
+ int num = 6;
|
13
|
13
|
|
14
|
|
- /**
|
15
|
|
- * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
16
|
|
- * the first character of each word in upper case.
|
17
|
|
- * For example, given "Ruby on Rails", this method will return "ROR"
|
18
|
|
- * @param phrase
|
19
|
|
- * @return Upper case string of the first letter of each word
|
20
|
|
- */
|
21
|
|
- public String acronym(String phrase) {
|
22
|
|
- return null;
|
23
|
|
- }
|
|
14
|
+ for(int i = 1; i <= number; i++)
|
|
15
|
+ {
|
|
16
|
+ factorial *= i;
|
|
17
|
+ }
|
|
18
|
+ return factorial;
|
|
19
|
+ }
|
24
|
20
|
|
25
|
|
- /**
|
26
|
|
- * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
27
|
|
- * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
28
|
|
- * at the end of the alphabet, it will wraps around.
|
29
|
|
- * For example:
|
30
|
|
- * 'a' => 'd'
|
31
|
|
- * 'w' => 'z'
|
32
|
|
- * 'x' => 'a'
|
33
|
|
- * 'y' => 'b'
|
34
|
|
- * @param word
|
35
|
|
- * @return the encrypted string by shifting each character by three character
|
36
|
|
- */
|
37
|
|
- public String encrypt(String word) {
|
38
|
|
- return null;
|
39
|
|
- }
|
|
21
|
+ /**
|
|
22
|
+ * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
|
23
|
+ * the first character of each word in upper case.
|
|
24
|
+ * For example, given "Ruby on Rails", this method will return "ROR"
|
|
25
|
+ * @param phrase
|
|
26
|
+ * @return Upper case string of the first letter of each word
|
|
27
|
+ */
|
|
28
|
+ public String acronym(String phrase) {
|
|
29
|
+
|
|
30
|
+ String initials = "";
|
|
31
|
+
|
|
32
|
+ for (String s : phrase.split(" ")) {
|
|
33
|
+
|
|
34
|
+ initials += s.charAt(0);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ return initials;
|
|
38
|
+
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ /**
|
|
42
|
+ * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
|
43
|
+ * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
|
44
|
+ * at the end of the alphabet, it will wraps around.
|
|
45
|
+ * For example:
|
|
46
|
+ * 'a' => 'd'
|
|
47
|
+ * 'w' => 'z'
|
|
48
|
+ * 'x' => 'a'
|
|
49
|
+ * 'y' => 'b'
|
|
50
|
+ * @param word
|
|
51
|
+ * @return the encrypted string by shifting each character by three character
|
|
52
|
+ */
|
|
53
|
+ public String encrypt(String word) {
|
|
54
|
+ return null;
|
|
55
|
+ }
|
40
|
56
|
}
|