How to Implement Union Method in Java Interface?

  • Comp Sci
  • Thread starter Robben
  • Start date
  • Tags
    Java
In summary: Do I just do: public NewSet<T> union(NewSet<T> set) { // ... return new NewSet<T>(set); }Or is that totally wrong and I am not understanding what it means for an object to be called on?You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.In summary,
  • #1
Robben
166
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
  • #2
You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.
 
  • #3
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?
 
  • #5
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>();
 
  • #6
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?
 

FAQ: How to Implement Union Method in Java Interface?

1.

What is "Java: Calling On An Object"?

Java: Calling On An Object is a feature in the Java programming language that allows a program to access and manipulate data within an object. It involves using the dot operator (.) to call methods or access variables within an object.

2.

How do you call on an object in Java?

To call on an object in Java, you first need to create an instance of the object using the "new" keyword. Then, you can use the dot operator (.) to access methods or variables within the object. For example: ObjectName.methodName(); or ObjectName.variableName;

3.

What is the difference between calling a method and accessing a variable in an object?

Calling a method in an object will execute the code within that method, while accessing a variable will simply retrieve the value of that variable. Methods can also have parameters and return values, while variables do not.

4.

Can you call on an object from another class?

Yes, you can call on an object from another class as long as the object is accessible from the current class. This can be done by creating an instance of the other class or by using a static method from the other class.

5.

What happens if you try to call on a non-existent object in Java?

If you try to call on a non-existent object in Java, you will receive a "NullPointerException" error. This means that the object you are trying to call does not exist in memory and cannot be accessed. It is important to properly initialize and assign values to objects before calling on them in order to avoid this error.

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
933
Replies
1
Views
1K
Replies
1
Views
1K
Back
Top