CS 212 — Homework Two

Stacks & Queues

Due: 15 February 2013, 11:59p

Worth:80 points

Part one: Implementing Stacks and Queues

The first part of the assignment is to finish commenting, testing and implementing the Stack and the Queue. I would like you to implement the actual data storage for each using a linked list (rather than a LinkedList). For both, use a singly linked-list. I will be looking for efficient implementations, so think carefully about how you implement these two, thinking about minimizing the number of steps you need to perform for each function.

Part two: Playing with stacks

One of the main uses of stacks is for parsing. For this assignment you will be implementing a simple parser that checks for matched parentheses. There are two requirements for matched parentheses. First, for every left parenthesis there must be a matching right parenthesis. The second requirement is that they must be ordered correctly. In other words, the left parenthesis must proceed the right one. To make this problem a little harder, you must also be able to handle brackets ("[" and "]") and curly braces ("{" and "}"). This increases the difficulty slightly because it is important that everything is properly nested (i.e., "[{]}" is illegal, because the "{}" is not fully contained in the outer "[]"). Also, the input you will receive can contain any characters. You should ignore everything that not in the set "()[]{}".

To implement this, create a new class called AssignmentTwo. On this class, create the following method:

public static int parenthesesMatch(String str) throws ParseException
This method should parse the String and return the number of matching pairs of "(){}[]". If there is a problem, this should return a ParseException indicating the mismatched character. You will need to catch this in your tests.

Part three: Playing with queues

When a share of common stock is sold, the capital gain (or loss) is the difference between the share's selling price and the price when it was bought. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time at different prices, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sld in such a case is to use a FIFO protocol — the shares sold are the ones that we have held the longest. For example, suppose we buy 100 shares at $20 on day one, 20 shares at $24 on day two, 200 shares at $36 on day three, and then sell 150 shares on day four at $30. Applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day one, 20 were bought on day two, and 30 were bought on day three. The capital gains would then be 100*10 + 20*6 + 30*(-6) = $940.

To implement this, add the following methods to AssignmentTwo:

void buyStock(int numShares, int price)
This method should record the purchase of the stock.
int sellStock(int numShares, int price)
This sells numShares stock shares and returns the capital gains on the transaction. This should throw a NoSuchElementException with an appropriate error message if the number of stocks to sell exceeds the number of stocks that have already been bought.

You will probably want to create some kind of Record class to manage the stock. Use an inner class for this.

Part four: A command line interface

For this assignment, we will leave behind the comfortable confines of the GUI and write a command line interface (or the console in Eclipse). Of course, the problem with a user interface is that it makes it difficult to do testing. Difficult, but not impossible. When we are interacting on the command line, we use System.in and System.out. We want to add a layer of abstraction, so we can interact through these two channels, but we can also divert them and read and write to them directly. To do that, write another method in the class that looks like this:

public void cliHandler(Scanner in, PrintWriter out)
This function should do handle all of the user interaction. You will get your input form the user via the Scanner and you will write your output to out

Your main method will only do two things. It will create an instance of the class, and then it will call cliHandler(new Scanner(System.in), new PrintWriter(System.out, true)). Now, to test cliHandler you just need to provide your own Scanner and PrintWriter. Start by defining all of your input in a String, using new lines ("\n") to separate each different command. You can then create a Scanner using the String as the input.

The PrintWriter is a little tricker. You need to create a StringWriter and then create a PrintWriter that wraps it (by passing the StringWriter into the constructor). You then can use the PrintWriter object the way you use System.out (which is a PrintWriter). When you are done, call the toString method on the StringWriter and you will have a String containing all of the output you wrote to the PrintWriter. Here is a little example:

StringWriter stringWriter = new StringWriter(); PrintWriter out = new PrintWriter(stringWriter); out.println("This is a line of output"); out.println("this is another line of output"); out.close(); System.out.println(stringWriter.toString());

Now, as for the actual interface, here is the set of valid inputs and the responses that we expect from your application

buy x shares at $y
This should record the transaction and report success to the user. x and y are obviously variables and you will need to parse out the actual values. This says that each share should be bought at $y, so if x is 3 and y is 5, the purchase will cost you $15. This will return the text "Bought $z worth of shares", where z is the full amount spent. If x is less than zero, treat it as if it was zero.
buy 1 share at $y
This is a specialized version of the above.
sell x shares at $y
Perform the transaction and report the capital gains (or loss) from the sale. The output text of this will be "Capital gains $z", where z is the capital gains (which may be negative). If there are insufficient shares to cover the sale, this should report "Insufficient shares". If x is less than zero, treat it as if it was zero.
sell 1 share at $y
Perform the transaction and report the capital gains (or loss) from the sale.
quit
quit (obviously)
anything else...
Anything else that the user types in should be sent through the parentheses matcher. Your program should either report "No errors were found" or tell the user the location of the bad character "Error at character c" where c is the location of the bad character.

Grading

PointsItemComments
Correctness
12 ptsStack
12 ptsQueue
12 ptsparentheseMatch
12 ptsbuy
12 ptssell
10 ptsmain
Misc
10 ptsCommentsAll methods and classes should have Javadoc style comments and non-trivial code should have explanations.

Last modified: Wed Feb 13 18:13:02 EST 2013