Question: Using LinkedList to implement a specialized list . Implement Bag , page 155 by using a LinkedList, i.e., set up a java source Bag.java for

  1. Using LinkedList to implement a specialized list. Implement Bag, page 155 by using a LinkedList, i.e., set up a java source Bag.java for class Bag that has an instance variable of type List, fill it in with a LinkedListat the time of the Bag's creation. Use the LinkedList to store new elements as they come in. New elements come into Bag via method void add(Item x), so the code for this method uses method add of List to store the incoming Item. Note that the LinkedList can provide a working iterator, so you don't need a private class for the iterator, or the Node class either. The iterator object obtained from LinkedList, an Iterator, can simply be returned as an Iterator for Bag. This iterator will have a working remove(), unlike the iterators shown in S&W, but that's OK. This Bag class provides an example of wrapping one object with another to restrict the API of the result. This can be done with just one line of code for each method, and the constructor!

    Write a client program, TestBag, that puts "apple", "pear", and another "apple" in a Bag, and then counts the apples in the bag. Consider what methods are available on the Bag object for this client program. In your homework answer, show Bag.java and TestBag.java. Also think about how you could similarly implement Stack and Queue following S&W's APIs on page 121 (no homework answer needed).

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!