Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  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). I need two java program: bag.java and testbag.java =================================================================================================================================== image text in transcribed

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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

What is dividend payout ratio ?

Answered: 1 week ago

Question

Explain the factors affecting dividend policy in detail.

Answered: 1 week ago