Lab 9: Working with stacks and queues

The goal of this lab is to write several small programs that make use of stacks and/or queues.

You will be using a version of the stack classes we looked at in class and Java's Queue interface and LinkedList class. (LinkedList implements Queue).

To set up, make a directory for this lab and copy files from the course directory.

cp -r /homes/tvandrun/Public/cs245/lab9/. .

Make an Eclipse project for this lab.

1. Grouping consistency

Open the file VerifyNested.java.

You have probably noticed that a text editor like Xemacs or Eclipse highlights text between grouping symbols-- () {} [] or < >-- if they match and are properly nested. In this exercise, you will write a program which takes a string from the command line and determines whether or not it is properly nested with respect to these four grouping symbol pairs.

For example, the line "( a ( {b & 6 } [ 1 t ^] ~ ) )" is properly nested; "( a ( {b & 6 ) [ 1 t ^] ~ ) }" is not. Use the provided Stack interface and ListStack class. Since some of those grouping symbols have special meaning in the command line, you will have to include them in quotes when you run your program, for example

java stqulab.VerifyNested "( a ( {b & 6 } [ 1 t ^] ~ ) )"

Finish the method verifyNested() You can then test it by hand from the command line, and, once you are fairly confident that what you're doing works, run the JUnit test Ex1Test. Hint: The Stack class is generic, and you'll probably want to use the Character wrapper type as its type parameter.

2. Conversion from infix to RPN

You learned about reverse Polish notation in the pre-lab reading. In this exercise you will write a program which takes a string (of numbers, parentheses, and arithmetic operators--no other characters, even spaces) from the command line assumed to be in infix form and, using a stack, converts it to RPN. Open the file InfixToRPN.java. You will be writing the method infixToRPN().

You may assume that the input is fully parenthesized, and that it does not contain any errors. For example,

java stqulab.InfixToRPN "((5*27)/(2+(3-8)))"

should print out

5 27 * 2 3 8 - + /

Note that your output, on the other hand, should contain spaces.

Again, test this by hand (from the command line) and then verify using the JUnit test.

Hint. This is how you should organize your thoughts. Your program, like exercise 1, will primarily be a loop over the input string. Each character is one of four things: a digit, an operand, a left parenthesis, and a right parenthesis. Before doing anything, first figure out what should be done in each of these four scenarios.

3. Computing capital gains

Review the introduction to this problem in the prelab-reading Open StackTracker.java and complete the given class.

Use an instance (or two) of java.util.LinkedList (as java.util.Queue) to compute capital gains (or loss).

There is no commandline driver program for this one. Test it with the JUnit test Ex3Test, which you should feel free to inspect. The test cases test an instance of your class against various sequences of transactions. The test case stressTest() generates random transactions until all the shares are sold; it will have different results each time.


Thomas VanDrunen
Last modified: Mon Aug 3 15:21:15 CDT 2015