L05

Due by the end of today by midnight

Starter Files:


SUBMIT   /   Submission Summary

Isn't There an EASIER Way?

In class we went to the trouble of crafting our own array-based and chain-based implementations of the Bag interface. The purpose of doing that is to introduce you to the structures underlying ArrayList and LinkedList, and to see a bit how they work. Later we'll talk about why you should use one rather than the other, and the reasons for your choice have to do with how they work.

But of course a modern programmer wouldn't go to all that trouble. After all, ArrayList and LinkedList already exist. We could implement Bag using them. So much quicker and easier.

So today we're going to implement Bag using a LinkedList.

Activity 1
Download the Bag interface and the TestBag and TestInterface programs.
These programs are slightly modified from the versions you've seen before, and so don't get the different versions mixed up!

Create a class called LLBag that implements Bag. Give it a LinkedList instance variable. Give it a constructor that initializes that instance variable.

Add stubs for all the methods that need to be implemented. (It's OK if the "stub" throws an exception.)

For as many of the methods as possible, simply ask your instance variable to do it. For example, the clear command for the LLBag is implemented by asking its LinkedList to clear itself.

Activity 1

That will work for almost every method. There are two methods that don't have corresponding LinkedList methods: remove() and count(T). You need to write those methods on your own.

Run both TestBag and TestIterator to make sure you've got everything the way it's supposed to be.

You may notice that one method throws the wrong exception. (Whether you get it depends on how you implement remove().) Check to make sure you don't.

If you do get the wrong exception error, you will need to go in and ask one method to check to make sure that its list is not empty before trying to remove.

Submit this/these files:


SUBMIT   /   Submission Summary