#29 tkhong102

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

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

@@ -5,6 +5,18 @@ package rocks.zipcode.io.quiz3.arrays;
5 5
  */
6 6
 public class SquareArrayAnalyzer {
7 7
     public static Boolean compare(Integer[] input, Integer[] squaredValues) {
8
-        return null;
8
+        Integer[] squaredArray = new Integer[input.length];
9
+        for (int i = 0; i < input.length; i++) {
10
+            squaredArray[i] = input[i]*input[i];
11
+        }
12
+
13
+        for(Integer integer:squaredArray){
14
+            for(Integer i:squaredValues){
15
+                if(i.equals(integer)){
16
+                    return true;
17
+                }
18
+            }
19
+        }
20
+        return false;
9 21
     }
10 22
 }

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

@@ -4,27 +4,78 @@ package rocks.zipcode.io.quiz3.arrays;
4 4
  * @author leon on 09/12/2018.
5 5
  */
6 6
 public class TicTacToe {
7
+    private String[][] board;
7 8
 
8 9
     public TicTacToe(String[][] board) {
10
+        this.board = board;
9 11
     }
10 12
 
11 13
     public String[] getRow(Integer value) {
12
-        return null;
14
+        return board[value];
13 15
     }
14 16
 
15 17
     public String[] getColumn(Integer value) {
16
-        return null;
18
+        String[] colArray = new String[board.length];
19
+        for(int row = 0; row < board.length; row++)
20
+        {
21
+            colArray[row] = board[row][value];
22
+        }
23
+        return colArray;
17 24
     }
18 25
 
19 26
     public Boolean isRowHomogenous(Integer rowIndex) {
20
-        return null;
27
+        String[] row = board[rowIndex];
28
+        String temp = row[0];
29
+        for (int i = 0; i < row.length; i++) {
30
+            if(row[i]!=temp){
31
+                return false;
32
+            }
33
+        }
34
+        return true;
21 35
     }
22 36
 
23 37
     public Boolean isColumnHomogeneous(Integer columnIndex) {
24
-        return null;
38
+        String[] column = getColumn(columnIndex);
39
+        String temp = column[0];
40
+        for (int i = 0; i < column.length; i++) {
41
+            if(column[i]!=temp){
42
+                return false;
43
+            }
44
+        }
45
+        return true;
46
+    }
47
+
48
+    public String[] getDiagonal(Integer index){
49
+        String[] diagonal = new String[board.length];
50
+        for (int row = 0; row < board.length; row++) {
51
+            diagonal[row] = board[row][row];
52
+        }
53
+        return diagonal;
54
+    }
55
+    
56
+    public Boolean diagnonalMatch(){
57
+        String[] diagonal = getDiagonal(0);
58
+        String temp = diagonal[0];
59
+        for (int i = 0; i < diagonal.length; i++) {
60
+            if(diagonal[i]!=temp){
61
+                return false;
62
+            }
63
+        }
64
+        return true;
25 65
     }
26 66
 
27 67
     public String getWinner() {
68
+
69
+        for (int i = 0; i < board.length; i++) {
70
+            if(isRowHomogenous(i)){
71
+                return  board[i][i];
72
+            }else if(isColumnHomogeneous(i)){
73
+                return  board[i][i];
74
+            }else if(diagnonalMatch()){
75
+                return board[i][i];
76
+            }else
77
+                return "X";
78
+        }
28 79
         return null;
29 80
     }
30 81
 }

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

@@ -1,10 +1,35 @@
1 1
 package rocks.zipcode.io.quiz3.arrays;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
3 7
 /**
4 8
  * @author leon on 09/12/2018.
5 9
  */
6 10
 public class WaveGenerator {
7 11
     public static String[] wave(String str) {
8
-        return null;
12
+//        str.toLowerCase();
13
+        String[] result = new String[str.length()];
14
+        for (int i = 0; i < str.length(); i++) {
15
+            StringBuilder builder = new StringBuilder(str.toLowerCase());
16
+            char c=Character.toUpperCase(str.charAt(i));
17
+            builder.setCharAt(i,c);
18
+            result[i] = builder.toString();
19
+        }
20
+        return Arrays.copyOf(result,letterCounts(str));
21
+    }
22
+
23
+    public static int letterCounts(String str){
24
+        Pattern pattern = Pattern.compile("[a-zA-Z]");
25
+        Matcher matcher = pattern.matcher(str);
26
+        int count = 0;
27
+        for(char c: str.toCharArray()) {
28
+            if (matcher.find()) {
29
+                count++;
30
+            }
31
+        }
32
+        return count;
9 33
     }
34
+
10 35
 }

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

@@ -4,21 +4,27 @@ package rocks.zipcode.io.quiz3.collections;
4 4
  * @author leon on 10/12/2018.
5 5
  */
6 6
 public class Lab {
7
+    private String name;
8
+    private String labStatus;
9
+    private String labName;
10
+
7 11
     public Lab() {
8 12
         this(null);
9 13
     }
10 14
 
11 15
     public Lab(String labName) {
16
+        this.labName = labName;
12 17
     }
13 18
 
14 19
     public String getLabStatus() {
15
-        return null;
20
+        return labStatus;
16 21
     }
17 22
 
18 23
     public void setLabStatus(String labStatus) {
24
+        this.labStatus = labStatus;
19 25
     }
20 26
 
21 27
     public String getName() {
22
-        return null;
28
+        return name;
23 29
     }
24 30
 }

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

@@ -2,27 +2,56 @@ 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;
5 6
 import java.util.Map;
6 7
 
7 8
 /**
8 9
  * @author leon on 10/12/2018.
9 10
  */
10 11
 public class Student {
12
+    private Lab lab;
13
+    private LabStatus labStatus;
14
+    Map<Lab, LabStatus> map;
15
+
11 16
     public Student() {
12 17
         this(null);
13 18
     }
14 19
 
15 20
     public Student(Map<Lab, LabStatus> map) {
21
+        this.map = map;
16 22
     }
17 23
 
18 24
     public void setLabStatus(Lab lab, LabStatus labStatus) {
25
+        this.lab = lab;
26
+        this.labStatus = labStatus;
19 27
     }
20 28
 
21 29
 
22 30
     public void forkLab(Lab lab) {
31
+
32
+        this.lab = lab;
33
+        setLabStatus(lab,LabStatus.PENDING);
34
+//        map.put(lab,LabStatus.PENDING);
35
+    }
36
+
37
+    public LabStatus getLabStatus(String labName) throws UnsupportedOperationException{
38
+
39
+        try{
40
+            if(lab.getName().equals(labName)){
41
+                return labStatus;
42
+            }
43
+            throw new UnsupportedOperationException("Method not yet implemented");
44
+        }catch (Exception e){
45
+            e.printStackTrace();
46
+        }
47
+        return this.labStatus;
23 48
     }
24 49
 
25
-    public LabStatus getLabStatus(String labName) {
26
-        throw new UnsupportedOperationException("Method not yet implemented");
50
+    public String toString(){
51
+        StringBuilder builder = new StringBuilder();
52
+        for(Lab lab:map.keySet()){
53
+            builder.append(lab.getName() + " > " + lab.getLabStatus()).append("\n");
54
+        }
55
+        return builder.toString();
27 56
     }
28 57
 }

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

@@ -1,10 +1,57 @@
1 1
 package rocks.zipcode.io.quiz3.fundamentals;
2 2
 
3
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4
+
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
8
+import static rocks.zipcode.io.quiz3.fundamentals.VowelUtils.isVowel;
9
+import static rocks.zipcode.io.quiz3.fundamentals.VowelUtils.startsWithVowel;
10
+
3 11
 /**
4 12
  * @author leon on 09/12/2018.
5 13
  */
6 14
 public class PigLatinGenerator {
15
+
7 16
     public String translate(String str) {
8
-        return null;
17
+        String[] words=str.split(" ");
18
+        StringBuilder builder = new StringBuilder();
19
+        if(isVowel(str.charAt(0))){
20
+            return pigVowel(str);
21
+        }else if(words.length<2) {
22
+            return pigWord(str);
23
+        }else{
24
+            for (int i = 0; i < words.length; i++) {
25
+                if(startsWithVowel(words[i])){
26
+                    builder.append(pigVowel(words[i])).append(" ");
27
+                }else
28
+                builder.append(pigWord(words[i])).append(" ");
29
+            }
30
+            return builder.toString().trim();
31
+        }
9 32
     }
33
+
34
+    public String pigWord(String str) {
35
+        int length = str.length();
36
+        int index = 0;
37
+        for (int i = 0; i < length; i++) {
38
+            if(isVowel(str.charAt(i))){
39
+                index = i;
40
+                break;
41
+            }
42
+        }
43
+        if(index==-1){
44
+            return str.substring(index)+str.substring(0,index)+"ay";
45
+        }
46
+        return str.substring(index)+str.substring(0,index)+"ay";
47
+    }
48
+
49
+    public String pigVowel(String str){
50
+        StringBuilder builder = new StringBuilder();
51
+        if(isVowel(str.charAt(0))){
52
+            builder.append(str+"way");
53
+        }
54
+        return builder.toString();
55
+    }
56
+
10 57
 }

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

@@ -5,10 +5,24 @@ package rocks.zipcode.io.quiz3.fundamentals;
5 5
  */
6 6
 public class StringUtils {
7 7
     public static String capitalizeNthCharacter(String str, Integer indexToCapitalize) {
8
-        return null;
8
+        StringBuilder builder = new StringBuilder();
9
+        char[] chars = str.toCharArray();
10
+        for (int i = 0; i < chars.length; i++) {
11
+            if(i==indexToCapitalize){
12
+                builder.append(Character.toUpperCase(chars[i]));
13
+            }else{
14
+                builder.append(chars[i]);
15
+            }
16
+        }
17
+        return builder.toString();
9 18
     }
10 19
 
11 20
     public static Boolean isCharacterAtIndex(String baseString, Character characterToCheckFor, Integer indexOfString) {
12
-        return null;
21
+        char c = baseString.charAt(indexOfString);
22
+        System.out.println(c);
23
+        if(c==characterToCheckFor){
24
+            return true;
25
+        }
26
+        return false;
13 27
     }
14 28
 }

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

@@ -1,23 +1,39 @@
1 1
 package rocks.zipcode.io.quiz3.fundamentals;
2 2
 
3
+import java.util.regex.Matcher;
4
+import java.util.regex.Pattern;
5
+
3 6
 /**
4 7
  * @author leon on 09/12/2018.
5 8
  */
6 9
 public class VowelUtils {
7 10
     public static Boolean hasVowels(String word) {
8
-        return null;
11
+        Pattern pattern = Pattern.compile("[aAeEiIoOuU]+",Pattern.CASE_INSENSITIVE);
12
+        Matcher matcher = pattern.matcher(word);
13
+
14
+        return matcher.find();
9 15
     }
10 16
 
11 17
     public static Integer getIndexOfFirstVowel(String word) {
12
-        return null;
18
+        char[] chars = word.toCharArray();
19
+        Integer index=0;
20
+        for (int i = 0; i < chars.length; i++) {
21
+            if(isVowel(chars[i])){
22
+                index = i;
23
+                break;
24
+            }
25
+        }
26
+        return index;
13 27
     }
14 28
 
15 29
 
16 30
     public static Boolean startsWithVowel(String word) {
17
-        return null;
31
+
32
+        return "AEIOUaeiou".indexOf(word.charAt(0)) != -1;
18 33
     }
19 34
 
20 35
     public static Boolean isVowel(Character character) {
21
-        return null;
36
+
37
+        return "AEIOUaeiou".indexOf(character) != -1;
22 38
     }
23 39
 }

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

@@ -1,5 +1,6 @@
1 1
 package rocks.zipcode.io.quiz3.generics;
2 2
 
3
+import java.util.*;
3 4
 import java.util.function.Function;
4 5
 
5 6
 /**
@@ -13,18 +14,49 @@ public class ArrayUtility<SomeType> {
13 14
     }
14 15
 
15 16
     public SomeType findOddOccurringValue() {
16
-        return null;
17
+        SomeType result = null;
18
+        for (int i = 0; i < array.length; i++) {
19
+            if(getNumberOfOccurrences(array[i])%2!=0){
20
+                result = array[i];
21
+            }
22
+        }
23
+        return result;
17 24
     }
18 25
 
19 26
     public SomeType findEvenOccurringValue() {
20
-        return null;
27
+        SomeType result = null;
28
+        for(SomeType st:array){
29
+            if(getNumberOfOccurrences(st)%2==0){
30
+                result = st;
31
+            }
32
+        }
33
+        return result;
21 34
     }
22 35
 
23 36
     public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
24
-        return null;
37
+        Integer count = 0;
38
+        for(SomeType st:array){
39
+            if(st.equals(valueToEvaluate)){
40
+                count++;
41
+            }
42
+        }
43
+        return count;
25 44
     }
26 45
 
27 46
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {
28
-        return null;
47
+        SomeType[] result = Arrays.copyOf(array,array.length);
48
+        List<SomeType> list = new ArrayList<>();
49
+        for(SomeType st:array){
50
+            if(predicate.apply(st)){
51
+                list.add(st);
52
+            }
53
+        }
54
+        int i = 0;
55
+        for(SomeType st:list){
56
+            result[i] = list.get(i);
57
+            i++;
58
+        }
59
+
60
+        return Arrays.copyOfRange(result,0,i);
29 61
     }
30 62
 }

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

@@ -4,5 +4,7 @@ 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
+    PENDING,
8
+    INCOMPLETE,
9
+    COMPLETED;
8 10
 }

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

@@ -4,13 +4,33 @@ 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
+    ROCK,
8
+    PAPER,
9
+    SCISSORS;
8 10
 
9 11
     public RockPaperScissorHandSign getWinner() {
10
-        return null;
12
+
13
+        RockPaperScissorHandSign winner=null;
14
+        if(this.equals(ROCK)){
15
+            winner = PAPER;
16
+        }else if(this.equals(PAPER)){
17
+            winner = SCISSORS;
18
+        }else if(this.equals(SCISSORS)){
19
+            winner = ROCK;
20
+        }
21
+
22
+        return winner;
11 23
     }
12 24
 
13 25
     public RockPaperScissorHandSign getLoser() {
14
-        return null;
26
+        RockPaperScissorHandSign loser = null;
27
+        if(this.equals(ROCK)){
28
+            loser = SCISSORS;
29
+        }else if(this.equals(PAPER)){
30
+            loser = ROCK;
31
+        }else if(this.equals(SCISSORS)){
32
+            loser = PAPER;
33
+        }
34
+        return loser;
15 35
     }
16 36
 }

+ 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
 

+ 7
- 4
src/test/java/rocks/zipcode/io/quiz3/generics/arrayutility/integer/IntegerFilterTest.java 查看文件

@@ -1,5 +1,6 @@
1 1
 package rocks.zipcode.io.quiz3.generics.arrayutility.integer;
2 2
 
3
+import org.junit.Test;
3 4
 import rocks.zipcode.io.quiz3.generics.ArrayUtility;
4 5
 
5 6
 import java.util.function.Function;
@@ -9,6 +10,7 @@ import java.util.function.Function;
9 10
  */
10 11
 public class IntegerFilterTest {
11 12
 
13
+    @Test
12 14
     public void test1() {
13 15
         // given
14 16
         Integer[] array = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@@ -23,7 +25,7 @@ public class IntegerFilterTest {
23 25
         compare(expected, actual);
24 26
     }
25 27
 
26
-
28
+    @Test
27 29
     public void test2() {
28 30
         // given
29 31
         Integer[] array = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@@ -38,7 +40,7 @@ public class IntegerFilterTest {
38 40
         compare(expected, actual);
39 41
     }
40 42
 
41
-
43
+    @Test
42 44
     public void test3() {
43 45
         // given
44 46
         Integer[] array = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
@@ -53,7 +55,7 @@ public class IntegerFilterTest {
53 55
         compare(expected, actual);
54 56
     }
55 57
 
56
-
58
+    @Test
57 59
     public void test4() {
58 60
         // given
59 61
         Integer[] array = new Integer[]{10, 15, 20, 25, 55, 100, 150, 300, 900, 1000};
@@ -68,7 +70,7 @@ public class IntegerFilterTest {
68 70
         compare(expected, actual);
69 71
     }
70 72
 
71
-
73
+    @Test
72 74
     public void test5() {
73 75
         // given
74 76
         Integer[] array = new Integer[]{10, 15, 20, 25, 55, 100, 150, 300, 900, 1000};
@@ -83,6 +85,7 @@ public class IntegerFilterTest {
83 85
         compare(expected, actual);
84 86
     }
85 87
 
88
+
86 89
     private void compare(Integer[] expected, Integer[] actual) {
87 90
 //        TestUtils.assertArrayEquals(expected, actual);
88 91
     }

+ 4
- 3
src/test/java/rocks/zipcode/io/quiz3/generics/arrayutility/string/StringFilterTest.java 查看文件

@@ -8,7 +8,8 @@ import rocks.zipcode.io.quiz3.generics.ArrayUtility;
8 8
  * @author leon on 09/12/2018.
9 9
  */
10 10
 public class StringFilterTest {
11
-    
11
+
12
+    @Test
12 13
     public void test1() {
13 14
         // given
14 15
         String[] expected = {"quick", "brown", "jumps", "over", "lazy"};
@@ -22,7 +23,7 @@ public class StringFilterTest {
22 23
         TestUtils.assertArrayEquals(expected, actual);
23 24
     }
24 25
 
25
-    
26
+    @Test
26 27
     public void test2() {
27 28
         // given
28 29
         String[] expected = {"The", "fox", "the", "dog"};
@@ -36,7 +37,7 @@ public class StringFilterTest {
36 37
         TestUtils.assertArrayEquals(expected, actual);
37 38
     }
38 39
 
39
-    
40
+    @Test
40 41
     public void test3() {
41 42
         // given
42 43
         String[] expected = {"The", "over", "the"};

+ 3
- 2
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,10 +11,10 @@ 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
-        this.sign = RockPaperScissorHandSign.valueOf("SCISSOR");
17
+        this.sign = RockPaperScissorHandSign.valueOf("SCISSORS");
17 18
     }
18 19
 
19 20