|
public class sets_2 {
public static int set1 = 0;
public static int set2 = 0;
public static int set3 = 0;
/*------------------------------------------------------------*/
/**
* Function isetDiff(int set1, int set2)
* returns the set difference of sets 'set1' and 'set2',
* i.e. 'set1' \ 'set2'
*/
static int isetDiff(int set1, int set2) {
int a=0;
a = set1 ^ set2;
return a;
}
/*------------------------------------------------------------*/
/**
* Function isetInsertElement(int theElement, int theSet)
* inserts an element 'theElement' into the set 'theSet'
* and returns the resulting set.
*/
static int isetInsertElement(int theElement, int theSet) {
theSet = theSet | (1<<theElement);
// System.out.println(»versuch1«+theSet);
// Fehlersuche
// theSet = theSet | (1<<3);
// funktioniert theSet?
// System.out.println(»the Set schifft 3! «+theSet+»?«+theElement);
// funktioniert theElement?
// set1 = set1 | (1<<theElement);
// System.out.println(»versuch 2 !«+theSet+»?«+theElement);
return theSet;
}
/*------------------------------------------------------------*/
/**
* Function isetIntersection(int set1, int set2)
* returns the set intersection of sets 'set1' and 'set2'.
*/
static int isetIntersection(int set1, int set2) {
int b=0;
b= set1 & set2;
return b;
}
/*------------------------------------------------------------*/
/**
* Function isetIsEmpty(int theSet) returns true whenever
* the set 'theSet' is empty.
*/
static boolean isetIsEmpty(int theSet) {
boolean isetIsEmpty=true;
if (theSet !=0) isetIsEmpty=false;
if (isetIsEmpty == true) System.out.println(»TheSet ist leer.«);
else System.out.println(»Es ist mindestens ein Element vorhanden.«);
return isetIsEmpty;
}
/*------------------------------------------------------------*/
/**
* Function isetPrint(int theSet, String theSetsName)
* prints all elements of 'theSet' together with the string
* 'theSetsName' (for identification purposes).
*/
static void isetPrint(int theSet, String theSetsName) {
for (int i=0;i<32;i++){
int theSetb=0;
theSetb = theSet;
theSetb = theSet & (1<<i);
if (theSetb !=0) System.out.print(i+» in «+theSetsName+», «);
}
}
/*------------------------------------------------------------*/
/**
* Function isetUnion(int set1, int set2)
* returns the set union of sets 'set1' and 'set2'.
*/
static int isetUnion(int set1, int set2) {
int a =0;
a = set1 ^ set2;
return a;
}
/*------------------------------------------------------------*/
public static void main(String[] args) {
// Define three variables representing empty sets
// Check if set1 is empty and print result
isetIsEmpty(set1);
// Insert element 15 into set1
set1 = isetInsertElement(15,set1);
// Insert element 5 into set1
set1 = isetInsertElement(5,set1);
// Check if set1 is empty and print result
isetIsEmpty(set1);
// Output the elements contained in set1
isetPrint(set1,»set1«);
System.out.println(» «);
System.out.println(» «);
// Insert element 5 into set2
set2 = isetInsertElement(5,set2);
// Insert element 8 into set2
set2 = isetInsertElement(8,set2);
// Calculate set3 = union of set1 and set2
set3 = isetUnion(set1,set2);
System.out.println(»Union of set1 and set2«);
// Output the elements contained in set3
isetPrint(set3,»set3«);
System.out.println(» «);
System.out.println(» «);
// Calculate set3 = intersection of set1 and set2
set3 = isetIntersection(set1,set2);
System.out.println(»Intersection of set1 and set2«);
// Output the elements contained in set3
isetPrint(set3,»set3«);
System.out.println(» «);
System.out.println(» «);
// Calculate set3 = difference of set1 and set2 (set1 \ set2)
set3 = isetDiff(set1,set2);
System.out.println(»Difference of set1 and set2«);
// Output the elements contained in set3
isetPrint(set3,»set3«);
System.out.println(» «);
}
}
|