For the purposes of the rest of this lab, a set is a collection of objects, all of the same type, containing no duplicates. Various operations and functionality will be associated with sets.
By creating a set interface you are defining what you mean by a set and deciding in advance many of the purposes for which you need a set data structure. The Java API does it one way. For this week's lab and lectures I have done it another way. You can look at Bailey's book and see yet another way. You, of course, can do better:
Despite your criticisms, I am going to go ahead with my interface anyway. That means you will have to also!
Save my interface with the name SetInterface.java. It should compile once you create classes for the two Exceptions mentioned. Here is my code for
public class NoSuchElementException extends Exception { }I know you can do the other yourself.
Now look at this testing program. If you save it as Test.java you should find it compiles too. That's because the Test program does not yet instantiate a set. Look for two commented regions:
/* You will need to insert a line testMe = new SetImplementation(); where SetImplementation is a class implementing SetInterface */
and
/* You will need to add a line other= new SetImplementation(); */
Remove the comments and you'll get an error message when you try to compile:
Test.java:23: cannot find symbol symbol : class SetImplementation location: class Test testMe = new SetImplementation(); ^ Test.java:85: cannot find symbol symbol: class SetImplementation other= new SetImplementation (); ^ 2 errors
In order to fix those errors, we need to create a SetImplementation class. In the next section you will create such a class. For now, I want you to look at mine. It implements the interface using a java.util.HashSet as the underlying data structure. Saving mine as SetImplementation.java you can compile it. Now try to compile the Test.java code that failed to compile a moment ago. It should now compile and you can run it. If you do, you may get a window like this:
I got to this stage by:
Now if I click the union button, the window becomes
Play with the program and get comfortable with how it enables you to test some of the functionality of my SetImplementation code.