// Program to test solutions to problem #2 on the cse143 midterm, summer 2019. // Fill in your solution to isPalindrome, then compile and run the program. public class Test2 { public static boolean isPalindrome(String s) { // fill in your solution here // you can also rename the parameter above } // this is a sample solution public static boolean isPalindrome2(String s) { if (s.length() <= 1) { return true; } else { return s.charAt(0) == s.charAt(s.length() - 1) && isPalindrome2(s.substring(1, s.length() - 1)); } } private static int passCount; private static int failCount; public static void main(String[] args) { test("radar"); test("hah"); test("peep"); test("x"); test(""); test("123321"); test("peer"); test("ab"); test("a ba"); test("ABba"); System.out.println("tests passed: " + passCount); System.out.println("tests failed: " + failCount); } public static void test(String s) { System.out.println("testing \"" + s + "\"..."); boolean expected = isPalindrome2(s); boolean observed = false; try { observed = isPalindrome(s); System.out.println(" expected output = " + expected); System.out.println(" your output = " + observed); } catch (RuntimeException e) { int line = e.getStackTrace()[2].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); observed = !expected; } if (expected == observed) { passCount++; System.out.println("passed"); } else { failCount++; System.out.println("failed"); } System.out.println(); } }