This lab will satisfy several objectives. You will become familiar with linked lists. The power of classification and the use of abstract classes will be demonstrated. The importance of using existing classes will be reinforced.
To start, you will build an abstract stack class for integers. If you recall, we already have an integer stack class implemented as an array. An abstract class is purely a design tool to empower us to describe behavior without implementation. We will then modify our integer stack implemented with an array to become a subclass of our abstract integer stack. to Make our integer stack classification complete, we will build a linked list version of the stack. We end the lab by building a test program that will test either version of the integer stack.
cd public-html/classes/csc241
cp -r ~mohammad/public-html/classes/csc241/Stack/ .
-- don't forget the dot or the -r. This command replicates my Stack directory and its subdirectories in your account.
chmod og+rx Stack
chmod og+rx Stack/genStack
cp -r ~mohammad/public-html/classes/csc241/Queue/ .
chmod og+rx Queue
pwd
at a Unix prompt should make that clear.
ls -R Queue
provides a list of all files and
subdirectories and their contents staring from the Queue
directory. This list should look like the following:
Queue: stringQueue Queue/stringQueue: Node.java fixedStringQueue.java testStringQueue dynamicStringQueue.java stringQueue.java Queue/stringQueue/testStringQueue: test1.dat test1.java
Stack: genStack intStack Stack/genStack: genStack.java testGenStack Stack/genStack/testGenStack: test1.dat test1.java Stack/intStack: intStack.java testIntStack Stack/intStack/testIntStack: test1.dat test1.java
javac *.java
will compile all
Java files in that directory. So, change to each directory
listed above and compile the .java files there. There is an
order inherent in the lists above. For instance, you need to compile
the files in Stack/intStack before compiling the files in
Stack/intStack/testIntStack.
java test1
-- you will be prompted for the
data file, type test1.dat in response.
java test1 test1.dat dynamic
java test1 test1.dat fixed 10
Make intStack into an abstract class.
cp intStack.java fixedIntStack.java
. You will
use what is currently in intStack.java in creating the subclass
fixedIntStack.
public abstract class stringQueue { public abstract boolean empty(); public abstract boolean full(); public abstract void enqueue(String x); public abstract String dequeue(); }
javac intStack.java
Modify fixedIntStack to make it an extend intStack.
javac fixedIntStack.java
Develop the dynamic version of intStack.
class Node { String element; Node next; Node prev; Node (Node n,Node p, String e) { next=n; prev=p; element=e; } }
We have already covered how you run that testprogram, but here I'll expand on some of the features utilized.
With the statement stringQueue q;
, we are
declaring a queue object, but notice that we are not invoking a
constructor. For instance, the sequence if (arg[1].equals(dynamic)) q = new dynamicStringQueue();
is responsible for the
construction of q using the Dynamic version. This code checks
the second parameter from the command line, if it contains the word
dynamic, that version of the Queue class is used in constructing
q. arg[0] is the first command line parameter, arg[1] is the
second and so on.
The data file is setup to have an indicator as the first charctare of each line. If the first character is a d we dequeue,if it is an e we enqueue. Once all data from the data file is processed, we dequeue all elements leftover in the queue.