Quellcode durchsuchen

Merge branch 'master' into change

Stuart Kent vor 9 Jahren
Ursprung
Commit
38c5dedf6c

+ 8
- 0
.travis.yml Datei anzeigen

@@ -3,6 +3,14 @@ language: java
3 3
 jdk:
4 4
   - oraclejdk8
5 5
 
6
+notifications:
7
+  webhooks:
8
+    urls:
9
+      - https://webhooks.gitter.im/e/5e9ccd41111917b19478
10
+    on_success: change  # options: [always|never|change] default: always
11
+    on_failure: always  # options: [always|never|change] default: always
12
+    on_start: never     # options: [always|never|change] default: always
13
+
6 14
 # http://docs.travis-ci.com/user/migrating-from-legacy
7 15
 sudo: false
8 16
 cache: bundler

+ 1
- 1
README.md Datei anzeigen

@@ -1,4 +1,4 @@
1
-# xJava [![Build Status](https://travis-ci.org/exercism/xjava.svg?branch=master)](https://travis-ci.org/exercism/xjava)
1
+# xJava [![Build Status](https://travis-ci.org/exercism/xjava.svg?branch=master)](https://travis-ci.org/exercism/xjava) [![Join the chat at https://gitter.im/exercism/xjava](https://badges.gitter.im/exercism/xjava.svg)](https://gitter.im/exercism/xjava?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
2 2
 
3 3
 Source for Exercism Exercises in Java.
4 4
 

+ 6
- 0
config.json Datei anzeigen

@@ -58,6 +58,7 @@
58 58
     "sublist",
59 59
     "rectangles",
60 60
     "matrix",
61
+    "clock",
61 62
     "diamond",
62 63
     "secret-handshake",
63 64
     "flatten-array",
@@ -335,6 +336,11 @@
335 336
       "topics": []
336 337
     },
337 338
     {
339
+      "slug": "clock",
340
+      "difficulty": 1,
341
+      "topics": []
342
+    },
343
+    {
338 344
       "slug": "diamond",
339 345
       "difficulty": 1,
340 346
       "topics": []

+ 17
- 0
exercises/clock/build.gradle Datei anzeigen

@@ -0,0 +1,17 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+test {
13
+    testLogging {
14
+        exceptionFormat = 'full'
15
+        events = ["passed", "failed", "skipped"]
16
+    }
17
+}

+ 49
- 0
exercises/clock/src/example/java/Clock.java Datei anzeigen

@@ -0,0 +1,49 @@
1
+public class Clock {
2
+    private static final int MINUTES_IN_AN_HOUR = 60;
3
+    private static final int HOURS_IN_A_DAY = 24;
4
+
5
+    private int hours;
6
+    private int minutes;
7
+
8
+    public Clock(int hours, int minutes) {
9
+        this.hours = hours;
10
+        this.minutes = minutes;
11
+        sanitiseTime();
12
+    }
13
+
14
+    public void add(int minutes) {
15
+        this.minutes += minutes;
16
+        sanitiseTime();
17
+    }
18
+
19
+    private void sanitiseTime() {
20
+        while (minutes < 0) {
21
+            minutes += MINUTES_IN_AN_HOUR;
22
+            hours--;
23
+        }
24
+        while (hours < 0) {
25
+            hours += HOURS_IN_A_DAY;
26
+        }
27
+        int minutesOverflow = minutes / MINUTES_IN_AN_HOUR;
28
+        minutes = minutes % MINUTES_IN_AN_HOUR;
29
+        hours = (hours + minutesOverflow) % HOURS_IN_A_DAY;
30
+    }
31
+
32
+    @Override
33
+    public String toString() {
34
+        return toTimeString(hours) + ":" + toTimeString(minutes);
35
+    }
36
+
37
+    private String toTimeString(int number) {
38
+        if (number < 10) return "0" + number;
39
+        return String.valueOf(number);
40
+    }
41
+
42
+    @Override
43
+    public boolean equals(Object obj) {
44
+        if (!(obj instanceof Clock)) return false;
45
+
46
+        Clock clock = (Clock) obj;
47
+        return hours == clock.hours && minutes == clock.minutes;
48
+    }
49
+}

+ 0
- 0
exercises/clock/src/main/java/.keep Datei anzeigen


+ 135
- 0
exercises/clock/src/test/java/ClockAddTest.java Datei anzeigen

@@ -0,0 +1,135 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class ClockAddTest {
7
+
8
+    @Ignore
9
+    @Test
10
+    public void addMinutes() {
11
+        Clock clock = new Clock(10, 0);
12
+        clock.add(3);
13
+        assertEquals("10:03", clock.toString());
14
+    }
15
+
16
+    @Ignore
17
+    @Test
18
+    public void addNoMinutes() {
19
+        Clock clock = new Clock(6, 41);
20
+        clock.add(0);
21
+        assertEquals("06:41", clock.toString());
22
+    }
23
+
24
+    @Ignore
25
+    @Test
26
+    public void addToNextHour() {
27
+        Clock clock = new Clock(0, 45);
28
+        clock.add(40);
29
+        assertEquals("01:25", clock.toString());
30
+    }
31
+
32
+    @Ignore
33
+    @Test
34
+    public void addMoreThanOneHour() {
35
+        Clock clock = new Clock(10, 0);
36
+        clock.add(61);
37
+        assertEquals("11:01", clock.toString());
38
+    }
39
+
40
+    @Ignore
41
+    @Test
42
+    public void addMoreThanTwoHoursWithCarry() {
43
+        Clock clock = new Clock(0, 45);
44
+        clock.add(160);
45
+        assertEquals("03:25", clock.toString());
46
+    }
47
+
48
+    @Ignore
49
+    @Test
50
+    public void addAcrossMidnight() {
51
+        Clock clock = new Clock(23, 59);
52
+        clock.add(2);
53
+        assertEquals("00:01", clock.toString());
54
+    }
55
+
56
+    @Ignore
57
+    @Test
58
+    public void addMoreThanOneDay() {
59
+        Clock clock = new Clock(5, 32);
60
+        clock.add(1500);
61
+        assertEquals("06:32", clock.toString());
62
+    }
63
+
64
+    @Ignore
65
+    @Test
66
+    public void addMoreThanTwoDays() {
67
+        Clock clock = new Clock(1, 1);
68
+        clock.add(3500);
69
+        assertEquals("11:21", clock.toString());
70
+    }
71
+
72
+    @Ignore
73
+    @Test
74
+    public void subtractMinutes() {
75
+        Clock clock = new Clock(10, 3);
76
+        clock.add(-3);
77
+        assertEquals("10:00", clock.toString());
78
+    }
79
+
80
+    @Ignore
81
+    @Test
82
+    public void subtractToPreviousHour() {
83
+        Clock clock = new Clock(10, 3);
84
+        clock.add(-30);
85
+        assertEquals("09:33", clock.toString());
86
+    }
87
+
88
+    @Ignore
89
+    @Test
90
+    public void subtractMoreThanAnHour() {
91
+        Clock clock = new Clock(10, 3);
92
+        clock.add(-70);
93
+        assertEquals("08:53", clock.toString());
94
+    }
95
+
96
+    @Ignore
97
+    @Test
98
+    public void subtractAcrossMidnight() {
99
+        Clock clock = new Clock(0, 3);
100
+        clock.add(-4);
101
+        assertEquals("23:59", clock.toString());
102
+    }
103
+
104
+    @Ignore
105
+    @Test
106
+    public void subtractMoreThanTwoHours() {
107
+        Clock clock = new Clock(0, 0);
108
+        clock.add(-160);
109
+        assertEquals("21:20", clock.toString());
110
+    }
111
+
112
+    @Ignore
113
+    @Test
114
+    public void subtractMoreThanTwoHoursWithBorrow() {
115
+        Clock clock = new Clock(6, 15);
116
+        clock.add(-160);
117
+        assertEquals("03:35", clock.toString());
118
+    }
119
+
120
+    @Ignore
121
+    @Test
122
+    public void subtractMoreThanOneDay() {
123
+        Clock clock = new Clock(5, 32);
124
+        clock.add(-1500);
125
+        assertEquals("04:32", clock.toString());
126
+    }
127
+
128
+    @Ignore
129
+    @Test
130
+    public void subtractMoreThanTwoDays() {
131
+        Clock clock = new Clock(2, 20);
132
+        clock.add(-3000);
133
+        assertEquals("00:20", clock.toString());
134
+    }
135
+}

+ 120
- 0
exercises/clock/src/test/java/ClockCreationTest.java Datei anzeigen

@@ -0,0 +1,120 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertEquals;
5
+
6
+public class ClockCreationTest {
7
+
8
+    @Test
9
+    public void canPrintTimeOnTheHour() {
10
+        assertEquals("08:00", new Clock(8, 0).toString());
11
+    }
12
+
13
+    @Ignore
14
+    @Test
15
+    public void canPrintTimeWithMinutes() {
16
+        assertEquals("11:09", new Clock(11, 9).toString());
17
+    }
18
+
19
+    @Ignore
20
+    @Test
21
+    public void midnightPrintsAsZero() {
22
+        assertEquals("00:00", new Clock(24, 0).toString());
23
+    }
24
+
25
+    @Ignore
26
+    @Test
27
+    public void hourRollsOver() {
28
+        assertEquals("01:00", new Clock(25, 0).toString());
29
+    }
30
+
31
+    @Ignore
32
+    @Test
33
+    public void hourRollsOverContinuously() {
34
+        assertEquals("04:00", new Clock(100, 0).toString());
35
+    }
36
+
37
+    @Ignore
38
+    @Test
39
+    public void sixtyMinutesIsNextHour() {
40
+        assertEquals("02:00", new Clock(1, 60).toString());
41
+    }
42
+
43
+    @Ignore
44
+    @Test
45
+    public void minutesRollOver() {
46
+        assertEquals("02:40", new Clock(0, 160).toString());
47
+    }
48
+
49
+    @Ignore
50
+    @Test
51
+    public void minutesRollOverContinuously() {
52
+        assertEquals("04:43", new Clock(0, 1723).toString());
53
+    }
54
+
55
+    @Ignore
56
+    @Test
57
+    public void hourAndMinutesRollOver() {
58
+        assertEquals("03:40", new Clock(25, 160).toString());
59
+    }
60
+
61
+    @Ignore
62
+    @Test
63
+    public void hourAndMinutesRollOverContinuously() {
64
+        assertEquals("11:01", new Clock(201, 3001).toString());
65
+    }
66
+
67
+    @Ignore
68
+    @Test
69
+    public void hourAndMinutesRollOverToExactlyMidnight() {
70
+        assertEquals("00:00", new Clock(72, 8640).toString());
71
+    }
72
+
73
+    @Ignore
74
+    @Test
75
+    public void negativeHour() {
76
+        assertEquals("23:15", new Clock(-1, 15).toString());
77
+    }
78
+
79
+    @Ignore
80
+    @Test
81
+    public void negativeHourRollsOver() {
82
+        assertEquals("23:00", new Clock(-25, 0).toString());
83
+    }
84
+
85
+    @Ignore
86
+    @Test
87
+    public void negativeHourRollsOverContinuously() {
88
+        assertEquals("05:00", new Clock(-91, 0).toString());
89
+    }
90
+
91
+    @Ignore
92
+    @Test
93
+    public void negativeMinutes() {
94
+        assertEquals("00:20", new Clock(1, -40).toString());
95
+    }
96
+
97
+    @Ignore
98
+    @Test
99
+    public void negativeMinutesRollOver() {
100
+        assertEquals("22:20", new Clock(1, -160).toString());
101
+    }
102
+
103
+    @Ignore
104
+    @Test
105
+    public void negativeMinutesRollOverContinuously() {
106
+        assertEquals("16:40", new Clock(1, -4820).toString());
107
+    }
108
+
109
+    @Ignore
110
+    @Test
111
+    public void negativeHourAndMinutesBothRollOver() {
112
+        assertEquals("20:20", new Clock(-25, -160).toString());
113
+    }
114
+
115
+    @Ignore
116
+    @Test
117
+    public void negativeHourAndMinutesBothRollOverContinuously() {
118
+        assertEquals("22:10", new Clock(-121, -5810).toString());
119
+    }
120
+}

+ 98
- 0
exercises/clock/src/test/java/ClockEqualTest.java Datei anzeigen

@@ -0,0 +1,98 @@
1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.junit.Assert.assertFalse;
5
+import static org.junit.Assert.assertTrue;
6
+
7
+public class ClockEqualTest {
8
+
9
+    @Ignore
10
+    @Test
11
+    public void clocksWithSameTimeAreEqual() {
12
+        assertTrue(new Clock(15, 37).equals(new Clock(15, 37)));
13
+    }
14
+
15
+    @Ignore
16
+    @Test
17
+    public void clocksAMinuteApartAreNotEqual() {
18
+        assertFalse(new Clock(15, 36).equals(new Clock(15, 37)));
19
+    }
20
+
21
+    @Ignore
22
+    @Test
23
+    public void clocksAnHourApartAreNotEqual() {
24
+        assertFalse(new Clock(14, 37).equals(new Clock(15, 37)));
25
+    }
26
+
27
+    @Ignore
28
+    @Test
29
+    public void clocksWithHourOverflow() {
30
+        assertTrue(new Clock(10, 37).equals(new Clock(34, 37)));
31
+    }
32
+
33
+    @Ignore
34
+    @Test
35
+    public void clocksWithHourOverflowBySeveralDays() {
36
+        assertTrue(new Clock(3, 11).equals(new Clock(99, 11)));
37
+    }
38
+
39
+    @Ignore
40
+    @Test
41
+    public void clocksWithNegateHour() {
42
+        assertTrue(new Clock(22, 40).equals(new Clock(-2, 40)));
43
+    }
44
+
45
+    @Ignore
46
+    @Test
47
+    public void clocksWithNegativeHourThatWraps() {
48
+        assertTrue(new Clock(17, 3).equals(new Clock(-31, 3)));
49
+    }
50
+
51
+    @Ignore
52
+    @Test
53
+    public void clocksWithNegativeHourThatWrapsMultipleTimes() {
54
+        assertTrue(new Clock(13, 49).equals(new Clock(-83, 49)));
55
+    }
56
+
57
+    @Ignore
58
+    @Test
59
+    public void clocksWithMinuteOverflow() {
60
+        assertTrue(new Clock(0, 1).equals(new Clock(0, 1441)));
61
+    }
62
+
63
+    @Ignore
64
+    @Test
65
+    public void clocksWithMinuteOverflowBySeveralDays() {
66
+        assertTrue(new Clock(2, 2).equals(new Clock(2, 4322)));
67
+    }
68
+
69
+    @Ignore
70
+    @Test
71
+    public void clocksWithNegativeMinutes() {
72
+        assertTrue(new Clock(2, 40).equals(new Clock(3, -20)));
73
+    }
74
+
75
+    @Ignore
76
+    @Test
77
+    public void clocksWithNegativeMinutesThatWraps() {
78
+        assertTrue(new Clock(4, 10).equals(new Clock(5, -1490)));
79
+    }
80
+
81
+    @Ignore
82
+    @Test
83
+    public void clocksWithNegativeMinutesThatWrapsMultipleTimes() {
84
+        assertTrue(new Clock(6, 15).equals(new Clock(6, -4305)));
85
+    }
86
+
87
+    @Ignore
88
+    @Test
89
+    public void clocksWithNegativeHoursAndMinutes() {
90
+        assertTrue(new Clock(7, 32).equals(new Clock(-12, -268)));
91
+    }
92
+
93
+    @Ignore
94
+    @Test
95
+    public void clocksWithNegativeHoursAndMinutesThatWrap() {
96
+        assertTrue(new Clock(18, 7).equals(new Clock(-54, -11513)));
97
+    }
98
+}

+ 1
- 0
exercises/grade-school/build.gradle Datei anzeigen

@@ -7,6 +7,7 @@ repositories {
7 7
 }
8 8
 
9 9
 dependencies {
10
+  testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
10 11
   testCompile "junit:junit:4.12"
11 12
 }
12 13
 test {

+ 39
- 28
exercises/grade-school/src/test/java/SchoolTest.java Datei anzeigen

@@ -1,14 +1,14 @@
1 1
 import org.junit.Ignore;
2 2
 import org.junit.Test;
3 3
 
4
-import java.lang.Integer;
5
-import java.util.*;
6 4
 import java.util.ArrayList;
7
-import java.util.Arrays;
8 5
 import java.util.HashMap;
9
-import java.util.List;
10 6
 import java.util.Map;
7
+import java.util.List;
8
+import java.util.Collection;
11 9
 
10
+import org.hamcrest.Matcher;
11
+import org.hamcrest.collection.IsIterableContainingInOrder;
12 12
 import static org.hamcrest.CoreMatchers.*;
13 13
 import static org.junit.Assert.assertThat;
14 14
 import static org.junit.Assert.assertTrue;
@@ -62,29 +62,27 @@ public class SchoolTest {
62 62
 
63 63
   @Ignore
64 64
   @Test
65
-  public void gradeReturnsStudentsInTheOrderTheyWereInserted() {
66
-    int grade = 4;
67
-    school.add("Bartimaeus", grade);
68
-    school.add("Nathaniel", grade);
69
-    school.add("Faquarl", grade);
70
-    List<String> studentsInGrade = school.grade(grade);
71
-    assertThat(studentsInGrade.get(0), is("Bartimaeus"));
72
-    assertThat(studentsInGrade.get(1), is("Nathaniel"));
73
-    assertThat(studentsInGrade.get(2), is("Faquarl"));
74
-  }
75
-
76
-  @Ignore
77
-  @Test
78 65
   public void sortsSchool() {
66
+    school.add("Kyle", 4);
67
+    school.add("Zed", 4);
68
+    school.add("Adam", 4);
79 69
     school.add("Jennifer", 4);
80 70
     school.add("Kareem", 6);
81 71
     school.add("Christopher", 4);
82
-    school.add("Kyle", 3);
83
-    Map<Integer, List<String>> sortedStudents = new HashMap<Integer, List<String>>();
84
-    sortedStudents.put(6, Arrays.asList("Kareem"));
85
-    sortedStudents.put(4, Arrays.asList("Christopher", "Jennifer"));
86
-    sortedStudents.put(3, Arrays.asList("Kyle"));
87
-    assertEquals(school.studentsByGradeAlphabetical(), sortedStudents);
72
+    school.add("Kylie", 3);
73
+    Map<Integer, Matcher> sortedStudents = new HashMap<Integer, Matcher>();
74
+    sortedStudents.put(6, IsIterableContainingInOrder
75
+      .contains("Kareem"));
76
+    sortedStudents.put(4, IsIterableContainingInOrder
77
+      .contains("Adam", "Christopher", "Jennifer", "Kyle", "Zed"));
78
+    sortedStudents.put(3, IsIterableContainingInOrder
79
+      .contains("Kylie"));
80
+
81
+    Map schoolStudents = school.studentsByGradeAlphabetical();
82
+    for (Map.Entry<?, Matcher> entry : sortedStudents.entrySet()) {
83
+
84
+      assertThat((Collection) schoolStudents.get(entry.getKey()), entry.getValue());
85
+    }
88 86
   }
89 87
 
90 88
   @Ignore
@@ -93,8 +91,14 @@ public class SchoolTest {
93 91
     String shouldNotBeAdded = "Should not be added to school";
94 92
     int grade = 1;
95 93
 
96
-    List<String> students = school.grade(grade);
97
-    students.add(shouldNotBeAdded);
94
+    Collection students = school.grade(grade);
95
+
96
+    try {
97
+      students.add(shouldNotBeAdded);
98
+    } catch (Exception exception) {
99
+      // Also valid that the add operation throws an exception
100
+      // Such as UnsupportedOperationException when an umodifiable collection type is used
101
+    }
98 102
 
99 103
     assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
100 104
   }
@@ -107,9 +111,16 @@ public class SchoolTest {
107 111
     List<String> listWhichShouldNotBeAdded = new ArrayList<>();
108 112
     listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
109 113
 
110
-    Map<Integer, List<String>> sortedStudents = school.studentsByGradeAlphabetical();
111
-    sortedStudents.put(grade,listWhichShouldNotBeAdded);
114
+    Map sortedStudents = school.studentsByGradeAlphabetical();
115
+
116
+    try {
117
+      sortedStudents.put(grade, listWhichShouldNotBeAdded);
118
+    } catch (Exception exception) {
119
+      // Also valid that the put operation throws an exception
120
+      // Such as UnsupportedOperationException when an unmodifiableMap is used
121
+    }
112 122
 
113
-    assertThat(school.studentsByGradeAlphabetical().get(grade), not(hasItem(studentWhichShouldNotBeAdded)));
123
+    assertThat(school.studentsByGradeAlphabetical().get(grade), 
124
+      not(hasItem(studentWhichShouldNotBeAdded)));
114 125
   }
115 126
 }

+ 1
- 0
exercises/settings.gradle Datei anzeigen

@@ -11,6 +11,7 @@ include 'binary-search-tree'
11 11
 include 'bob'
12 12
 include 'bracket-push'
13 13
 include 'change'
14
+include 'clock'
14 15
 include 'crypto-square'
15 16
 include 'custom-set'
16 17
 include 'diamond'