Browse Source

Curtis Cook's Quiz 1

Curtis Cook 6 years ago
parent
commit
20e03a0928
3 changed files with 49 additions and 11 deletions
  1. 27
    3
      LoopFun.java
  2. 5
    5
      MathUtilities.java
  3. 17
    3
      StringUtilities.java

+ 27
- 3
LoopFun.java View File

@@ -8,7 +8,11 @@ public class LoopFun
8 8
        * @return the factorial of the number
9 9
        */
10 10
       public int factorial(int number){
11
-          return -1;
11
+          int factorial = 1;
12
+          for(int i = 1; i <= number; i++) {
13
+              factorial *= i;
14
+            }  
15
+          return factorial;
12 16
       }
13 17
 
14 18
       /**
@@ -19,7 +23,14 @@ public class LoopFun
19 23
        * @return Upper case string of the first letter of each word
20 24
        */
21 25
       public String acronym(String phrase) {
22
-          return null;
26
+          StringBuilder acronym = new StringBuilder();
27
+          acronym.append(phrase.charAt(0));
28
+          for(int i = 0; i < phrase.length(); i++){
29
+              if(Character.isWhitespace(phrase.charAt(i))) {
30
+                  acronym.append(phrase.charAt(i + 1));
31
+              }
32
+          }
33
+          return acronym.toString().toUpperCase();
23 34
       }
24 35
 
25 36
       /**
@@ -35,6 +46,19 @@ public class LoopFun
35 46
        * @return the encrypted string by shifting each character by three character
36 47
        */
37 48
       public String encrypt(String word) {
38
-          return null;
49
+          String alphabet = "abcdefghijklmnopqrstuvwxyz";
50
+
51
+          StringBuilder encryptedWord = new StringBuilder();
52
+          for(int i = 0; i < word.length(); i++) {
53
+              char letter = word.charAt(i);
54
+              int index = 0;
55
+              if(alphabet.indexOf(letter + 3) == -1) {
56
+                  index = ((alphabet.indexOf(letter) - 26) + 3);
57
+              } else {
58
+                  index = alphabet.indexOf(letter + 3);
59
+              }
60
+              encryptedWord.append(alphabet.charAt(index));
61
+          }
62
+          return encryptedWord.toString();
39 63
       }
40 64
 }

+ 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 (double) number / 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 ? true : false;
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
 }

+ 17
- 3
StringUtilities.java View File

@@ -1,13 +1,27 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       int middleIndex = word.length() / 2;
4
+       char middleCharacter = word.charAt(middleIndex);
5
+       return middleCharacter;
4 6
    }
5 7
    
6 8
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
9
+       StringBuilder sb = new StringBuilder(value);
10
+       
11
+       while(sb.toString().indexOf(charToRemove) != -1) {
12
+           sb = sb.deleteCharAt(sb.toString().indexOf(charToRemove));
13
+       }       
14
+       return sb.toString();  
8 15
    }
9 16
    
10 17
    public String getLastWord(String value) {
11
-       return null;
18
+       String lastWord = "";
19
+       for(int i = value.length() - 1; i > 0; i--) {
20
+           if(Character.isWhitespace(value.charAt(i))) {
21
+               lastWord = value.substring(i + 1, value.length());
22
+               break;
23
+           }
24
+       }
25
+       return lastWord;
12 26
    }
13 27
 }