Search code examples
javaarraysobjectreturn

Java returning object


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;
    }

}

Solution

  • 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!