Browse Source

Updated methods in LoopFun, MathUtilities, and StringUtilities

Trinh Tong 6 years ago
parent
commit
8ce8a1f670
3 changed files with 149 additions and 82 deletions
  1. 89
    36
      LoopFun.java
  2. 47
    43
      MathUtilities.java
  3. 13
    3
      StringUtilities.java

+ 89
- 36
LoopFun.java View File

@@ -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
 }

+ 47
- 43
MathUtilities.java View File

@@ -1,50 +1,54 @@
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
+        return num1 + num2;
10
+    }
11 11
 
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
-  }
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 num1 + num2;
20
+    }
21 21
 
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
-  }
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
+        double halfNum = (double) number;
29
+        return halfNum/2;
30
+    }
30 31
 
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
-  }
32
+    /**
33
+     * Determine if the number is odd
34
+     * @param number the number given
35
+     * @return true if the number is odd, false if it is even
36
+     */
37
+    public boolean isOdd(int number){
38
+        if (number % 2 == 0) {
39
+            return false;
40
+        } else {
41
+            return true;
42
+        }
43
+    }
39 44
 
40
-
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
-  }
45
+    /**
46
+     * Multiply the number by itself
47
+     * @param number the number given
48
+     * @return the result of the number multiply by itself
49
+     */
50
+    public int square(int number) {
51
+        return (int) Math.pow(number, 2);
52
+    }
49 53
 
50 54
 }

+ 13
- 3
StringUtilities.java View File

@@ -1,13 +1,23 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+        int middleVal = (word.length() / 2);
4
+        return word.charAt(middleVal);
4 5
    }
5 6
    
6 7
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
8
+     // remove the characters in the string using replace with ""
9
+     
10
+     String newString = value.replace(Character.toString(charToRemove), "");
11
+     
12
+     return newString;  
8 13
    }
9 14
    
10 15
    public String getLastWord(String value) {
11
-       return null;
16
+       // return the last word of the string
17
+       // use split and return the last word of the array
18
+       String words = "";
19
+       String[] famousLastWords = value.split(" ");
20
+       
21
+       return famousLastWords[famousLastWords.length -1];
12 22
    }
13 23
 }