Explorar el Código

Completed the quiz and all of the test passed

William Brown hace 6 años
padre
commit
b220ada4d5
Se han modificado 3 ficheros con 60 adiciones y 11 borrados
  1. 34
    3
      LoopFun.java
  2. 11
    5
      MathUtilities.java
  3. 15
    3
      StringUtilities.java

+ 34
- 3
LoopFun.java Ver fichero

@@ -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 total = 1;
12
+          for(int i = number; i > 0; i--){
13
+            total = i * total;
14
+          }
15
+          return total;
12 16
       }
13 17
 
14 18
       /**
@@ -19,7 +23,21 @@ 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 finalString = new StringBuilder();
27
+          //ALWAYS TAKES FIRST LETTER
28
+          finalString.append(phrase.charAt(0));
29
+          for(int i = 0; i < phrase.length(); i++){
30
+          //CHECKS IF THERES A SPACE
31
+              if(phrase.charAt(i) == ' ') {  
32
+                //IF THERE IS IT ADD IT TO OUR STRING
33
+                i++;
34
+                finalString.append(phrase.charAt(i));
35
+            }
36
+          }
37
+          //TURNS STRINGBUILDER INTO STRING
38
+          String answer = "" + finalString;
39
+          //MAKES STRING ALL CAPITALS
40
+          return answer.toUpperCase();
23 41
       }
24 42
 
25 43
       /**
@@ -35,6 +53,19 @@ public class LoopFun
35 53
        * @return the encrypted string by shifting each character by three character
36 54
        */
37 55
       public String encrypt(String word) {
38
-          return null;
56
+          String newString = "";
57
+          for(int i = 0; i < word.length(); i++){
58
+              char encrypted = word.charAt(i);
59
+              int number = (int) encrypted;
60
+              if(number <= 119){
61
+              char newChar = (char)(number + 3);
62
+              newString = newString + newChar;
63
+              } else if(number >= 118){
64
+              char newChar = (char)(number - 23);
65
+              newString = newString + newChar;
66
+              }
67
+            }
68
+            System.out.println((char) 97);
69
+          return newString;
39 70
       }
40 71
 }

+ 11
- 5
MathUtilities.java Ver fichero

@@ -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,13 @@ 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
+      boolean answer = true;
38
+      if(number % 2 == 0){
39
+          answer = false;
40
+      }else if(number % 2 != 0){
41
+          answer = true;
42
+        }
43
+        return answer;
38 44
   }
39 45
 
40 46
 
@@ -44,7 +50,7 @@ public class MathUtilities{
44 50
    * @return the result of the number multiply by itself
45 51
    */
46 52
   public int square(int number) {
47
-      return -1;
53
+      return number * number;
48 54
   }
49 55
 
50 56
 }

+ 15
- 3
StringUtilities.java Ver fichero

@@ -1,13 +1,25 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       //FIND LENGTH
4
+       int wordLength = word.length();
5
+       //FIND MIDDLE INDEX NUMBER
6
+       int wordHalf = wordLength / 2;
7
+       char wordMiddleLetter = word.charAt(wordHalf);
8
+       //GET THAT CHARAT THAT INDEX NUMBER
9
+       return wordMiddleLetter;
4 10
    }
5 11
    
6 12
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
13
+       //INDEX OF charToRemove
14
+       String a = "" + charToRemove;
15
+       value = value.replace(a,"");
16
+       return value;
8 17
    }
9 18
    
10 19
    public String getLastWord(String value) {
11
-       return null;
20
+       //MAKE SUBSTRINGS OF EACH WORD
21
+       int space = value.lastIndexOf(" ");
22
+       String LastWord = value.substring(space + 1, value.length());
23
+       return LastWord;
12 24
    }
13 25
 }