help me with this union method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EquinoX
    New Member
    • Feb 2007
    • 3

    #1

    help me with this union method

    I am writing a method with header like this:

    public Set<E> union (Set<E> other){

    }

    say I have a test method:
    Code:
    @Test
    	public void testUnion() {
    		ArraySet<Integer> a = new ArraySet<Integer>(10,20);
    		a.add(1);
    		a.add(3);
    		a.add(5);
    		a.add(7);
    		a.add(9);
    		
    		ArraySet<Integer> b = new ArraySet<Integer>(10,20);
    		b.add(2);
    		b.add(4);
    		b.add(6);
    		b.add(8);
    		b.add(10);
    		
    		Set<Integer> c = a.union(b);
    		assertEquals(10,c.size());
    		for (int i = 1; i<=c.size(); i++)
    			assertTrue(c.contains(i));
    	}
    how can I say the representation of a in the method body?? I know b can be represented as other in the union method head because the union method takes other as it's argument and that argument is b in this case. but what is a??
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    a would be referenced as this. Since you have the function as public Set..., it will be defined for each object - in this case, for Set a. Thus, the method will be called with all the variables of a in place - thus, if you had a private data member called setElements, you would simply access them by typing setElements, or this.setElement s.

    If you wanted to convert this function to a static function (a.k.a. not dependent on any one set), you would change the header to have 2 arguments - your function call would then look something like this:

    Code:
    Set<Integer> c = Set.union(a, b);
    And if the header was public static Set union(Set one, Set two), you would access each element as one.setElemtent s and two.setElements .

    Comment

    Working...