// Program to test solutions to problem #2 on the cse143 midterm, summer 2022. // Fill in your solution to startsWith, then compile and run the program. import java.util.*; public class Test2 { public static boolean startsWith(String s1, String s2) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static boolean startsWith2(String s1, String s2) { if (s2.length() == 0) { return true; } else if (s1.length() == 0) { return false; } else { return s1.charAt(0) == s2.charAt(0) && startsWith2(s1.substring(1), s2.substring(1)); } } public static void main(String[] args) { testHelper("Washington", "Wash"); testHelper("Washington", "Washed"); testHelper("hello", "he"); testHelper("hello", "hen"); testHelper("this", "t"); testHelper("the", ""); testHelper("", "the"); testHelper("him", "HIM"); testHelper("he", "."); testHelper("<*>!!", "<*>"); testHelper("s", "she"); testHelper("this", "that"); testHelper("tom", "Tom"); testHelper("hello", "hel"); testHelper("this", "that"); testHelper("Wash", "Washington"); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } private static int testCount; private static int failCount; public static void testHelper(String s1, String s2) { test(s1, s2); test(s2, s1); } public static void test(String s1, String s2) { System.out.println("startsWith(\"" + s1 + "\", \"" + s2 + "\")"); boolean expected = startsWith2(s1, s2); System.out.println("\texpected: " + expected); boolean fail = false; try { boolean actual = startsWith(s1, s2); if (expected != actual) { System.out.println("\tyour answer: " + actual); fail = true; } } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println("\tthrew " + e + " at line #" + line); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }