Pārlūkot izejas kodu

{Joson QuizWeek1}

Jacqueline Joson 6 gadus atpakaļ
vecāks
revīzija
f0f34975a1
3 mainītis faili ar 57 papildinājumiem un 15 dzēšanām
  1. 11
    4
      LoopFun.java
  2. 13
    5
      MathUtilities.java
  3. 33
    6
      StringUtilities.java

+ 11
- 4
LoopFun.java Parādīt failu

@@ -1,14 +1,17 @@
1 1
 public class LoopFun
2 2
 {
3
-
4
-      /**
3
+ /**
5 4
        * Given a number, return the factorial of that number.
6 5
        * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
7 6
        * @param number
8 7
        * @return the factorial of the number
9 8
        */
10 9
       public int factorial(int number){
11
-          return -1;
10
+          int fact = 1;
11
+          for (int i = 1; i <= number; i++) {
12
+              fact *= i;
13
+            }
14
+          return fact;
12 15
       }
13 16
 
14 17
       /**
@@ -19,8 +22,12 @@ public class LoopFun
19 22
        * @return Upper case string of the first letter of each word
20 23
        */
21 24
       public String acronym(String phrase) {
22
-          return null;
25
+          String [] word = phrase.split(" ");
26
+          String acr = Character.toString(word[0].charAt(0)) + 
27
+          Character.toString(word[1].charAt(0)) + Character.toString(word[2].charAt(0));
28
+          return acr.toUpperCase();
23 29
       }
30
+    
24 31
 
25 32
       /**
26 33
        * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its

+ 13
- 5
MathUtilities.java Parādīt failu

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

+ 33
- 6
StringUtilities.java Parādīt failu

@@ -1,13 +1,40 @@
1 1
 public class StringUtilities {
2 2
    public Character getMiddleCharacter(String word){
3
-       return null;
3
+       int theMiddleOne;
4
+       if ((word.length()/2) != 0) {
5
+             theMiddleOne = ((word.length()/2) - 1);
6
+        } else {
7
+            theMiddleOne = (word.length()/2); 
8
+        }
9
+         return word.charAt(theMiddleOne);  
4 10
    }
11
+
5 12
    
6
-   public String removeCharacter(String value, char charToRemove){
7
-     return null;  
8
-   }
13
+public String removeCharacter(String value, char charToRemove){
14
+        StringBuilder sb = new StringBuilder(); 
15
+    
16
+        /*for (int i = 0; i < value.length(); i++) {
17
+            if (value.charAt(i).equals(charToRemove)) {
18
+                sb = sb.append(value.charAt(0, i)); 
19
+            }
20
+            String newVal = sb.append(value) + sb;
21
+        }
22
+        return newVal; 
23
+        */
24
+       return null;
25
+    }
26
+
9 27
    
10 28
    public String getLastWord(String value) {
11
-       return null;
12
-   }
29
+      StringBuilder sb = new StringBuilder();
30
+      for (int i = value.length()-1; i >= 0; i--) {
31
+          char z = value.charAt(i);
32
+          if (Character.isWhitespace(z)) {
33
+              break;
34
+            }
35
+            //return append();
36
+        }  
37
+   //return sb.append().toReverse();
38
+   return null;
13 39
 }
40
+}