Comp Sci How to Implement Union Method in Java Interface?

  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
To implement the union method in a Java interface, the NewSet interface is defined to extend the Set interface, allowing for additional functionality. The union method should take another NewSet as a parameter and return a new NewSet that represents the union of both sets. The confusion arises around how to reference the current instance (the object called on) and the parameter set to perform the union operation. A suggested approach involves creating a new instance of GSet to store the result and iterating through the elements of both sets to add unique items to the result set. Proper implementation requires understanding how to iterate through the elements of the NewSet and manage the data structure correctly.
Robben
Messages
166
Reaction score
2

Homework Statement



How do I take the union between an object called on and the object passed in the parameter?

Homework Equations



None

The Attempt at a Solution



I have an interface:

Java:
import java.util.Set;

    /**
     * An extended Set where there is added functionality
     *
     * @version 3/23/14
    */
    public interface NewSet<T> extends Set<T> {
        /**
         * Calculates the set union with the given input
         *
         * @param set - the set to perform the operation along with
         * @return  the set representing the invoked set and the parameter
        */
         NewSet<T> union(NewSet<T> set);

         //and a bunch more methods ...
    }

And I have a class that extends this interface:

Java:
    import java.util.ArrayList;
    public class gSet<T> implements NewSet<T> {
        // ...
    }

Now, the union method requires a NewSet object to be called on. The Union will be performed between the object called on and the object passed in the parameter, which is set. But I am confused on how to do this?
How do I call on a NewSet object in order to take the union between the object called on and the object passed in?

Do I just do:

Java:
    public NewSet<T> union(NewSet<T> set) {
        NewSet<T> calledOn = new ArrayList<T>();
        // ...
    }

Or is that totally wrong and I am not understanding what it means for an object to be called on?
 
Physics news on Phys.org
You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.
 
jedishrfu said:
You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.
Hm, what do you mean by walk the list?
 
jedishrfu said:
Use a for loop
Java:
Set myset = new HashSet();
myset.add(obj1);
...

for (Object x : myset) {
    ...
}

http://tutorials.jenkov.com/java-collections/set.html#java-set-example
I can't use Hashset, I can only use ArrayList. But from what I can understand from your code is you created an instance of a Set, which I can't do because Arraylist isn't a set. So do I just create an instance of NewSet, i.e.

Java:
NewSet<T> calledOn = new ArrayList<T>();
 
I am really not understanding this. The changes I made was:

Java:
public class GSet<T> implements NewSet<T> {
    private List<T> list = new ArrayList<>();

    public NewSet<T> union (NewSet<T> set)  {
        NewSet<T> rSet = new GSet<T>();
        // ...
        return rSet;
    }
}

But I am still not understanding how to call on a NewSet object in order to take the union between the object called on and the object passed in?
 

Similar threads

Replies
4
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
2
Views
4K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
1
Views
1K
Back
Top