• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scroll down to the bottom of the third post for the problem that is currently being asked about.  RSS feed

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method:
public MySet union(MySet s){...}

And it joins two sets together, so this method is declared by:

mySet=mySet.union(unionSet);
(alternativly, it could be called with mySet=unionSet.union(mySet);)

I'm implementing the abstract interface MySet using arrays, so each set has an arrElements, which is an array of all of their elements that is created at the same time the sets are initialized.

Inside of the union method, arrElements just refers to the arrElements of mySet, and I need to know how to access the arrElements of unionSet in order to manipulate the values. Does anybody know how I would do this?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to qualify the access using the reference identifier:

s.arrElements
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried that, and it didn't work. After hearing the suggestion form somebody else, I decided to play around with it for a bit. MySet is an interface, and doesn't have a constructor, so when I declare 'MySet unionSet' I need to use =new MyArraySet(); since MyArraySet implements MySet. So when the method union required MySet s, there was no arrElements. I had to change it to union(MyArraySet s){...} in order to access the arrElements. Thanks for the confirmation.

Well, remember how MyArraySet needs to implement MySet? By changing union(MySet s) into union(MyArraySet s), MyArraySet no longer implements MySet because the method requires a diferent input. And the real kicker here is that I can't change MySet's union to require an input of MyArraySet because after I implement MySet with arrays, I need to do it with linked lists. This does not make me happy. I'll throw down some code to show what I mean.



This version of MyArraySet implements MySet.



This version of MyArraySet does NOT implement MySet. The only difference is the input of union form MySet to MyArraySet, but it's a big enough change.

[strikethrough]So, the new problem is: how can I get this to work? My idea to be tested at the moment is to change the name of union to something like.. 'sneaky'. THEN, I can make a new method called union, and all that union does is call sneaky. how does that sound? It doesn't solve the spirit of the problem, but it does work around it nicely.[/strikethrough]
(yes, I know there is no strikethrough on this)

So, I just looked at it, and my union is set up SO WELL that I just can't force myself to do ^^. Now, I'm back to the original problem, but with more info, and a clearer question.

I have MySet union(MySet s){...}

When the input MySet is first created, it's done by:
MySet s = new MyArraySet();

What I need to do, is turn the input MySet s into a MyArraySet. How would I do this?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to solve the wrong problem. You need to make MySet into a sufficiently rich interface so that union() can be implemented using methods of the interface. For example, if MySet had a "toArray()" method (and I'm not saying that's the best way to do it, just an easy way!) then union() could be implemented like



Instead of "toArray()", you might have a method "size()" and a method "get(int)" to allow a client to access each element of the set, or (better) implement something analogous to java.util.Iterator, but with int instead of Object.
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I will try that.

Since I have to declare sets as MySet s = new MyArraySet(); I thought that there would be a simple way to turn a set into MyArraySet. I do have a size() method, so I'll try making a get(int) method and working that way, thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!