CSC 241- Lab 3 (Due March 12, 2001)


Theme ...

This lab will help you learn about linked lists. You will also become familiar with packaging, classification, and polymorphism in Java and Object Oriented Design in general. Based on the class hierarchy built for Queue you will build a the components of the class hierarchy for Stack.


Create directories and copy some files

  1. cd public-html/classes/csc241
  2. cp -r ~mohammad/public-html/classes/csc241/Queue . -- don't forget the dot or the -r. This command replicates my Queue directory and places it in your account.
  • cd Queue

    Compile 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, in the Queue directories compile the .java files.
    2. Run the program that tests Queue.
      • Change to directory public-html/classes/csc241/Queue/testQueue
      • test.java is designed to test both the dynamic and the fixed array versions of our Queue class. I have designed it to use command line parameters for giving the data file name and other information. So, type the following to first test the dynamic version and then the fixed versions.
        1. java test test.dat dynamic
        2. java test test.dat fixed 10
    3. Make the Queue directory and all .class files in it public.

    Create the Stack class hierarchy

    You need to create the Stack directory and the three files Stack.java, fixedStack.java, and dynamicStack.java and compile them. These files need to be packaged as csc241.Stack. You will also need to create testStack directory as it exists for the Queue package as well and test to make sure your Stack classes work correctly.

    Stack.java will hold the interface for stack and follows closely the Queue interface.

    fixedStack.java implements the Stack interface. You should follow the code we developed in class for Stack of Objects. You will need a peek method as the interface calls for it; it didn't exist in our class example. Also, keep in mind that push and pop's return value types need to be void.

    dynamicStack.java also implements the Stack interface. Following dynamicQueue works only partially.

    You will need a Snode class which will be basically the same as the Qnode class used for dynamicQueue.

    You will only need one field here, a top_ which references the top node in our linked list. top_ should be null at construction.

    The methods empty, full, and peek are similar to their couterparts in dynamicQueue.

    The pop should be similar to the dequeue method from dynamicQueue as the concept of removing from the top of the stack is really the same as removing from the front of the queue. We don't have a back of the list concept in the stack so there is no back_ reference that may have to be set to null, you only need to deal with top_.

    The push method, on the other hand, is different from enqueue as it needs to put the new element on top, not in the back. To push an element, you simply put the element in element_ of a new node and have its next_ reference the current node on top( i.e. the node referenced by top_); top_ would then refernce this new node.

    test.java will be just like the test program for the queue class, except for the fact that it will be testing a stack; it needs stack's constructors and methods. Make sure that both implementations work before turning the lab in.


    What to Turn In

    Stack.java, fixedStack.java, dynamicStack.java, Snode.java, and test.java (for stacks).