Przeglądaj źródła

nth-prime: make use instance method

Frida Johanne Tveit 9 lat temu
rodzic
commit
0f655addb5

exercises/nth-prime/src/example/java/Prime.java → exercises/nth-prime/src/example/java/PrimeCalculator.java Wyświetl plik

1
 import java.util.stream.IntStream;
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
         if (nth < 1) {
5
         if (nth < 1) {
6
             throw new IllegalArgumentException();
6
             throw new IllegalArgumentException();
7
         }
7
         }
20
         return possiblePrime;
20
         return possiblePrime;
21
     }
21
     }
22
 
22
 
23
-    private static boolean isPrime(int n) {
23
+    private boolean isPrime(int n) {
24
         if (n == 1) {
24
         if (n == 1) {
25
             return false;
25
             return false;
26
         }
26
         }

exercises/nth-prime/src/test/java/PrimeTest.java → exercises/nth-prime/src/test/java/PrimeCalculatorTest.java Wyświetl plik

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