Question 3

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). */
 public ArrayList<String> getDelimitersList(String[] tokens)
 { /* to be implemented in part (a) */ }
 /** 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<String> delimiters)
 { /* to be implemented in part (b) */ }
 // There may be instance variables, constructors, and methods that are not shown.
}

Question 3.1

A string containing text and possibly delimiters has been split into tokens and stored in String[] tokens. Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter. You will write the method getDelimitersList, which returns an ArrayList containing all the open and close delimiters found in tokens in their original order. The following examples show the contents of an ArrayList returned by getDelimitersList for different open and close delimiters and different tokens arrays.

Complete method getDelimitersList below. /** Returns an ArrayList of delimiters from the array tokens, as described in part (a). */ public ArrayList getDelimitersList(String[] tokens)


ArrayList<String> delims = new ArrayList<String>(); // Creates the array string
for(String str: tokens){ // iterates through each string in tokens
    if(str.equals(openDel) || str.equals(closeDel)){ // If the string matches the parameters for an open delimiter or a closed delimiter then its added to the array
    delims.add(str);
    }
}
return delims; // returns the array list 

Question 3.2

Write the method isBalanced, which returns true when the delimiters are balanced and returns false otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise, they are not balanced.

  1. When traversing the ArrayList from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
  2. The total number of open delimiters is equal to the total number of close delimiters. Consider a Delimiters object for which openDel is “” and closeDel is “”. The examples below show different ArrayList objects that could be returned by calls to getDelimitersList and the value that would be returned by a call to isBalanced.
int numOpenDels = 0;
int numCloseDels = 0;

for(String str: delimeters){ // runs through all the delimiters identified in the previous 
    if (str.equals(openDel)){ // if string is an open delimiter add 1 to the number of Open Delimiters identified so far
        numOpenDels++;
    }
    else 
        if(str.equals(closeDel)){ // if string is an closed delimiter add 1 to the number of Closed Delimiters identified so far
        numCloseDels ++;
        }
    if(numCloseDels > numOpenDels){ // if more closed delimiters are identified than open delimiters then return false
        return false;
    }
        if(numCloseDels < numOpenDels){ // if more open delimiters are identified than closed delimiters then return false
        return false;
    }
    else 
        if(numCloseDels = numOpenDels){ // if same # of open delimiters are identified than closed delimiters then return true
            return true;
        }
}

Full Question 3 Combined

import java.util.ArrayList;
import java.util.Arrays;

public class Delimiters {
    private String openDel;
    private String closeDel;

    public Delimiters(String open, String close) {
        openDel = open;
        closeDel = close;
    }

    public ArrayList<String> getDelimitersList(String[] tokens) {
        ArrayList<String> delims = new ArrayList<String>();
        for (String str : tokens) {
            if (str.equals(openDel) || str.equals(closeDel)) {
                delims.add(str);
            }
        }
        return delims;
    }

    public boolean isBalanced(ArrayList<String> delims) {
        int numOpenDels = 0;
        int numCloseDels = 0;

        for (String str : delimiters) {
            if (str.equals(openDel)) {
                numOpenDels++;
            }
            if (str.equals(closeDel)) {
                numCloseDels++;
            }
            if (numCloseDels > numOpenDels) {
                return false;
            }
        }
        return numOpenDels == numCloseDels;
    }

    public String getOpenDel() {
        return openDel;
    }

    public String getCloseDel() {
        return closeDel;
    }

public static void main(String[] args) {
    // Sample tokens
    String[] tokens = {"(", "{", "}", ")", "[", "]", "(", ")"};

    // Create an instance of Delimiters with open and close delimiters
    Delimiters delimiters = new Delimiters("(", ")");

    // Get a list of delimiters from the tokens
    ArrayList<String> delimitersList = delimiters.getDelimitersList(tokens);

    // Check if the delimiters are balanced
    boolean isBalanced = delimiters.isBalanced(delimitersList);

    System.out.println("Tokens: " + Arrays.toString(tokens));
    System.out.println("Open Delimiter: " + delimiters.getOpenDel());
    System.out.println("Close Delimiter: " + delimiters.getCloseDel());
    System.out.println("Delimiters List: " + delims);
    System.out.println("Are the delimiters balanced? " + isBalanced);
    }
}

Scoring Guidelines

Part (a) getDelimitersList 4 points

Intent: Store delimiters from an array in an ArrayList

+1 Creates ArrayList

+1 Accesses all elements in array tokens (no bounds errors)

+1 Compares strings in tokens with both instance variables (must be in the context of a loop)

+1 Adds delimiters into ArrayList in original order

Part (b) isBalanced 5 points

Intent: Determine whether open and close delimiters in an ArrayList are balanced

+1 Initializes accumulator(s)

+1 Accesses all elements in ArrayList delimiters (no bounds errors)

+1 Compares strings in delimiters with instance variables and updates accumulator(s) accordingly

+1 Identifies and returns appropriate boolean value to implement one rule

+1 Identifies and returns appropriate boolean values for all cases

Notes for other students:

Code Monkeys:

Classes - 2022 FRQ #2: 0.95 -

  • Need to know public vs. private scope
  • Need to know @overide
  • Need to know how classes work
  • Know getters and setters

Methods and Control Classes - 2022 FRQ #1:

  • Given class and sequence and need to write 2 methods
  • Added your own things onto the original code and showed example outputs
  • Explains exactly what happened
  • Shows testing and explains things needed to know

Array/Array List - 2022 FRQ #3:

  • Don’t go too in depth with the code and try to stick more with the basic functions
  • Explain the core functions

Classes - 2015 FRQ #2: 0.88 - late but had everything

  • Make sure you have a class with the type of variable
  • Private header
  • differentiating between capitals and lengths
  • Get basics done and that will lead to the rest being done

Notes from other students:

  • Take more time use more vocab
  • Focus on whole group instead of one person
  • Focus more on code