Lab: Set Secondary Methods
Objective
In this lab you will practice implementing instance methods for the
Set family of components. In particular, you will implement the add
and remove secondary methods in a new class that extends Set1L.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectSetSecondaryMethods. - Open the
srcfolder of this project and then open(default package). As a starting point you can use any of the Java files. Rename itSetSecondary1Land delete the other files from the project. - Follow the link to
SetSecondary1L.java, select all the code on that page and copy it to the clipboard; then open theSetSecondary1L.javafile in Eclipse and paste the code to replace the file contents. Save the file. - In the
testfolder of this project create a new JUnit test fixture as practiced in an earlier lab. Name itSetSecondary1LTest. - Follow the link to
SetSecondary1LTest.java, select all the code on that page and copy it to the clipboard; then open theSetSecondary1LTest.javafile in Eclipse and paste the code to replace the file contents. Save the file.
Method
- In
SetSecondary1L.java, complete the body of theremovemethod. The contract is available in theSetcomponent documentation. For this method you can use any of theSetmethods (except for calling theremove(Set<T> s)method inherited fromSet1L). In particular, you can use theremove(T x)SetKernelmethod. - Run the
SetSecondary1LTesttest fixture and test your implementation ofremove. Of course, some of the test cases foraddwill fail because the operation has not been implemented yet. Ignore those for now. Fix any problems inremoverevealed by the corresponding test cases. - Back in
SetSecondary1L.java, complete the body of theaddmethod. The contract is available in theSetcomponent documentation. For this method you can use any of theSetmethods (except for calling theadd(Set<T> s)method inherited fromSet1L). In particular, you can use theadd(T x)SetKernelmethod. - Again run the
SetSecondary1LTesttest fixture and test your implementation ofadd. Fix any problems inaddrevealed by the corresponding test cases.
Additional Activities
-
In
SetSecondary1L.java, paste and implement the following instance method (for this method you can use onlySetKernelmethods—including inherited methods):/** * Reports whether {@code this} is a subset of {@code s}. (A is a subset of * B exactly when every element of A is also an element of B.) * * @param s * the second set * @return whether {@code this} is a subset of {@code s} * @ensures isSubset = (this is subset of s) */ public boolean isSubset(Set<T> s) { assert s != null : "Violation of: s is not null"; assert s != this : "Violation of: s is not this"; // TODO - fill in body } -
In
SetSecondary1LTest.javaadd appropriate test cases for the new method. -
Run the
SetSecondary1LTesttest fixture and test your implementation ofisSubset. Fix any problems inisSubsetand inSetSecondary1LTestuntil you are confident that your implementation ofisSubsetworks and that your test cases adequately test the method's behavior.