import java.util.*; public class SampleExamTester { static int num = -1; public static void main (String[] argv) { try { // Get the prob# from student. This is the same prob# in // the HTML list. num = Integer.parseInt (argv[0]); } catch (Exception e) { System.out.println ("Improper option"); System.exit (0); } try { // We'll use the same numbering as in exam/Examples.java. if (num == 0) { System.out.println ("TESTING ALL PROBLEMS:"); num++; simpleProblem12 (); num++; simpleProblem8 (); num++; simpleProblem4 (); num++; stringProblem4 (); num++; loopSum4 (); num++; conditionals2 (); num++; arrayCount2 (); num++; thirdArray2 (); num++; rearrangeArray2 (); num++; modifiedArray2 (); } else if (num == 1) { simpleProblem12 (); } else if (num == 2) { simpleProblem8 (); } else if (num == 3) { simpleProblem4 (); } else if (num == 4) { stringProblem4 (); } else if (num == 5) { loopSum4 (); } else if (num == 6) { conditionals2 (); } else if (num == 7) { arrayCount2 (); } else if (num == 8) { thirdArray2 (); } else if (num == 9) { rearrangeArray2 (); } else if (num == 10) { modifiedArray2 (); } else { System.out.println ("No such problem: " + num); System.exit (0); } } catch (Exception e) { System.out.println ("Error in problem# " + num); System.exit (0); } } static void simpleProblem12 () { //simpleProblem12 in Examples. int[] A = {1,2,3,4}; int[] B = {1,2,1,3}; int a = SampleExam.problem1 (A,B); System.out.println ("What your method returned: " + a); // a == 2 check ("Problem# " + num + ": Test 1: ", a==2); int[] A2 = {4,2,3,9,8}; int[] B2 = {1,0,1,3,1}; int a2 = SampleExam.problem1 (A2,B2); System.out.println ("What your method returned: " + a2); // a == 4 check ("Problem# " + num + ": Test 2: ", a2==4); } static void simpleProblem8 () { // Prob# 2 in sample exam. // Find elements in an array that have larger elements on either side. int[] A = {8,1,4,2,3,5,6}; int[] B = SampleExam.problem2 (A); int[] C = {1,2}; System.out.println ("What your method returned: " + Arrays.toString(C)); check ("Problem# " + num + ": Test 1: ", equalContents(B,C)); int[] A2 = {1,2,3,6,4,1,3}; int[] B2 = SampleExam.problem2 (A2); int[] C2 = {1}; System.out.println ("What your method returned: " + Arrays.toString(C2)); check ("Problem# " + num + ": Test 2: ", equalContents(B2,C2)); } static void simpleProblem4 () { // Check whether a word has two chars in sequence like "de" in "den" String s = "den"; boolean a = SampleExam.problem3 (s); System.out.println ("What your method returned: " + a); boolean b = true; check ("Problem# " + num + ": Test 1: ", a==b); String s2 = "cast"; boolean a2 = SampleExam.problem3 (s2); System.out.println ("What your method returned: " + a2); boolean b2 = true; check ("Problem# " + num + ": Test 2: ", a2==b2); String s3 = "base"; boolean a3 = SampleExam.problem3 (s3); System.out.println ("What your method returned: " + a3); boolean b3 = false; check ("Problem# " + num + ": Test 3: ", a2==b2); } static void stringProblem4 () { // Extract a part of a char array. char[] c = {'e','m','b','e','d','d','e','d'}; char[] d = SampleExam.problem4 (c, 2, 4); System.out.println ("What your method returned: " + Arrays.toString(d)); char[] e = {'b','e','d'}; check ("Problem# " + num + ": Test 1: ", equal(d,e)); char[] c2 = {'e','m','b','e','d'}; char[] d2 = SampleExam.problem4 (c2, 2, 4); System.out.println ("What your method returned: " + Arrays.toString(d2)); char[] e2 = {'b','e','d'}; check ("Problem# " + num + ": Test 2: ", equal(d2,e2)); } static void loopSum4 () { /* - first time sum 1+3+5... crosses or reaches N */ // Test 1 int s = SampleExam.problem5 (49); System.out.println ("What your method returned: " + s); // Should return 7 check ("Problem# " + num + ": Test 1: ", s==7); // Test 2 s = SampleExam.problem5 (10000); System.out.println ("What your method returned: " + s); // Should return 100 check ("Problem# " + num + ": Test 2: ", s==100); } static void conditionals2 () { // Negatives followed by positives double[] A = {1,2,3}; boolean a = SampleExam.problem6 (A); System.out.println ("What your method returned: " + a); // Should return true check ("Problem# " + num + ": Test 1: ", a); double[] A2 = {-1,-2,-3,-4}; boolean a2 = SampleExam.problem6 (A2); System.out.println ("What your method returned: " + a2); // Should return true check ("Problem# " + num + ": Test 2: ", a2); double[] A3 = {-1,-2,-3,4}; boolean a3 = SampleExam.problem6 (A3); System.out.println ("What your method returned: " + a3); // Should return true check ("Problem# " + num + ": Test 3: ", a3); double[] A4 = {-1,2,-3,4}; boolean a4 = SampleExam.problem6 (A4); System.out.println ("What your method returned: " + a4); // Should return false check ("Problem# " + num + ": Test 4: ", !a4); } static void arrayCount2 () { // See if one is a substring of the other. char[] A = {'c', 'a', 't'}; char[] B = {'s', 'c', 'a', 't'}; boolean c = SampleExam.problem7 (A, B); System.out.println ("What your method returned: " + c); boolean d = true; check ("Problem# " + num + ": Test 1: ", c==d); char[] A2 = {'b', 'a', 't'}; char[] B2 = {'a', 'b', 'a', 't', 'e'}; c = SampleExam.problem7 (A2, B2); System.out.println ("What your method returned: " + c); d = true; check ("Problem# " + num + ": Test 2: ", c==d); char[] A3 = {'b','a', 't'}; char[] B3 = {'t', 'a', 'b'}; c = SampleExam.problem7 (A3, B3); System.out.println ("What your method returned: " + c); d = false; check ("Problem# " + num + ": Test 3: ", c==d); } static void thirdArray2 () { // Union. Use only arrays (not lists). // Test 1: int[] A = {1,3,5,9,7}; int[] B = {2,3,4,5}; int[] C = SampleExam.problem8 (A, B); System.out.println ("What your method returned: " + Arrays.toString(C)); int[] D = {1,3,5,9,7,2,4}; //System.out.println (Arrays.toString(C)); check ("Problem# " + num + ": Test 1: ", equalContents(C,D)); // Test 2: int[] A2 = {3,5,7}; int[] B2 = {1,2,3,5,6,7}; int[] C2 = SampleExam.problem8 (A2, B2); System.out.println ("What your method returned: " + Arrays.toString(C2)); int[] D2 = {3,5,7,2,1,6}; //System.out.println (Arrays.toString(C2)); check ("Problem# " + num + ": Test 2: ", equalContents(C2,D2)); } static void rearrangeArray2 () { // Vowels first. char[] A = {'f','a','c','e'}; char[] B = SampleExam.problem9 (A); System.out.println ("What your method returned: " + Arrays.toString(B)); char[] C = {'a','e','f','c'}; check ("Problem# " + num + ": Test 1: ", equalContents(B,C)); char[] A2 = {'m','o','o','n'}; char[] B2 = SampleExam.problem9 (A2); System.out.println ("What your method returned: " + Arrays.toString(B2)); char[] C2 = {'o','o','m','n'}; check ("Problem# " + num + ": Test 2: ", equalContents(B2,C2)); } static void modifiedArray2 () { // Remove negatives // Test 1: int[] A = {1, -1, -2, 3, -4}; int[] B = SampleExam.problem10 (A); System.out.println ("What your method returned: " + Arrays.toString(B)); int[] C = {1,3}; check ("Problem# " + num + ": Test 1: ", equalContents(B,C)); // Test 2: int[] A2 = {0, 1, 2, -3}; int[] B2 = SampleExam.problem10 (A2); System.out.println ("What your method returned: " + Arrays.toString(B2)); int[] C2 = {0,1,2}; check ("Problem# " + num + ": Test 2: ", equalContents(B2,C2)); } ////////////////////////////////////////////////////////////// static void check (String test, boolean correct) { if (correct) { System.out.println (test + " => passed"); } else { System.out.println (test + " => failed"); } } static boolean equalStrings (String[] set1, String[] set2) { if (set1.length != set2.length) { return false; } for (int i=0; i