// Program to test solutions to problem #8 on the cse142 midterm, autumn 2014. // 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; public static void main(String[] args) { test(""); test("a"); test("aa"); test("ab"); test("aab"); test("aabb"); 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"); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + count + " tests"); } } public static void test(String s) { count++; System.out.println("testing \"" + s + "\""); String s2 = undouble2(s); System.out.println("expected = \"" + s2 + "\""); String s1 = ""; boolean fail = false; try { s1 = undouble(s); } catch (RuntimeException e) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); fail = true; } if (!fail && !s1.equals(s2)) { System.out.println("actual = " + s1); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }