From fa85372c139b477a0148f94d39f5e70ad9dce8f3 Mon Sep 17 00:00:00 2001 From: peet Date: Thu, 13 Jun 2024 15:37:45 +0200 Subject: [PATCH] initial commit --- bin/uebung09/chess/Board.class | Bin 0 -> 1081 bytes bin/uebung09/chess/ChessApp.class | Bin 0 -> 1401 bytes bin/uebung09/chess/Color.class | Bin 0 -> 554 bytes bin/uebung09/chess/Knight.class | Bin 0 -> 823 bytes bin/uebung09/chess/Piece.class | Bin 0 -> 1078 bytes bin/uebung09/chess/Queen.class | Bin 0 -> 820 bytes bin/uebung09/collection/Set.class | Bin 0 -> 999 bytes bin/uebung09/collection/SetFactory.class | Bin 0 -> 1034 bytes bin/uebung10/logo/DarkForest.class | Bin 0 -> 447 bytes bin/uebung10/logo/Demo.class | Bin 0 -> 2990 bytes bin/uebung10/logo/HanselGretelFrame.class | Bin 0 -> 870 bytes bin/uebung10/logo/Turtle.class | Bin 0 -> 907 bytes src/uebung09/chess/Board.java | 54 ++++++++ src/uebung09/chess/ChessApp.java | 82 ++++++++++++ src/uebung09/chess/Color.java | 5 + src/uebung09/chess/Knight.java | 28 ++++ src/uebung09/chess/Piece.java | 38 ++++++ src/uebung09/chess/Queen.java | 40 ++++++ src/uebung09/collection/Set.java | 61 +++++++++ src/uebung09/collection/SetFactory.java | 41 ++++++ src/uebung10/logo/DarkForest.java | 5 + src/uebung10/logo/Demo.java | 19 +++ src/uebung10/logo/HanselGretelFrame.java | 56 ++++++++ src/uebung10/logo/Turtle.java | 35 +++++ .../iterator/Array2dIteratorTest.java | 43 ++++++ .../iterator/SkipNullIteratorTest.java | 122 ++++++++++++++++++ 26 files changed, 629 insertions(+) create mode 100644 bin/uebung09/chess/Board.class create mode 100644 bin/uebung09/chess/ChessApp.class create mode 100644 bin/uebung09/chess/Color.class create mode 100644 bin/uebung09/chess/Knight.class create mode 100644 bin/uebung09/chess/Piece.class create mode 100644 bin/uebung09/chess/Queen.class create mode 100644 bin/uebung09/collection/Set.class create mode 100644 bin/uebung09/collection/SetFactory.class create mode 100644 bin/uebung10/logo/DarkForest.class create mode 100644 bin/uebung10/logo/Demo.class create mode 100644 bin/uebung10/logo/HanselGretelFrame.class create mode 100644 bin/uebung10/logo/Turtle.class create mode 100644 src/uebung09/chess/Board.java create mode 100644 src/uebung09/chess/ChessApp.java create mode 100644 src/uebung09/chess/Color.java create mode 100644 src/uebung09/chess/Knight.java create mode 100644 src/uebung09/chess/Piece.java create mode 100644 src/uebung09/chess/Queen.java create mode 100644 src/uebung09/collection/Set.java create mode 100644 src/uebung09/collection/SetFactory.java create mode 100644 src/uebung10/logo/DarkForest.java create mode 100644 src/uebung10/logo/Demo.java create mode 100644 src/uebung10/logo/HanselGretelFrame.java create mode 100644 src/uebung10/logo/Turtle.java create mode 100644 test/uebung/uebung09/iterator/Array2dIteratorTest.java create mode 100644 test/uebung/uebung09/iterator/SkipNullIteratorTest.java diff --git a/bin/uebung09/chess/Board.class b/bin/uebung09/chess/Board.class new file mode 100644 index 0000000000000000000000000000000000000000..0c92eac39b1436a5d4c976b2b7e1f79f643a8c7a GIT binary patch literal 1081 zcmb7CU2oGc6g}>`r43`(D}- zVfCC}azExt zWz3CKiJNLQipAu$Yd25E!VN_bb4{3(2NygNZnyN`bwedGH&Mo&a1)GOLs;=OCHu43 zYx8^4BHP9a?k(dE+6?Pg*2>NejFw5XU+8Z2Wg8bjkS4I@&F7~B=nWfq$k6#`6Bz82q?wYI!79ggL^Iy@j*olQQ9Yv;$k%rpPrzt;m%O4=Nb! z3I>Kpw`izhXjd{k#*-Nhh3nfj>{K$i|DoYoB|`_@84Wj`ta9<9l3|B**YNZ&bAH>adPeF=&venPR)t2et7aRJ3W0{JMi+wZ(; zWBwIXIjT|UM7@e?pFqY{Dd)@~Fdud5dxoj(MB)Cmr?SMPX73LAsWK~$UVN%Z!T(%uCGMTj_=W?(P zLxt%o$KL2o6A~)ErXB2>4j69;X{Z>2TZk@MIXZ zYRNNJh7u?Qy#IEZ9q3Hfm}*i7r?IK}k&c;M3hzrv)&z59Ebi*aEe7d*E{lg>3+9v%HGJy-_VCgfsN{e!>CxngPsit+gG1l=~E|BTB|(UwvX1)+GU3LevK*p2QcC7Z4OGeJS`{wQ&F zRrJuyOlI=T~MHnxfq!}7hDiB2qZ*g4l( z!cZQ^B6wif9L}rh67U!)CmAX|rIqUwxjX!8GICbwuzR_8%TPKq6In%rq4@`#TWf3$ z%M9*DTbY`8CMP^F$yCL{DWmz+nsF?X6YkfqBgrQ+h=nC)O1eZSdApdQ%_l~tTpPy| z;ev=eV&&6R2JS!mJH-BXk?&&>D-G1J%CIpohKHoKpX%TOPwPz#q?y7>Q8zUbck_4d1iLoh7R-+6-_lu-f5 zS94E!kA&CB`xG4!m2>_p7Vlb1&uG3={{G|hR{&3NCqRo~SgCns(z_2PGE-$ad0}*#`2cN( z-L+4zw33Qp>yb9vKW4Z$nyo3OHn%QbuRgWHCGqj`_>7@FwTTL_&Cq`@&c!4bCY?NU z&bkm?2LHx5RoeVqC0yD?p>yH2HN0?ko~y+}9&MjyiYH3u!jYyB@`FefKU~5e^2Dl= z8|(Q(c$sleTJ^C|(ywZNLmxGlq6i@__t3!=8pmk%hwH>UZPK{r?$5MQFRR5|xl=(X z41<}KB0m#O*ZHqv+h@9D*jpb#Gu4$-_<19nvRi~|rA{Te`%JMVtPE&xR*<-XAtb0ks#F2m#w56N9V^L5liY zo$@n2hRM+P>c6|orVPr^7f zPZ=Kc%9-=g0yG-+PZ+90MpSNPWyH6^71O2_Btr9p>-9gH;oRX^EG%MdW4E zm&O{;u-WU+8&$NViJJ_&e>I|jS#L{Wv-Z{L$iuvY3%x$|Fn^_U7#bqr`cNkJ!;rxd zJRb7IWSWZGy42dDg8q9F9;r+V`7+F1aBO@(_pG`LV$nUVqJkRgw5p(irkxkz*!hZ` z>9UgDS4n@3q7P|Tjb`U7+V48cpRw}YYS(G3lLqu`(ActT2b|7?$Uw<2WWIDqg+_k>kqp^bCFP9zQ;{X5v literal 0 HcmV?d00001 diff --git a/bin/uebung09/chess/Queen.class b/bin/uebung09/chess/Queen.class new file mode 100644 index 0000000000000000000000000000000000000000..fd98e42f6fe268f28f1bedcafe2608da7d49ef12 GIT binary patch literal 820 zcma))PjAye5XIl7N$r|IXlMiFkF@P6xild4QjCxw0;y7jfR;*d+pI_N7H8L5ud9AD zB#_|FheE6!5K$5V2ak8gZ=Qa$yjvs`7uDWiFA%_O7QV;*gtrNk4GnXq1y3;9t@i67ML2Rt!UaBUo) z2`5wTyq2zVl5SD^8~SkG6h#Pey^R)bFzoinf4EMaReBm%+?}z~^s1aq$esz0!q6QX zDYA26Rh9oLHeIR;hTifO)R!7F1Tq!&E#(Xw{lO?ik756xBQu1~%vt#&Er3l5FH OGW6Cu-19vR-2Vxrjk4PS literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/Set.class b/bin/uebung09/collection/Set.class new file mode 100644 index 0000000000000000000000000000000000000000..90e4147296b524e341a773c6f61d92094347219e GIT binary patch literal 999 zcmaJ=O>fgc5S=Y)oSHyNpp=h7my{2u5)B6~G*+Yv1*uX*4+)jvwzd~{i?eI3*A;N! zZy|vMcYYLNY{!i$GPc&MnKv{0=I!{`?{7Z<-~b#8RtRjS+)HKn?8U%WQN(>ClpIXC zv7kX<{gj=tLBwP@IPy*rw4h0#yYwC!u9+8c0xj2%gcN2(;F8lnCeV1P0&c-&0-a_2 zSZk$QaD~8^H&XLNMQ1#qzM97(V%gC&)(Y#*_o=-$JK;3oe#A7!#LWM|LQZ>2nmrmQ zo=~X_oipQ~P=g8lV~jkj31x$Ol}y_P8#--Rhie3y?t=J+Gg(rd8ZBfv%v$M8gygT& zxySXa=(amiK8uc-7MZYzLJIsnGpEvm_pZKA467C zi3$*S?o3L~7cOPkuSE!~O+_e~Ni}j9)s}E)LuJ9y(h3$OFI{6yB2nLu_u(!o~l&){FTT@632KiyHElL?s&g) zw~JOr|GFxka<&WFIQOUuu2uYbTKHPHP;|6*(Vso`cd*}f+h5`8PqbE`i{B>30Iut~ zC;O*vzy@UdP1wx$TW~wy@4#J*+sfnaA$}jW&uxnKg9~^McFysKc-o^p^YNm>lRrB+ B2Alu@ literal 0 HcmV?d00001 diff --git a/bin/uebung09/collection/SetFactory.class b/bin/uebung09/collection/SetFactory.class new file mode 100644 index 0000000000000000000000000000000000000000..032e4b2f9f5bd631af1297214f76f54f1e5a697b GIT binary patch literal 1034 zcma))L2uJQ5QX2G&^RU~B~YLg+HEDI6p4fbLTVgELWrIsIK)&!J#98C$HnnRn{@>j z&io@Jkl@abLX49%jzkPSc6IR;GAazUT>IJ>ZCu=7 z`1~$0YO3Q4HRMu{Cs8c2I(%aEAXeix_bS0i@u8BjFr=9X`9Xw=ALgD8`B1AVPqgJ@ zVdaQh0@TL|xmg7+yJ@bFo(C73Rb0nShV4c#ci6WkN?xUU4^{wcjn~ zJ3-)geHZr`4*ol0hNr8&FS7s3+i`&(s#=K6Eip73XMeX+NYNnL1;x&;QqAk@)JSy_ zWk0T#uWvK^*Ej&}DeXK^fax5-L^EnCSIHX|^#QR1vgRWJ@AJWEwri zD@F|he@F$F+}t5J=v^)lR7Wad9yAbyd-_3ZrF}R=pr55ZPKhl`FjZO31uF<&yAFe-?)Oit*UqzreRPZuYOZLm}ZXT;DYHWp>Cxi$HZ{=K1J_5=a=aMXlj z2ocnWJK0Wey4RZ-&1HJ)^usBa?6JsVrsu>OA!tt(C1OD|cl9m|V$Dc0(iTKe>y5{* zH<+nHQ#RqQE!@wp?{2Ciup_dg2B=t}8q{s=+3ISS1|Q(Rw1YQjew9qi?v4SwPRhi6 MK^soX<^TWy literal 0 HcmV?d00001 diff --git a/bin/uebung10/logo/Demo.class b/bin/uebung10/logo/Demo.class new file mode 100644 index 0000000000000000000000000000000000000000..66160764f25d2c5c09a31f007c2a2eb0a5cf0969 GIT binary patch literal 2990 zcmds(QE$^Q5XaA@U7JwoDBFNHv!}9$bx1rhgoGH3hYFC|RYE*D?kV+f?BF;9J{b~7 z@XnXun;Ou27`W%QEuY92SaURJ3PX~ zwxP*j^HiLPp%x|{zKBjydIlR0lu`a6gG+-uQwGh)c7|;T81$CfC(c>dfy=CQaAF*0 zR-fUFOPi!h3$Luv=iy1f8^F(+#=boha zKBYkZmYxT{zt|H7W1n~~g(>z$$o~Z3Ef?H>NRcG?Hb*0|MD`NRk$q=MRL8bNFv(qQ zeAM!AYnc=k?@3xxnWd$>q7JAM%bL1mMHx+1ro43}=le{U4E3=?kGj&15IS(B3oW?H zV0$oHGIi{oGVx)-+}=nTe4Zx}x``l+62C`Q3OyB074zSVO+Qx|gRRjyAD3`HBycA} z83sFp*B5=H13Rqwy#l;z**R;f#M0)g<>A&csVaOc{oN}fQQH;fT*WBg8nh^T zhR?7Bc?Pvl{G4BU@r6*K_wLGHWYD;o+~sE4(*|S9p!ngadvZ2lz`6{UI$Fh+D?8#c z!QyK=KYbYFhsP(U3|9N;0Up1w-oP3?#WsZ^ZYK;@=Apz2>GefyDN-4TP;r|WWYGL0 zd~l6v!Xs=Src{BpF@r+H6-owc8RmC8?^7?KiU&dod&pqDH7Ajrx}|!k>Gtl3XgWF| zJsvPv&dSZ=Z;a8V3=bKYGexayd5(eW>scfuw?Zp7GI}g=_R4iC!wI?p`jQ*MMBJb9 z5Zz`feA5jy#;(%VopI|=TuV^AjL^5&p$onDvK+^O20X69I@GDAzEF6Y%*JSjoDQU0 z>uaCO5jP^Wubg>15v0Xde?j$+dnZAhu|Xbm3~`cpt!{UBREBNJ{cod`JJ5;ou_w|( z>VHkuUMe7L(FEL~uR!kxl%P!e3e80h9I~shMEe@tg=O-uWd2pMYd7p1+=I0&Mi&Ql zLf$89pLlchcfLa7V{PLzJo=ttp3qt(Ogej;v{tjd11{B)`kL^R)&)a>d~=3pw=nGA L%CJK@Ie7jHH5bo- literal 0 HcmV?d00001 diff --git a/bin/uebung10/logo/Turtle.class b/bin/uebung10/logo/Turtle.class new file mode 100644 index 0000000000000000000000000000000000000000..c40331a94f343a86497c0919be6ab52ff6e736b5 GIT binary patch literal 907 zcmb7>%T60H6o!8fA(tT#nnGwP7bmN*A+6L^sVX6+T}ADJ6qThju_mUD9XTFEpR7_V z!M+bw^~?lB7E_@te0*&G=Q|hw`TOe@z$rQv1PoiL9Hu6D^SY;PVta$sc`Yj_F>GFm zYthrfB)u=gD;awRc0((o^@CxR9<*dAzgI^259D;CC_1`eD4p36A=VihkBE=XSyx4a z!F@GO&aA$cBOcqyRB7RrHGJyqP|L|X9@Yk9$wxBQ!jWbw;_o7nypt8!;Ug<&+*r>i z!pCFoNh)uqRNkWWp{e6MCJa%>vpQ;MGHiGIk5!#}r%ZB^Ro&<-BR{8;p>zX5g)lVx zHWvCqIF+66FH3%`W(=+VLyE<@olx_M^xqg(x<^qJyA16oyPd~eGE^z_bUxIQPo1HM zZa&Z;u?*q4O6?UH-2LCE{}OmHdBJu7TJ34huB6dzuUo!1wy8q6J8b&HVO%^mLN0>342QrT7`st a94vH^&F%kP>=qIZmmnMz5;$cI(EbOA@15`f literal 0 HcmV?d00001 diff --git a/src/uebung09/chess/Board.java b/src/uebung09/chess/Board.java new file mode 100644 index 0000000..2cfa92a --- /dev/null +++ b/src/uebung09/chess/Board.java @@ -0,0 +1,54 @@ +package chess; + +import java.util.ArrayList; +import java.util.List; + +public class Board { + private final Piece[][] field = new Piece[8][8]; + private final List pieces = new ArrayList<>(); + + void add(Piece piece) { + if (piece.getBoard() != this) + throw new IllegalArgumentException("wrong board"); + final Piece existing = pieceAt(piece.getRow(), piece.getCol()); + if (existing != null) + throw new IllegalArgumentException("already occupied by " + existing); + field[piece.getRow() - 1][piece.getCol() - 1] = piece; + pieces.add(piece); + } + + public void printBoard() { + System.out.println(" 1 2 3 4 5 6 7 8"); + System.out.println(" +---+---+---+---+---+---+---+---+"); + for (int row = 1; row <= 8; row++) { + System.out.print("" + row + " "); + for (int col = 1; col <= 8; col++) { + final Piece p = pieceAt(row, col); + final char c = p == null ? ' ' : p.charRep(); + System.out.print("| " + c + " "); + } + System.out.println("|"); + System.out.println(" +---+---+---+---+---+---+---+---+"); + } + } + + public Piece pieceAt(int row, int col) { + return field[row - 1][col - 1]; + } + + public void check() { + for (Piece p1 : pieces) { + System.out.println(p1.toString()); + for (Piece p2 : pieces) + if (p1 != p2) + if (p1.canCapture(p2)) + System.out.println(" can capture " + p2.toString()); + else + System.out.println(" cannot capture " + p2.toString()); + } + } + + public String toString() { + return pieces.toString(); + } +} diff --git a/src/uebung09/chess/ChessApp.java b/src/uebung09/chess/ChessApp.java new file mode 100644 index 0000000..3b6442c --- /dev/null +++ b/src/uebung09/chess/ChessApp.java @@ -0,0 +1,82 @@ +package chess; + +import java.io.InputStream; +import java.util.NoSuchElementException; +import java.util.Scanner; + +public class ChessApp { + private static final String HELP = "h"; + private static final String CHECK = "c"; + private static final String ABORT = "a"; + private static final String WHITE_QUEEN = "q"; + private static final String BLACK_QUEEN = "Q"; + private static final String WHITE_KNIGHT = "n"; + private static final String BLACK_KNIGHT = "N"; + + private final Scanner scanner; + private final Board board; + + public static void main(String[] args) { + new ChessApp(System.in, new Board()).playChess(); + } + + private ChessApp(InputStream in, Board board) { + scanner = new Scanner(in); + this.board = board; + } + + private void playChess() { + board.printBoard(); + commandLoop(); + System.out.println("Terminated"); + } + + private void commandLoop() { + while (true) { + System.out.printf("Type in command (%s for help):%n", HELP); + try { + final String command = scanner.next(); + if (ABORT.equals(command)) return; + switch (command) { + case HELP -> help(); + case CHECK -> board.check(); + case BLACK_QUEEN -> addQueen(Color.black); + case WHITE_QUEEN -> addQueen(Color.white); + case BLACK_KNIGHT -> addKnight(Color.black); + case WHITE_KNIGHT -> addKnight(Color.white); + default -> System.out.println("Invalid command " + command); + } + } + catch (IllegalArgumentException ex) { + System.out.println(ex.getMessage()); + } + catch (NoSuchElementException ex) { + return; + } + } + } + + private void addKnight(Color color) { + final int row = scanner.nextInt(); + final int col = scanner.nextInt(); + new Knight(color, board, row, col); + board.printBoard(); + } + + private void addQueen(Color color) { + final int row = scanner.nextInt(); + final int col = scanner.nextInt(); + new Queen(color, board, row, col); + board.printBoard(); + } + + private void help() { + System.out.println("Commands:"); + System.out.println(ABORT + ": terminate the program"); + System.out.println(CHECK + ": check the pieces on the board"); + System.out.println(WHITE_KNIGHT + " : place a new white knight at specified position"); + System.out.println(BLACK_KNIGHT + " : place a new black knight at specified position"); + System.out.println(WHITE_QUEEN + " : place a new white queen at specified position"); + System.out.println(BLACK_QUEEN + " : place a new black queen at specified position"); + } +} diff --git a/src/uebung09/chess/Color.java b/src/uebung09/chess/Color.java new file mode 100644 index 0000000..3d5151b --- /dev/null +++ b/src/uebung09/chess/Color.java @@ -0,0 +1,5 @@ +package chess; + +public enum Color { + black, white +} diff --git a/src/uebung09/chess/Knight.java b/src/uebung09/chess/Knight.java new file mode 100644 index 0000000..815cfee --- /dev/null +++ b/src/uebung09/chess/Knight.java @@ -0,0 +1,28 @@ +package chess; + +import static java.lang.Math.abs; + +public class Knight extends Piece { + public Knight(Color color, Board board, int row, int col) { + super(color, board, row, col); + } + + @Override + public char charRep() { + return getColor() == Color.white ? 'n' : 'N'; + } + + @Override + public String toString() { + return String.format("%s knight at (%d,%d)", getColor(), getRow(), getCol()); + } + + @Override + public boolean canCapture(Piece other) { + if (getBoard() != other.getBoard() || getColor() == other.getColor()) + return false; + final int dr = abs(getRow() - other.getRow()); + final int dc = abs(getCol() - other.getCol()); + return dr == 2 && dc == 1 || dr == 1 && dc == 2; + } +} diff --git a/src/uebung09/chess/Piece.java b/src/uebung09/chess/Piece.java new file mode 100644 index 0000000..5b6a716 --- /dev/null +++ b/src/uebung09/chess/Piece.java @@ -0,0 +1,38 @@ +package chess; + +public abstract class Piece { + private Color color; + private Board board; + private int row; + private int col; + + protected Piece(Color color, Board board, int row, int col) { + if (row < 1 || row > 8 || col < 1 || col > 8) + throw new IllegalArgumentException("Invalid pos " + row + "/" + col); + this.color = color; + this.board = board; + this.row = row; + this.col = col; + board.add(this); + } + + public Color getColor() { + return color; + } + + public Board getBoard() { + return board; + } + + public int getRow() { + return row; + } + + public int getCol() { + return col; + } + + public abstract char charRep(); + + public abstract boolean canCapture(Piece other); +} diff --git a/src/uebung09/chess/Queen.java b/src/uebung09/chess/Queen.java new file mode 100644 index 0000000..d75f4b6 --- /dev/null +++ b/src/uebung09/chess/Queen.java @@ -0,0 +1,40 @@ +package chess; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +public class Queen extends Piece { + public Queen(Color color, Board board, int row, int col) { + super(color, board, row, col); + } + + @Override + public char charRep() { + return getColor() == Color.white ? 'q' : 'Q'; + } + + @Override + public String toString() { + return String.format("%s queen at (%d,%d)", getColor(), getRow(), getCol()); + } + + @Override + public boolean canCapture(Piece other) { + if (getBoard() != other.getBoard() || getColor() == other.getColor()) + return false; + if (other.getRow() != getRow() && + other.getCol() != getCol() && + abs(other.getRow() - getRow()) != abs(other.getCol() - getCol())) + return false; + final int dr = signum(other.getRow() - getRow()); + final int dc = signum(other.getCol() - getCol()); + int r = getRow() + dr; + int c = getCol() + dc; + while (r != other.getRow() || c != other.getCol()) { + if (getBoard().pieceAt(r, c) != null) return false; + r += dr; + c += dc; + } + return true; + } +} diff --git a/src/uebung09/collection/Set.java b/src/uebung09/collection/Set.java new file mode 100644 index 0000000..cdcb82d --- /dev/null +++ b/src/uebung09/collection/Set.java @@ -0,0 +1,61 @@ +package collection; + +/** + * A set of elements that does not contain any element twice. + * + * @param the type of all contained elements. + */ +public interface Set extends Iterable { + /** + * Returns the number of elements stored in this set. + * + * @return the number of elements in this set + */ + int size(); + + /** + * Returns true if this set contains no elements. + * + * @return true if this set contains no elements + */ + boolean isEmpty(); + + /** + * Returns true if this set contains the specified element. + * + * @return true if this set contains the specified element. + */ + boolean contains(Object el); + + /** + * Returns the union set of this set and the specified set. + * + * @param other a set + * @return the union set of this set and the specified set. + */ + Set union(Set other); + + /** + * returns the set resulting from adding the specified element to this set. + * + * @param element an element (must not be null) + * @return the set resulting from adding the specified element to this set. + */ + Set add(E element); + + /** + * Returns the intersection of this set and the specified set. + * + * @param other a set + * @return the intersection of this set and the specified set. + */ + Set intersection(Set other); + + /** + * Returns true if all elements of this set are contained in the specified set. + * + * @param other a set + * @return true if all elements of this set are contained in the specified set. + */ + boolean subsetOf(Set other); +} \ No newline at end of file diff --git a/src/uebung09/collection/SetFactory.java b/src/uebung09/collection/SetFactory.java new file mode 100644 index 0000000..1359aca --- /dev/null +++ b/src/uebung09/collection/SetFactory.java @@ -0,0 +1,41 @@ +package collection; + +public class SetFactory { + + private SetFactory() { /* don't instantiate */ } + + /** + * Returns the empty set. + * + * @param the element type of the returned set. + * @return the empty set. + */ + static Set create() { + // TODO implement + return null; + } + + /** + * Returns the singleton set containing the specified element. + * + * @param element an element (must not be null) + * @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; + } + + /** + * Returns a set containing the specified elements. The specified elements may contain equal elements. + * + * @param elems elements of the returned set (may contain equal elements) + * @param the element type of the returned set. + * @return a set containing the specified elements. + */ + static Set create(T... elems) { + // TODO implement + return null; + } +} diff --git a/src/uebung10/logo/DarkForest.java b/src/uebung10/logo/DarkForest.java new file mode 100644 index 0000000..c8a660d --- /dev/null +++ b/src/uebung10/logo/DarkForest.java @@ -0,0 +1,5 @@ +package logo; + +public interface DarkForest { + void breadCrumb(double x, double y); +} diff --git a/src/uebung10/logo/Demo.java b/src/uebung10/logo/Demo.java new file mode 100644 index 0000000..94740c7 --- /dev/null +++ b/src/uebung10/logo/Demo.java @@ -0,0 +1,19 @@ +package logo; + +public class Demo { + public static void main(String[] args) { + final Stmt prog = new Sequence(new PenDown(), new Go(95), + new Turn(90), new Go(95), + new Turn(90), new Go(95), + new Turn(90), new Go(95)); + final Stmt prog2 = new Sequence(new PenDown(), new Go(100), new Turn(120), new Go(100)); + final Stmt prog3 = new Sequence(new PenDown(), new Go(100), new Turn(120), + new Go(25), new PenUp(), new Go(20), new PenDown(), new Go(55), + new PenUp(), new Turn(60), new Go(50), new Turn(90), + new PenDown(), new Go(60)); + final DarkForest gr = new HanselGretelFrame("Logo!"); + final Turtle turtle = new Turtle(250, 250, 0); + final Visitor visitor = new HanselGretelVisitor(30, turtle, gr); + prog3.accept(visitor); + } +} diff --git a/src/uebung10/logo/HanselGretelFrame.java b/src/uebung10/logo/HanselGretelFrame.java new file mode 100644 index 0000000..734f4df --- /dev/null +++ b/src/uebung10/logo/HanselGretelFrame.java @@ -0,0 +1,56 @@ +package logo; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import java.awt.BasicStroke; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.Shape; +import java.awt.Stroke; +import java.awt.geom.Ellipse2D; +import java.util.ArrayList; +import java.util.List; + +public class HanselGretelFrame extends JFrame implements DarkForest { + private static final double RADIUS = 2; + private static final Stroke THIN = new BasicStroke(0.2f); + + private final List path = new ArrayList<>(); + private final List dots = new ArrayList<>(); + + private JPanel panel = new JPanel() { + @Override + protected void paintComponent(Graphics g) { + final Graphics2D g2d = (Graphics2D) g; + + super.paintComponent(g2d); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + g2d.setColor(Color.BLACK); + g2d.setStroke(THIN); + for (Shape pe : new ArrayList<>(path)) + g2d.draw(pe); + g2d.setColor(Color.RED); + for (Shape pe : new ArrayList<>(dots)) + g2d.fill(pe); + } + }; + + public HanselGretelFrame(String title) { + super(title); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(500, 500); + setLayout(new BorderLayout()); + add(panel, BorderLayout.CENTER); + setVisible(true); + } + + @Override + public void breadCrumb(double x, double y) { + dots.add(new Ellipse2D.Double(x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS)); + repaint(); + } +} diff --git a/src/uebung10/logo/Turtle.java b/src/uebung10/logo/Turtle.java new file mode 100644 index 0000000..497bfd3 --- /dev/null +++ b/src/uebung10/logo/Turtle.java @@ -0,0 +1,35 @@ +package logo; + +public class Turtle { + private double x; + private double y; + private double angle; + + public Turtle(double x, double y, double angle) { + this.x = x; + this.y = y; + this.angle = angle; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + + public double getAngle() { + return angle; + } + + public void turn(double degrees) { + angle += degrees; + } + + public void go(double dist) { + final double rad = Math.toRadians(angle); + x += dist * Math.cos(rad); + y -= dist * Math.sin(rad); + } +} diff --git a/test/uebung/uebung09/iterator/Array2dIteratorTest.java b/test/uebung/uebung09/iterator/Array2dIteratorTest.java new file mode 100644 index 0000000..14e3a8e --- /dev/null +++ b/test/uebung/uebung09/iterator/Array2dIteratorTest.java @@ -0,0 +1,43 @@ +package iterator; + +import org.junit.Test; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class Array2dIteratorTest { + @Test + public void testArray2dIterator() { + final String[][] array = {{}, {"foo", "bar"}, {"baz"}, {}}; + final Iterator it = new Array2dIterator<>(array); + assertTrue(it.hasNext()); + assertEquals("foo", it.next()); + assertTrue(it.hasNext()); + assertEquals("bar", it.next()); + assertTrue(it.hasNext()); + assertEquals("baz", it.next()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testArray2dIteratorOnlyEmpty() { + final String[][] array = {{}, {}, {}}; + final Iterator it = new Array2dIterator<>(array); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testArray2dIteratorEmpty() { + final String[][] array = {}; + final Iterator it = new Array2dIterator<>(array); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } +} diff --git a/test/uebung/uebung09/iterator/SkipNullIteratorTest.java b/test/uebung/uebung09/iterator/SkipNullIteratorTest.java new file mode 100644 index 0000000..372fb2c --- /dev/null +++ b/test/uebung/uebung09/iterator/SkipNullIteratorTest.java @@ -0,0 +1,122 @@ +package iterator; + +import org.junit.Test; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class SkipNullIteratorTest { + @Test + public void testEmpty() { + final Iterator it = new SkipNullIterator<>(emptyList().iterator()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testNull() { + final Iterator it = new SkipNullIterator<>(asList(null, null).iterator()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testNonNull() { + final Iterator it = new SkipNullIterator<>(asList("foo").iterator()); + assertTrue(it.hasNext()); + assertEquals("foo", it.next()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testMixed() { + final Iterator it = new SkipNullIterator<>(asList(null, "foo", null).iterator()); + assertTrue(it.hasNext()); + assertEquals("foo", it.next()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testMixed2() { + final Iterator oriIt = asList("a", "b", null, "c").iterator(); + Iterator it = new SkipNullIterator<>(oriIt); + assertTrue(it.hasNext()); + assertEquals("a", it.next()); + assertTrue(it.hasNext()); + assertEquals("b", it.next()); + assertTrue(it.hasNext()); + assertEquals("c", it.next()); + assertFalse(it.hasNext()); + assertThrows(NoSuchElementException.class, it::next); + } + + @Test + public void testDontCallInCtor() { + final Iterator dontCallNext = new Iterator<>() { + @Override + public boolean hasNext() { + throw new RuntimeException(); + } + + @Override + public String next() { + throw new RuntimeException(); + } + }; + + new SkipNullIterator<>(dontCallNext); + } + + @Test + public void testSkipNullIteratorInfinity() { + final Iterator oriIt = new Iterator<>() { + @Override + public boolean hasNext() { + return true; + } + + @Override + public String next() { + return "infinity"; + } + }; + + final Iterator it = new SkipNullIterator<>(oriIt); + for (int i = 0; i < 1000; i++) { + assertTrue(it.hasNext()); + assertEquals("infinity", it.next()); + } + } + + @Test + public void testPathological() { + final Iterator oriIt = new Iterator<>() { + private int ctr; + + @Override + public boolean hasNext() { + return true; + } + + @Override + public String next() { + return ctr++ > 100000 ? "infinity" : null; + } + }; + + final Iterator it = new SkipNullIterator<>(oriIt); + for (int i = 0; i < 1000; i++) { + assertTrue(it.hasNext()); + assertEquals("infinity", it.next()); + } + } +} \ No newline at end of file