How to Implement Union Method in Java Interface?

  • Context: Comp Sci 
  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around implementing a union method in a Java interface that extends the Set interface. Participants are exploring how to correctly perform the union operation between the invoking object and a parameter object of the same type.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant provides an interface definition for NewSet and asks how to implement the union method, expressing confusion about calling on a NewSet object.
  • Another participant suggests iterating through the argument NewSet and adding elements to the invoking instance if they are not already present.
  • A follow-up question seeks clarification on what is meant by "walking the list."
  • Participants suggest using a for loop to iterate through the elements of a set, providing a code snippet as an example.
  • One participant clarifies that they cannot use HashSet and must use ArrayList, questioning how to instantiate a NewSet object correctly.
  • A later post indicates an attempt to implement the union method but expresses ongoing confusion about how to properly call on a NewSet object for the union operation.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the implementation details of the union method, with ongoing confusion and multiple interpretations of how to proceed.

Contextual Notes

There are limitations in understanding the relationship between the NewSet interface and the ArrayList implementation, as well as the specifics of how to perform the union operation 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 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K