CPSC 170 Prelab 9: Sets

  1. Write a method public SetADT<T> intersection(SetADT<T> set) for the ArraySet class that returns a new set containing the items that are in both the current set and the parameter. Use an iterator to access elements of the current set.




















  2. The equals method in the ArraySet class in the text is unnecessarily complicated. The code given would be needed if sets could contain duplicates, but they can't. For two sets to be equal, they must be the same size and every element in one set must be in the other set. (Think: do you need to try this both ways, that is, everything in set1 in set2 and everything in set2 in set1, or is one way good enough?) Rewrite and simplify the equals method to take advantage of the fact that sets cannot contain duplicates.




















  3. Consider the LinearNode class that we discussed in class. Assume that variable front is of type LinearNode, and that it points to a list containing at least one node.
    1. What does the following code do?
      LinearNode newNode = new LinearNode("whatever");
      LinearNode temp = front;
      while (temp.getNext() != null)
          temp =  temp.getNext();
      temp.setNext(newNode);
      



    2. What would the code above do if the list were empty? Explain.





    3. Write code to print each element in the list.







    4. Write code that creates a new node and makes it be the second node in the list.