// Program to test solutions to problem #2 on the cse143 midterm, fall 2012. // Fill in your solution to digitMatch, then compile and run the program. import java.util.*; import java.io.*; public class Test2 { public static int digitMatch(int x, int y) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static int digitMatch2(int x, int y) { if (x < 0 || y < 0) throw new IllegalArgumentException(); else if (x < 10 || y < 10) { if (x % 10 == y % 10) return 1; else return 0; } else if (x % 10 == y % 10) return 1 + digitMatch2(x / 10, y / 10); else return digitMatch2(x / 10, y / 10); } public static void main(String[] args) { test(1072503891, 62530841); test(38, 34); test(5, 5552); test(892, 892); test(298892, 7892); test(380, 0); test(123456, 654321); test(1234567, 67); test(0, 4); test(42, 24); test(0, 0); } public static void test(int x, int y) { int result1 = digitMatch2(x, y); System.out.println("digitMatch(" + x + ", " + y + ") = " + result1); boolean fail = false; try { int result2 = digitMatch(x, y); 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 { System.out.println("failed"); } System.out.println(); } }