Browse Source

Elliott First Quiz

Elliott Stansbury 6 years ago
parent
commit
7d105e1227
3 changed files with 99 additions and 44 deletions
  1. 63
    36
      LoopFun.java
  2. 17
    5
      MathUtilities.java
  3. 19
    3
      StringUtilities.java

+ 63
- 36
LoopFun.java View File

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

+ 17
- 5
MathUtilities.java View File

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

+ 19
- 3
StringUtilities.java View File

1
 public class StringUtilities {
1
 public class StringUtilities {
2
    public Character getMiddleCharacter(String word){
2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       int position;
4
+       int length;
5
+       if(word.length() % 2 == 0){
6
+        position = word.length() / 2;
7
+        char c = word.charAt(position);
8
+        } else {
9
+        position = word.length() /2;
10
+        char c = word.charAt(position);
11
+        }
12
+       char c = word.charAt(position);
13
+       return c;
4
    }
14
    }
5
    
15
    
6
    public String removeCharacter(String value, char charToRemove){
16
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
17
+       String x = Character.toString(charToRemove);
18
+       String y = value.replaceAll(x, "");
19
+       
20
+       return y;  
8
    }
21
    }
9
    
22
    
10
    public String getLastWord(String value) {
23
    public String getLastWord(String value) {
11
-       return null;
24
+       
25
+       String everythingButLast = value.substring(value.lastIndexOf(" ") + 1);
26
+       
27
+       return everythingButLast;
12
    }
28
    }
13
 }
29
 }