Browse Source

Quiz Week 1

Michelle DiMarino 6 years ago
parent
commit
dfd98eda5c
7 changed files with 73 additions and 41 deletions
  1. 18
    3
      LoopFun.java
  2. 2
    0
      LoopFunTest.java
  3. 14
    5
      MathUtilities.java
  4. 2
    0
      MathUtilitiesTest.java
  5. 17
    3
      StringUtilities.java
  6. 2
    0
      StringUtilitiesTest.java
  7. 18
    30
      package.bluej

+ 18
- 3
LoopFun.java View File

@@ -1,3 +1,5 @@
1
+package QuizWeek1;
2
+
1 3
 public class LoopFun
2 4
 {
3 5
 
@@ -7,8 +9,15 @@ public class LoopFun
7 9
        * @param number
8 10
        * @return the factorial of the number
9 11
        */
10
-      public int factorial(int number){
11
-          return -1;
12
+      public int factorial(int number){ 
13
+         int factorial = 0; 
14
+         int i = 0;
15
+          while (i <= number){
16
+              i*=i;
17
+              i++;
18
+              
19
+            }
20
+          return factorial;
12 21
       }
13 22
 
14 23
       /**
@@ -19,7 +28,13 @@ public class LoopFun
19 28
        * @return Upper case string of the first letter of each word
20 29
        */
21 30
       public String acronym(String phrase) {
22
-          return null;
31
+          int spaceIndex = phrase.indexOf(" ");
32
+          char acro = phrase.charAt(spaceIndex+1);
33
+          for (int i = 1; i < phrase.length(); i++){
34
+              acro = Character.toUpperCase(phrase.charAt(spaceIndex+1));
35
+            }
36
+          
37
+          return Character.toUpperCase(phrase.charAt(0)) + Character.toString(acro);
23 38
       }
24 39
 
25 40
       /**

+ 2
- 0
LoopFunTest.java View File

@@ -1,3 +1,5 @@
1
+package QuizWeek1;
2
+
1 3
 
2 4
 
3 5
 import static org.junit.Assert.*;

+ 14
- 5
MathUtilities.java View File

@@ -1,3 +1,5 @@
1
+package QuizWeek1;
2
+
1 3
 public class MathUtilities{
2 4
   /**
3 5
    * Add two number together
@@ -6,7 +8,7 @@ public class MathUtilities{
6 8
    * @return the sum of the two numbers
7 9
    */
8 10
   public int add(int num1, int num2){
9
-      return -1;
11
+      return num1+num2;
10 12
   }
11 13
 
12 14
   /**
@@ -16,7 +18,7 @@ public class MathUtilities{
16 18
    * @return the sum of the two numbers
17 19
    */
18 20
   public double add(double num1, double num2){
19
-      return -1;
21
+      return num1+num2;
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 x = number * .5;
31
+      return x;
29 32
   }
30 33
 
31 34
   /**
@@ -34,7 +37,13 @@ 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
+      boolean isOdd= false;
41
+      if (number % 2 != 0){
42
+        isOdd = true;
43
+    }else{
44
+        isOdd = false;
45
+    }
46
+   return isOdd;
38 47
   }
39 48
 
40 49
 
@@ -44,7 +53,7 @@ public class MathUtilities{
44 53
    * @return the result of the number multiply by itself
45 54
    */
46 55
   public int square(int number) {
47
-      return -1;
56
+      return number*=number;
48 57
   }
49 58
 
50 59
 }

+ 2
- 0
MathUtilitiesTest.java View File

@@ -1,3 +1,5 @@
1
+package QuizWeek1;
2
+
1 3
 
2 4
 
3 5
 import static org.junit.Assert.*;

+ 17
- 3
StringUtilities.java View File

@@ -1,13 +1,27 @@
1
+package QuizWeek1;
2
+
1 3
 public class StringUtilities {
2 4
    public Character getMiddleCharacter(String word){
3
-       return null;
5
+       int middleChar;
6
+        if ((word.length()/2)%2 == 0){
7
+            middleChar = word.length()/2+1;
8
+            
9
+        }else{
10
+            middleChar = Math.round(word.length()/2);   
11
+            
12
+        }
13
+        return word.charAt(middleChar);
14
+       
4 15
    }
5 16
    
6 17
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
18
+     int removeChar = value.indexOf(charToRemove);      
19
+     return value.substring(0,removeChar) + value.substring(removeChar+1); 
8 20
    }
9 21
    
10 22
    public String getLastWord(String value) {
11
-       return null;
23
+       int indexLastWord = value.lastIndexOf(" ");
24
+        
25
+       return value.substring(indexLastWord+1);
12 26
    }
13 27
 }

+ 2
- 0
StringUtilitiesTest.java View File

@@ -1,3 +1,5 @@
1
+package QuizWeek1;
2
+
1 3
 import static org.junit.Assert.*;
2 4
 import org.junit.After;
3 5
 import org.junit.Before;

+ 18
- 30
package.bluej View File

@@ -1,59 +1,47 @@
1 1
 #BlueJ package file
2
-dependency1.from=StringUtilitiesTest
3
-dependency1.to=StringUtilities
2
+dependency1.from=MathUtilitiesTest
3
+dependency1.to=MathUtilities
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=BonusTest
6
-dependency2.to=Bonus
7
-dependency2.type=UsesDependency
8
-dependency3.from=MathUtilitiesTest
9
-dependency3.to=MathUtilities
10
-dependency3.type=UsesDependency
11
-editor.fx.0.height=722
12
-editor.fx.0.width=876
13
-editor.fx.0.x=158
14
-editor.fx.0.y=216
15
-objectbench.height=164
5
+objectbench.height=147
16 6
 objectbench.width=669
17 7
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7628294036061026
19
-package.editor.height=543
8
+package.divider.vertical=0.7634408602150538
9
+package.editor.height=490
20 10
 package.editor.width=567
21 11
 package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
12
+package.editor.y=23
13
+package.frame.height=709
24 14
 package.frame.width=693
25
-package.numDependencies=3
15
+package.numDependencies=1
26 16
 package.numTargets=6
27 17
 package.showExtends=true
28 18
 package.showUses=true
29
-project.charset=UTF-8
30 19
 readme.height=58
31 20
 readme.name=@README
32 21
 readme.width=47
33 22
 readme.x=10
34 23
 readme.y=10
35 24
 target1.height=50
36
-target1.name=StringUtilitiesTest
25
+target1.name=LoopFunTest
37 26
 target1.showInterface=false
38 27
 target1.type=UnitTestTargetJunit4
39 28
 target1.width=110
40
-target1.x=110
41
-target1.y=150
29
+target1.x=10
30
+target1.y=240
42 31
 target2.height=50
43
-target2.name=BonusTest
32
+target2.name=StringUtilitiesTest
44 33
 target2.showInterface=false
45 34
 target2.type=UnitTestTargetJunit4
46
-target2.width=80
47
-target2.x=120
48
-target2.y=270
49
-target3.association=BonusTest
35
+target2.width=110
36
+target2.x=110
37
+target2.y=150
50 38
 target3.height=50
51
-target3.name=Bonus
39
+target3.name=LoopFun
52 40
 target3.showInterface=false
53 41
 target3.type=ClassTarget
54 42
 target3.width=80
55
-target3.x=90
56
-target3.y=300
43
+target3.x=120
44
+target3.y=290
57 45
 target4.association=MathUtilitiesTest
58 46
 target4.height=50
59 47
 target4.name=MathUtilities