CPSC 170 Lab 9: The Set ADT and Implementations

As usual, create a lab9 subdirectory for today's lab, open this document in Mozilla, and start emacs.

An Array Based Set Implementation

Files SetADT.java, ArraySet.java and ArrayIterator.java contain the code that we discussed in class from Chapter 3 of Lewis and Chase. TestSet.java contains a simple program that illustrates some of the set operations, and Sentences.java contains a skeleton of a program that uses sets to make random sentences. Save these files to your directory.
  1. Open TestSet.java and study it to understand how it works. Then compile and run it. Particularly note how the iterators are used to print the sets in table form. Could this be done using just the ArraySet toString method instead of using iterators? Explain how or why not.





  2. Open Sentences.java. Fill in the code as indicated by the comments so that the program sets up three sets of strings to hold subjects, verbs, and objects respectively. After it reads values for the sets (this code is already in place), it should do the following: Compile and run your program to make sure it works. To facilitate testing, you can use redirection to take input from file words.dat -- just save the file to your directory and run your program as java Sentences < words.dat

  3. Add method intersection from the prelab to SetADT.java and ArraySet.java. Then modify the TestSet program so that after it prints the union of the two sets it prints their intersection (neatly labeled, of course).

  4. Simplify method equals as described in the prelab -- the new method should be significantly simpler than the original. Run TestSet again to be sure it still works.

    Introduction to Linked Structures

  5. File TestList.java contains a simple program that uses the LinearNode class to create a 3-element linked list. Save these files to your directory, then compile and run TestList to see what it does.

    Now print out TestList.java and study the code. There are seven lines marked with numbers (//**** 1 ****, etc). For each of these lines, draw a diagram like those in Chapter 4 of the text showing front and temp and everything they refer to. Your diagrams should show the structures after the line executes. Draw the diagrams on the TestList printout.

  6. Of course, it's silly to have separate code each time you put something on the front of the list. Modify TestList so that it reads a positive integer N from the user and uses a loop to create a list that holds the integers from N to 1. Add a node to the front of the list each time, as in the example, so the numbers will still be backwards in the list.

  7. Sometimes you want to add nodes to the end of a list, not the beginning. There are two standard strategies for this.