1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java: Calling On An Object

  1. Apr 5, 2015 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations

    None

    3. The attempt at a solution

    I have an interface:

    Code (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:

    Code (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:

    Code (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?
     
  2. jcsd
  3. Apr 5, 2015 #2

    jedishrfu

    Staff: Mentor

    You'd walk the list of your argument NewSet and if not present in the 'this' instance then add it to the 'this' instance.
     
  4. Apr 5, 2015 #3
    Hm, what do you mean by walk the list?
     
  5. Apr 5, 2015 #4

    jedishrfu

    Staff: Mentor

  6. Apr 5, 2015 #5
    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.

    Code (Java):

    NewSet<T> calledOn = new ArrayList<T>();
     
     
  7. Apr 6, 2015 #6
    I am really not understanding this. The changes I made was:

    Code (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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java: Calling On An Object
  1. Java adding two objects (Replies: 31)

  2. Java object method (Replies: 23)

Loading...