// Program to test solutions to problem #8 on the cse142 midterm, autumn 2016. // Fill in your solution to undouble, then compile and run the program. import java.util.*; public class Test8 { public static String undouble(String s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static String undouble2(String s) { String result = ""; if (s.length() > 0) { char last = s.charAt(0); result += last; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != last) { result += s.charAt(i); } last = s.charAt(i); } } return result; } private static int count, failCount; private static int DISPLAY = 20; // how many failed cases to display public static void main(String[] args) { test(""); test("m"); test("nn"); test("pq"); test("sstt"); test("odegaard"); test("bookkeeper"); test("baz"); test("foobar"); test("mississippi"); test("apple"); test("carry"); test("berry"); test("aardvark"); test("glass"); test("committment"); test("juggle"); test("little"); test("cac"); test("theses"); test("ccaatt"); test("aabbccddeeffgg"); test2("", 6); if (failCount == 0) { System.out.println("passed all tests"); } else { if (failCount > DISPLAY) { System.out.println("failed cases exeeds display max of " + DISPLAY); System.out.println(); } System.out.println("failed " + failCount + " of " + count + " tests"); } } public static void test2(String s, int len) { if (!s.contains("aaa") && !s.contains("bbb") && !s.contains("ccc") && !s.contains("ddd") && !s.contains("eee")) { test(s); } if (len > 0) { for (char ch = 'a'; ch <= 'e'; ch++) { String s2 = s + ch; test2(s2, len - 1); } } } public static void test(String s) { count++; boolean display = failCount < DISPLAY; String s2 = undouble2(s); String s1 = ""; boolean fail = false; try { s1 = undouble(s); } catch (RuntimeException e) { if (display) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); } fail = true; } if (!fail && !s1.equals(s2)) { fail = true; } if (fail) { failCount++; if (display) { System.out.println("testing \"" + s + "\""); System.out.println("expected = \"" + s2 + "\""); System.out.println("actual = " + s1); if (fail) { System.out.println("failed"); } System.out.println(); } } } }