Browse Source

completed encrypt

Yesoda Sanka 6 years ago
parent
commit
73b471eeea
2 changed files with 76 additions and 73 deletions
  1. 56
    52
      LoopFun.java
  2. 20
    21
      package.bluej

+ 56
- 52
LoopFun.java View File

@@ -1,59 +1,63 @@
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
-          int sum=0;
12
-          for(int i=1;i<=number;i++)
13
-          { 
14
-              int fact=i*number;
15
-               sum=sum+fact;
16
-              
17
-          
18
-      }return sum;
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 sum=1;
12
+        for(int i=1;i<=number;i++)
13
+        { 
14
+            sum=sum*i;
15
+
16
+        }return sum;
17
+    }
18
+
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
+
29
+        return result;
19 30
     }
20 31
 
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
-          phrase.trim();
30
-          int str=phrase.length();
31
-          char ch;
32
-          
33
-          
34
-          
35
-          
36
-          
37
-          
38
-          return null;
39
-      }
32
+    /**
33
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
34
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
35
+     * at the end of the alphabet, it will wraps around.
36
+     * For example:
37
+     *  'a' => 'd'
38
+     *  'w' => 'z'
39
+     *  'x' => 'a'
40
+     *  'y' => 'b'
41
+     * @param word
42
+     * @return the encrypted string by shifting each character by three character
43
+     */
44
+    public String encrypt(String word) {
45
+        String s = "";
46
+        int shift = 3;
47
+        int len = word.length();
48
+        for (int i =0; i <len; i++) {
49
+            char c = (char) (word.charAt(i) + shift);
50
+            if (c > 'z') {
51
+                s += (char) (word.charAt(i) - (26-shift));
52
+            } else { 
53
+                s+= (char) (word.charAt(i) + shift);
54
+            }
55
+            
56
+        }
57
+        
58
+        return s;
59
+
60
+ 
61
+}
40 62
 
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
-          String str=word.replace('a','d');
55
-          
56
-          
57
-          return str;
58
-      }
59 63
 }

+ 20
- 21
package.bluej View File

@@ -2,25 +2,25 @@
2 2
 dependency1.from=StringUtilitiesTest
3 3
 dependency1.to=StringUtilities
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=BonusTest
6
-dependency2.to=Bonus
5
+dependency2.from=LoopFunTest
6
+dependency2.to=LoopFun
7 7
 dependency2.type=UsesDependency
8 8
 dependency3.from=MathUtilitiesTest
9 9
 dependency3.to=MathUtilities
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=722
11
+editor.fx.0.height=709
12 12
 editor.fx.0.width=876
13
-editor.fx.0.x=158
14
-editor.fx.0.y=216
15
-objectbench.height=164
16
-objectbench.width=669
17
-package.divider.horizontal=0.6
18
-package.divider.vertical=0.7628294036061026
19
-package.editor.height=543
13
+editor.fx.0.x=404
14
+editor.fx.0.y=23
15
+objectbench.height=148
16
+objectbench.width=397
17
+package.divider.horizontal=0.600297176820208
18
+package.divider.vertical=0.7483766233766234
19
+package.editor.height=454
20 20
 package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
21
+package.editor.x=394
22
+package.editor.y=23
23
+package.frame.height=674
24 24
 package.frame.width=693
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
@@ -40,20 +40,19 @@ target1.width=110
40 40
 target1.x=110
41 41
 target1.y=150
42 42
 target2.height=50
43
-target2.name=BonusTest
43
+target2.name=LoopFunTest
44 44
 target2.showInterface=false
45 45
 target2.type=UnitTestTargetJunit4
46
-target2.width=80
47
-target2.x=120
48
-target2.y=270
49
-target3.association=BonusTest
46
+target2.width=110
47
+target2.x=10
48
+target2.y=240
50 49
 target3.height=50
51
-target3.name=Bonus
50
+target3.name=LoopFun
52 51
 target3.showInterface=false
53 52
 target3.type=ClassTarget
54 53
 target3.width=80
55
-target3.x=90
56
-target3.y=300
54
+target3.x=200
55
+target3.y=210
57 56
 target4.association=MathUtilitiesTest
58 57
 target4.height=50
59 58
 target4.name=MathUtilities