The goal of this lab is to write several small programs that make use of stacks and/or queues.
To set up, make a directory for this lab and copy files from the course directory.
cp /homes/tvandrun/Public/cs245/lab12/* .
Finish what we started in class: implement a class for queues
of integers, using the "circular" approach.
A stub for this is found in the file ArrayQueue.java
.
It implements the Queue.java
interface, also given.
The next exercise will test your class.
You have probably noticed that a text editor like xemacs 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.
Open the file Exercise1.java
.
For example, the line "( a ( {b & 6 } [ 1 t ^] ~ ) )
" is
properly nested;
"( a ( {b & 6 ) [ 1 t ^] ~ ) }
" is not.
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 Exercise1 "( a ( {b & 6 } [ 1 t ^] ~ ) )"
Your program may simply print "yes" or "no" to the screen.
Polish mathematician Jan Lukasiewicz invented a notation for arithmetic where the operator comes before the operands, for example + 5 3 instead of 5 + 3. The advantage of this is that the order of operation is always unambiguous without the need for parentheses (or even precedence rules). For example, 5 * 3 - 9 is written - * 5 3 9, whereas 5 * (3 - 9) is written * 5 - 3 9. In honor of its inventor (or his nationality, at any rate), this is referred to as Polish notation. (It is also called prefix notation, in contrast to normal arithmetic notation which is thus infix.)
Reverse Polish notation is similar except that the operator is put after the operands (so it is also postfix notation). The advantage is that all the operands are then known before the operator is read, if we read the string from left to right. This notation is particularly interesting for our purposes, because a stack can be used to evaluate expressions in RPN. (Your task in the next project will be to write a calculator that evaluates expressions entered in RPN.)
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 Exercise2.java
.
You may assume that the input is fully parenthesized, and that it does not contain any errors. For example,
java Exercise2 "((5*27)/(2+(3-8)))"
should print out
5 27 * 2 3 8 - + /
Note that your output, on the other hand, should contain spaces.
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.
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.
This will be implemented using the given classes
Transaction
and Transactor
.
Transaction
is a simple value class--it even
has public final instance variables instead of getters.
It represents a transaction: a number of shares and a price per share.
The class Transactor
has a public static method
nextTransaction()
that returns the results of a transaction
using a Transaction
object.
You will write a program that repeatedly calls
Transactor.nextTransaction()
until there are no shares left.
Open and complete the file Exercise4.java
.
The Transactor
class is written so that the first
transaction is a purchase,
and that after about 25 transactions it will sell all remaining shares.
Make a new class like your array-based queue class but for
int
s instead of strings.
Use an instance (or two) of this class to compute capital
gains (or loss).
Make a script file that, for each exercise, shows your code and a demonstration of it running. Exercise 4's demonstration will serve also to demonstrate that your solution to Exercise 3 works. Turn in a hard copy.