Skip to content

Commit

Permalink
palindrome-products: make class use instance methods
Browse files Browse the repository at this point in the history
Fixes issue exercism#377.
  • Loading branch information
FridaTveit committed Apr 12, 2017
1 parent 165511f commit e99bffb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import java.util.*;

public class Palindromes {
public class PalindromeCalculator {

public static SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
public SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFactors(int minFactor, int maxFactor) {
SortedMap<Long, List<List<Integer>>> palindromeSortedList = new TreeMap<>();
long num;
List<List<Integer>> factors;
Expand All @@ -23,7 +23,7 @@ public static SortedMap<Long, List<List<Integer>>> getPalindromeProductsWithFact
}

// http://stackoverflow.com/questions/23984654/how-to-print-all-palindromes-upto-1000-without-using-any-string-stringbuilder
private static long reverseNumber(long number) {
private long reverseNumber(long number) {
if (number < 10l) {
return number;
}
Expand All @@ -37,7 +37,7 @@ private static long reverseNumber(long number) {
return result;
}

private static boolean isPalindrome(long number) {
private boolean isPalindrome(long number) {
return number == reverseNumber(number);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -9,7 +10,13 @@

import static junit.framework.TestCase.*;

public class PalindromesTest {
public class PalindromeCalculatorTest {
private PalindromeCalculator palindromeCalculator;

@Before
public void setup() {
palindromeCalculator = new PalindromeCalculator();
}

@Test
public void largestPalindromeFromSingleDigitFactors() {
Expand All @@ -21,7 +28,7 @@ public void largestPalindromeFromSingleDigitFactors() {
);
final long expectedValue = 9l;

final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(1, 9);
final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);

checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
}
Expand All @@ -36,7 +43,7 @@ public void largestPalindromeFromDoubleDigitFactors() {
);
final long expectedValue = 9009l;

final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);

checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
}
Expand All @@ -51,7 +58,7 @@ public void smallestPalindromeFromDoubleDigitFactors() {
);
final long expectedValue = 121l;

final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(10, 99);
final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10, 99);

checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
}
Expand All @@ -66,7 +73,7 @@ public void largestPalindromeFromTripleDigitFactors() {
);
final long expectedValue = 906609l;

final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);

checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
}
Expand All @@ -81,7 +88,7 @@ public void smallestPalindromeFromTripleDigitFactors() {
);
final long expectedValue = 10201l;

final SortedMap<Long, List<List<Integer>>> palindromes = Palindromes.getPalindromeProductsWithFactors(100, 999);
final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(100, 999);

checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
}
Expand Down

0 comments on commit e99bffb

Please sign in to comment.