// Program to test solutions to problem #2 on the cse143 midterm, winter 2022. // Note that it does not test whether the method throws proper exceptions. // Fill in your solution to countDigit, then compile and run the program. import java.util.*; import java.io.*; public class Test2 { public static int countDigit(int digit, int number) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static int countDigit2(int digit, int number) { if (number < 0 || digit < 0 || digit >= 10) { throw new IllegalArgumentException(); } if (number < 10) { if (number == digit) { return 1; } else { return 0; } } else if (number % 10 == digit) { return 1 + countDigit2(digit, number / 10); } else { return countDigit2(digit, number / 10); } } private static int testCount; private static int failCount; public static void main(String[] args) { test(3, 5323313); test(2, 2); test(0, 0); test(0, 304); test(7, 12345); test(5, 555); test(9, 919); test(0, 1234); test(5, 152535); test(7, 777777); test(4, 4); if (failCount == 0) { System.out.println("passed all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int digit, int n) { testCount++; int result1 = countDigit2(digit, n); System.out.println("countDigit(" + digit + ", " + n + ") = " + result1); boolean fail = false; try { int result2 = countDigit(digit, n); 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(); } }