0

I'm creating a program that makes and modifies integer sets

Here is my code:

public class IntSet{
    private final int MAXALLOWEDSETVALUE=2000;
    private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1];

    public IntSet(int... elts) {
        for(int iteration = 0; iteration < elts.length; iteration++) {
            if(elts[iteration] <= MAXALLOWEDSETVALUE)
            data[elts[iteration]] = true;
        }
    }

    public IntSet(IntSet source){
        System.arraycopy(source.data, 0, this.data, 0, MAXALLOWEDSETVALUE);
    }
    public void setTo(IntSet source){
        System.arraycopy(source.data, 0, this.data, 0, MAXALLOWEDSETVALUE);
    }
    public void insertElement(int element){
        data[element] = true;
    }
    public void deleteElement(int element){
        data[element] = false;
}
    public boolean hasElement(int element){
        if(data[element] == true)
            return true;
        else
            return false;
    }

    public boolean equals(IntSet other){
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(data[iteration] == other.data[iteration]) {

            } else {
                return false;
            }
        }
        return true;
    }

    public String toString() {
        String output = "{";
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(data[iteration] == true)
                output += (iteration + ", ");
        }
        output += "}";
        return output;
    }

I'm struggling with my subtract function: The subtract function forms a new set that is equal to the first set, except that any element from the second set is removed. I know to I need to return an object, but I'm not sure how to. Any help is appreciated.

    public IntSet subtract(IntSet other) {
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(data[iteration] == true && other.data[iteration] == true) {
                other.data[iteration] = false;
            }
            if(data[iteration] == true && other.data[iteration] == false) {
                other.data[iteration] = true;
            }
        }
        System.arraycopy(other.data, 0, this.data, 0, MAXALLOWEDSETVALUE);
    }
    public int getUpperLimit(){
        return MAXALLOWEDSETVALUE;
    }

}
7
  • 1
    Welcome to Stack Overflow! It looks like you may be asking for homework help. While we have no issues with that per se, please observe these dos and don'ts, and edit your question accordingly. (Even if this isn't homework, please consider the advice anyway.) Commented Mar 9, 2017 at 22:39
  • Thank you! I still personally believe I'm not violating any rules or anything. But thanks for the suggestion! Commented Mar 9, 2017 at 22:41
  • Are you tryiing to store a set of integers? If so, where are you planning to store it? You are using an array of boolean. Why? Commented Mar 9, 2017 at 22:44
  • Never mind, I understood your logic, but I don't understand the reason.. But it's very "different".. Commented Mar 9, 2017 at 22:48
  • 1
    So you must create a copy of your set (you already have a constructor to do that), and then remove every element of the second set from this copy (and you also have a method to remove an element from a set). Commented Mar 9, 2017 at 22:49

1 Answer 1

1

Your subtract method can be implemented like this:

  1. Create a copy of your current object. Call this newSet.
  2. If other set contains an element, set that newSet element to false, which essentially "removes" that element from newSet.
  3. Return newSet.

The code would be like this:


public IntSet subtract(IntSet other) {
    IntSet newSet = new IntSet (this);
    for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
        if(other.data[iteration]) {
            newSet.data[iteration] = false;
        }
    }
    return newSet;
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Cool so I can create another object just while I'm in that function? That's pretty awesome
@T.Dog yes. You could also make changes to the same object you're at, and finish with return this.
The body of the loop could be reduced to if (other.data[iteration]) { newSet.data[iteration] = false; }
@JBNizet, Updated the answer per you comment. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.