Ver código fonte

Quiz Week 1 submission

Nathan Hall 6 anos atrás
pai
commit
4fa7e91d8f
7 arquivos alterados com 194 adições e 118 exclusões
  1. 73
    36
      LoopFun.java
  2. 2
    0
      LoopFunTest.java
  3. 48
    43
      MathUtilities.java
  4. 2
    0
      MathUtilitiesTest.java
  5. 51
    11
      StringUtilities.java
  6. 2
    0
      StringUtilitiesTest.java
  7. 16
    28
      package.bluej

+ 73
- 36
LoopFun.java Ver arquivo

@@ -1,40 +1,77 @@
1
+package QuizWeek1;
2
+
1 3
 public class LoopFun
2 4
 {
3 5
 
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
-      }
6
+    /**
7
+     * Given a number, return the factorial of that number.
8
+     * For example, given 5, the factorial is 5 x 4 x 3 x 2 x 1 which should return 120.
9
+     * @param number
10
+     * @return the factorial of the number
11
+     */
12
+    public int factorial(int number){
13
+        int result = 1;
14
+        if (number == 0){
15
+            result = 1;
16
+        } else {
17
+            for (int i = 1; i <= number; i++){
18
+                result *= i;
19
+
20
+            }
21
+
22
+        }
23
+        return result;
24
+    }
25
+
26
+    /**
27
+     * Given a phrase, get the acronym of that phrase. Acronym is the combination of
28
+     * the first character of each word in upper case.
29
+     * For example, given "Ruby on Rails", this method will return "ROR"
30
+     * @param phrase
31
+     * @return Upper case string of the first letter of each word
32
+     */
33
+    public String acronym(String phrase) {
34
+        String newPhrase = phrase.toUpperCase();
35
+        
36
+        char[] charArray = newPhrase.toCharArray();
37
+        String firstLet = Character.toString(charArray[0]);
38
+        //System.out.println(firstLet);
39
+        
40
+        for (int i = 1; i < charArray.length; i++){
41
+            if (charArray[i] == ' '){
42
+                System.out.println(charArray[i]);
43
+                firstLet += (charArray[i]+1);
44
+                System.out.println(firstLet);
45
+            }
46
+        }
47
+
48
+        
49
+        return firstLet;
50
+    }
51
+
52
+    /**
53
+     * To prevent anyone from reading our messages, we can encrypt it so it will only be readable by its
54
+     * intended audience. This method encrypt the message by shifting the letter by 3 characters. If the character is
55
+     * at the end of the alphabet, it will wraps around.
56
+     * For example:
57
+     *  'a' => 'd'
58
+     *  'w' => 'z'
59
+     *  'x' => 'a'
60
+     *  'y' => 'b'
61
+     * @param word
62
+     * @return the encrypted string by shifting each character by three character
63
+     */
64
+    public String encrypt(String word) {
65
+        // ASCII Table - a = 97, z = 122
66
+
67
+        char[] charArray = word.toCharArray();
68
+        for (int i = 0; i < charArray.length; i++){
69
+            (int)charArray[i] += 3;
70
+            if ((int)charArray[i] > 119){
71
+                (int)charArray[i] -= 23;
72
+            }
73
+        }
74
+
75
+        return null;
76
+    }
40 77
 }

+ 2
- 0
LoopFunTest.java Ver arquivo

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

+ 48
- 43
MathUtilities.java Ver arquivo

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

+ 2
- 0
MathUtilitiesTest.java Ver arquivo

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

+ 51
- 11
StringUtilities.java Ver arquivo

@@ -1,13 +1,53 @@
1
+package QuizWeek1;
2
+
1 3
 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
-   }
4
+    public Character getMiddleCharacter(String word){
5
+        char middle;
6
+        int mid;
7
+        String midString = "";
8
+
9
+        //find index of middle letter
10
+        mid = ((word.length())/2);
11
+        System.out.println(mid);
12
+
13
+        //midString = word.substring(mid, (mid+1));
14
+        middle = word.charAt(mid);
15
+
16
+        // char[] charArray = word.toCharArray();
17
+
18
+        // middle = charArray[mid];
19
+        //System.out.println(middle);
20
+
21
+        return middle;
22
+    }
23
+
24
+    public String removeCharacter(String value, char charToRemove){
25
+        String newWord = value.substring(0, charToRemove-1) + value.substring((charToRemove-1) + 1); 
26
+        System.out.println(newWord);
27
+        return newWord;  
28
+    }
29
+
30
+    public String getLastWord(String value) {
31
+        int i = 0;
32
+        int pos;
33
+
34
+        int len = (value.length()-1);
35
+        System.out.println(len);
36
+        int count = len;
37
+
38
+        char[] charArray = value.toCharArray();
39
+
40
+        //loop through until find last space
41
+        while (charArray[count] != ' '){
42
+
43
+            count--;
44
+
45
+        }
46
+
47
+        String last = value.substring(count + 1);
48
+        System.out.println(last);
49
+
50
+       
51
+        return last;
52
+    }
13 53
 }

+ 2
- 0
StringUtilitiesTest.java Ver arquivo

@@ -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;

+ 16
- 28
package.bluej Ver arquivo

@@ -1,32 +1,21 @@
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=152
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.7630402384500745
9
+package.editor.height=505
20 10
 package.editor.width=567
21
-package.editor.x=557
22
-package.editor.y=43
23
-package.frame.height=779
11
+package.editor.x=545
12
+package.editor.y=110
13
+package.frame.height=729
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
@@ -40,20 +29,19 @@ target1.width=110
40 29
 target1.x=110
41 30
 target1.y=150
42 31
 target2.height=50
43
-target2.name=BonusTest
32
+target2.name=LoopFunTest
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=10
37
+target2.y=240
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