From 0c95c28bc4ad63961fa1344ba6267a7f560e44c2 Mon Sep 17 00:00:00 2001 From: peet Date: Sat, 15 Jun 2024 23:30:02 +0200 Subject: [PATCH] testing --- .../uebung/uebung09/collection/SetTest.class | Bin 0 -> 3094 bytes bin/uebung09/collection/BigSet$1.class | Bin 0 -> 1163 bytes bin/uebung09/collection/BigSet.class | Bin 0 -> 2825 bytes bin/uebung09/collection/EmptySet$1.class | Bin 0 -> 866 bytes bin/uebung09/collection/EmptySet.class | Bin 0 -> 2334 bytes bin/uebung09/collection/SetFactory.class | Bin 919 -> 1361 bytes bin/uebung09/collection/SingeltonSet$1.class | Bin 0 -> 1053 bytes bin/uebung09/collection/SingeltonSet.class | Bin 0 -> 2410 bytes .../uebung/uebung09/collection/SetTest.java | 76 ++++++++++++++ src/uebung09/collection/BigSet.java | 94 ++++++++++++++++++ src/uebung09/collection/EmptySet.java | 71 +++++++++++++ src/uebung09/collection/Set.java | 4 +- src/uebung09/collection/SetFactory.java | 22 ++-- src/uebung09/collection/SingeltonSet.java | 77 ++++++++++++++ 14 files changed, 333 insertions(+), 11 deletions(-) create mode 100644 bin/test/uebung/uebung09/collection/SetTest.class create mode 100644 bin/uebung09/collection/BigSet$1.class create mode 100644 bin/uebung09/collection/BigSet.class create mode 100644 bin/uebung09/collection/EmptySet$1.class create mode 100644 bin/uebung09/collection/EmptySet.class create mode 100644 bin/uebung09/collection/SingeltonSet$1.class create mode 100644 bin/uebung09/collection/SingeltonSet.class create mode 100644 src/test/uebung/uebung09/collection/SetTest.java create mode 100644 src/uebung09/collection/BigSet.java create mode 100644 src/uebung09/collection/EmptySet.java create mode 100644 src/uebung09/collection/SingeltonSet.java diff --git a/bin/test/uebung/uebung09/collection/SetTest.class b/bin/test/uebung/uebung09/collection/SetTest.class new file mode 100644 index 0000000000000000000000000000000000000000..1aa1f927983e176e95ef8adb7c446aa7f6522484 GIT binary patch literal 3094 zcmb7GTUQ%Z6#fng83+TVfl_I;(kp~pdbKS<loI2 zk{elmasoB|In%HVcR(Q25uXwWkJxj14eI$Meb+)@pNeWkYOr6RTFTB!VUr3)3J(Or zb5eLvMGYc6kv1%SqLk0-&ZL$#bs2=6)66N&G34(+80q(1M-jryDh?qkoh}$ffp~h4 zzzHRs*9=RbrQ`N?pdKR4nWxF73flr|DqnC{0u}{YZ%^(O{fImSww|B$@^q z)3!68SS*pPgycLKMLXgOIs}gR8QPYbQ7z}%&WegobO}Uqj;^^R<8Vhjy~BxQ9eR*Z z&`UCRP*HIjXP7y|)g6uICG@xtz2q8ZVl0r;)Hz&GaGvaY6|iYn#cQ}Ipe$&`34NKR z!t~EZaT!SkeFVFKb*Na>9alv^UKgm-ykDc5StO8f$83B`!5ac?8y+U?OewdJGWEP} zxvAxxUXTRJBoC<=#t1{7*WD45mY9|G(7m$tgvl5iAf;jyR~eO4Vs4WimGH&O5^}F( z`3*6-;MfmY<4mL_OO08&j4>5&;ySbKNAKy-`FLQq%=ey525fuBJX@Y4nYnQl6SzTy zIoonsmUMEU+=<_p$@r{PzNz9CGGwu6JR+G)z*rP-V_E_Gux%p&i=~3hbwVo`2|pRK zAXU7BnHP{!_e)F(d$D6|EIuWz-&XOimtmvm)wty0u8Q|$7NY?VPKkb7cdSK@2t%N` z689{+%^kf`Snukdo$U2*@6Q4DZ>8MspxT&WW_8oGt)OFN3ogj^Vw^pdh~6m{uQ`@f zn8-5$me&Ezn`$vgl|X&Q)pASYS|ONP(P3l$#n`>SY3%yrRR+cMbb?*gyJ;$|r+>$& zn3{}Ta&r2pA*ZGq`0171MNh-F?G{}}D~#*zf<0G^Vg(;4cqDLaW5yfS1A9qNq_uo@ zPNSlBZ+!}Vh>zI8`3;f4-{qtQCw=4BG;7Y(izm{yy;LeBx96@*vgzO?OQBqS@0-5M z-OrZyUSN4dGtCS;B)KnwsZY=lYkY!OtoaFA9#d9@ zm-yY_*=wS;W}Z5LR&NJ9$2&5xlHe-sRCoS{!y885*b1l zKk~5Hda$+Zpdhkip5k$Ot-=XDOYP8^AMkgaTOyZ^$JN% zVVSQRpHcEXuHi?F;TOIzJjV4hvR4DL58;P^?4$T@3)%jjB-`IxC%Xxe=jdA}`|zOP iKWQy7tSUUfL$0z}Kc?&xe9AyRr$l!87x)U_p!Gk7lHQ5{ literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/BigSet$1.class b/bin/uebung09/collection/BigSet$1.class new file mode 100644 index 0000000000000000000000000000000000000000..5479c6d61a86d0bdfd2b36debbed05ccfd17d771 GIT binary patch literal 1163 zcmaJ=+fEZv6kVqmhPH#PwSp8ZcWtLgLGgk`h!Tw?1rtknAn{>ZPRiggV`iqtZ}A&^ zz@)_BgCF2W8P}N>(nzt%x$U!Nowe8A{r%_XF90vGsUadT*_MsA(^_7uG+o=4&A@V< z$}6i?m%;pshL}L+*!*l(Y}08~-ZhSi&@d#B4WsSAvMbwx^vuBZ1cp5sv^_^23Wx)N zxF`K!TOe1v3a$zy>Q>7!`GFJ|9{^e3+pBI6HxwLM{`@kv)~*+;aO;+11seiW#s2|I z`vS2o_fRGva7)JsZX^-KZ5?Th3Pg*geRXq3hlb$<@|e^xA&|fRZ5?+}AgM}Mr7{Rf zKdUKBV^+hAz}Phq9dnqckJhE7qs7tyYpP1&Jsk-o1=2Okk-P1ahV=H#hD|rKHMeQn z`=)29b1xWEHVPC5%DbpIZPboTe^-8CCbZ%pg*cXUl(EPJoiLockkr=`{4QQBv0%!u zM>HCsa4DYgFZ(ToNU6?ILoO49go&S)Ca+k4G zxYsio)hXOZInv1R<|cRViHmTZG|Fel8b6RTqGuR0${mcKV@lu*3*UJk!F|qI#KJj& z9Jxj@!AOx2oowVl2?kdtip3uPDpz%vG&)!~#{+?HJayTI!kc^_L-LY=))Lj#z7i_^ zz_Vg1kZJ5IGDb}4>>%60!_zMN6M<7AqJ3JY=*Bel%~0`N-%EwwOV2{Q$kDpBP}uX{ ZqgAZ)9Ai7o;stlgO7*<@ZE!@8{|h>H^9ld} literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/BigSet.class b/bin/uebung09/collection/BigSet.class new file mode 100644 index 0000000000000000000000000000000000000000..9997eb49eeb5c5ad452166e7d16a1d3a03989d80 GIT binary patch literal 2825 zcmbVOU2_v<7(MT1OWJe`we~|OLV;@Y0hBKlNR2{iwVJkSO0^Y{ZSqCE~76viDS(J$ZLa4-l*;ZV`@^-61JOE=cstt#gn zDb~tvb=_@v@*ElM;I7wHI2zmAkk7%!_ohCmx6@C&BCpL#KljjZgIa@i(iuZcEAXnb zOOc%oCSx6@LK3G4Fm5dd-}LzuPU0s4GT5zz4k0^9s-{&qy z+p_3->+>n9{DNFQk(D2k!bJz~;9c&p7Tnd;S;DPU6wbu0Ds~n_@+o-<VxCuM{*$Zm8$}W^1ddgVpc2-JUYZXDH(y?}iMQEij@V zw;2=b0fz5}YO9*NyqA z+iYsSl!GfgyqC2cc1=uC8VP>-VDXp7BEJvt?~H{MKRg^e&qK5;(Gcw>Vi>P6@+Uqg zct4bVhLLRU3C6OgpJ3uwni4q7>j-RKQ@lD%9Ab%sI6}*mNy8+L@`(ka_VP^{TX=I% z)Ui&K=-EUKF^XGYtv09=A*h9LC}7|PPHqs`^kXI%I0wSykr0AR$Djy;wA-~vJBk!( zUS-bPY+>;}Y|l+*f5XIcWEAeqO+KQ{R6ZCElw;Isj(9fgFm7_94i@b>dQ~`@=`~EB z>2-!VrdNS?L1ra(s~g-GyKr0kaEry%;^7+Asz$iSazs7$3;OIw0zda0=PgKiJKT~1 zf0+Wf!d>ONz>h_3fH7PQ1+q(&_@v>bkOU=*;n~8DMj`Ko%sma&1r&3?<1ztcALsr? z;UO|vg7{G3Ij$=_#m7Hm;sw-3myw%WdMq}99-;o!f>=Ai$5~zeFG}J%yUz2k+YKUE z>>8lh8K8*eaKkcy*^KyP0HULBi1=j`8`gkqX$7mDSw0JAX^D>!quCr0Ka^oY%puY+ zQzUQ5ERw8clDXQ2Wwe836rXp;ZUQCs0;P?So6qp&UwrBo z`FdC5RHt!j9|__!jm0M=%B LyZlu6vE}(csT4IZ literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/EmptySet$1.class b/bin/uebung09/collection/EmptySet$1.class new file mode 100644 index 0000000000000000000000000000000000000000..d2706cf4bc1d2f262f206c4abecf508feaba85da GIT binary patch literal 866 zcmaJ<(M}UV6g{)scDL(dD2m~=NYJNM??bI#nmfBydX1>h|j4lIV{O!PA~*nHCr<0ulLma%HK zMyL8qSLphdg91bKgn#DEh^s;Kuzx}r2PKBtJez45H4n5%xQ-Ksk{-&mzDd08X~a8( z+?7h|J%-g_I<#@bP}q-;MHvh;J{-6nY*c)dQDv|L1E{p65}j<+7fFxzBSH4samb@1 zo=6jqlLcdsp*B^qzD4$&Ax}HvOy>fh$brg(xuDU$-3>yQyKNSo-8f4^@lG1!GgsYzWvUrA{ujK=+-UtSY3`Ae zN=t=NoJuwLAoMUkcJP#8Y1%A&Ji~JaS6&Zs{_2u96o}kEP)a2G5l>S=ZFD;#)$ij} zqei2#X%)e$RWl*_!)#h*c$B?Er)^mD0^a(@clhh)xciMF3-@R*5eAsYEbV@N_HZ9_ z6fr!Y{3XLn`Wl>Ne>M9=<-$azbzS)oj|lZR$1da^UQ=us+@Dzdc#bE(Z}F^5@VH~1 eB9?R1%9yE!Rf-E(Lme;ZWbS7;%@feFuyz6W+`DZ6 literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/EmptySet.class b/bin/uebung09/collection/EmptySet.class new file mode 100644 index 0000000000000000000000000000000000000000..73456b779458cdd08f90250d32622adbc4f8a1f7 GIT binary patch literal 2334 zcmbVOU2hXd6g^`*aqKuBPzX>`5(pnoQgCQVTVmHhLn17NAYltBeOo(Ivve~XdB>ID zH}TNIL!=`0wNign)pK|4#Ie_gveNE++?jLkz30x@|NQ&sUjQCqJB@_Gd|TJsrnz>% z(C|G^H>~TM!q&l|eO=Wyjg-RdE9W<-;5nvQcvgSK6={qsT!`DpEeaEr-D>U8?&g-l zm7Ta%UG$$HFo~SP%#LgH zZu_9FTQ#Td5%uhj-*CJaPRo^XXEJ49x`D#nY1`|=u6=2=E45ltoSAHDTQPxkOoK#= z`Q?$jxS?38ZS3@CvsnJf#!^U^fx@L6(wNAyH={6BwNB&Lr_Ny~rU}>TmSg=E<04{6 z)3&ZxsC4G(z~xv&M3BZJ1skQ;^}AUt;F_d*iE`yfha2h>xNn#mid9iayFsXYIR2S+ z((sLSTtgww=LgfW%q0o!iGl+uQy6O-7OKL%crRFp#dtZyqEH^XElS4Fy^)#xzI~~g z>PxXnh)CkApSCVY(%IivxE&Y#2-u}O3*sXVrtuvsGEU5@Yns}#zM&sktm6lf^ZpPC zc*N4$(+Aq{TqjAZkaG>|DTpo#iWs!(fws?n;Y~6$r$y0nVW?S-wyc;R7Ibv>vsJ&{ zYUpiOj`dXc61gMAp|Bl)v<6FOC`ac|=PJhN)~4qKfo5e+?&`ok)pDT(Smen|@*U%w zm7~Zu!@D%WQI=m`L7bTiZ&$@0Ft&1p=|9*>VwT_Wa59TC?8{Ovz*)ioU!0mb$5967 zF-Opd;6ezy#j!l+-r?fknEC*Q$@(p$kQ_nc^S;^Ei0O?#x#oiOU> zUG&xd46YABFJP^Me!?vA3v&T?X;b(H-yZYa3widKQSp<4t?#WK;ro8NjUKwLU{Y2a z$H7E&UFjlQF{CT~kFXx}@bo6_Q^NX@Mbb^*gVX675c=_F@jPTaDgN7$Wd76t+ Pmfz6@w(@N8P09E_wsONy literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/SetFactory.class b/bin/uebung09/collection/SetFactory.class index 4914357fd5eb699f4d1f18769e6667cb562321ce..4762368c8b4efa4c996e759fb95bd9ce20d28351 100644 GIT binary patch delta 565 zcmYjOJ5SqC6#kCw#EG3kXyTYq5=;n?2arN5B#KmtfuT|mNI|VKI>}W8BqmaPK}v3&%-o`M~Rno`3AI z*1!~U2}F=5Sf%=*``)efT(4W(Z5@iXzgd18oY5!K!~#k>776w!X`2a@F-cI}PAAw< zoR~TkJ|SBVu}y*|dZJIr2gl@0JZboD`)J1pRpfeIQ(s>qm5t=qcB)I?F|VNhQKg)vUEJSQ!=p8M zbcP=inCEU07D~u*T|iYvaX21(qQZU1FJfsA%bd?b3@e;Zk@+%QC5YN1R^Zul7BP0u VufTtDRcBu!-T>pwTVqtgi$BsFP&5Dl delta 264 zcmcb}HJx4H)W2Q(7#J8F8RXa*m>3!4CLd%HpSYHNqTc7pb&NTajhNO<=4Muy+`*h) z&%waQz|X+MAOKX)#30DP4y2VB7=a{^&$t0ZGH?M&Hn12!12K@_M;42XFm;6ma+ZU@j%3Amw>U_*6K3`KT~I*OrE4AMXo M7#U=MBol)y01B@atN;K2 diff --git a/bin/uebung09/collection/SingeltonSet$1.class b/bin/uebung09/collection/SingeltonSet$1.class new file mode 100644 index 0000000000000000000000000000000000000000..427fab013c6e9b51fce9303d52efd270e7444fcf GIT binary patch literal 1053 zcmah{T~8B16g|^jSX#EUl!_k|Em*KEO2Ib+Au7R0QtCqqiTbo%Chg*~OSZFyzoLJ@ z7auf%kZ7Xs{wU*_wFt;Vn@so4{W#~$z4zDeZ$AJ$!($66f%#Z=;-I_oq~V6XFJ0w@ zL8I*jUFoYZXiHUDwO|V551jW-!*_yi<5lN?c@{wIQ%j9iCp>6EHW!eVK+p&PE!U3=B-#7)L?CsMhv$ zq-ev!7&Dq)AY1WaM@G9&$CuRK3|+_Hb0SaQ2ToH97N`v6udGtA)pPnSd8`sTJ~!J| zj{9!MIXXuRl3=k~YyRItR;SxuH*nZ4iW#Zac6Zm(n8SSw^8)2z?Xpq90z2R-d8prV z4d228!d%R#6}Dryx8=)28K|veS03qcXRwG#flND$BUirgbYmtjufVcK5LmiS@Yi!? z{_A|h249xj3S2+zbCY(Y>V^9@meJsHQ0!n&rq5e)rJkksb`Z#D(|7uP$w7>@WM91u z`wC?a)8H9FiYv^S0{Oh2I&TKgIBZ6<$$F=foLqhB6chC`r0p=`(3(HxR=O$OH1RHH10==G%1qy#0kn@zGLzA8LB`3(G+0Pp5enw)H;hIQ7&|s kE>W^cMe^1Xs``L(2@iQUxnGa4LMuaRO|E~dJSoim0eHdUlK=n! literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/SingeltonSet.class b/bin/uebung09/collection/SingeltonSet.class new file mode 100644 index 0000000000000000000000000000000000000000..8624d52a7593c1ca0347425aa884e758940d0acf GIT binary patch literal 2410 zcma)6ZF3V<6n<{9ZIUHGi2@?DL|)8Gp`n5zGz6ixT1n}QDIJTSHpw+z-0WtuH;(wl zf8!@U;EYaY?2LZ*M>(9cH`y*p){>dceL44>=RD6j_x}6eUw;F5gzXd(0{MY#4&2VI zdzF^wII^W|&#g3Uw<8_pxecjOFa#D}SU*`6$8tNBo#qQVmBPHh`I){_2^hALU4dME z(r-;5)37_Pr3QT|klfv+o%yQm+NvgSwm9uj+M@$SI>!@Z-D^t|Ni1Y>0%s^dv9y@Uk}! zQaIMTN@>?9M4II2TgvNGv|>qf94OnVY>oEm%*B}o9U+A!O7^&1udSL`M4pxTNZ@+$ z*z~ldzWobVqFCAzNZI~Ix2IkP>*ths%X5`wyQJ03+R%}NbOcT|l-2ri+v<(vg(W5MSCF>B&BKIhpzft4AV#Nh;`O-V;~rAzlQYrE8GzP{cm zJP;@x-OOjMKj`(mzLM>o9``AgZsX^c?C~Uxhs5cbYj=A(m~7{}QXP2hd?6aNV4{jg zoay6yG97k}k`Mr_xv3lAj?Td8%fTzB&35 z{7li9p(i*2lSVp>EY0=KNt_}}lhz3u&GHbZ%gcWv_dBhUc%S}H(MT2;?#Jv>YZUlJ(nL{k@6ZYlToyxGRNmI8)?)h%N=)b>Uo#J9A|HBE>~QSU}z z#zJs++{6m`9My0viMCb9CU6U@A test = SetFactory.create(); + + Iterator i = test.iterator(); + + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertFalse(test.contains(a)); + assertEquals(0,test.size()); + assertTrue(test.isEmpty()); + } + + @Test + public void SingeltonSetTest(){ + + Set test = SetFactory.create(a); + + Iterator i = test.iterator(); + + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertTrue(test.contains(a)); + assertEquals(1,test.size()); + assertFalse(test.isEmpty()); + } + + @Test + public void BigSetTest() { + + Set test = SetFactory.create(a,b,c); + + Iterator i = test.iterator(); + + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertTrue(test.contains(a)); + assertEquals(1,test.size()); + assertFalse(test.isEmpty()); + } +} diff --git a/src/uebung09/collection/BigSet.java b/src/uebung09/collection/BigSet.java new file mode 100644 index 0000000..7de961b --- /dev/null +++ b/src/uebung09/collection/BigSet.java @@ -0,0 +1,94 @@ +package uebung09.collection; + +import java.util.Iterator; + +class BigSet implements Set{ + + private T item; + private Set rest; + + public BigSet(T item, Set rest){ + //TODO + this.item = item; + this.rest = rest; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + private boolean returned = false; + + private Iterator restI = rest.iterator(); + + @Override + public boolean hasNext() { + return restI.hasNext(); + } + + @Override + public T next() { + if (!returned) { + returned = true; + return item; + } else { + return restI.next(); + } + } + }; + } + + @Override + public int size() { + int count = 1; + count += rest.size(); + + return count; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object el) { + if(el == item) { + return true; + } else { + return rest.contains(el); + } + } + + @Override + public Set union(Set other) { + if (other == EmptySet.getInstance()) { + return this; + } else if (other instanceof SingeltonSet && !contains(other.iterator().next())) { + return new BigSet(other.iterator().next(), this); + } else { + return new BigSet(item, rest.union(other)); + } + } + + @Override + public Set add(T element) { + if (contains(element)) { + return this; + } else { + return new BigSet(element, this); + } + } + + @Override + public Set intersection(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'intersection'"); + } + + @Override + public boolean subsetOf(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'subsetOf'"); + } +} diff --git a/src/uebung09/collection/EmptySet.java b/src/uebung09/collection/EmptySet.java new file mode 100644 index 0000000..55aa694 --- /dev/null +++ b/src/uebung09/collection/EmptySet.java @@ -0,0 +1,71 @@ +package uebung09.collection; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class EmptySet implements Set { + + private static EmptySet INSTANCE; + + private EmptySet() {} + + @SuppressWarnings("unchecked") + public static EmptySet getInstance() { + if (INSTANCE == null) { + INSTANCE = new EmptySet<>(); + } + return (EmptySet) INSTANCE; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + @Override + public boolean hasNext() { + return false; + } + + @Override + public T next() { + throw new NoSuchElementException(); + } + }; + } + + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public boolean contains(Object el) { + return false; + } + + @Override + public Set union(Set other) { + return other; + } + + @Override + public Set add(T element) { + return new SingeltonSet(element); + } + + @Override + public Set intersection(Set other) { + return other; + } + + @Override + public boolean subsetOf(Set other) { + return true; + } + +} diff --git a/src/uebung09/collection/Set.java b/src/uebung09/collection/Set.java index 00171e8..d982b02 100644 --- a/src/uebung09/collection/Set.java +++ b/src/uebung09/collection/Set.java @@ -38,10 +38,10 @@ public interface Set extends Iterable { /** * returns the set resulting from adding the specified element to this set. * - * @param element an element (must not be null) + * @param elem an element (must not be null) * @return the set resulting from adding the specified element to this set. */ - Set add(E element); + Set add(E elem); /** * Returns the intersection of this set and the specified set. diff --git a/src/uebung09/collection/SetFactory.java b/src/uebung09/collection/SetFactory.java index f8cfeb3..fb96697 100644 --- a/src/uebung09/collection/SetFactory.java +++ b/src/uebung09/collection/SetFactory.java @@ -10,9 +10,8 @@ public class SetFactory { * @param the element type of the returned set. * @return the empty set. */ - static Set create() { - // TODO implement - return null; + public static Set create() { + return EmptySet.getInstance(); } /** @@ -22,9 +21,8 @@ public class SetFactory { * @param the element type of the returned set. * @return the singleton set containing the specified element. */ - static Set create(T element) { - // TODO implement - return null; + public static Set create(T element) { + return new SingeltonSet(element); } /** @@ -34,8 +32,14 @@ public class SetFactory { * @param the element type of the returned set. * @return a set containing the specified elements. */ - static Set create(T... elems) { - // TODO implement - return null; + public static Set create(T... elems) { + // BigSet rtn = new BigSet(elems[1], new SingeltonSet<>(elems[1])); + Set set = EmptySet.getInstance(); + + for(T elem : elems) { + set = set.add(elem); + } + + return set; } } diff --git a/src/uebung09/collection/SingeltonSet.java b/src/uebung09/collection/SingeltonSet.java new file mode 100644 index 0000000..afa3870 --- /dev/null +++ b/src/uebung09/collection/SingeltonSet.java @@ -0,0 +1,77 @@ +package uebung09.collection; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class SingeltonSet implements Set { + + private T item; + + public SingeltonSet(T item) { + this.item = item; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + boolean returned = false; + + @Override + public boolean hasNext() { + return !returned; + } + + @Override + public T next() { + if (!returned) { + returned = true; + return item; + } else { + throw new NoSuchElementException(); + } + } + }; + } + + @Override + public int size() { + return 1; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object el) { + return el == item; + } + + @Override + public Set union(Set other) { + //TODO + return null; + } + + @Override + public Set add(T element) { + if (element == item) { + return this; + } else { + return new BigSet(item, new SingeltonSet(element)); + } + } + + @Override + public Set intersection(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'intersection'"); + } + + @Override + public boolean subsetOf(Set other) { + throw new UnsupportedOperationException(); + } +}