' CompSci 201: Discussion 3 - 02/06

Discussion 3

Pre-discussion Warmup

  1. Import the code from git@coursework.cs.duke.edu:201spring17/class_03_sets.git
  2. Complete the Set Operations classwork.
  3. Review the Thesaurus APT.


Install checkstyle plugin and configure with compsci201cfg.xml configuration file.
  1. Install the plugin
  2. Enable automated style checking for a project
  3. Configure checkstyle to use our compsci201cfg.xml configuration file.

Unit Testing

To test your SetOperations class you're given testing code. This code tests individual methods in your class, these tests are called unit tests and so you need to use the standard JUnit unit-testing library with the SetOperationsTest.java file to test your implementation.

To choose Run as JUnit test first use the Run As option in the Run menu as shown on the left below. You have to select the JUnit option as shown on the right below. Most of you will have that as the only option.

run as

There are three tests in SetOperationsTest: one each for the correctness of union, intersection, and difference methods.

If the JUnit tests pass, you'll get all green as shown on the left below. Otherwise you'll get red -- on the right below -- and an indication of the first test to fail. Fix that, go on to more tests. The red was obtained from the code you're given. You'll work to make the testing all green.

green junit   red junit

Currently, SetOperationsTest includes one test each for the union and intersection and none for difference. Each test uses the JUnit assertEquals method. An assertion is a statement that allows a programmer to reason assumptions he or she makes in the program. In this case, assertEquals is testing whether the 3rd argument, the actual value, is equal to the 2nd argument, the expected value. If values are not equal (i.e., .equals returns false), then the assertion and the test fails. The first argument is the message to print if the asertion fails.

In this section, you will add more tests to SetOperationsTest. Go through these problems as a group taking turns.

  1. Using testUnion and testIntersection as models, add a test to testDifference using the sets test1, test2, or test2.
  2. Add at least one more test to each of the testUnion and testIntersection methods. You will need to create new data sets (like test1) as necessary. with whatever data you like to show that your tests work.
  3. Add assertions to testUnion and testIntersection to test the union and intersection methods on lists of sets.

Using the Debugger


Using the debugger, set a breakpoint in your intersection method and examine the values of the variables a, b, and any local variables in the Variables view.

Discussion Exercises: Thesaurus

The Thesaurus problem asks you to find common words among a set of entries. There are many different approaches to this problem. This lab outlines a particular approach that addresses conversions between different kinds of Collections, basic Set operations, taking advantage of the ordered nature of a TreeSet, and decomposing a problem into functions.

The starter code is available via Git (201spring17/class_03_sets) or https://coursework.cs.duke.edu/201spring17/class_03_sets/blob/master/src/Thesaurus.java.

Assume you have the following entries

{"C B A", "F D E", "I G H", "B G C E F"}
what should edit return? Choose the correct answer in the Sakai quiz.

Complete the remainder of the problems in your group with each member taking turns taking the lead.

  1. Complete the method sToSet that converts a String of the form in the assignment ("B G C E F") to a Set.

  2. Complete the complementary method collToS that converts a Collection to a String of the form above. How would you call collToS on a TreeSet?

  3. Adapting your work from the Set Algorithms classwork, complete:
    1. numInCommon so that it returns the number of elements in common between two sets, a and b
    2. union so that it returns the union of two sets, , a and b.
    Note: Your methods should not change a or b.

  4. Add code to the main method to test out your sToSet, collToS, numInCommon, and union methods.

    A better extension would be to write JUnit tests. Check out the Set Algorithms classwork or this tutorial.

  5. As a group, using the methods above, sketch out on paper a solution to edit for the APT. Start with the following:

    Convert entry to an ArrayList of sorted Sets: entrySet


Submit answers to questions in your Sakai Quiz before the end of your discussion. You will submit Thesaurus.java as part of assignment APT 2.
Last modified: Mon Feb 6 09:48:42 EST 2017