600.108 Intro Programming Lab, Fall 2017, Week 11

Getting Started: Do the posted Blackboard poll. Your lab leaders will review the answers before you get started on this week's exercise.

All our phases this week will be coding. You will be writing several recursive methods to work up to a full solution to the problem. As is always the case, at each step you should make sure that your code not only compiles, but is also checkstyle compliant. Make sure that your lab leaders review your work after each stage.

The Problem:

Recursive Symbol Strings

Write a program which will randomly generate a balanced, properly formed series of parentheses (), braces {}, brackets [] and angles <> of a specified even length. A series is properly formed if each left symbol has a matching right symbol, and all subseries are sequentially placed next to other subseries or fully nested within a pair of matched symbols.

More formally, we will define a properly formed series as follows, and generate them with these rule probilities:

Examples of proper combinations are: ({}<>) or [{<>}(({[]})<>)]. Examples of improper combinations are: ({}] (mismatch) or ({}[)] (bad nesting). You should be able to make up some more examples of each, and for the valid ones explain what combination of rules leads to each of their formations.

You must use a recursive method to create each series. In addition, your solution must follow these implementation guidelines:

  1. The only global variables allowed are a Random object, a keyboard object for input, and a constant String or array to hold the set of valid symbol characters. That's it!
  2. Your recursive methods may not print anything. You can only print from main.

Step 1) Make up 2 more examples of invalid series and 3 examples of valid series that illustrate different combinations of the rules. For each valid one, write down the combination of rules that would produce it. Then write a (non-recursive) method that randomly chooses a pair of symbols and returns them as a 2-character string, to support the base case. All four pairs should be equally likely. Write a main method to test this method.

--- Switch driver/nagivator roles ---

Step 2) Write a recursive method called nest that creates and returns a nested set of parentheses. It must take a parameter for the number of nested pairs; e.g., 5 produces ((((())))). Add to your main method to demonstrate this functionality. [This method is a stepping stone to the actual solution.]

--- Switch driver/nagivator roles ---

Step 3) Change your recursive nest method to randomly choose any combination of these four symbol pairs at each level of nesting: {}, [], (), or <> by calling the method you wrote in step 1. The symbols must match correctly with their other half in the nesting, such as (([<{}>])). Update your main method so that it gets user input for the length of the series to generate. Do error checking to make sure that the input is an even number.

--- Switch driver/nagivator roles ---

Step 4) Write a second recursive method called sequence that creates and returns a sequence of matched symbol pairs. It must have a parameter specifying the number of pairs in the sequence. For example, 5 may produce (){}[][]<>. Call this method from main so that for a user provided input length, two different strings are printed: a nested series and a sequenced series.

--- Switch driver/nagivator roles ---

Step 5) Now that we have a feel for how to created nested series and how to create sequenced series, we need a method that will create a combination according to the probabilities above. Write a third recursive method that creates a series of matched symbols that is a combination of nested or sequenced symbols according to the expected probabilities. This method should also have a parameter indicating the size of the series. From main, also call this method on the input length and print the result. Warning: don't try write this method by calling the nested or sequenced methods from the previous steps. They were just warm-ups. Make this method recurse on itself instead, adapting code from the other two. That way the result will be a completely random mix of nested and sequenced series.

Step 6) CLEAN-UP: Email your solution to both partners, submit on Blackboard as usual, and then clean-up any local files as usual. Failure to do so constitutes an ethics violation.