// Fill in your solution to encode, then compile and run the program. import java.util.*; public class Encode { public static String encode(String s, int n) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static String encode2(String s, int n) { String result = ""; for (int j = 0; j < n; j++) { for (int i = 0; i < s.length() - j; i += n) { result += s.charAt(i + j); } } return result; } private static int count, failCount; public static void main(String[] args) { String s = ""; for (int i = 0; i < 10; i++) { s = s + i; for (int j = 2; j < s.length(); j++) { test(s, j); } } if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + count + " tests"); } } public static void test(String s, int n) { count++; System.out.println("testing encode(\"" + s + "\", " + n + ") "); String s1 = null; boolean fail = false; try { s1 = encode(s, n); } catch (RuntimeException e) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); fail = true; } String s2 = encode2(s, n); System.out.println("correct = " + s2); if (!fail && !s1.equals(s2)) { System.out.println("theirs = " + s1); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }