Browse Source

Finished all the methods so that the test cases pass

Saurav Kamath 6 years ago
parent
commit
ec8be1ecf3
4 changed files with 94 additions and 74 deletions
  1. 52
    34
      LoopFun.java
  2. 7
    5
      MathUtilities.java
  3. 5
    4
      StringUtilities.java
  4. 30
    31
      package.bluej

+ 52
- 34
LoopFun.java View File

@@ -1,40 +1,58 @@
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
+        
12
+        if(number == 1){
13
+            return number;
14
+        }
15
+        number *= factorial(number-1);
16
+        return number;
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[] arr = phrase.split(" ");
28
+       String acronym = "";
29
+       for(int i = 0; i < arr.length; i++){
30
+          acronym += arr[i].toUpperCase().charAt(0);
31
+        
32
+        }
33
+        return acronym;
34
+    }
24 35
 
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
-      }
36
+    /**
37
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
38
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
39
+     * at the end of the alphabet, it will wraps around.
40
+     * For example:
41
+     *  'a' => 'd'
42
+     *  'w' => 'z'
43
+     *  'x' => 'a'
44
+     *  'y' => 'b'
45
+     * @param word
46
+     * @return the encrypted string by shifting each character by three character
47
+     */
48
+    public String encrypt(String word) {
49
+        String[] encrypted = word.split("");
50
+        char start = 'a';
51
+        //start = (char) (((start-'a'+3) %26) + 'a');
52
+        String fin = "";
53
+        for(int i = 0; i < encrypted.length; i++){
54
+            fin += String.valueOf((char) (((encrypted[i].charAt(0)-'a'+3) %26) + 'a'));
55
+        }
56
+        return fin;
57
+    }
40 58
 }

+ 7
- 5
MathUtilities.java View File

@@ -6,7 +6,7 @@ public class MathUtilities{
6 6
    * @return the sum of the two numbers
7 7
    */
8 8
   public int add(int num1, int num2){
9
-      return -1;
9
+      return num1 + num2;
10 10
   }
11 11
 
12 12
   /**
@@ -16,7 +16,7 @@ public class MathUtilities{
16 16
    * @return the sum of the two numbers
17 17
    */
18 18
   public double add(double num1, double num2){
19
-      return -1;
19
+      return num1 + num2;
20 20
   }
21 21
 
22 22
   /**
@@ -25,7 +25,7 @@ public class MathUtilities{
25 25
    * @return the half of the number in double
26 26
    */
27 27
   public double half(int number) {
28
-      return -1;
28
+      return (double)number/2;
29 29
   }
30 30
 
31 31
   /**
@@ -34,7 +34,9 @@ public class MathUtilities{
34 34
    * @return true if the number is odd, false if it is even
35 35
    */
36 36
   public boolean isOdd(int number){
37
-      return false;
37
+      if(number%2 != 0){
38
+          return true;
39
+        }else{return false;}
38 40
   }
39 41
 
40 42
 
@@ -44,7 +46,7 @@ public class MathUtilities{
44 46
    * @return the result of the number multiply by itself
45 47
    */
46 48
   public int square(int number) {
47
-      return -1;
49
+      return number*=number;
48 50
   }
49 51
 
50 52
 }

+ 5
- 4
StringUtilities.java View File

@@ -1,13 +1,14 @@
1 1
 public class StringUtilities {
2
-   public Character getMiddleCharacter(String word){
3
-       return null;
2
+   public Character getMiddleCharacter(String word){       
3
+       return word.charAt(word.length()/2);
4 4
    }
5 5
    
6 6
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
7
+     return value.replace(String.valueOf(charToRemove),"");
8 8
    }
9 9
    
10 10
    public String getLastWord(String value) {
11
-       return null;
11
+       String[] arr = value.split(" ");
12
+       return arr[arr.length-1];
12 13
    }
13 14
 }

+ 30
- 31
package.bluej View File

@@ -2,26 +2,26 @@
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
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
11
+editor.fx.0.height=1080
12
+editor.fx.0.width=1920
13
+editor.fx.0.x=-319
14
+editor.fx.0.y=-1080
15
+objectbench.height=148
16
+objectbench.width=1896
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7628294036061026
19
-package.editor.height=543
20
-package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
24
-package.frame.width=693
18
+package.divider.vertical=0.8515325670498084
19
+package.editor.height=882
20
+package.editor.width=1794
21
+package.editor.x=-319
22
+package.editor.y=-1080
23
+package.frame.height=1080
24
+package.frame.width=1920
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true
@@ -37,43 +37,42 @@ target1.name=StringUtilitiesTest
37 37
 target1.showInterface=false
38 38
 target1.type=UnitTestTargetJunit4
39 39
 target1.width=110
40
-target1.x=110
41
-target1.y=150
40
+target1.x=500
41
+target1.y=210
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
60 59
 target4.showInterface=false
61 60
 target4.type=ClassTarget
62 61
 target4.width=100
63
-target4.x=80
64
-target4.y=70
62
+target4.x=210
63
+target4.y=60
65 64
 target5.association=StringUtilitiesTest
66 65
 target5.height=50
67 66
 target5.name=StringUtilities
68 67
 target5.showInterface=false
69 68
 target5.type=ClassTarget
70 69
 target5.width=110
71
-target5.x=80
72
-target5.y=180
70
+target5.x=470
71
+target5.y=240
73 72
 target6.height=50
74 73
 target6.name=MathUtilitiesTest
75 74
 target6.showInterface=false
76 75
 target6.type=UnitTestTargetJunit4
77 76
 target6.width=100
78
-target6.x=110
79
-target6.y=40
77
+target6.x=240
78
+target6.y=30