ソースを参照

Merge pull request #409 from FridaTveit/NthPrimeUseInstanceMethod

nth-prime: make use instance method [Fix #358]
Stuart Kent 9 年 前
コミット
89c90721ab
共有2 個のファイルを変更した19 個の追加12 個の削除を含む
  1. 3
    3
      exercises/nth-prime/src/example/java/PrimeCalculator.java
  2. 16
    9
      exercises/nth-prime/src/test/java/PrimeCalculatorTest.java

exercises/nth-prime/src/example/java/Prime.java → exercises/nth-prime/src/example/java/PrimeCalculator.java ファイルの表示

@@ -1,7 +1,7 @@
1 1
 import java.util.stream.IntStream;
2 2
 
3
-public final class Prime {
4
-    public static int nth(int nth) {
3
+public final class PrimeCalculator {
4
+    public int nth(int nth) {
5 5
         if (nth < 1) {
6 6
             throw new IllegalArgumentException();
7 7
         }
@@ -20,7 +20,7 @@ public final class Prime {
20 20
         return possiblePrime;
21 21
     }
22 22
 
23
-    private static boolean isPrime(int n) {
23
+    private boolean isPrime(int n) {
24 24
         if (n == 1) {
25 25
             return false;
26 26
         }

exercises/nth-prime/src/test/java/PrimeTest.java → exercises/nth-prime/src/test/java/PrimeCalculatorTest.java ファイルの表示

@@ -1,44 +1,51 @@
1
-import org.junit.Test;
1
+import org.junit.Before;
2 2
 import org.junit.Ignore;
3 3
 import org.junit.Rule;
4
+import org.junit.Test;
4 5
 import org.junit.rules.ExpectedException;
5 6
 
6 7
 import static org.hamcrest.CoreMatchers.*;
7 8
 import static org.junit.Assert.*;
8 9
 
9
-public class PrimeTest {
10
+public class PrimeCalculatorTest {
11
+    private PrimeCalculator primeCalculator;
12
+
13
+    @Before
14
+    public void setup() {
15
+        primeCalculator = new PrimeCalculator();
16
+    }
10 17
 
11 18
     @Rule
12 19
     public ExpectedException thrown = ExpectedException.none();
13
-    
20
+
14 21
     @Test
15 22
     public void testFirstPrime() {
16
-        assertThat(Prime.nth(1), is(2));
23
+        assertThat(primeCalculator.nth(1), is(2));
17 24
     }
18 25
 
19 26
     @Ignore
20 27
     @Test
21 28
     public void testSecondPrime() {
22
-        assertThat(Prime.nth(2), is(3));
29
+        assertThat(primeCalculator.nth(2), is(3));
23 30
     }
24 31
 
25 32
     @Ignore
26 33
     @Test
27 34
     public void testSixthPrime() {
28
-        assertThat(Prime.nth(6), is(13));
35
+        assertThat(primeCalculator.nth(6), is(13));
29 36
     }
30 37
 
31 38
     @Ignore
32 39
     @Test
33 40
     public void testBigPrime() {
34
-        assertThat(Prime.nth(10001), is(104743));
41
+        assertThat(primeCalculator.nth(10001), is(104743));
35 42
     }
36 43
 
37 44
     @Ignore
38 45
     @Test
39 46
     public void testUndefinedPrime() {
40 47
         thrown.expect(IllegalArgumentException.class);
41
-        Prime.nth(0);
48
+        primeCalculator.nth(0);
42 49
     }
43
-    
50
+
44 51
 }