Просмотр исходного кода

Make hamming use instance methods

Fixes issue #346
Frida Johanne Tveit 9 лет назад
Родитель
Сommit
c9f5bd0a01

+ 11
- 2
exercises/hamming/src/example/java/Hamming.java Просмотреть файл

@@ -1,6 +1,15 @@
1 1
 public class Hamming {
2
+    private final int hammingDistance;
2 3
 
3
-    public static int compute(String leftStrand, String rightStrand) {
4
+    public Hamming(String leftStrand, String rightStrand) {
5
+        hammingDistance = computeHammingDistance(leftStrand, rightStrand);
6
+    }
7
+
8
+    public int getHammingDistance() {
9
+        return hammingDistance;
10
+    }
11
+
12
+    private int computeHammingDistance(String leftStrand, String rightStrand) {
4 13
         if(leftStrand.length() != rightStrand.length()) {
5 14
             throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length.");
6 15
         }
@@ -13,7 +22,7 @@ public class Hamming {
13 22
         return distance;
14 23
     }
15 24
 
16
-    private static int hammingContributionAt(int index, String leftStrand, String rightStrand) {
25
+    private int hammingContributionAt(int index, String leftStrand, String rightStrand) {
17 26
       return leftStrand.charAt(index) != rightStrand.charAt(index) ? 1 : 0;
18 27
     }
19 28
 

+ 9
- 10
exercises/hamming/src/test/java/HammingTest.java Просмотреть файл

@@ -7,64 +7,63 @@ import org.junit.Rule;
7 7
 import org.junit.rules.ExpectedException;
8 8
 
9 9
 public class HammingTest {
10
-    
11 10
 
12 11
     @Rule
13 12
     public ExpectedException thrown = ExpectedException.none();
14 13
     
15 14
     @Test
16 15
     public void testNoDifferenceBetweenIdenticalStrands() {
17
-        assertThat(Hamming.compute("A", "A"), is(0));
16
+        assertThat(new Hamming("A", "A").getHammingDistance(), is(0));
18 17
     }
19 18
 
20 19
     @Ignore
21 20
     @Test
22 21
     public void testHammingDistanceForSingleNucleotideStrand() {
23
-        assertThat(Hamming.compute("A", "G"), is(1));
22
+        assertThat(new Hamming("A", "G").getHammingDistance(), is(1));
24 23
     }
25 24
 
26 25
     @Ignore
27 26
     @Test
28 27
     public void testHammingDistanceForSmallStrand() {
29
-        assertThat(Hamming.compute("AG", "CT"), is(2)); 
28
+        assertThat(new Hamming("AG", "CT").getHammingDistance(), is(2));
30 29
     }
31 30
 
32 31
     @Ignore
33 32
     @Test
34 33
     public void testSmallHammingDistance() {
35
-        assertThat(Hamming.compute("AT", "CT"), is(1));
34
+        assertThat(new Hamming("AT", "CT").getHammingDistance(), is(1));
36 35
     }
37 36
 
38 37
     @Ignore
39 38
     @Test
40 39
     public void testSmallHammingDistanceInLongerStrand() {
41
-        assertThat(Hamming.compute("GGACG", "GGTCG"), is(1));
40
+        assertThat(new Hamming("GGACG", "GGTCG").getHammingDistance(), is(1));
42 41
     }
43 42
 
44 43
     @Ignore
45 44
     @Test
46 45
     public void testValidatesFirstStrandNotLonger() {
47 46
         thrown.expect(IllegalArgumentException.class);
48
-        Hamming.compute("AAAG", "AAA");                
47
+        new Hamming("AAAG", "AAA");
49 48
     }
50 49
 
51 50
     @Ignore
52 51
     @Test
53 52
     public void testValidatesOtherStrandNotLonger() {
54 53
         thrown.expect(IllegalArgumentException.class);
55
-        Hamming.compute("AAA", "AAAG");
54
+        new Hamming("AAA", "AAAG");
56 55
     }
57 56
 
58 57
     @Ignore
59 58
     @Test
60 59
     public void testLargeHammingDistance() {
61
-        assertThat(Hamming.compute("GATACA", "GCATAA"), is(4));
60
+        assertThat(new Hamming("GATACA", "GCATAA").getHammingDistance(), is(4));
62 61
     }
63 62
 
64 63
     @Ignore
65 64
     @Test
66 65
     public void testHammingDistanceInVeryLongStrand() {
67
-        assertThat(Hamming.compute("GGACGGATTCTG", "AGGACGGATTCT"), is(9));
66
+        assertThat(new Hamming("GGACGGATTCTG", "AGGACGGATTCT").getHammingDistance(), is(9));
68 67
     }
69 68
 
70 69
 }