Browse Source

wrote methods for math utilities and string utilities and some loops

Margaret Pierse 6 years ago
parent
commit
325056a1ef
3 changed files with 27 additions and 17 deletions
  1. 6
    1
      LoopFun.java
  2. 5
    5
      MathUtilities.java
  3. 16
    11
      StringUtilities.java

+ 6
- 1
LoopFun.java View File

@@ -8,7 +8,12 @@ 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 result = 1;
12
+          for(int i=1; i<=number; i++){
13
+              result = result * i;
14
+              System.out.println();
15
+            }
16
+          return result;
12 17
       }
13 18
 
14 19
       /**

+ 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/2.0);
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) == 1);
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
 }

+ 16
- 11
StringUtilities.java View File

@@ -1,13 +1,18 @@
1
+import java.util.*;
2
+
3
+
1 4
 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
-   }
5
+    public Character getMiddleCharacter(String word){
6
+        double middleIndex = word.length()/2;
7
+        return (word.charAt((int)middleIndex));
8
+    }
9
+
10
+    public String removeCharacter(String value, char charToRemove){
11
+        return (value.replace(String.valueOf(charToRemove), ""));  
12
+    }
13
+
14
+    public String getLastWord(String value) {
15
+        int lastSpaceIndex = value.lastIndexOf(" ");
16
+        return value.substring(lastSpaceIndex+1);
17
+    }
13 18
 }