소스 검색

Merge pull request #423 from FridaTveit/PalindromeProductsUseInstanceMethod

palindrome-products: make class use instance methods [Fix #377]
Stuart Kent 9 년 전
부모
커밋
935db42775

exercises/palindrome-products/src/example/java/Palindromes.java → exercises/palindrome-products/src/example/java/PalindromeCalculator.java 파일 보기

@@ -1,8 +1,8 @@
1 1
 import java.util.*;
2 2
 
3
-public class Palindromes {
3
+public class PalindromeCalculator {
4 4
 
5
-    public static SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
5
+    public SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
6 6
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7 7
         long num;
8 8
         List<List<Integer>> factors;
@@ -23,7 +23,7 @@ public class Palindromes {
23 23
     }
24 24
 
25 25
     // http://stackoverflow.com/questions/23984654/how-to-print-all-palindromes-upto-1000-without-using-any-string-stringbuilder
26
-    private static long reverseNumber(long number) {
26
+    private long reverseNumber(long number) {
27 27
         if (number < 10l) {
28 28
             return number;
29 29
         }
@@ -37,7 +37,7 @@ public class Palindromes {
37 37
         return result;
38 38
     }
39 39
 
40
-    private static boolean isPalindrome(long number) {
40
+    private boolean isPalindrome(long number) {
41 41
         return number == reverseNumber(number);
42 42
     }
43 43
 }

exercises/palindrome-products/src/test/java/PalindromesTest.java → exercises/palindrome-products/src/test/java/PalindromeCalculatorTest.java 파일 보기

@@ -1,3 +1,4 @@
1
+import org.junit.Before;
1 2
 import org.junit.Ignore;
2 3
 import org.junit.Test;
3 4
 
@@ -9,7 +10,13 @@ import java.util.stream.Collectors;
9 10
 
10 11
 import static junit.framework.TestCase.*;
11 12
 
12
-public class PalindromesTest {
13
+public class PalindromeCalculatorTest {
14
+    private PalindromeCalculator palindromeCalculator;
15
+
16
+    @Before
17
+    public void setup() {
18
+        palindromeCalculator = new PalindromeCalculator();
19
+    }
13 20
 
14 21
     @Test
15 22
     public void largestPalindromeFromSingleDigitFactors() {
@@ -21,7 +28,7 @@ public class PalindromesTest {
21 28
         );
22 29
         final long expectedValue = 9l;
23 30
 
24
-        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(1, 9);
31
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
25 32
 
26 33
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
27 34
     }
@@ -36,7 +43,7 @@ public class PalindromesTest {
36 43
         );
37 44
         final long expectedValue = 9009l;
38 45
 
39
-        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
46
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
40 47
 
41 48
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
42 49
     }
@@ -51,7 +58,7 @@ public class PalindromesTest {
51 58
         );
52 59
         final long expectedValue = 121l;
53 60
 
54
-        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
61
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);
55 62
 
56 63
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
57 64
     }
@@ -66,7 +73,7 @@ public class PalindromesTest {
66 73
         );
67 74
         final long expectedValue = 906609l;
68 75
 
69
-        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
76
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
70 77
 
71 78
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
72 79
     }
@@ -81,7 +88,7 @@ public class PalindromesTest {
81 88
         );
82 89
         final long expectedValue = 10201l;
83 90
 
84
-        final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
91
+        final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);
85 92
 
86 93
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
87 94
     }