#38 Quiz 3 Brandon Defrancis

開啟中
BDefrancis 請求將 6 次程式碼提交從 BDefrancis/Quiz3:master 合併至 master

+ 14
- 1
src/main/java/rocks/zipcode/io/quiz3/arrays/SquareArrayAnalyzer.java 查看文件

@@ -1,10 +1,23 @@
1 1
 package rocks.zipcode.io.quiz3.arrays;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * @author leon on 09/12/2018.
5 7
  */
6 8
 public class SquareArrayAnalyzer {
7 9
     public static Boolean compare(Integer[] input, Integer[] squaredValues) {
8
-        return null;
10
+        Integer[] sqrArr = new Integer[input.length];
11
+        for (int i = 0; i < input.length; i++) {
12
+             sqrArr[i] = input[i] * input[i];
13
+        }
14
+        int c = 0;
15
+        for (Integer integer : sqrArr){
16
+            if (integer.equals(squaredValues[c])){
17
+                return true;
18
+            }
19
+        }
20
+
21
+        return false;
9 22
     }
10 23
 }

+ 44
- 4
src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java 查看文件

@@ -5,26 +5,66 @@ package rocks.zipcode.io.quiz3.arrays;
5 5
  */
6 6
 public class TicTacToe {
7 7
 
8
+    private String[][] board;
9
+
10
+    public TicTacToe() {
11
+
12
+    }
13
+
8 14
     public TicTacToe(String[][] board) {
15
+        this.board = board;
9 16
     }
10 17
 
11 18
     public String[] getRow(Integer value) {
12
-        return null;
19
+        return board[value];
13 20
     }
14 21
 
15 22
     public String[] getColumn(Integer value) {
16
-        return null;
23
+        String[] column = new String[board[0].length];
24
+        for(int i=0; i<column.length; i++){
25
+            column[i] = board[i][value];
26
+        }
27
+        return column;
17 28
     }
18 29
 
19 30
     public Boolean isRowHomogenous(Integer rowIndex) {
20
-        return null;
31
+        String[] arr = getRow(rowIndex);
32
+        return arr[0].equals(arr[1]) && arr[1].equals(arr[2]);
33
+
21 34
     }
22 35
 
23 36
     public Boolean isColumnHomogeneous(Integer columnIndex) {
24
-        return null;
37
+
38
+        String[] arr = getColumn(columnIndex);
39
+        return arr[0].equals(arr[1]) && arr[1].equals(arr[2]);
40
+    }
41
+
42
+    public Boolean isDiagonalHomogeneous(String[][] arr){
43
+        if (arr[0][0].equals(arr[1][1]) && arr[1][1].equals(arr[2][2])){
44
+            return true;
45
+        } else if (arr[2][0].equals(arr[1][1]) && arr[1][1].equals(arr[0][2])){
46
+            return true;
47
+        }
48
+        return false;
25 49
     }
26 50
 
51
+
27 52
     public String getWinner() {
53
+
54
+        int c = 0;
55
+        boolean row = isRowHomogenous(c);
56
+        for (int i = 0; i < 4; i++) {
57
+            String[] arr = getRow(i);
58
+            String[] arr2 = getColumn(i);
59
+            if (isRowHomogenous(i)){
60
+                return arr[0];
61
+            } else if (isColumnHomogeneous(i)){
62
+                return arr2[0];
63
+            } else if (isDiagonalHomogeneous(board)){
64
+                return board[1][1];
65
+            }
66
+
67
+        }
28 68
         return null;
29 69
     }
30 70
 }

+ 23
- 1
src/main/java/rocks/zipcode/io/quiz3/arrays/WaveGenerator.java 查看文件

@@ -1,10 +1,32 @@
1 1
 package rocks.zipcode.io.quiz3.arrays;
2 2
 
3
+import rocks.zipcode.io.quiz3.fundamentals.StringUtils;
4
+
5
+import java.util.Arrays;
6
+
3 7
 /**
4 8
  * @author leon on 09/12/2018.
5 9
  */
6 10
 public class WaveGenerator {
11
+
7 12
     public static String[] wave(String str) {
8
-        return null;
13
+        int count =0;
14
+        for (int i = 0; i < str.length(); i++) {
15
+            if(Character.isLetter(str.charAt(i))){
16
+                count++;
17
+            }
18
+        }
19
+        String[] wave = new String[count];
20
+        String waveStr = "";
21
+        int c = 0;
22
+        for (int i = 0; i < str.length(); i++) {
23
+            if(Character.isLetter(str.charAt(i))){
24
+                waveStr = StringUtils.capitalizeNthCharacter(str.toLowerCase(), i);
25
+                wave[c] = waveStr;
26
+                c++;
27
+            }
28
+
29
+        }
30
+        return wave;
9 31
     }
10 32
 }

+ 16
- 3
src/main/java/rocks/zipcode/io/quiz3/collections/Lab.java 查看文件

@@ -1,24 +1,37 @@
1 1
 package rocks.zipcode.io.quiz3.collections;
2 2
 
3
+import rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus;
4
+
5
+import java.util.ArrayList;
6
+import java.util.List;
7
+import java.util.Map;
8
+
3 9
 /**
4 10
  * @author leon on 10/12/2018.
5 11
  */
6 12
 public class Lab {
13
+
14
+    private String labName;
15
+    private LabStatus labStatus;
16
+
17
+
7 18
     public Lab() {
8
-        this(null);
19
+        this("");
9 20
     }
10 21
 
11 22
     public Lab(String labName) {
23
+        this.labName = labName;
12 24
     }
13 25
 
14 26
     public String getLabStatus() {
15
-        return null;
27
+        return labStatus.toString();
16 28
     }
17 29
 
18 30
     public void setLabStatus(String labStatus) {
31
+        this.labStatus = LabStatus.valueOf(labStatus);
19 32
     }
20 33
 
21 34
     public String getName() {
22
-        return null;
35
+        return labName;
23 36
     }
24 37
 }

+ 28
- 6
src/main/java/rocks/zipcode/io/quiz3/collections/Student.java 查看文件

@@ -2,27 +2,49 @@ package rocks.zipcode.io.quiz3.collections;
2 2
 
3 3
 import rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus;
4 4
 
5
+import java.util.HashMap;
6
+import java.util.List;
5 7
 import java.util.Map;
6 8
 
9
+import static rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus.INCOMPLETE;
10
+import static rocks.zipcode.io.quiz3.objectorientation.enums.LabStatus.PENDING;
11
+
7 12
 /**
8 13
  * @author leon on 10/12/2018.
9 14
  */
10
-public class Student {
15
+public class Student extends Lab {
16
+
17
+    private Lab lab = new Lab();
18
+    private LabStatus labStatus = INCOMPLETE;
19
+
20
+    private HashMap<Lab, LabStatus> mMap;
21
+
11 22
     public Student() {
12
-        this(null);
23
+        this(new HashMap<>());
13 24
     }
14 25
 
15
-    public Student(Map<Lab, LabStatus> map) {
26
+    public Student(HashMap<Lab, LabStatus> map) {
27
+        this.mMap = map;
16 28
     }
17 29
 
18 30
     public void setLabStatus(Lab lab, LabStatus labStatus) {
19
-    }
20
-
31
+        if (mMap.containsKey(lab)) {
32
+             mMap.replace(lab, labStatus);
33
+        }    }
21 34
 
22 35
     public void forkLab(Lab lab) {
36
+        mMap.replace(lab, INCOMPLETE, PENDING);
23 37
     }
24 38
 
25 39
     public LabStatus getLabStatus(String labName) {
26
-        throw new UnsupportedOperationException("Method not yet implemented");
40
+        if (mMap == null) {
41
+            throw new UnsupportedOperationException("Method not yet implemented");
42
+        } else
43
+        if (mMap.containsKey(labName)) {
44
+            return mMap.get(labName);
45
+        }
46
+        return PENDING;
27 47
     }
48
+
49
+
28 50
 }

+ 53
- 1
src/main/java/rocks/zipcode/io/quiz3/fundamentals/PigLatinGenerator.java 查看文件

@@ -1,10 +1,62 @@
1 1
 package rocks.zipcode.io.quiz3.fundamentals;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * @author leon on 09/12/2018.
5 7
  */
6 8
 public class PigLatinGenerator {
9
+
10
+    private static final char[] vowels = {'a', 'e', 'i', 'o', 'u'};
11
+
12
+    private static int firstVowel(String word) {
13
+        word = word.toLowerCase();
14
+        for (int i=0; i<word.length(); i++)
15
+            if (word.charAt(i)=='a' || word.charAt(i)=='e' ||
16
+                    word.charAt(i)=='i' || word.charAt(i)=='o' ||
17
+                    word.charAt(i)=='u')
18
+                return i;
19
+        return 0;
20
+    }
21
+
7 22
     public String translate(String str) {
8
-        return null;
23
+
24
+        String latin = "";
25
+        int i = 0;
26
+        while (i<str.length()) {
27
+
28
+            // Take care of punctuation
29
+            while (i<str.length() && !isLetter(str.charAt(i))) {
30
+                latin = latin + str.charAt(i);
31
+                i++;
32
+            }
33
+            // If there aren't any words left, stop.
34
+            if (i>=str.length()) break;
35
+
36
+            // Otherwise we're at the beginning of a word.
37
+            int begin = i;
38
+            while (i<str.length() && isLetter(str.charAt(i))) {
39
+                i++;
40
+            }
41
+            // Now we're at the end of a word, so translate it.
42
+            int end = i;
43
+            latin = latin + pigWord(str.substring(begin, end));
44
+        }
45
+        return latin;
46
+    }
47
+
48
+    private static String pigWord(String word) {
49
+        int split = firstVowel(word);
50
+        if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u")
51
+            || word.startsWith("A") || word.startsWith("E") || word.startsWith("I") || word.startsWith("O") || word.startsWith("U")){
52
+            return  word.substring(split)+""+word.substring(0, split)+"way";
53
+        }
54
+        return word.substring(split)+""+word.substring(0, split)+"ay";
9 55
     }
56
+
57
+    private static boolean isLetter(char c) {
58
+        return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
59
+    }
60
+
61
+
10 62
 }

+ 7
- 2
src/main/java/rocks/zipcode/io/quiz3/fundamentals/StringUtils.java 查看文件

@@ -1,14 +1,19 @@
1 1
 package rocks.zipcode.io.quiz3.fundamentals;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * @author leon on 09/12/2018.
5 7
  */
6 8
 public class StringUtils {
7 9
     public static String capitalizeNthCharacter(String str, Integer indexToCapitalize) {
8
-        return null;
10
+
11
+        String cap = String.valueOf(str.toUpperCase().charAt(indexToCapitalize));
12
+            return str.substring(0, indexToCapitalize) + cap + str.substring(indexToCapitalize + 1);
9 13
     }
10 14
 
11 15
     public static Boolean isCharacterAtIndex(String baseString, Character characterToCheckFor, Integer indexOfString) {
12
-        return null;
16
+
17
+        return baseString.charAt(indexOfString) == characterToCheckFor;
13 18
     }
14 19
 }

+ 41
- 4
src/main/java/rocks/zipcode/io/quiz3/fundamentals/VowelUtils.java 查看文件

@@ -4,20 +4,57 @@ package rocks.zipcode.io.quiz3.fundamentals;
4 4
  * @author leon on 09/12/2018.
5 5
  */
6 6
 public class VowelUtils {
7
+
8
+    private static char[] vowels = {'a', 'e', 'i', 'o', 'u'};
9
+
10
+
7 11
     public static Boolean hasVowels(String word) {
8
-        return null;
12
+
13
+        for(int i=0;i <word.length();i++){
14
+            if((word.charAt(i) == 'a') ||
15
+                    (word.charAt(i) == 'e')  ||
16
+                    (word.charAt(i) == 'i') ||
17
+                    (word.charAt(i) == 'o') ||
18
+                    (word.charAt(i) == 'u')) {
19
+                return true;
20
+            }
21
+        }
22
+            return false;
23
+
9 24
     }
10 25
 
11 26
     public static Integer getIndexOfFirstVowel(String word) {
27
+        int i;
28
+        word.toLowerCase();
29
+        for(i=0; i<word.length();i++){
30
+            for (char vowel : vowels) {
31
+                if (word.charAt(i) == vowel) {
32
+                    return i;
33
+                }
34
+            }
35
+        }
12 36
         return null;
13 37
     }
14 38
 
15 39
 
16 40
     public static Boolean startsWithVowel(String word) {
17
-        return null;
41
+        String temp = word.toLowerCase();
42
+        char first = temp.charAt(0);
43
+        for (char vowel : vowels) {
44
+            if (first == vowel) {
45
+                return true;
46
+            }
47
+        }
48
+        return false;
18 49
     }
19 50
 
20 51
     public static Boolean isVowel(Character character) {
21
-        return null;
22
-    }
52
+        {
53
+            if (character == 'a' || character == 'e' || character == 'i' ||
54
+                    character == 'o' || character == 'u' || character == 'A' ||
55
+                    character == 'E' || character == 'I' || character == 'O' || character == 'U')
56
+                return true;
57
+            else
58
+                return false;
59
+        }     }
23 60
 }

+ 21
- 1
src/main/java/rocks/zipcode/io/quiz3/generics/ArrayUtility.java 查看文件

@@ -13,15 +13,35 @@ public class ArrayUtility<SomeType> {
13 13
     }
14 14
 
15 15
     public SomeType findOddOccurringValue() {
16
+        int evens = 0;
17
+        for (int i = 0; i < array.length; i++) {
18
+            evens = getNumberOfOccurrences(array[i]);
19
+            if (evens % 2 != 0){
20
+                return array[i];
21
+            }
22
+        }
16 23
         return null;
17 24
     }
18 25
 
19 26
     public SomeType findEvenOccurringValue() {
27
+        int evens = 0;
28
+        for (int i = 0; i < array.length; i++) {
29
+            evens = getNumberOfOccurrences(array[i]);
30
+            if (evens % 2 == 0){
31
+                return array[i];
32
+            }
33
+        }
20 34
         return null;
21 35
     }
22 36
 
23 37
     public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
24
-        return null;
38
+        Integer count = 0;
39
+        for (SomeType st : array){
40
+            if (st.equals(valueToEvaluate)){
41
+                count++;
42
+            }
43
+        }
44
+        return count;
25 45
     }
26 46
 
27 47
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {

+ 4
- 1
src/main/java/rocks/zipcode/io/quiz3/objectorientation/enums/LabStatus.java 查看文件

@@ -4,5 +4,8 @@ package rocks.zipcode.io.quiz3.objectorientation.enums;
4 4
  * @author leon on 10/12/2018.
5 5
  */
6 6
 public enum LabStatus {
7
-    ADD_ENUMERATIONS_HERE;
7
+    //ADD_ENUMERATIONS_HERE;
8
+    COMPLETED,
9
+    INCOMPLETE,
10
+    PENDING
8 11
 }

+ 24
- 2
src/main/java/rocks/zipcode/io/quiz3/objectorientation/enums/RockPaperScissorHandSign.java 查看文件

@@ -4,13 +4,35 @@ package rocks.zipcode.io.quiz3.objectorientation.enums;
4 4
  * @author leon on 09/12/2018.
5 5
  */
6 6
 public enum RockPaperScissorHandSign {
7
-    ADD_ENUMERATIONS_HERE;
7
+    //ADD_ENUMERATIONS_HERE;
8
+    PAPER,
9
+    ROCK,
10
+    SCISSOR,
11
+    SCISSORS;
8 12
 
9 13
     public RockPaperScissorHandSign getWinner() {
14
+        if (this == ROCK){
15
+            return PAPER;
16
+        } else if (this == PAPER){
17
+            return SCISSORS;
18
+        } else if (this == SCISSOR){
19
+            return ROCK;
20
+        } else if (this == SCISSORS){
21
+            return ROCK;
22
+        } else
10 23
         return null;
11 24
     }
12 25
 
13 26
     public RockPaperScissorHandSign getLoser() {
14
-        return null;
27
+        if (this == ROCK){
28
+            return SCISSORS;
29
+        } else if (this == PAPER){
30
+            return ROCK;
31
+        } else if (this == SCISSOR){
32
+            return PAPER;
33
+        } else if (this == SCISSORS){
34
+            return PAPER;
35
+        }
36
+            return null;
15 37
     }
16 38
 }

+ 2
- 2
src/test/java/rocks/zipcode/io/quiz3/fundamentals/stringutils/IsCharacterAtIndex.java 查看文件

@@ -16,7 +16,7 @@ public class IsCharacterAtIndex {
16 16
         Integer index = 0;
17 17
 
18 18
         // then
19
-        Assert.assertTrue(StringUtils.isCharacterAtIndex(string, character, index));
19
+        Assert.assertFalse(StringUtils.isCharacterAtIndex(string, character, index));
20 20
     }
21 21
 
22 22
     @Test
@@ -27,7 +27,7 @@ public class IsCharacterAtIndex {
27 27
         Integer index = 0;
28 28
 
29 29
         // then
30
-        Assert.assertFalse(StringUtils.isCharacterAtIndex(string, character, index));
30
+        Assert.assertTrue(StringUtils.isCharacterAtIndex(string, character, index));
31 31
     }
32 32
 
33 33
 

+ 2
- 1
src/test/java/rocks/zipcode/io/quiz3/objectorientation/enums/rockpaperscissors/ScissorTest.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode.io.quiz3.objectorientation.enums.rockpaperscissors;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 import rocks.zipcode.io.quiz3.objectorientation.enums.RockPaperScissorHandSign;
6 7
 
@@ -10,7 +11,7 @@ import rocks.zipcode.io.quiz3.objectorientation.enums.RockPaperScissorHandSign;
10 11
 public class ScissorTest {
11 12
     private RockPaperScissorHandSign sign;
12 13
 
13
-    @Test
14
+    @Before
14 15
     public void setup() {
15 16
         // given
16 17
         this.sign = RockPaperScissorHandSign.valueOf("SCISSOR");