Activity 2.8 - Adding methods to the Bag class

Topic

This activity asks you to add extra methods to an existing class implementing a bag data structure.

Materials

In addition to the discussion of the solution in the Solution document, the project for this activity contains two classes:

Tasks

A bag is a collection of data that keeps track of the number of times each item occurs in it. In this case the items are the digits 0 - 9. The numbers of occurrences of each digit are held in an integer array bagVals.

For example, the following collection has two 1's, one 2, five 3's, a 4 and an 8 in it:

1, 1, 2, 3, 3, 3, 3, 3, 4, 8

This can be represented by an array with the following values

{0, 2, 1, 5, 1, 0, 0, 0, 1, 0}

The additional methods to be added to the bag class are:

Instructions

Open the class Bag and examine the existing methods. Using these as a guide, add the new methods, as described above, at the point indicated.

When you have done this, open the class BagTest and read the code there to see what it does. Now run the project to try out your solution.