// Program to test solutions to problem #2 on the cse143 midterm, spring 2018. // Note that it does not test whether the method throws proper exceptions. // Fill in your solution to doubleDigit, then compile and run the program. import java.util.*; import java.io.*; public class Test2 { public static int doubleDigit(int n, int d) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static int doubleDigit2(int n, int d) { if (d < 0 || d > 9) { throw new IllegalArgumentException(); } if (n < 0) { return -doubleDigit2(-n, d); } else if (n == 0) { return 0; } else if (n % 10 == d) { return doubleDigit2(n / 10, d) * 100 + d * 11; } else { return doubleDigit2(n / 10, d) * 10 + n % 10; } } private static int testCount; private static int failCount; public static void main(String[] args) { test(3797, 7); test(2, 2); test(0, 0); test(8, 6); test(55, 2); test(33, 3); test(-101, 1); test(323, 3); test(2001, 0); test(12345, 6); test(72773, 7); test(3445, 5); test(54224, 4); test(-624243, 4); test(5340909, 0); test(121121, 1); if (failCount == 0) { System.out.println("passed all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int n, int d) { testCount++; int result1 = doubleDigit2(n, d); System.out.println("doubleDigit(" + n + ", " + d + ") = " + result1); boolean fail = false; try { int result2 = doubleDigit(n, d); if (result1 != result2) { fail = true; System.out.println("yours = " + result2); } } catch (RuntimeException e) { System.out.println("threw " + e); fail = true; } if (!fail) { System.out.println("passed"); } else { failCount++; System.out.println("failed"); } System.out.println(); } }