Browse Source

Completed

Demetrius Murray 6 years ago
parent
commit
4a9be869b8
4 changed files with 161 additions and 103 deletions
  1. 70
    36
      LoopFun.java
  2. 52
    42
      MathUtilities.java
  3. 18
    3
      StringUtilities.java
  4. 21
    22
      package.bluej

+ 70
- 36
LoopFun.java View File

@@ -1,40 +1,74 @@
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
+        int result = 1;
12
+        for (int i = number; i>1; i--){
13
+            result*=i;
14
+        }
15
+        return result;
16
+    }
17
+
18
+    /**
19
+     * Given a phrase, get the acronym of that phrase. Acronym is the combination of
20
+     * the first character of each word in upper case.
21
+     * For example, given "Ruby on Rails", this method will return "ROR"
22
+     * @param phrase
23
+     * @return Upper case string of the first letter of each word
24
+     */
25
+    public String acronym(String phrase) {
26
+        String[] strArray = phrase.split(" ");
27
+        StringBuilder sb = new StringBuilder();
28
+
29
+        for(String word : strArray){
30
+            sb.append(word.charAt(0));
31
+        }
32
+
33
+        String result = sb.toString().toUpperCase();
34
+
35
+        return result;
36
+    }
37
+
38
+    /**
39
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
40
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
41
+     * at the end of the alphabet, it will wraps around.
42
+     * For example:
43
+     *  'a' => 'd'
44
+     *  'w' => 'z'
45
+     *  'x' => 'a'
46
+     *  'y' => 'b'
47
+     * @param word
48
+     * @return the encrypted string by shifting each character by three character
49
+     */
50
+    public String encrypt(String word) {
51
+        StringBuilder sb = new StringBuilder();
52
+        char testChar;
53
+        //a = 97; z = 122; y = 120;
54
+
55
+        for (int i=0; i<word.length(); i++){
56
+
57
+            if(word.charAt(i)>=120){
58
+                testChar = word.charAt(i);
59
+                testChar -= 23;
60
+                sb.append(testChar);
61
+            } else {
62
+                testChar = word.charAt(i);
63
+                testChar += 3;
64
+                sb.append(testChar);
65
+            }
66
+
67
+        }
68
+
69
+        String result = sb.toString();
70
+        System.out.print(result);
71
+
72
+        return result;
73
+    }
40 74
 }

+ 52
- 42
MathUtilities.java View File

@@ -1,50 +1,60 @@
1 1
 public class MathUtilities{
2
-  /**
3
-   * Add two number together
4
-   * @param num1 first number
5
-   * @param num2 second number
6
-   * @return the sum of the two numbers
7
-   */
8
-  public int add(int num1, int num2){
9
-      return -1;
10
-  }
2
+    /**
3
+     * Add two number together
4
+     * @param num1 first number
5
+     * @param num2 second number
6
+     * @return the sum of the two numbers
7
+     */
8
+    public int add(int num1, int num2){
9
+        int result = num1+num2;
10
+        return result;
11
+    }
11 12
 
12
-  /**
13
-   * Add two number together
14
-   * @param num1 first number
15
-   * @param num2 second number
16
-   * @return the sum of the two numbers
17
-   */
18
-  public double add(double num1, double num2){
19
-      return -1;
20
-  }
13
+    /**
14
+     * Add two number together
15
+     * @param num1 first number
16
+     * @param num2 second number
17
+     * @return the sum of the two numbers
18
+     */
19
+    public double add(double num1, double num2){
20
+        double result = num1+num2;
21
+        return result;
22
+    }
21 23
 
22
-  /**
23
-   * Get half the value of the number
24
-   * @param number the number given
25
-   * @return the half of the number in double
26
-   */
27
-  public double half(int number) {
28
-      return -1;
29
-  }
24
+    /**
25
+     * Get half the value of the number
26
+     * @param number the number given
27
+     * @return the half of the number in double
28
+     */
29
+    public double half(int number) {
30
+        double result = number/2.0;
31
+        return result;
32
+    }
30 33
 
31
-  /**
32
-   * Determine if the number is odd
33
-   * @param number the number given
34
-   * @return true if the number is odd, false if it is even
35
-   */
36
-  public boolean isOdd(int number){
37
-      return false;
38
-  }
34
+    /**
35
+     * Determine if the number is odd
36
+     * @param number the number given
37
+     * @return true if the number is odd, false if it is even
38
+     */
39
+    public boolean isOdd(int number){
40
+        boolean result;
41
+        if (number%2 == 1){
42
+            result = true;
43
+        } else {
44
+            result = false;
45
+        }
39 46
 
47
+        return result;
48
+    }
40 49
 
41
-  /**
42
-   * Multiply the number by itself
43
-   * @param number the number given
44
-   * @return the result of the number multiply by itself
45
-   */
46
-  public int square(int number) {
47
-      return -1;
48
-  }
50
+    /**
51
+     * Multiply the number by itself
52
+     * @param number the number given
53
+     * @return the result of the number multiply by itself
54
+     */
55
+    public int square(int number) {
56
+        int result = number*number;
57
+        return result;
58
+    }
49 59
 
50 60
 }

+ 18
- 3
StringUtilities.java View File

@@ -1,13 +1,28 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       char result;
4
+       int length = word.length();
5
+       
6
+       if(length%2 == 1){ //odd
7
+           result = word.charAt((length-1)/2);
8
+        } else { //even
9
+            result = word.charAt((length/2));
10
+        }
11
+        
12
+       return result;
4 13
    }
5 14
    
6 15
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
16
+       String strToRemove = Character.toString(charToRemove);
17
+       String result = value.replace(strToRemove,"");
18
+     
19
+       return result;  
8 20
    }
9 21
    
10 22
    public String getLastWord(String value) {
11
-       return null;
23
+       String[] strArray = value.split(" ");
24
+       String result = strArray[strArray.length-1];
25
+       
26
+       return result;
12 27
    }
13 28
 }

+ 21
- 22
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
11
+editor.fx.0.height=716
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
13
+editor.fx.0.x=390
14
+editor.fx.0.y=23
15
+objectbench.height=148
16
+objectbench.width=747
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.7644376899696048
19
+package.editor.height=496
20
+package.editor.width=645
21
+package.editor.x=63
22
+package.editor.y=161
23
+package.frame.height=716
24
+package.frame.width=771
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true
@@ -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