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:
- Bag, which is the class you are asked to add a method to;
- BagTest, which is run to test the code.
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:
- removeFromBag(int) where the argument represents one of the digits. When
called, the method should remove one occurrence of the specified digit. (You may
assume that the method will not be called unless the digit occurs at least
once.) The method does not return any value.
- removeAllFromBag(int), where the argument represents one of the digits.
When called, the method should remove all occurrences of the specified digit
from the bag. The method does not return any value.
- emptyBag(), which resets the bag to its empty state, where there are no
occurrences of any digit. The method does not return any value. Do this using a
for-loop to set each array location to zero.
- printCounts(), which prints the number of each digit in the bag. For the
example given above, it would print 0 2 1 5 1 0 0 0 1 0 followed by a blank line.
Do this using a for-loop to print out the number of each digit in the bag.
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.