Question: Understanding inner classes , needed for clean HashSet/HashMap implementations and other container classes. Consider again Bag.java on page 155, but this time let's keep the

  1.  Understanding inner classes, needed for clean HashSet/HashMap implementations and other containerUnderstanding inner classes, needed for clean HashSet/HashMap implementations and other container classes. Consider again Bag.java on page 155, but this time let's keep the Nodes and see how they work. Note that the "private" inside the nested class ListIterator is ineffective and would be better removed. Also note that ListIterator is not a JDK ListIterator at all.
    1. Consider the Bag with "apple", "banana", "pear" added in that order. Draw the Bag object using the same boxes as shown on slide 6 of Feb.18, i.e, the same format as used in problem 1. Hand-drawn is fine. You can show the string objects hanging off or put the string values in boxes in the Node as done there.
    2. Suppose a ListIterator object has just been created by Iterator itr = bag.iterator(), in client code to Bag, where bag is the Bag of 3 strings described in part a. This new object itr is separate from the bag object, but its current instance variable references the bag's very first Node object. To the client, itr's type is simply Iterator, no hints of its concrete type (and no downcasts allowed either: that ListIterator type is private to Bag). Draw an object diagram showing these two objects (itr and bag) and their references.
    3. Suppose itr.next() is called. Draw the new object diagram.
    4. Can a single Bag object like bag have two iterators existing at the same time? Explain how or why not.

============================================================================================================================================== Here is the format for the problem, pls draw the diagram. thank you classes. Consider again Bag.java on page 155, but this time let's keep

ALGORITHM 1.4 Bag import java.util.Iterator; public class Bag implements Iterable private Node first; // first node in list private class Node { Item item; Node next; } public void add(Item item) { // same as push() in Stack Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; } public Iterator iterator() { return new ListIterator(); } private class ListIterator implements Iterator { private Node current = first; public boolean hasNext() { return current != null; } public void remove() { } public Item next() { Item item = current.item; current = current.next; return item; } } } This Bag implementation maintains a linked list of the items provided in calls to add(). Code for i sEmpty() and size() is the same as in Stack and is omitted. The iterator traverses the list, main- taining the current node in current. We can make Stack and Queue iterable by adding the code highlighted in red to ALGORITHMS 1.2 and ALGORITHM 1.3, because they use the same underlying data structure and Stack and Queue maintain the list in LIFO and FIFO order, respectively. first part of diagram on pg. 464 A8 E12

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!