• Post Reply Bookmark Topic Watch Topic
  • New Topic

union and intersection method!  RSS feed

 
Aditya Herlambang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am asked to write a method of union and intersection which the method header is like this:




It says that I have to return a Set<E> method, and get a Set<E> variable from the constructor. How am I suppose to do that?? It says that I have to implement the iterator interface, do I have to use that here? I need some hint's here. What I understand throughout now is that by using the union and intersection method, that means that I have to have two objects which is compared to each other whether they have the same value (intersection) and the value of both of them combined (union).. One more thing, whenever I try to call the hasNext() method which is on the iterator method, it gives me an error the method hasNext() is undefined for Set<E>, how is this suppose to be right? For your info I am writing this class inside the ArraySet class that implements the Set<E> and the Set<E> extends the Iterator
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says that I have to return a Set<E> method, and get a Set<E> variable from the constructor. How am I suppose to do that??


Unfortunately, you will need to elaborate a bit more. The "return a Set<E> method" doesn't make sense, and the "get a Set<E> variable from the constructor" seem out of context.

It says that I have to implement the iterator interface, do I have to use that here? I need some hint's here. What I understand throughout now is that by using the union and intersection method, that means that I have to have two objects which is compared to each other whether they have the same value (intersection) and the value of both of them combined (union)..


The Set interface actually have built in methods that will make getting the union or intersection with another Set easily done (the addAll() and retainAll() methods). I believe what your instructor is saying is that, you are not allowed to use those methods. Instead, you have to iterate through the two sets yourself and calculate the union and intersection.

One more thing, whenever I try to call the hasNext() method which is on the iterator method, it gives me an error the method hasNext() is undefined for Set<E>, how is this suppose to be right? For your info I am writing this class inside the ArraySet class that implements the Set<E> and the Set<E> extends the Iterator


The hasNext() method is part of the iterator, not part of the Set. You have to get the iterator from the set, and use the hasNext() and next() methods of the iterator. The Set interface does not extend the iterator.

Henry
 
Aditya Herlambang
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does this also means that I also have to implement the iterator in the set and put the iterator methods in the set class??
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!