Part 1:  Set Operations

 

Assume A = {1, 2, 3, 4};  B = {3, 4, 5, 6};  C = {2, 4, 6, 8};  D = {x | x in Z+ and x is even};

E = {x | x in Z+ and x is odd}  (all as sets of positive integers)

 

Compute:

 

a)  A u B u C

 

 

 

 

 


b)  A u B

 

 

 

 

 


c)  A  n  B

 

 

 

 

 

d)  Z+  -  D

 

 

 

 

 

e)  A  n  (B u E)

 

 

 

 

 

f)  (A n B) u (A n E)

 

 

 

 

g)  B – A

 


Part 2

 

a)  Use your knowledge of simpler cases to find the formula for  |A u B u C u D|

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b)  Check your answer for the cases where

            A = {1, 2, 3, 4}

            B = {3, 4, 5, 6, 7, 8}

            C = {2, 4, 6, 8, 10}

            D = {1, 4, 7, 10}

 


Part 3:  Power Set

 

 

For a set A, the power set of A, written P(A), is the set of all subsets of A. 

For example, if A = {a, c, k}, then

 

P(A) = { {}, {a}, {c}, {k}, {a,c}, {a,k}, {c,k}, A}

 

(note {} is the empty set)

 

a)  Show how the elements of P(A) (i.e., the subsets of A) can be represented by binary sequences of length three.  (Which subset does each binary sequence of length three go with – there should be a one-to-one correspondence.)

 

 

 

 

 

 

 

 

 

 

b)  Explain how (a) generalizes for any finite set.

 

 

 

 

 

 

 

 

 

 

 

 

 

c)  Use (b) to show that  |P(A)|  =  2|A|

 


Part 4:  Computer exercise.

 

Complete the Set and Predicate classes specified below.

 

The class SetOfSet defines sets developed from the simple axioms discussed in class. The only set that can be constructed is the empty set. All other sets (of type SetOfSet) must be constructed using the SetOfSet methods:

·      add

·      powerSet

·      unionWith

·      intersectWith

·      eltsSatisfy

 

The SetOfSet class has the methods contains and equals already defined. You are to complete the four methods other than add listed above.

 

A predicate is boolean function about sets. In order to define the SetOfSet methods forAll, exists, and eltsSatisfy, we must be able to use such a predicate as a parameter to these methods. Since functions (methods) are not “first-class” objects in Java, we cannot pass them as parameters. However, we can pass objects that encapsulate such functions, using the interface SOSPredicate1. (The one indicates a single parameter for the predicate.)

 

The class IsEqualTo shows how we can implement a SOSPredicate1 with an instance variable set by its constructor to encapsulate the predicate that the parameter of the predicate is equal to the instance variable.

 

We would like to build a predicate that captures the notion that a set, A, is a singleton – it has only one element. We can do this in logic with the predicate:

There exists an element of A, x, such that all elements of A are equal to x.

1.      We have captured the “equal to” part with our Java predicate IsEqualTo.

2.      We can capture the “all elements of A are equal to” by defining a new predicate that uses IsEqualTo and the SetOfSet method forAll. Call this new class SetEqualsAllElts.

3.      We can then define another Java predicate that captures the full predicate above using SetEqualsAllElts and the SetOfSet method exists. Call this new class IsSingleton.

 

Set building and power set operations are tested  by the driver SetOfSetTest. Add tests for unionWith and intersectWith to this driver and test your methods.

 

The predicate IsSingleton and the method eltsSatisfy are tested in the driver SOSPredicatesTest. Add tests for the predicate IsEqualTo and test your predicates.

 


Code to be written.

 

1. Complete these methods in the class SetOfSet:

·        powerSet

·        unionWith

·        intersectWith

·        eltsSatisfy

 

2. Add tests for unionWith and intersectWith to the driver class SetOfSetTest and test your work.

 

3. Complete the predicate classes SetEqualsAllElts and IsSingleton.

 

4. Add a test forIsEqualTo to the driver class SOSPredicatesTest and test your work. (A test for IsSingleton is already there.)

 

Extra credit: Develop a predicate that tests whether a set is an ordered-pair. Recall from class discussion that an ordered pair <a, b> is represented by the set { a, {a, b} } (a and b must be sets, of course). It is probably necessary to have a predicate that tests whether a set has exactly two elements. I have not worked through this myself, so I am not sure of the details.

 

Second extra credit: If you succeed at the first, develop a predicate that tests whether a set is the first item in an ordered pair.

 

The starting code for this project is in the zip file: Lab1Start