A bag or multiset is a collection of things in which duplicates may occur. Think of a bag of marbles into which is placed 3 red marbles, two blue marbles and a yellow marble. There are 6 items in the bag, three of which are indistinguishable (all red), another two that are indistinguishable (both blue) and one other. Additional marbles can be added and marbles can be removed. If you look into the bag, you can remove a marble of a particular colour (for example any of the 3 indistinguishable red ones). Alternatively, you can simply reach into the bag and remove any marble, without knowing the colour until you remove it. Again, if you look into the bag, you can determine whether or not there is a marble of a particular colour or how many marbles of a particular colour there are.

Thus Bag is an abstract data type (ADT) representing collections of things and which has a set of operations as described above.

Part A - Bag ADT

The Bags folder contains a DrJava project for the Bags library package. It contains the interface for the Bag ADT as Bag.java.

As part of a package called Bags, write the necessary Java classes to provide the Bag ADT. The interface for the Bag ADT is provided as Bag.java. You must write the exception classes: NoSpaceException and NoItemException and one implementation class: ConBag.

For this assignment, the items in the bag are Strings. That is, in the example above a red marble might be represented by the String "red" and placing 3 red marbles into a Bag called aBag could be done by:

aBag.add("red");
aBag.add("red");
aBag.add("red");

The implementation class ConBag is a contiguous (array-based) implementation of the Bag interface. The items in the bag are represented by a "variable-sized" array of Strings. Items added to the bag are added at the end of the array. When an item is removed, the items following it in the array are moved one position to the left. The selection of an arbitrary item from the bag is done by the random generation of an index position within the array. When comparing Strings for equality, be sure to use the equals method.

The ConBag class should provide at least three constructors. The default constructor (no parameters) creates a new empty bag with capacity of 100 items. A constructor with an int parameter creates a new empty bag with capacity given by the parameter. A constructor that takes an array of Strings as a parameter should copy all of the elements from the array into the new empty bag whose capacity is 100 items.

Creating a library .jar file

In order to use (import) a library package in other projects, a .jar (Java Archive) file must be created. Be sure that the project has first been compiled. In the project menu, select Create Jar File From Project. In the resulting dialog, check Jar classes and then click on the button beside Jar File. Navigate to the folder within which you want to create the archive file (in this case, probably the folder for the Bags project itself), fill in the file name (should be the package name: Bags.jar) and press Save. Now press OK to dismiss the dialog.

Part B - Testing the Bag ADT

As a package TestBags within your Assign_3 folder, write a test harness to fully test the ConBag implementation of Bag. Be sure to test all limiting cases (including a full bag) and test and catch all exceptions.

Making a library accessible to a project

To make use of a library package that is not part of the standard libraries, an additional classpath must be added to the project to enable the compiler to find the library. Before attempting to compile the project, select Project Properties from the Project menu. In the resulting dialog, press Add under the Extra Classpath box. Navigate to the folder containing the .jar file for the desired library (the Bags.jar file in this case), select the .jar file and press Select. The classpath for this file should now be in the Extra Classpath box. Press OK to dismiss the dialog.

Part C - Using the Bag ADT

As a package called Draft within your Assign_3 folder, write a Java program to simulate the entry draft for a hockey Little League.

The local little league is holding its annual entry draft to select new players for the coming season. The league consists of a number of teams, each team named by a different colour. The standings at the end of the previous season determine how many tokens each team gets going into the entry draft. Teams with a lower standing are given more tokens than teams with a high standing. For example, the standings at the end of the year and the tokens are given below:

Team Place Tokens
Red 1 1
Gold 1 1
Purple 3 2
Blue 3 2
Orange 5 3
Green 6 4
White 7 5

At the start of the draft all teams put their tokens into a box. The league commissioner reaches into the box, randomly selecting a token. The team whose token was selected then chooses a player and the remainder of the tokens for that team are removed. The commissioner then selects another token and so on until all teams have selected a player. This method makes it fair for lower ranked teams which have a better chance that they select first.

Use a Bag to represent the box where the team names (colours) will be the items in the bag. Use the ConBag implementation. The ASCIIDataFile standings.txt contains one line per team giving the team name (String) followed by the number of tokens (int). Use this to load the Bag. The output from your program should be to an ASCIIDisplayer listing the order of the draft selections by team name such as below: see image.

Don't forget to add the Bags.jar file to the Extra Classpath in the Project Properties as above.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.