00001
00030 class Set32 {
00031
00039 static int getEmpty() {
00040 return 0;
00041 }
00042
00057 static int insertElem(int theSet, int theElem) {
00058
00059 if ( 0 <= theElem && theElem <= 31 ) {
00060
00061
00062
00063
00064
00065
00066 theSet = theSet | ( 1 << theElem );
00067
00068 }
00069
00070 return theSet;
00071
00072 }
00073
00074
00085 static int union(int set1, int set2) {
00086
00087
00088
00089
00090
00091
00092 return set1 | set2;
00093
00094 }
00095
00105 static int intersection(int set1, int set2) {
00106
00107
00108
00109
00110
00111
00112 return set1 & set2;
00113
00114 }
00115
00116
00130 static int setminus(int set1, int set2) {
00131
00132
00133
00134
00135
00136
00137 return set1 & (~set2);
00138
00139 }
00140
00151 static int complement32(int set1) {
00152
00153
00154
00155
00156 return ~set1;
00157
00158 }
00159
00160
00175 static void alpha(String name, int theSet) {
00176
00177 boolean haveOneElement = false;
00178
00179 System.out.print(name + " = { ");
00180
00181
00182 for (int i = 0; i < 32; i = i + 1) {
00183
00184
00185 if ( (theSet & ( 1 << i )) != 0 ) {
00186 if ( haveOneElement ) {
00187
00188
00189 System.out.print(",");
00190 }
00191
00192 System.out.print(i);
00193 haveOneElement = true;
00194 }
00195 }
00196
00197 System.out.println(" }");
00198
00199 }
00200
00205 public static void main(String args[]) {
00206
00207
00208 int s1;
00209 int s2;
00210 int s3;
00211
00212
00213 s1 = getEmpty();
00214 s2 = getEmpty();
00215
00216
00217 s1 = insertElem(s1,1);
00218 s1 = insertElem(s1,5);
00219 s1 = insertElem(s1,31);
00220
00221
00222 alpha("s1",s1);
00223
00224
00225 s2 = insertElem(s2,0);
00226 s2 = insertElem(s2,5);
00227 s2 = insertElem(s2,7);
00228 s2 = insertElem(s2,9);
00229 s2 = insertElem(s2,11);
00230
00231
00232 alpha("s2",s2);
00233
00234
00235 System.out.println("Teste Vereiningung von s1 und s2");
00236 System.out.println("Erwartetes Resultat: { 0,1,5,7,9,11,31 }");
00237
00238 s3 = union(s1,s2);
00239 alpha("Resultat s1 union s2",s3);
00240
00241
00242 System.out.println("Teste Durchschnitt von s1 und s2");
00243 System.out.println("Erwartetes Resultat: { 5 }");
00244
00245 s3 = intersection(s1,s2);
00246 alpha("Resultat s1 intersection s2",s3);
00247
00248
00249
00250 System.out.println("Teste s1 \\ s2");
00251 System.out.println("Erwartetes Resultat: { 1,31 }");
00252
00253 s3 = setminus(s1,s2);
00254 alpha("Resultat s1 \\ s2 ",s3);
00255
00256
00257 System.out.println("Teste Complement von s2 in {0..31}");
00258 System.out.print("Erwartetes Resultat: { 1,2,3,4,6,8,10,12,13,14,15,16,17,18,19,");
00259 System.out.println("20,21,22,23,24,25,26,27,28,29,30,31 }");
00260
00261 s3 = complement32(s2);
00262 alpha("Resultat complement s2 ",s3);
00263
00264
00265 }
00266
00267 }