CS 112
Spring 2019

Old version

This is the CS 112 site as it appeared on May 8, 2019.

Lab 9: Using lists, stacks and queues

Task 0: Prepare for lab

Download the following zip file: lab9.zip

Unzip this archive, and you should find a folder named lab9, and within it the files you will need for this lab.

Task 1: Use a stack to check for balanced delimiters

In lecture, we discussed how you could use a stack to check if the delimiters in an expression (i.e., the parentheses, brackets, and curly braces) are properly balanced. In this task, you will complete code that does this.

  1. We’ve given you some starter code in Lab9Task1.java. However, it includes a syntax error. Compile the file to see the error.

  2. The error stems from the fact that generic classes in Java require you to use reference types – types that represent objects. Therefore, we can’t use a primitive type like char when we declare or create an instance of a generic collection.

    Instead, Java provides a “wrapper class” for each primitive type that allows us to treat primitive values as if they were objects. In the case of char, the corresponding wrapper class is called Character.

    Change the code so that it uses this wrapper class when declaring and creating the stack that we’re using in this task. Feel free to ask for help as needed.

  3. We have given you three helper methods: isLeftDelim(), isRightDelim(), and matches().

    Review these methods, reading the comments that accompany them and examining how they work.

    At the moment, they each support two types of delimiters but not the third. Extend each method so that it supports all three types of delimiters. Here are some test cases:

    > System.out.println(Lab9Task1.isLeftDelim('('));
    true
    > System.out.println(Lab9Task1.isLeftDelim('{'));
    true
    > System.out.println(Lab9Task1.isLeftDelim(']'));
    false
    > System.out.println(Lab9Task1.isRightDelim(']'));
    true
    > System.out.println(Lab9Task1.isRightDelim('}'));
    true
    > System.out.println(Lab9Task1.isRightDelim('('));
    false
    > System.out.printkn(Lab9Task1.matches('[', ']'));
    true
    > System.out.println(Lab9Task1.matches('{', '}'));
    true
    > System.out.println(Lab9Task1.matches('(', '}'));
    false
    
  4. The key method for delimiter checking is called isBalanced(). We have given you a partial implementation, and you should complete it. Consult the lecture notes on delimiter checking as needed to remind yourself of how we can use a stack for this purpose.

    In particular, you will need to:

    • Add code that properly handles right delimiters. As you do so, take advantage of the helper methods.

    • Make whatever other changes are needed so that the method returns true when the delimiters are balanced and false when they are not.

    Here are some test calls:

    > System.out.println( Lab9Task1.isBalanced("[(3 + 2) * 7] / 2") );
    true
    > System.out.println( Lab9Task1.isBalanced("[(3 + 2] * 7) / 2") );
    false
    > System.out.println( Lab9Task1.isBalanced("[(({}))]") );
    true
    > System.out.println( Lab9Task1.isBalanced("[(({))]}") );
    false
    > System.out.println( Lab9Task1.isBalanced("((((())") );
    false
    > System.out.println( Lab9Task1.isBalanced("(())))") );
    false
    

Task 2: Use collection objects to divide numbers into subgroups

Suppose that we have an array containing integers between 0 and 999, and that we want to divide these integers into subgroups based on how many digits they have. In Lab9Task2.java, you will complete a method that does this.

The method is called divideByLength(). In takes an array of integers called values that we will assume contains integers between 0 and 999. It should return a list (either an ArrayList or LLList) in which:

For example:

> int[] vals = {7, 300, 55, 3, 213, 24, 78, 8, 411};
> List results = Lab9Task2.divideByLength(vals);
> System.out.println( results );
{7, 3, 8, 55, 24, 78, 300, 213, 411}

Note that the method does not sort the numbers in increasing order by value. Rather, it reorders them based on how many digits they have.

Requirements:

Two other notes:

Task 3: Submit your work

Don’t worry if you didn’t finish all of the tasks. You should show the TF whatever work you were able to complete during lab.