// Program to test solutions to problem #10 on the cse142b final, winter 2013. // Fill in your solution to count, then compile and run the program. import java.util.*; public class FinalTestB10 { public static int count(String target, String source) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static int count2(String target, String source) { target = target.toUpperCase(); source = source.toUpperCase(); int count = 0; for (int i = 0; i < source.length(); i++) { if (source.substring(i).startsWith(target)) { count++; } } return count; } private static int count, failCount; public static void main(String[] args) { count = 0; boolean caseCorrect = true; test("I", "Mississippi"); test("m", "Mississippi"); test("iss", "MISSISSIPPI"); test("foo", "Mississippi"); test("AiN", "The rain in Spain falls mainly in th plain."); test("IN", "The rain in Spain falls mainly in the plain."); test("EE", "EeEeE"); test("long string", "short"); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + count + " tests"); } } public static void test(String target, String source) { test2(target, source); test2(target.toUpperCase(), source.toUpperCase()); } public static void test2(String target, String source) { count++; System.out.println("testing count(\"" + target + "\", \"" + source + "\")"); int n1 = 0; boolean fail = false; try { n1 = count(target, source); } catch (RuntimeException e) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); fail = true; } int n2 = count2(target, source); System.out.println("actual = " + n2); if (!fail && n1 != n2) { System.out.println("theirs = " + n1); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }