|
@@ -1,40 +1,93 @@
|
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
|
|
- }
|
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
|
|
- }
|
24
|
|
-
|
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
|
|
- }
|
|
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
|
+
|
|
12
|
+ int counter = number;
|
|
13
|
+ for (int i = number - 1; i > 1; i --) {
|
|
14
|
+ counter = counter * i;
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ return counter;
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ /**
|
|
21
|
+ * Given a phrase, get the acronym of that phrase. Acronym is the combination of
|
|
22
|
+ * the first character of each word in upper case.
|
|
23
|
+ * For example, given "Ruby on Rails", this method will return "ROR"
|
|
24
|
+ * @param phrase
|
|
25
|
+ * @return Upper case string of the first letter of each word
|
|
26
|
+ */
|
|
27
|
+ public String acronym(String phrase) {
|
|
28
|
+
|
|
29
|
+ String upperAcro = "";
|
|
30
|
+
|
|
31
|
+ String[] words = phrase.split(" ");
|
|
32
|
+
|
|
33
|
+ // cycle through and grab each char @ 0 and return to string
|
|
34
|
+
|
|
35
|
+ for (int i = 0; i < words.length; i++) {
|
|
36
|
+ upperAcro += Character.toString(words[i].charAt(0));
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ return upperAcro.toUpperCase();
|
|
40
|
+
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ /**
|
|
44
|
+ * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
|
|
45
|
+ * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
|
|
46
|
+ * at the end of the alphabet, it will wraps around.
|
|
47
|
+ * For example:
|
|
48
|
+ * 'a' => 'd'
|
|
49
|
+ * 'w' => 'z'
|
|
50
|
+ * 'x' => 'a'
|
|
51
|
+ * 'y' => 'b'
|
|
52
|
+ * @param word
|
|
53
|
+ * @return the encrypted string by shifting each character by three character
|
|
54
|
+ */
|
|
55
|
+ public String encrypt(String word) {
|
|
56
|
+ // encoder of full alphabet
|
|
57
|
+ String encoder = "abcdefghijklmnopqrstuvwxyz";
|
|
58
|
+ String decryptMessage = "";
|
|
59
|
+ int encodeMove = 3;
|
|
60
|
+
|
|
61
|
+ // nested for loop to check chars to chars of the string
|
|
62
|
+ for (int i = 0; i < word.length(); i++){
|
|
63
|
+ // this loop checks char @ i and compares to the encoder string
|
|
64
|
+ for (int j = 0; j < encoder.length(); j++) {
|
|
65
|
+ // cycles through encoder and finds the position in the encoder
|
|
66
|
+ // loops back to front, if j == encoder.length();
|
|
67
|
+ // j = 0
|
|
68
|
+ if (word.charAt(i) == (encoder.charAt(j))) {
|
|
69
|
+ if (j >= 23) {
|
|
70
|
+
|
|
71
|
+ int k = j;
|
|
72
|
+ int end = encoder.length() - 1;
|
|
73
|
+ // add until it hits 26, then reinitialize to -1 (for case "x" it should be at index 0)
|
|
74
|
+ for (int l = 0; l <= 2; l++) {
|
|
75
|
+ if ( k == end ) {
|
|
76
|
+ k = -1;
|
|
77
|
+ }
|
|
78
|
+ k ++;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ decryptMessage += Character.toString(encoder.charAt(k));
|
|
82
|
+ } else {
|
|
83
|
+ decryptMessage += Character.toString(encoder.charAt(j+3));
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ }
|
|
89
|
+ // if statement to wrap around
|
|
90
|
+
|
|
91
|
+ return decryptMessage;
|
|
92
|
+ }
|
40
|
93
|
}
|