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