// Program to test solutions to problem #9 on the cse143x midterm, fall 2018. // Fill in your solution to samePattern, then compile and run the program. import java.util.*; public class Test9 { public static boolean samePattern(String s1, String s2) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static boolean samePattern2(String s1, String s2) { if (s1.length() != s2.length()) { return false; } for (int i = 0; i < s1.length(); i++) { for (int j = i + 1; j < s1.length(); j++) { if (s1.charAt(i) == s1.charAt(j)) { if (s2.charAt(i) != s2.charAt(j)) { return false; } } if (s2.charAt(i) == s2.charAt(j)) { if (s1.charAt(i) != s1.charAt(j)) { return false; } } } } return true; } // this solution checks in just one direction (str1 match => str2 match) public static boolean samePattern3(String s1, String s2) { if (s1.length() != s2.length()) { return false; } for (int i = 0; i < s1.length(); i++) { for (int j = i + 1; j < s1.length(); j++) { if (s1.charAt(i) == s1.charAt(j)) { if (s2.charAt(i) != s2.charAt(j)) { return false; } } } } return true; } // this solution checks in just one direction (str2 match => str1 match) public static boolean samePattern4(String s1, String s2) { if (s1.length() != s2.length()) { return false; } for (int i = 0; i < s1.length(); i++) { for (int j = i + 1; j < s1.length(); j++) { if (s2.charAt(i) == s2.charAt(j)) { if (s1.charAt(i) != s1.charAt(j)) { return false; } } } } return true; } private static int count; private static int[] failCount = new int[3]; public static void main(String[] args) { test("", ""); test("", "."); test("a", "x"); test("a", "ab"); test("ab", "ab"); test("aa", "xy"); test("aba", "+-+"); test("---", "aba"); test("abcabc", "zodzod"); test("abcabd", "zodzoe"); test("abcabc", "xxxxxx"); test("aaassscccn", "aaabbbcccd"); test("asasasasas", "xyxyxyxyxy"); test("ascneencsa", "aeiouuoiea"); test("aaassscccn", "aaabbbcccd"); test("asasasasas", "xxxxxyyyyy"); test("ascneencsa", "aeiouaeiou"); test("aaassscccn", "xxxyyyzzzz"); test("aaasssiiii", "gggdddfffh"); if (failCount[0] == 0) { System.out.println("passed all " + count + " tests"); } else { /* for TA grading String[] group = {"bidirectional", "str1 => str2 only", "str2 => str1 only"}; for (int i = 0; i < failCount.length; i++) { System.out.printf("%-20s failed %d of %d cases\n", group[i], failCount[i], count); } */ System.out.println("failed " + failCount[0] + " of " + count + " tests"); } } public static void test(String s1, String s2) { test2(s1, s2); if (!s1.equals(s2)) { test2(s2, s1); } } public static void test2(String s1, String s2) { count++; boolean result1 = false; Exception problem = null; try { result1 = samePattern(s1, s2); } catch (RuntimeException e) { problem = e; } boolean result2 = samePattern2(s1, s2); boolean result3 = samePattern3(s1, s2); boolean result4 = samePattern4(s1,s2); boolean fail = result1 != result2; if (problem != null || result1 != result3) { failCount[1]++; } if (problem != null || result1 != result4) { failCount[2]++; } if (fail || problem != null) { failCount[0]++; System.out.println("failed samePattern(\"" + s1 + "\", \"" + s2 + "\")"); if (problem != null) { int line = problem.getStackTrace()[1].getLineNumber(); System.out.println("threw " + problem); System.out.println("line # = " + line); } else { System.out.println("correct = " + result2); System.out.println("return = " + result1); } System.out.println(); } } }