Quellcode durchsuchen

completed week 1 quiz

Jonathan Hinds vor 6 Jahren
Ursprung
Commit
e2329e61c4
4 geänderte Dateien mit 126 neuen und 32 gelöschten Zeilen
  1. 56
    4
      LoopFun.java
  2. 7
    5
      MathUtilities.java
  3. 44
    3
      StringUtilities.java
  4. 19
    20
      package.bluej

+ 56
- 4
LoopFun.java Datei anzeigen

@@ -7,8 +7,15 @@ public class LoopFun
7 7
        * @param number
8 8
        * @return the factorial of the number
9 9
        */
10
-      public int factorial(int number){
11
-          return -1;
10
+      public int factorial(int n){
11
+          int result = 0;
12
+          if(n == 0)
13
+          {
14
+              result = 1;
15
+          } else {
16
+              return n * factorial(n - 1);
17
+          }
18
+          return result;
12 19
       }
13 20
 
14 21
       /**
@@ -19,7 +26,29 @@ public class LoopFun
19 26
        * @return Upper case string of the first letter of each word
20 27
        */
21 28
       public String acronym(String phrase) {
22
-          return null;
29
+          //String result = "";
30
+          //String newString = "";
31
+          //String phra = phrase;
32
+          //String[] words = phra.split(" ");
33
+          //String word = words[0];
34
+          //if(word.length() > 0){
35
+            //  String[] letters = word.split("");
36
+            //  result += letters[0].toUpperCase();
37
+            //  newString = StringUtilities.searchReplaceFirst(phra, words[0], "");
38
+            //  return result += acronym(newString);
39
+          //}else{
40
+            //  return result;
41
+          //}
42
+          String result = "";
43
+          String[] words = phrase.split(" ");
44
+          
45
+          for(int i = 0; i < words.length; i ++)
46
+          {
47
+              String word = words[i];
48
+              String[] letters = word.split("");
49
+              result += letters[0].toUpperCase();
50
+          }
51
+          return result;
23 52
       }
24 53
 
25 54
       /**
@@ -35,6 +64,29 @@ public class LoopFun
35 64
        * @return the encrypted string by shifting each character by three character
36 65
        */
37 66
       public String encrypt(String word) {
38
-          return null;
67
+          char[] chars = word.toCharArray();
68
+          String result = "";
69
+          for(int i = 0; i < chars.length; i ++)
70
+          {
71
+              char currentChar = chars[i];
72
+              int charValue = (int)(currentChar);
73
+              
74
+              for(int m = 0; m < 3; m ++)
75
+              {
76
+                  charValue ++;
77
+                  if(charValue > 122)
78
+                  {
79
+                      charValue = 97;
80
+                  } 
81
+              }
82
+              
83
+              char newChar = (char)(charValue);
84
+              String charString = String.valueOf(newChar);
85
+              System.out.println(charString);
86
+              result += charString;
87
+              
88
+          }
89
+          
90
+          return result;
39 91
       }
40 92
 }

+ 7
- 5
MathUtilities.java Datei anzeigen

@@ -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,9 @@ 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
+      double temp = (double) number;
29
+      double result = temp / 2;
30
+      return result;
29 31
   }
30 32
 
31 33
   /**
@@ -34,7 +36,7 @@ public class MathUtilities{
34 36
    * @return true if the number is odd, false if it is even
35 37
    */
36 38
   public boolean isOdd(int number){
37
-      return false;
39
+      if(number % 2 == 0){return false;}else{return true;}
38 40
   }
39 41
 
40 42
 
@@ -44,7 +46,7 @@ public class MathUtilities{
44 46
    * @return the result of the number multiply by itself
45 47
    */
46 48
   public int square(int number) {
47
-      return -1;
49
+      return number*number;
48 50
   }
49 51
 
50 52
 }

+ 44
- 3
StringUtilities.java Datei anzeigen

@@ -1,13 +1,54 @@
1 1
 public class StringUtilities {
2
+    
3
+    
2 4
    public Character getMiddleCharacter(String word){
3
-       return null;
5
+        char[] character = word.toCharArray();
6
+        int len = (character.length) / 2;
7
+        
8
+        if(character.length % 2 == 0)
9
+        {
10
+            return character[len];
11
+        } else {
12
+            return character[len];
13
+        }
4 14
    }
5 15
    
6 16
    public String removeCharacter(String value, char charToRemove){
7
-     return null;  
17
+       //store input at the time this is called
18
+       String n = value;
19
+       //convert the to be removed char to a string
20
+       String charString = String.valueOf(charToRemove);
21
+       //replace the char with an empty string - removing it from the string
22
+       //if there is nothing to remove, this function will return the original string
23
+       n = searchReplaceFirst(n , charString, "");
24
+       //if n is now still the same as the starting value, this means nothing could be replaced
25
+       if(n.equals(value)){
26
+          //return n since all occurences have been removed
27
+          return n;
28
+       //if n is now different from value, after the char has been replaced, recall this method.
29
+       }else{
30
+          //convert the string back to a character
31
+          char remove = charString.charAt(0);
32
+          //recursivly call this function on the updated string
33
+          return removeCharacter(n, remove); 
34
+       }
35
+   }
36
+   
37
+   public static String searchReplaceFirst(String value, String replace, String replacement){
38
+       //if the two values are the same after something has been replaced, this means nothing has been replaced
39
+       if(value.equals(value.replaceFirst(replace, replacement)))
40
+       {
41
+           //so return the original string
42
+           return value;
43
+       //otherwise, this means something has been replaced
44
+       } else {
45
+          //return the new value
46
+          return value.replaceFirst(replace, replacement);
47
+       }
8 48
    }
9 49
    
10 50
    public String getLastWord(String value) {
11
-       return null;
51
+       String[] result = value.split(" ");
52
+       return result[result.length - 1];
12 53
    }
13 54
 }

+ 19
- 20
package.bluej Datei anzeigen

@@ -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
-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
11
+editor.fx.0.height=777
12
+editor.fx.0.width=1280
13
+editor.fx.0.x=0
14
+editor.fx.0.y=23
15
+objectbench.height=149
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.7632776934749621
19
+package.editor.height=496
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=406
22
+package.editor.y=57
23
+package.frame.height=717
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