-1

I have an assignment in which I need to write an implementation class for a Bag (or Multiset) ADT. Problem is, the assignment is worded in a way that's hard to follow and I'm not sure what exactly I need to do.

Here is the assignment description and here is the interface I was provided. This is my implementation class so far. I haven't written any of my methods yet because I'm not sure where to go from here, especially in regards to the 3 different constructors.

package Bags;


import java.io.*;


public class ConBag implements Bag, Serializable {


  private String[]  items;     // The items in the bag
  private int       itemCount; // The number of items
  private int       size;      // The size of the bag



  // This constructor creates a new empty bag able to hold 100 items.
  public ConBag ( ) {

    this(100);

  }; // Constructor



  // This constructor creates a new bag with a specified capacity.
  public ConBag ( int size ) {

    items = new String[size];

  }; // Constructor



  // This constructor takes an array of Strings and copies them into a bag of 100 or fewer items.
  public ConBag ( String[] items ) {



  }; // Constructor



  public void add ( String item ) {

    try{
      if(!contains(item) && (!(size == items.length))){
        items[itemCount] = item;
        itemCount++;
      }
    }catch (NoSpaceException exception) {
      System.out.println("Bag is full.");
    }    
  }; // Add



  public void remove ( String item ) {

    for (int i=0; i<size; i++) {
      if (contains(item)) {
        items[i] = items[itemCount-1];
      }else {
        NoItemException exception; 
        System.out.println("Item not in bag.");
      }
    }    
  };



  public int cardinality ( ) {

    return itemCount;    
  };



  public boolean contains ( String item ) {

    for (int i=0; i<itemCount; i++) {
      if(items[i].equals(item))
        return true;
    }
    return false;   
  };



  public int count ( String item ) {

    int count;

    return count;    
  };



  public String draw ( ) {



  };

}

I feel like I'm missing something important, but I don't know what. I already have NoItemException and NoSpaceException, but I don't think I need to include them in this post as they're pretty basic. Any help or a nudge in the right direction would be great. Thanks!

  • Your question is a little broad. Basically you need to implement a class that internally has an array of Strings. That is String[] data. The constructors need to make sure that the array is initialized with enough slots based on the size -- so the one taking a size would do data = new String[size] (for instance). Once you have all of that, a bunch of the methods should become pretty obvious. See if this gets you moving and where you get stuck next. – Joseph Larson Mar 07 '19 at 16:58
  • @JosephLarson the array is not necessarily needed. Some data structure is needed indeed, but a Map is much more better fit for this problem. – Lajos Arpad Mar 07 '19 at 17:00
  • @LajosArpad I think the idea is understanding how containers work. This is a class assignment, after all, not a "best practices". Otherwise why write an implementation for things that already exist? – Joseph Larson Mar 07 '19 at 17:02
  • @JosephLarson You are right, the spec asks for arrays. – Lajos Arpad Mar 07 '19 at 17:04
  • @NicoHaase I've updated my post with my current code. The main thing I'm having trouble with is how I'm writing my constructors and actually creating the bags. – user9353081 Mar 07 '19 at 17:32
  • @JosephLarson I updated my post. I think I have most of my methods, but I'm still stuck on my 3rd constructor. Do I need a loop for it? – user9353081 Mar 07 '19 at 17:40
  • @user9353081Yes. You'll need to allocate enough space for however many are already in the array passed to you, then copy the contents into place with a loop. – Joseph Larson Mar 07 '19 at 20:14

1 Answers1

0

You need to allow duplication, therefore using String array as a data structure makes things difficult. It's better to use a map where the key is a String and the value is an Integer.

It's unclear what is the limit of the room, so, for now you can define a private member called room, which will be int and whenever you intend to add a String, check cardinality against room. If it's smaller, then increment the value of the map entry if exists. If it did not, then just create it with a value of 1.

remove should check for contains. If the Map you have does not contain the item, throw an exception. Otherwise decrement the value of the map entry if it's higher than 1. If it is 1, then just remove it from the map.

To calculate cardinality traverse the map and calculate the sum of the values.

contains should be simple, you will just have to call a method of your map. count should be simple as well.

draw is interesting. First, calculate cardinality, use it as the unreachable upper bound of your randomization and initialize a sum and start traversing the map. On each iteration increase sum (which is 0 before the loop) with the value of the map entry. If the randomized number is smaller than sum, then call remove passing the key of the item and exit the loop.

EDIT

If you need to do this with an array of String items, then you can do so, but you will also need to store an integer for each String, that would be another array and the easiest representation would be to ensure that every item in the String array would be associated to the int value in the int array at the same index. Not too elegant, but can be used. Now, in this case you could not use Map methods, but will need to implement stuff yourself.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175