Browse Source

completed

Seth 6 years ago
parent
commit
54a2729ba9
3 changed files with 80 additions and 50 deletions
  1. 61
    34
      LoopFun.java
  2. 5
    5
      MathUtilities.java
  3. 14
    11
      StringUtilities.java

+ 61
- 34
LoopFun.java View File

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

+ 5
- 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 number / (double) 2;
29 29
   }
30 30
 
31 31
   /**
@@ -34,7 +34,7 @@ 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
+      return number % 2 == 0 ? false : true;
38 38
   }
39 39
 
40 40
 
@@ -44,7 +44,7 @@ public class MathUtilities{
44 44
    * @return the result of the number multiply by itself
45 45
    */
46 46
   public int square(int number) {
47
-      return -1;
47
+      return number * number;
48 48
   }
49 49
 
50 50
 }

+ 14
- 11
StringUtilities.java View File

@@ -1,13 +1,16 @@
1 1
 public class StringUtilities {
2
-   public Character getMiddleCharacter(String word){
3
-       return null;
4
-   }
5
-   
6
-   public String removeCharacter(String value, char charToRemove){
7
-     return null;  
8
-   }
9
-   
10
-   public String getLastWord(String value) {
11
-       return null;
12
-   }
2
+    public Character getMiddleCharacter(String word){
3
+        char midLetter;     
4
+        return midLetter =  word.charAt(word.length() / 2);
5
+        
6
+    }  
7
+
8
+    public String removeCharacter(String value, char charToRemove){
9
+        String result = value.replace(Character.toString(charToRemove), "");
10
+        return result;
11
+    }
12
+
13
+    public String getLastWord(String value) {
14
+        return value.substring(value.lastIndexOf(" ")+1);
15
+    }
13 16
 }