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.

1. Grouping consistency

You have probably noticed that a text editor like xemacs highlights text between grouping symbols-- () {} [] or < >-- if they match and are properly nested. 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. Hint: First concatenate all the Strings from args, and then use StringTokenizer to break it up, or in some other way ignore everything that is not one of these grouping symbols.

2. Conversion from infix to RPN

You learned about reverse Polish notation in the description of the current project. Write a program which takes a string (or numbers, spaces, and arithmetic operators) from the command line assumed to be in infix form and converts it to RPN.

3. Writing a queue class

Finish what we started in class yesterday: implement a class for queues of integers, using the "circular" approach.

4. Computing capital gains

When a person makes a profit on a purchase and eventual sale of a stock, he or she must pay taxes on the "capital gain", that is, the profit. (A capital loss can be used as a tax deduction.) However, sometimes a person purchases and sells shares of a stock over several transactions, and the capital gain depends on the interpretation of which shares were bought or sold. For example, suppose a person had the following transactions for stock of a certain company:

The total profit is (2437.50 + 10321.50) - (3000 + 3750 + 3200) = 5509. However, if a year ended after the third transaction (the first sale), then capital gains tax needed to be paid on the gain so far. But where did the 750 shares come from-- the original 1000 or the later 1500?

The standard way to compute capital gains is on a first-come-first-serve basis. Hence the 750 shares comes out of the original 1000 shares, and their original price (their basis) is 2250, and so the capital gain was 187.5. If this person then sold another 750 shares the next year without buying any more, then 250 of those shares would be the remaing shares from the original purchase, and the other 500 would come from the 1500 bought in the second purchase. If sold at $3.50 per share, this would be a capital gain of (750 * 3.50) - (250 * 3 + 500 * 2.5) = 2625 - 2000 = 625.

Copy the following files from the course public directory:

cp /homeemp/tvandrun/pub/245/Transaction.java .
cp /homeemp/tvandrun/pub/245/Transactor.class .

The class Transactor has a public static method nextTransaction() that returns the results of a transaction using a Transaction object.

Write a program that repeatedly calls Transactor.nextTransaction() until there are no shares left. (The class is written so that the first transaction is a purchase, and that after about 25 transactions it will sell all remaining shares.) Use an instance (or two) of your queue class to compute capital gains (or loss).


Thomas VanDrunen
Last modified: Thu Mar 15 09:02:56 CDT 2007