13.5 HOMEWORK


13.5 HOMEWORK

  1. Provide implementations for a copy constructor and a copy assignment operator for the LinkedList class of Section 13.1.1.

  2. Provide implementations for a copy constructor and a copy assignment operator for the parameterized LinkedList class of Section 13.1.2.

  3. Create a parameterized Java class Buffer<T> that could be used as a buffer for different types of data. At the least, your class should implement the following interface:

          interface BufferInterface<T> {          public void addElement(T data);          public void addElementAt(T data, int index);          public T removeElementAt(int index);          public int getSize();      } 
  4. Write a parameterized lookup table class, Lookup<T1, T2>, in Java for storing <key, value> pairs where T1 is the type parameter for key and T2 the type parameter for value. The class Lookup should support at least the following methods:

    boolean addEntry(T1 key, T2 value)—to add an entry into the table. This method should return true if the operation is successful, and false otherwise. Only one value entry is allowed for a key. If <key, value> is inserted a second time for the same key, the new value should replace the old.

    boolean removeEntry(T1 key)—to remove an entry from the table for a given key. It should return true if operation is successful, and false otherwise.

    T1 retrieveKey(T2 value)—to retrieve the key associated with a given value.

    T2 retrieveValue(T1 key)—to return the value associated with the specified key.

    boolean hasValue(T2 value)—to test whether a particular is stored in the table.

    boolean hasKey(T1 key)—to test whether the specified key is stored in the table.

    boolean expandTable(int size)—to increase the capacity of the table so that it can hold the specified number of <key, value> pairs. Must return false if the table cannot be expanded.

    boolean isFull()—returns true if the table is full, false otherwise.

    void printTable()—to display the table in a two-column format, with the first column for the keys and the second for the corresponding values.

    The user needs to specify the size of the table when it is first constructed.

  5. Write a parameterized C++ version of the Java class of the previous homework problem. As for the Java homework, the C++ class will have two template parameters, T1 for key type and T2 for value type. Assume that the types used for T1 and T2 have preexisting overload definitions for the operators ‘==’ and ‘!=’. No need to write separate template specializations for the pointer types for T1 and T2.

  6. Write a C++ template class that can serve as a dynamic FIFO queue for an arbitrary data type with support for at least the following methods. T is the template parameter for the element type in the queue.

    void enqueue(const T& item)—for adding a new element at the end of the queue.

    T& dequeue()—for removing the element at the front of the queue. The method must throw an exception if the element does not exist.

    int size()—for returning the number of elements currently in the queue.

    T elementAt(int i)—for ascertaining the element (without removing it) at the specified location.

    void printQueue()—for displaying the contents of the queue.

  7. Define a parameterized Java class MyQueue<T> to serve as a dynamic FIFO queue for elements of arbitrary data type. The class MyQueue<T> should implement the following interface

          interface QueueInterface<T> {          public void enqueue (T item);          public T dequeue();          public Iterator<T> getIterator();                    class NoSuchElementException extends RuntimeException {}      } 

    where Iterator<T> is a parameterized interface defined as

          interface Iterator<T> {          public boolean hasNext();          public T next();          public boolean getNext();      } 

    with all methods possessing the usual semantics for the queue container and for Java iterators.

  8. Augment your solution for the previous problem by providing the implementation code for the following methods:

    1. A parameterized Java method that returns an int for the number of elements in the queue.

    2. A parameterized Java method that returns the ith element in the queue.

    Both these methods should work on any implementation of the QueueInterface defined in the previous problem.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net