Algorithms and OOD (CSC 207 2014F) : EBoards

CSC 207.01 2014F: Extra Session 10 (Thursday, 13 November 2014)


Topics to discuss

Wrapping

Broad: Objects in one class include objects from another class and conceal those inner objects.

Wrapper-like patterns

Example: Wrapping/adapting java.util.Queue to meet the taojava.util.Linear interface.

    public class LinearQueue<T>
      implements LinearStructure<T>
    {
      // +--------+------------------------------------------------------
      // | Fields |
      // +--------+

      java.util.Queue<T> core = new java.util.Queue<T>();

      // +---------+-----------------------------------------------------
      // | Methods |
      // +---------+

      public void put(T val)
        throws Exception
      {
        core.add(val);
      } // put(T)

      public T get()
        throws Exception
      {
        core.remove();
      } // remove()


      public T peek()
        throws Exception
      {
        core.element();
      } // peek()

      public boolean isEmpty()
      {
        return (core.peek() != null);
      } // isEmpty()

      public boolean isFull()
      {
        return false;
      } // isFull()

      public Iterator<T> iterator()
      {
        return core.iterator();
      } // iterator()
    } // LinearQueue

What's the difference between Comparator and Comparable?

Comparator objects provide the method compare which takes two parameters and returns an integer that describes their ordering.

Comparable objects provide the method compareTo which takes one parameter and returns an integer that desribes the ordering of the object and its parameter.

Most of the standard objects implement Comparable<SameType>.