2

I am having some difficulty understanding my assignment and I just want to make sure I am doing it correctly and would like to get another pair of eyes on my code. My assignment is as follows:

Implement a Bag class using an Array as the base data structure, which I have done. In our UML diagram my instructor shows it being an array of objects and am confused as to how I should be doing this with objects and how to compare them. I created a Node class to act as the objects and will attach that to the end of code. I main problem is I don't know what to do for Union and contains and are therefore causing me to question the rest of my code.

public class Bag extends Node {

    public Node array[];
    public Node header = new Node(null, null, null);

    public int bagSize = 10000; // An Initial size of array for the Objects in
    // the bag

    public int MAX_SIZE = 1000000; // Max Size of elements in a undetermined
    // size bag

    static int count = 0; // Number of Elements currently in Bag

    // Constructor for base Bag
    // This bag has a maximum size
    // bag that a bag can have
    public Bag() {
        array = new Node[MAX_SIZE];
        bagSize = MAX_SIZE;
        array[count] = header;

    }

    // Constructor for bag of size
    // which user can input
    public Bag(int size) {
        array = new Node[size];
        bagSize = size;
        array[count] = header;
    }

    // Method which a user can add objects
    // to the bag, the count will go up each
    // time the method is called on the object.
    public void add(Node newNode) {
        int numOperations = 0;
        Node n = new Node();
        numOperations++;
        n = newNode;
        numOperations++;
        count++;
        numOperations++;
        array[count] = n;
        numOperations++;
        System.out.println("Operations = " + numOperations);
    }

    /** Remove a random Node from the bag **/
    public void removeRandom() {

    }

    /** Remove a specified Node from the bag **/
    public void remove(Node obj) {
        int numOperations = 0;
        int i;
        numOperations++;
        for (i = 0; i <= array.length - 1; i++) {
            if (array[i] == obj) {
                int pos = i;
                numOperations++;
                for (int j = i; j <= array.length - 1; j++) {
                    array[i] = array[i + 1];
                    numOperations++;
                    if (i + 1 == array.length)
                        break;
                    numOperations++;
                }
                break;
            }
        }

    }

    /** Check is bag is empty **/
    public boolean isEmpty() {
        System.out.println("Operations = 1");
        return (count == 0);
    }

    /** Check if bag contains the Node **/
    public boolean contains(String data) {
        boolean contain = false;
        if (!isEmpty()) {
            for (int i = 0; i <= count; i++) {
                if (data == array[i].data) {
                    return contain = true;
                } else {
                    return contain = false;
                }
            }
        }
        return contain;
    }

    /** Return the size of bag **/
    public int size() {
        return count;
    }

    /** Add all Nodes of bag a to the specified bag **/
    public static void addAll(Bag b, Bag a) {
        int numOperations = 0;
        if (b.bagSize >= a.size() + b.size()) {
            numOperations++;
            for (int i = 0; i <= a.size(); i++) {
                b.add(b.array[i]);
                numOperations++;
            }
        }

    }

    /**
     * Join all elements of the two bags into a new bag but without any
     * overlapping items. i.e No Duplicates
     */
    public static Bag union(Bag a, Bag b) {
        Bag bigger = new Bag(a.size() + b.size());
        if(!a.isEmpty() && !b.isEmpty() && a.equals(b)){
            for(int i=0;i<=bigger.bagSize;i++){
                if(a.contains(a.getData()) && b.contains(b.getData())){
                    bigger.add(b.getNext());    
                }else{
                    bigger.add(a.getNext());
                    bigger.add(b.getNext());
                }
             }
        }
        return b;
    }

    /** Determine if the bags equal each other in items **/
    public boolean equals(Bag a) {
        if(bagSize == a.size()){

        }
        return false;
    }
}

public class Node {
    String data;
    Node prev,next;

    public Node(Node next, Node prev, String data){
        this.next = next;
        this.prev = prev;
        this.data = data;
    }

    public Node(){

    }

    public String getData() {return data;}

    public Node getPrev() { return prev;}

    public Node getNext() {return next;}

    public void setData(String newName) {data = newName;}

    public void setPrev(Node newPrev) { prev = newPrev; }

    public void setNext(Node newNext) { next = newNext;}

}
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
sealsix
  • 73
  • 1
  • 2
  • 9
  • 1
    What don't you understand about the contains and union methods? The contains() method checks whether an element exists in your Bag. The definition of union is also pretty clear ([see Wikipedia](http://en.wikipedia.org/wiki/Union_(set_theory))) – wonderb0lt Feb 07 '12 at 15:25
  • 1
    Why are you using linked nodes (and thus making some kind of linked list), since you were asked to use an array to store your objects. You don't need a Node class. And the Bag is certainly not a Node, so it must not extend Node. – JB Nizet Feb 07 '12 at 15:29
  • It makes sense now, I just finished an assignment working on LinkedLists so I was just in that mindset but I have it figured out now, thanks, and I know what Union is but it was just confusing me since I was combining linked lists into what I was trying to do. It's cleared up now so I should be good to go. – sealsix Feb 07 '12 at 16:19

1 Answers1

1

You don't need a Node class, your Bag is backed by an array, creating a linked list is redundant.

Your contains seems to be on the right track (but not there yet), it should probably be using equals() instead of ==. You need to re-check how the contain flag is handled, though.

What's the issue you're having with union? Can you describe how you're trying to implement it (the code isn't entirely clear)?

Dmitri
  • 8,999
  • 5
  • 36
  • 43
  • Thanks, I was using a Node class because I was confused on how to add objects to my bag. We are supposed to use objects and then compare and work with the data within them so thats why I used Nodes in the first place but I'll change that around. As for the Union, I am just confused as to how it should be done. My Instructor said it should be done in two for loops but I don't see where that came from. I guess I am just having trouble comparing the data to make sure there are no copies then adding them to a new bag. – sealsix Feb 07 '12 at 16:00
  • Well, you already have a `contains()` method, seems like it could be useful here. And you're starting with two bags, which is probably why you need two loops. – Dmitri Feb 07 '12 at 16:31