import java.util.*; public class Delimiters { /** The open and close delimiters. */ private String openDel; private String closeDel; /** Constructs a Delimiters object where open is the open delimiter and close is the * close delimiter. * Precondition: open and close are non-empty strings. */ public Delimiters(String open, String close) { openDel = open; closeDel = close; } /** Returns an ArrayList of delimiters from the array tokens, as described in part (a). */ /* to be implemented in part (a) */ public ArrayList getDelimitersList(String[] tokens) { return null; } /** Returns true if the delimiters are balanced and false otherwise, as described in part (b). * Precondition: delimiters contains only valid open and close delimiters. */ public boolean isBalanced(ArrayList delimiters) { /* to be implemented in part (b) */ return false; } // There may be instance variables, constructors, and methods that are not shown. public static void main(String[] args) { //Example 1 Delimiters del = new Delimiters("(",")"); String[] tokens = new String[4]; tokens[0] = "("; tokens[1] = "x + y"; tokens[2] = ")"; tokens[3] = " * 5"; ArrayList correctDel = new ArrayList(); correctDel.add("("); correctDel.add(")"); //Example 2 Delimiters del2 = new Delimiters ( "", "" ); String[] tokens2 = {"", "yy", "zz", "", ""}; ArrayList correctDel2 = new ArrayList(); correctDel2.add(""); correctDel2.add(""); correctDel2.add(""); //Example 3 String[] tokens3 = { "(", "d", ")", "e"}; System.out.println("Testing Delimiters 2019 FRQ 3\n\n Part (a):" ); System.out.println("\ndelimiters are "+ del +"\ntokens are "+ Arrays.toString(tokens)+ "\nreturns " + del.getDelimitersList(tokens) + " " + passFail(correctDel, del.getDelimitersList(tokens))); System.out.println("\ndelimiters are "+ del2 +"\ntokens are "+ Arrays.toString(tokens2)+ "\nreturns " + del2.getDelimitersList(tokens2) + " " + passFail(correctDel2, del2.getDelimitersList(tokens2))); System.out.println("\ndelimiters are "+ del +"\ntokens are "+ Arrays.toString(tokens3)+ "\nreturns " + del.getDelimitersList(tokens3) + " " + passFail(correctDel, del.getDelimitersList(tokens3))); /* * Part (b) Testing */ Delimiters sup = new Delimiters ( "", "" ); ArrayList ex1 = new ArrayList();// ballenced ex1.add(""); ex1.add(""); ex1.add(""); ex1.add(""); ex1.add(""); ex1.add(""); ArrayList ex2 = new ArrayList(); // not balanced cond 1 violated ex2.add(""); ex2.add(""); ex2.add(""); ex2.add(""); ArrayList ex3 = new ArrayList();// not balanced cond 1 violated ex3.add(""); ArrayList ex4 = new ArrayList(); // not balanced cond 1 violated ex4.add(""); ex4.add(""); ex4.add(""); System.out.print("\nPart (b):\nReturns true if the delimiters are balanced and false otherwise: \n"); System.out.println( "\nArrayList is "+ ex1 +"\n isBalenced returns "+ sup.isBalanced(ex1) + " " + passFail(true, sup.isBalanced(ex1))); System.out.println( "\nArrayList is "+ ex2 +"\n isBalenced returns "+ sup.isBalanced(ex2) + " " + passFail(false, sup.isBalanced(ex2))); System.out.println( "\nArrayList is "+ ex3 +"\n isBalenced returns "+ sup.isBalanced(ex3) + " " + passFail(false, sup.isBalanced(ex3))); System.out.println( "\nArrayList is "+ ex4 +"\n isBalenced returns "+ sup.isBalanced(ex4) + " " + passFail(false, sup.isBalanced(ex4))); } public String toString() { return openDel +" and " + closeDel; } public static String passFail(ArrayList expected, ArrayList actual) { if ( (expected == null) || (expected.size() == 0 ) || (actual == null) || (actual.size() == 0)) return "Fail - array null"; if ( expected.size() != actual.size() ) return "Fail - wrong number of tokens"; String result = "Pass"; for (int i=0; i< expected.size(); i++) { if ( !expected.get(i).equals(actual.get(i)) ) result = "Fail"; } return result; } public static String passFail(Boolean expected, Boolean result) { if (expected == result) return "Pass"; return "Fail"; } }