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 class
- 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). I need two java program: bag.java and testbag.java ===================================================================================================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started