Bläddra i källkod

Completed quiz

Jennifer Chao 6 år sedan
förälder
incheckning
59c689cafa
4 ändrade filer med 182 tillägg och 108 borttagningar
  1. 80
    36
      LoopFun.java
  2. 47
    43
      MathUtilities.java
  3. 38
    11
      StringUtilities.java
  4. 17
    18
      package.bluej

+ 80
- 36
LoopFun.java Visa fil

@@ -1,40 +1,84 @@
1 1
 public class LoopFun
2 2
 {
3 3
 
4
-      /**
5
-       * Given a number, return the factorial of that number.
6
-       * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
7
-       * @param number
8
-       * @return the factorial of the number
9
-       */
10
-      public int factorial(int number){
11
-          return -1;
12
-      }
13
-
14
-      /**
15
-       * Given a phrase, get the acronym of that phrase. Acronym is the combination of
16
-       * the first character of each word in upper case.
17
-       * For example, given "Ruby on Rails", this method will return "ROR"
18
-       * @param phrase
19
-       * @return Upper case string of the first letter of each word
20
-       */
21
-      public String acronym(String phrase) {
22
-          return null;
23
-      }
24
-
25
-      /**
26
-       * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
27
-       * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
28
-       * at the end of the alphabet, it will wraps around.
29
-       * For example:
30
-       *  'a' => 'd'
31
-       *  'w' => 'z'
32
-       *  'x' => 'a'
33
-       *  'y' => 'b'
34
-       * @param word
35
-       * @return the encrypted string by shifting each character by three character
36
-       */
37
-      public String encrypt(String word) {
38
-          return null;
39
-      }
4
+    /**
5
+     * Given a number, return the factorial of that number.
6
+     * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
7
+     * @param number
8
+     * @return the factorial of the number
9
+     */
10
+    public int factorial(int number){
11
+        int factorial = 1;
12
+
13
+        for (int i = 2; i <= number; i++) {
14
+            factorial *= i;
15
+        }
16
+
17
+        return factorial;
18
+    }
19
+
20
+    /**
21
+     * Given a phrase, get the acronym of that phrase. Acronym is the combination of
22
+     * the first character of each word in upper case.
23
+     * For example, given "Ruby on Rails", this method will return "ROR"
24
+     * @param phrase
25
+     * @return Upper case string of the first letter of each word
26
+     */
27
+    public String acronym(String phrase) {
28
+        StringBuilder sbAcronym = new StringBuilder();
29
+
30
+        String firstCapital = phrase.substring(0, 1).toUpperCase();
31
+        sbAcronym.append(firstCapital);
32
+
33
+        for (int i = 1; i < phrase.length(); i++) {
34
+            if (phrase.charAt(i) == ' ') {
35
+                sbAcronym.append(phrase.charAt(i + 1));
36
+            }
37
+        }
38
+
39
+        String Acronym = sbAcronym.toString().toUpperCase();
40
+
41
+        return Acronym;
42
+    }
43
+
44
+    /**
45
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
46
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
47
+     * at the end of the alphabet, it will wraps around.
48
+     * For example:
49
+     *  'a' => 'd'
50
+     *  'w' => 'z'
51
+     *  'x' => 'a'
52
+     *  'y' => 'b'
53
+     * @param word
54
+     * @return the encrypted string by shifting each character by three character
55
+     */
56
+    public String encrypt(String word) {
57
+        // char[] toEncode = word.toCharArray();
58
+
59
+        StringBuilder sbEncryption = new StringBuilder(); 
60
+
61
+        char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
62
+
63
+        int k;
64
+
65
+        for (int i = 0; i < 26; i++) {
66
+            for (int j = 0; j < word.length(); j++) {
67
+                if (word.charAt(j) == arr[i]){
68
+                    k = i;
69
+
70
+                    if (k < 23) {
71
+                        sbEncryption.append(arr[(k + 3)]);
72
+                    }
73
+                    
74
+                    if (k >= 23) {
75
+                        sbEncryption.append(arr[(k - 23)]);
76
+                    } 
77
+                }
78
+            }
79
+        }
80
+        String encryption = sbEncryption.toString();
81
+
82
+        return encryption;
83
+    }
40 84
 }

+ 47
- 43
MathUtilities.java Visa fil

@@ -1,50 +1,54 @@
1 1
 public class MathUtilities{
2
-  /**
3
-   * Add two number together
4
-   * @param num1 first number
5
-   * @param num2 second number
6
-   * @return the sum of the two numbers
7
-   */
8
-  public int add(int num1, int num2){
9
-      return -1;
10
-  }
2
+    /**
3
+     * Add two number together
4
+     * @param num1 first number
5
+     * @param num2 second number
6
+     * @return the sum of the two numbers
7
+     */
8
+    public int add(int num1, int num2){     
9
+        return (num1 + num2);
10
+    }
11 11
 
12
-  /**
13
-   * Add two number together
14
-   * @param num1 first number
15
-   * @param num2 second number
16
-   * @return the sum of the two numbers
17
-   */
18
-  public double add(double num1, double num2){
19
-      return -1;
20
-  }
12
+    /**
13
+     * Add two number together
14
+     * @param num1 first number
15
+     * @param num2 second number
16
+     * @return the sum of the two numbers
17
+     */
18
+    public double add(double num1, double num2){
19
+        return (num1 + num2);
20
+    }
21 21
 
22
-  /**
23
-   * Get half the value of the number
24
-   * @param number the number given
25
-   * @return the half of the number in double
26
-   */
27
-  public double half(int number) {
28
-      return -1;
29
-  }
22
+    /**
23
+     * Get half the value of the number
24
+     * @param number the number given
25
+     * @return the half of the number in double
26
+     */
27
+    public double half(int number) {
28
+        double doubNum = (double) number;
29
+        return (doubNum / 2);
30
+    }
30 31
 
31
-  /**
32
-   * Determine if the number is odd
33
-   * @param number the number given
34
-   * @return true if the number is odd, false if it is even
35
-   */
36
-  public boolean isOdd(int number){
37
-      return false;
38
-  }
32
+    /**
33
+     * Determine if the number is odd
34
+     * @param number the number given
35
+     * @return true if the number is odd, false if it is even
36
+     */
37
+    public boolean isOdd(int number){
38
+        if (number % 2 == 1) {
39
+            return true;
40
+        } else {
41
+            return false;
42
+        }
43
+    }
39 44
 
40
-
41
-  /**
42
-   * Multiply the number by itself
43
-   * @param number the number given
44
-   * @return the result of the number multiply by itself
45
-   */
46
-  public int square(int number) {
47
-      return -1;
48
-  }
45
+    /**
46
+     * Multiply the number by itself
47
+     * @param number the number given
48
+     * @return the result of the number multiply by itself
49
+     */
50
+    public int square(int number) {
51
+        return (number * number);
52
+    }
49 53
 
50 54
 }

+ 38
- 11
StringUtilities.java Visa fil

@@ -1,13 +1,40 @@
1 1
 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
-   }
2
+    public Character getMiddleCharacter(String word){
3
+        char midChar = word.charAt(word.length() / 2);       
4
+        return midChar;
5
+    }
6
+
7
+    public String removeCharacter(String value, char charToRemove){
8
+        StringBuilder sb = new StringBuilder();
9
+
10
+        for (int i = 0; i < value.length(); i++) {
11
+            if (value.charAt(i) == charToRemove) {
12
+                continue;
13
+            } else {
14
+                sb.append(value.charAt(i));
15
+            }
16
+        }
17
+
18
+        String newValue = sb.toString();
19
+
20
+        return newValue;  
21
+    }
22
+
23
+    public String getLastWord(String value) {
24
+        // Cycle through each element of the string.
25
+        // If the element is a space, the word is the space 
26
+
27
+        StringBuilder sbLastWord = new StringBuilder();
28
+
29
+        for (int i = value.length() - 1; i > 0; i--) {
30
+            if (value.charAt(i) == ' ') {
31
+                sbLastWord.append(value.substring(i + 1, value.length()));
32
+                break;
33
+            }
34
+        }
35
+
36
+        String lastWord = sbLastWord.toString();
37
+        
38
+        return lastWord;
39
+    }
13 40
 }

+ 17
- 18
package.bluej Visa fil

@@ -2,24 +2,24 @@
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=MathUtilitiesTest
6
+dependency2.to=MathUtilities
7 7
 dependency2.type=UsesDependency
8
-dependency3.from=MathUtilitiesTest
9
-dependency3.to=MathUtilities
8
+dependency3.from=LoopFunTest
9
+dependency3.to=LoopFun
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
11
+editor.fx.0.height=883
12
+editor.fx.0.width=921
13
+editor.fx.0.x=323
14
+editor.fx.0.y=122
15 15
 objectbench.height=164
16 16
 objectbench.width=669
17 17
 package.divider.horizontal=0.6
18 18
 package.divider.vertical=0.7628294036061026
19 19
 package.editor.height=543
20 20
 package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
21
+package.editor.x=1002
22
+package.editor.y=209
23 23
 package.frame.height=779
24 24
 package.frame.width=693
25 25
 package.numDependencies=3
@@ -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=170
55
+target3.y=260
57 56
 target4.association=MathUtilitiesTest
58 57
 target4.height=50
59 58
 target4.name=MathUtilities