Browse Source

completed quiz

Connor Dunnigan 6 years ago
parent
commit
43ab1dafbf
4 changed files with 102 additions and 32 deletions
  1. 41
    3
      LoopFun.java
  2. 12
    5
      MathUtilities.java
  3. 32
    6
      StringUtilities.java
  4. 17
    18
      package.bluej

+ 41
- 3
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 fct = 1;
12
+            for(int i=1; i<=number; i++){
13
+              fct *= i;
14
+            }
15
+
16
+          return fct;
12 17
       }
13 18
 
14 19
       /**
@@ -19,7 +24,13 @@ public class LoopFun
19 24
        * @return Upper case string of the first letter of each word
20 25
        */
21 26
       public String acronym(String phrase) {
22
-          return null;
27
+
28
+        String acr = new String();
29
+        for(String str : phrase.split(" ")){
30
+          acr += str.charAt(0);
31
+        }
32
+        acr = acr.toUpperCase();
33
+        return acr;
23 34
       }
24 35
 
25 36
       /**
@@ -35,6 +46,33 @@ 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 cryptStr = new String();
50
+        word = word.toLowerCase();
51
+        char chArr[] = word.toCharArray();
52
+        for(char ch : chArr){
53
+          switch(ch){
54
+            case ' ':
55
+                    break;
56
+            case 'z':
57
+                        ch = 'c';
58
+                        cryptStr += ch;
59
+                    break;
60
+            case 'y':
61
+                        ch = 'b';
62
+                        cryptStr += ch;
63
+                    break;
64
+            case 'x':
65
+                        ch = 'a';
66
+                        cryptStr += ch;
67
+                    break;
68
+            default:
69
+                        ch += 3;
70
+                        cryptStr += ch;
71
+                    break;
72
+          }
73
+
74
+        }
75
+
76
+          return cryptStr;
39 77
       }
40 78
 }

+ 12
- 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,14 @@ 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 odd;
38
+    if(number%2 == 1){
39
+      odd = true;
40
+    }
41
+    else{
42
+      odd = false;
43
+    }
44
+      return odd;
38 45
   }
39 46
 
40 47
 
@@ -44,7 +51,7 @@ 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
+      return number*number;
48 55
   }
49 56
 
50 57
 }

+ 32
- 6
StringUtilities.java View File

@@ -1,13 +1,39 @@
1 1
 public class StringUtilities {
2
+
2 3
    public Character getMiddleCharacter(String word){
3
-       return null;
4
+      char middle;
5
+      int pos, len;
6
+      char ch[] = word.toCharArray();
7
+
8
+      len = ch.length;
9
+      if(ch.length%2 == 0){
10
+          pos = (len/2 - 1)+1;
11
+      } else{
12
+          pos = len/2;
13
+      }
14
+
15
+      middle = ch[pos];
16
+      return middle;
4 17
    }
5
-   
18
+
6 19
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
20
+
21
+     char ch[] = value.toCharArray();
22
+     String newStr = new String();
23
+     int len = ch.length;
24
+
25
+     for(int i=0; i < len; i++){
26
+       if(ch[i] != charToRemove){
27
+         newStr += ch[i];
28
+       } else{
29
+       }
30
+     }
31
+     return newStr;
8 32
    }
9
-   
10
-   public String getLastWord(String value) {
11
-       return null;
33
+
34
+   public String getLastWord(String value){
35
+
36
+      String lastWord = value.substring(value.lastIndexOf(" ")+1);
37
+       return lastWord;
12 38
    }
13 39
 }

+ 17
- 18
package.bluej View File

@@ -2,25 +2,25 @@
2 2
 dependency1.from=StringUtilitiesTest
3 3
 dependency1.to=StringUtilities
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=BonusTest
6
-dependency2.to=Bonus
5
+dependency2.from=LoopFunTest
6
+dependency2.to=LoopFun
7 7
 dependency2.type=UsesDependency
8 8
 dependency3.from=MathUtilitiesTest
9 9
 dependency3.to=MathUtilities
10 10
 dependency3.type=UsesDependency
11 11
 editor.fx.0.height=722
12 12
 editor.fx.0.width=876
13
-editor.fx.0.x=158
14
-editor.fx.0.y=216
15
-objectbench.height=164
13
+editor.fx.0.x=445
14
+editor.fx.0.y=24
15
+objectbench.height=151
16 16
 objectbench.width=669
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7628294036061026
19
-package.editor.height=543
18
+package.divider.vertical=0.7620481927710844
19
+package.editor.height=499
20 20
 package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
21
+package.editor.x=0
22
+package.editor.y=23
23
+package.frame.height=722
24 24
 package.frame.width=693
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
@@ -40,20 +40,19 @@ target1.width=110
40 40
 target1.x=110
41 41
 target1.y=150
42 42
 target2.height=50
43
-target2.name=BonusTest
43
+target2.name=LoopFunTest
44 44
 target2.showInterface=false
45 45
 target2.type=UnitTestTargetJunit4
46
-target2.width=80
47
-target2.x=120
48
-target2.y=270
49
-target3.association=BonusTest
46
+target2.width=110
47
+target2.x=10
48
+target2.y=240
50 49
 target3.height=50
51
-target3.name=Bonus
50
+target3.name=LoopFun
52 51
 target3.showInterface=false
53 52
 target3.type=ClassTarget
54 53
 target3.width=80
55
-target3.x=90
56
-target3.y=300
54
+target3.x=200
55
+target3.y=210
57 56
 target4.association=MathUtilitiesTest
58 57
 target4.height=50
59 58
 target4.name=MathUtilities