CSC 241- Lab 3 (Due March 19, 1998)


Theme ...

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.


Create directories and copy some files

  1. cd public-html/classes/csc241
  2. 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.
  3. chmod og+rx Stack
  4. chmod og+rx Stack/genStack
  5. cp -r ~mohammad/public-html/classes/csc241/Queue/ .
  6. chmod og+rx Queue
  7. To be sure that you have everything, do the following:

Compile all Java files and run the test programs

  1. Use javac to compile each of the above .java files. In any directory, 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.
  2. Run the program that tests intStack.
  3. Run the program that tests stringQueue.

classify intStack class

Make intStack into an abstract class.

  1. You need to be in public-html/classes/csc241/Stack/intStack/
  2. cp intStack.java fixedIntStack.java. You will use what is currently in intStack.java in creating the subclass fixedIntStack.
  3. Change your intStack.java to make it into an abstract class. Keep in mind that your stack holds int elements and be sure to have the correct package name csc241.Stack.intStack at the top. You should be able to use the stringQueue class shown below to model your abstract class. Check out stringQueue.java which you copied earlier or follow the link to stringQueue.java for more detail.
          public abstract class stringQueue {
             public abstract boolean empty();
             public abstract boolean full();
             public abstract void enqueue(String x);
             public abstract String dequeue();
          }
    
          
  4. javac intStack.java

Modify fixedIntStack to make it an extend intStack.

  1. You need to be in public-html/classes/csc241/Stack/intStack/, you also need be sure you have the file fixedIntStack.java.
  2. Change the class name in both documentation and the class header.
  3. Add the phrase extends intStack to the class header. Check out fixedStringQueue.java which you copied earlier or follow the link to fixedStringQueue.java for an example.
  4. javac fixedIntStack.java

Develop the dynamic version of intStack.


Test intStack class

Build your test program in public-html/classes/csc241/Stack/intStack/testIntStack directory. This test program is for your two versions of intStack. Check out test1.java in public-html/classes/csc241/Queue/stringQueue/testStringQueue directory or follow the link to test program test1.java for an an example. This test1.java tested the two implementations of stringQueue.

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.