CS 112
Spring 2018

Old version

This is the CS 112 site as it appeared on May 11, 2018.

Lab 9: Using lists, stacks and queues

FLR 267

If your lab meets in FLR 267, you should begin by following the instructions found here.

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:

    > Lab9Task1.isLeftDelim('(')
    true
    > Lab9Task1.isLeftDelim('{')
    true
    > Lab9Task1.isLeftDelim(']')
    false
    > Lab9Task1.isRightDelim(']')
    true
    > Lab9Task1.isRightDelim('}')
    true
    > Lab9Task1.isRightDelim('(')
    false
    > Lab9Task1.matches('[', ']')
    true
    > Lab9Task1.matches('{', '}')
    true
    > 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:

    > Lab9Task1.isBalanced("[(3 + 2) * 7] / 2")
    true
    > Lab9Task1.isBalanced("[(3 + 2] * 7) / 2")
    false
    > Lab9Task1.isBalanced("[(({}))]")
    true
    > Lab9Task1.isBalanced("[(({))]}")
    false
    > Lab9Task1.isBalanced("((((())")
    false
    > 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);
> 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

You should use Apollo to submit the following files:

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

Warnings

  • Make sure to use these exact file names, or Apollo will not accept your files. If Apollo reports that a file does not have the correct name, you should rename the file using the name listed in the assignment or on the Apollo upload page.

  • Make sure that you do not try to submit a .class file or a file with a ~ character at the end of its name.

  • Before submitting your files, make sure that your BU username is visible at the top of the Apollo page. If you don’t see your username, click the Log out button and login again.

  • If you make any last-minute changes to one of your Java files (e.g., adding additional comments), you should compile and run the file in DrJava after you make the changes to ensure that it still runs correctly. Even seemingly minor changes can cause your code to become unrunnable.

  • If you encounter problems with Apollo, close your browser and try again. If possible, you may also want to wait an hour or two before retrying. If you are unable to submit and it is close to the deadline, email your homework before the deadline to cs112-staff@cs.bu.edu

Here are the steps:

  1. Login to Apollo, using the link in the left-hand navigation bar. You will need to use your Kerberos user name and password.
  2. Check to see that your BU username is at the top of the Apollo page. If it isn’t, click the Log out button and login again.
  3. Find the appropriate lab section on the main page and click Upload files.
  4. For each file that you want to submit, find the matching upload section for the file. Make sure that you use the right section for each file. You may upload any number of files at a time.
  5. Click the Upload button at the bottom of the page.
  6. Review the upload results. If Apollo reports any issues, return to the upload page by clicking the link at the top of the results page, and try the upload again, per Apollo’s advice.
  7. Once all of your files have been successfully uploaded, return to the upload page by clicking the link at the top of the results page. The upload page will show you when you uploaded each file, and it will give you a way to view or download the uploaded file. Click on the link for each file so that you can ensure that you submitted the correct file.