LD13: ArrayList due Mon 22 Apr 10:00

\begin{purpose}
In this assignment you will:
\begin{itemize}
\item become pr...
...learn how to inherit from system-provided classes.
\end{itemize}
\end{purpose}

Preparation for Lab Day

One of the strengths of Java is that it provides a wide range of built-in libraries which can be leveraged to solve a problem. So far this semester we have written a variety of containers. Java provides its own collection of reusable and extendable containers. The focus of this lab is on Java's ArrayList container which uses an array as its underlying structure into which elements can be stored, searched, removed, and sorted.

Do these steps:

  1. In your workspace, make sure you have the latest homework files from the base repository and do your work in th provided hw13 directory.
  2. Copy these files from hw12 to hw13: ArrayDictionary.java, CodeTimer.java, Word.java, Driver.java.
  3. Take a moment to skim the documentation for the ArrayList class found at:

    https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html.

    Pay particular attention to the documentation for the methods add(), indexOf(), get(), remove(), and size().

  4. Read the code in DemoArrayList.java and then take some time to play around with it. Modify the existing examples to make use of the indexOf() method.
  5. Add a new section of code to DemoArrayList.java that will create an ArrayList that stores Word objects. Demonstrate each of the methods listed above.
  6. Commit and push.

Lab Day

In the lab work today you will continue learning to use the built-in ArrayList class.

  1. First let's learn about iterators. The ArrayList class makes its internal array private so no one can access it directly. It is still useful/needed to be able to iterate through the elements in the ArrayList. Java bridges this gap by providing what it calls “iterators”. An Iterator can be visualized as an arrow that you can move through the array/list and that gives access to the data in the array/list. Iterators are used for a variety of classes, including ArrayLists.

  2. An Iterator object provides several methods:
    hasNext()
    returns true if there is one or more elements in the list that have not been visited yet.
    next()
    returns the element being pointed to by the iterator and advances the iterator to the next element in the list.
    remove()
    removes the element currently being pointed to by the iterator.

  3. Use the code snippets below as a guide to use an iterator to display elements from the container without using get().
    import java.util.ListIterator;
    
    String fun;
    ListIterator<String> it;
    
    it= sc.listIterator(); // assumes sc is ArrayList<String> object that has data
    fun= it.next();
    System.out.println(fun);
    
    fun= it.next();
    System.out.println(fun);
    
    fun= it.next();
    System.out.println(fun);
    

  4. Once you get the code working it should be displaying the first few elements of the container. Modify the code so that it uses a loop to displays all the elements in the container. Your code should make use of the hasNext() method. Add loops for each of the three ArrayList objects.

  5. Test and commit.

  6. Sometimes you may have a container (like ArrayList) that provides almost everything you want—but not quite. This is where inheritance can help! Let's modify ArrayDictionary to inherit from from ArrayList as follows:

    public class ArrayDictionary extends ArrayList<Word>
    

    NOTE: You'll need to import ArrayList.

  7. If ArrayDictionary IS-A ArrayList then it inherits all of the methods and attributes (i.e., internal storage) of ArrayList. So, we no longer need the attributes data and n. Remove them.

  8. Comment out all the methods except for the constructor and display().

  9. In the constructor: comment out the call to sort and call the current object's (i.e., this) add method to insert a word when it is read.

  10. In display() modify the code to use an iterator to cycle through the list and display it.

  11. In the driver modify the code so it: creates the ArrayDictionary and then displays the contents of it.

  12. Once you get the code working: document, commit, push.

  13. If you finish early begin work on the homework assignment.