// Program to test solutions to problem #8 on the cse142 midterm, spring 2015. // Fill in your solution to digitsInARow, then compile and run the program. import java.util.*; public class Test8 { public static int digitsInARow(int n) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static int digitsInARow2(int n) { int max = 1; int count = 1; while (n > 0) { int next = n % 10; n = n / 10; if (n % 10 == next) { count++; } else { count = 1; } if (count > max) { max = count; } } return max; } private static int testCount, failCount; public static final int ERRORS_MAX = 15; public static void main(String[] args) { failCount = 0; for (int i = 0; i < 1000000; i++) { test(i); } 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) { testCount++; int n2 = digitsInARow2(n); int n1 = 0; boolean fail = false; RuntimeException except = null; try { n1 = digitsInARow(n); } catch (RuntimeException e) { fail = true; except = e; } fail = fail || n1 != n2; if (fail) { System.out.println("testing digitsInARow(" + n + ")"); if (except != null) { StackTraceElement[] trace = except.getStackTrace(); int line = trace[0].getLineNumber(); System.out.println("Threw exception at line#" + line); } else { System.out.println("correct: " + n2); System.out.println("yours : " + n1); } System.out.println(); failCount++; if (failCount > ERRORS_MAX) { System.out.println("exceeds " + ERRORS_MAX + " errors"); System.out.println("with " + (testCount - failCount) + " correct"); System.exit(0); } } } }