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

palindrome-products: make class use instance methods

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

exercises/palindrome-products/src/example/java/Palindromes.java → exercises/palindrome-products/src/example/java/PalindromeCalculator.java Просмотреть файл

1
 import java.util.*;
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
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
6
         SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
7
         long num;
7
         long num;
8
         List<List<Integer>> factors;
8
         List<List<Integer>> factors;
23
     }
23
     }
24
 
24
 
25
     // http://stackoverflow.com/questions/23984654/how-to-print-all-palindromes-upto-1000-without-using-any-string-stringbuilder
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
         if (number < 10l) {
27
         if (number < 10l) {
28
             return number;
28
             return number;
29
         }
29
         }
37
         return result;
37
         return result;
38
     }
38
     }
39
 
39
 
40
-    private static boolean isPalindrome(long number) {
40
+    private boolean isPalindrome(long number) {
41
         return number == reverseNumber(number);
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
+import org.junit.Before;
1
 import org.junit.Ignore;
2
 import org.junit.Ignore;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
9
 
10
 
10
 import static junit.framework.TestCase.*;
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
     @Test
21
     @Test
15
     public void largestPalindromeFromSingleDigitFactors() {
22
     public void largestPalindromeFromSingleDigitFactors() {
21
         );
28
         );
22
         final long expectedValue = 9l;
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
33
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
27
     }
34
     }
36
         );
43
         );
37
         final long expectedValue = 9009l;
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
48
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
42
     }
49
     }
51
         );
58
         );
52
         final long expectedValue = 121l;
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
63
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
57
     }
64
     }
66
         );
73
         );
67
         final long expectedValue = 906609l;
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
78
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
72
     }
79
     }
81
         );
88
         );
82
         final long expectedValue = 10201l;
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
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
93
         checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
87
     }
94
     }