commit 7ac35f607a5a91a9a051f4ac975373e36aa29c06 Author: peet Date: Mon May 6 01:38:39 2024 +0200 initial commit diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/bin/oop/ch01/chess1/Board.class b/bin/oop/ch01/chess1/Board.class new file mode 100644 index 0000000..a33a5ba Binary files /dev/null and b/bin/oop/ch01/chess1/Board.class differ diff --git a/bin/oop/ch01/chess1/Chess.class b/bin/oop/ch01/chess1/Chess.class new file mode 100644 index 0000000..bacd164 Binary files /dev/null and b/bin/oop/ch01/chess1/Chess.class differ diff --git a/bin/oop/ch01/chess1/Color.class b/bin/oop/ch01/chess1/Color.class new file mode 100644 index 0000000..2a34276 Binary files /dev/null and b/bin/oop/ch01/chess1/Color.class differ diff --git a/bin/oop/ch01/chess1/Kind.class b/bin/oop/ch01/chess1/Kind.class new file mode 100644 index 0000000..22e40ee Binary files /dev/null and b/bin/oop/ch01/chess1/Kind.class differ diff --git a/bin/oop/ch01/chess1/Piece.class b/bin/oop/ch01/chess1/Piece.class new file mode 100644 index 0000000..66823ff Binary files /dev/null and b/bin/oop/ch01/chess1/Piece.class differ diff --git a/bin/oop/ch01/chess1/package-info.class b/bin/oop/ch01/chess1/package-info.class new file mode 100644 index 0000000..a776fcc Binary files /dev/null and b/bin/oop/ch01/chess1/package-info.class differ diff --git a/bin/oop/ch01/chess2/Board.class b/bin/oop/ch01/chess2/Board.class new file mode 100644 index 0000000..0ab995a Binary files /dev/null and b/bin/oop/ch01/chess2/Board.class differ diff --git a/bin/oop/ch01/chess2/Chess.class b/bin/oop/ch01/chess2/Chess.class new file mode 100644 index 0000000..60d8933 Binary files /dev/null and b/bin/oop/ch01/chess2/Chess.class differ diff --git a/bin/oop/ch01/chess2/Color.class b/bin/oop/ch01/chess2/Color.class new file mode 100644 index 0000000..a503af6 Binary files /dev/null and b/bin/oop/ch01/chess2/Color.class differ diff --git a/bin/oop/ch01/chess2/Kind.class b/bin/oop/ch01/chess2/Kind.class new file mode 100644 index 0000000..22794e1 Binary files /dev/null and b/bin/oop/ch01/chess2/Kind.class differ diff --git a/bin/oop/ch01/chess2/Piece.class b/bin/oop/ch01/chess2/Piece.class new file mode 100644 index 0000000..598ebe7 Binary files /dev/null and b/bin/oop/ch01/chess2/Piece.class differ diff --git a/bin/oop/ch01/chess2/package-info.class b/bin/oop/ch01/chess2/package-info.class new file mode 100644 index 0000000..59b5160 Binary files /dev/null and b/bin/oop/ch01/chess2/package-info.class differ diff --git a/bin/oop/ch02/cards/Card.class b/bin/oop/ch02/cards/Card.class new file mode 100644 index 0000000..2114ebb Binary files /dev/null and b/bin/oop/ch02/cards/Card.class differ diff --git a/bin/oop/ch02/cards/CardExample.class b/bin/oop/ch02/cards/CardExample.class new file mode 100644 index 0000000..b465225 Binary files /dev/null and b/bin/oop/ch02/cards/CardExample.class differ diff --git a/bin/oop/ch02/cards0/Card.class b/bin/oop/ch02/cards0/Card.class new file mode 100644 index 0000000..f533554 Binary files /dev/null and b/bin/oop/ch02/cards0/Card.class differ diff --git a/bin/oop/ch02/chess1/Board.class b/bin/oop/ch02/chess1/Board.class new file mode 100644 index 0000000..39f3acd Binary files /dev/null and b/bin/oop/ch02/chess1/Board.class differ diff --git a/bin/oop/ch02/chess1/Chess.class b/bin/oop/ch02/chess1/Chess.class new file mode 100644 index 0000000..63581db Binary files /dev/null and b/bin/oop/ch02/chess1/Chess.class differ diff --git a/bin/oop/ch02/chess1/Color.class b/bin/oop/ch02/chess1/Color.class new file mode 100644 index 0000000..f0f8308 Binary files /dev/null and b/bin/oop/ch02/chess1/Color.class differ diff --git a/bin/oop/ch02/chess1/Kind.class b/bin/oop/ch02/chess1/Kind.class new file mode 100644 index 0000000..e070e20 Binary files /dev/null and b/bin/oop/ch02/chess1/Kind.class differ diff --git a/bin/oop/ch02/chess1/Piece.class b/bin/oop/ch02/chess1/Piece.class new file mode 100644 index 0000000..0baffdc Binary files /dev/null and b/bin/oop/ch02/chess1/Piece.class differ diff --git a/bin/oop/ch02/chess1/package-info.class b/bin/oop/ch02/chess1/package-info.class new file mode 100644 index 0000000..3458711 Binary files /dev/null and b/bin/oop/ch02/chess1/package-info.class differ diff --git a/bin/oop/ch02/chess2/Board.class b/bin/oop/ch02/chess2/Board.class new file mode 100644 index 0000000..bb77ee1 Binary files /dev/null and b/bin/oop/ch02/chess2/Board.class differ diff --git a/bin/oop/ch02/chess2/Chess.class b/bin/oop/ch02/chess2/Chess.class new file mode 100644 index 0000000..0cc845a Binary files /dev/null and b/bin/oop/ch02/chess2/Chess.class differ diff --git a/bin/oop/ch02/chess2/Color.class b/bin/oop/ch02/chess2/Color.class new file mode 100644 index 0000000..3a6bfae Binary files /dev/null and b/bin/oop/ch02/chess2/Color.class differ diff --git a/bin/oop/ch02/chess2/Kind.class b/bin/oop/ch02/chess2/Kind.class new file mode 100644 index 0000000..872ac4f Binary files /dev/null and b/bin/oop/ch02/chess2/Kind.class differ diff --git a/bin/oop/ch02/chess2/Piece.class b/bin/oop/ch02/chess2/Piece.class new file mode 100644 index 0000000..9d3cce1 Binary files /dev/null and b/bin/oop/ch02/chess2/Piece.class differ diff --git a/bin/oop/ch02/chess2/package-info.class b/bin/oop/ch02/chess2/package-info.class new file mode 100644 index 0000000..c13e064 Binary files /dev/null and b/bin/oop/ch02/chess2/package-info.class differ diff --git a/bin/oop/ch02/clock/Clock1.class b/bin/oop/ch02/clock/Clock1.class new file mode 100644 index 0000000..c518697 Binary files /dev/null and b/bin/oop/ch02/clock/Clock1.class differ diff --git a/bin/oop/ch02/clock/Clock2.class b/bin/oop/ch02/clock/Clock2.class new file mode 100644 index 0000000..8c5051e Binary files /dev/null and b/bin/oop/ch02/clock/Clock2.class differ diff --git a/bin/oop/ch02/clock/Clock3.class b/bin/oop/ch02/clock/Clock3.class new file mode 100644 index 0000000..5347124 Binary files /dev/null and b/bin/oop/ch02/clock/Clock3.class differ diff --git a/bin/oop/ch02/clock/Example.class b/bin/oop/ch02/clock/Example.class new file mode 100644 index 0000000..aabca4b Binary files /dev/null and b/bin/oop/ch02/clock/Example.class differ diff --git a/bin/oop/ch03/chess/Board.class b/bin/oop/ch03/chess/Board.class new file mode 100644 index 0000000..8eabe04 Binary files /dev/null and b/bin/oop/ch03/chess/Board.class differ diff --git a/bin/oop/ch03/chess/Chess.class b/bin/oop/ch03/chess/Chess.class new file mode 100644 index 0000000..1e574cc Binary files /dev/null and b/bin/oop/ch03/chess/Chess.class differ diff --git a/bin/oop/ch03/chess/Color.class b/bin/oop/ch03/chess/Color.class new file mode 100644 index 0000000..032dab7 Binary files /dev/null and b/bin/oop/ch03/chess/Color.class differ diff --git a/bin/oop/ch03/chess/Knight.class b/bin/oop/ch03/chess/Knight.class new file mode 100644 index 0000000..84f0e7d Binary files /dev/null and b/bin/oop/ch03/chess/Knight.class differ diff --git a/bin/oop/ch03/chess/Piece.class b/bin/oop/ch03/chess/Piece.class new file mode 100644 index 0000000..27d5ee4 Binary files /dev/null and b/bin/oop/ch03/chess/Piece.class differ diff --git a/bin/oop/ch03/chess/Queen.class b/bin/oop/ch03/chess/Queen.class new file mode 100644 index 0000000..9162f4b Binary files /dev/null and b/bin/oop/ch03/chess/Queen.class differ diff --git a/bin/oop/ch03/chess/Rook.class b/bin/oop/ch03/chess/Rook.class new file mode 100644 index 0000000..7f4e29f Binary files /dev/null and b/bin/oop/ch03/chess/Rook.class differ diff --git a/bin/oop/ch03/chess/package-info.class b/bin/oop/ch03/chess/package-info.class new file mode 100644 index 0000000..fca537a Binary files /dev/null and b/bin/oop/ch03/chess/package-info.class differ diff --git a/bin/oop/ch03/vehicles/AmphibienBsp.class b/bin/oop/ch03/vehicles/AmphibienBsp.class new file mode 100644 index 0000000..f94fd8f Binary files /dev/null and b/bin/oop/ch03/vehicles/AmphibienBsp.class differ diff --git a/bin/oop/ch03/vehicles/Amphibienfahrzeug.class b/bin/oop/ch03/vehicles/Amphibienfahrzeug.class new file mode 100644 index 0000000..00850b3 Binary files /dev/null and b/bin/oop/ch03/vehicles/Amphibienfahrzeug.class differ diff --git a/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl.class b/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl.class new file mode 100644 index 0000000..444af3d Binary files /dev/null and b/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl.class differ diff --git a/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl2.class b/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl2.class new file mode 100644 index 0000000..97054de Binary files /dev/null and b/bin/oop/ch03/vehicles/AmphibienfahrzeugImpl2.class differ diff --git a/bin/oop/ch03/vehicles/Auto.class b/bin/oop/ch03/vehicles/Auto.class new file mode 100644 index 0000000..c267f9e Binary files /dev/null and b/bin/oop/ch03/vehicles/Auto.class differ diff --git a/bin/oop/ch03/vehicles/AutoImpl.class b/bin/oop/ch03/vehicles/AutoImpl.class new file mode 100644 index 0000000..c0e7aa1 Binary files /dev/null and b/bin/oop/ch03/vehicles/AutoImpl.class differ diff --git a/bin/oop/ch03/vehicles/Fahrzeug.class b/bin/oop/ch03/vehicles/Fahrzeug.class new file mode 100644 index 0000000..78285cf Binary files /dev/null and b/bin/oop/ch03/vehicles/Fahrzeug.class differ diff --git a/bin/oop/ch03/vehicles/FahrzeugImpl.class b/bin/oop/ch03/vehicles/FahrzeugImpl.class new file mode 100644 index 0000000..b5d7bdf Binary files /dev/null and b/bin/oop/ch03/vehicles/FahrzeugImpl.class differ diff --git a/bin/oop/ch03/vehicles/Schiff.class b/bin/oop/ch03/vehicles/Schiff.class new file mode 100644 index 0000000..f98847d Binary files /dev/null and b/bin/oop/ch03/vehicles/Schiff.class differ diff --git a/bin/oop/ch03/vehicles/SchiffImpl.class b/bin/oop/ch03/vehicles/SchiffImpl.class new file mode 100644 index 0000000..f8cee9e Binary files /dev/null and b/bin/oop/ch03/vehicles/SchiffImpl.class differ diff --git a/bin/oop/ch04/equality/Example.class b/bin/oop/ch04/equality/Example.class new file mode 100644 index 0000000..a46ebc5 Binary files /dev/null and b/bin/oop/ch04/equality/Example.class differ diff --git a/bin/oop/ch04/equality/card1/Card.class b/bin/oop/ch04/equality/card1/Card.class new file mode 100644 index 0000000..f258dab Binary files /dev/null and b/bin/oop/ch04/equality/card1/Card.class differ diff --git a/bin/oop/ch04/equality/card1/Color.class b/bin/oop/ch04/equality/card1/Color.class new file mode 100644 index 0000000..ffd9444 Binary files /dev/null and b/bin/oop/ch04/equality/card1/Color.class differ diff --git a/bin/oop/ch04/equality/card1/ColoredCard.class b/bin/oop/ch04/equality/card1/ColoredCard.class new file mode 100644 index 0000000..be4d802 Binary files /dev/null and b/bin/oop/ch04/equality/card1/ColoredCard.class differ diff --git a/bin/oop/ch04/equality/card1/Example.class b/bin/oop/ch04/equality/card1/Example.class new file mode 100644 index 0000000..b0bac2d Binary files /dev/null and b/bin/oop/ch04/equality/card1/Example.class differ diff --git a/bin/oop/ch04/equality/card2/Card.class b/bin/oop/ch04/equality/card2/Card.class new file mode 100644 index 0000000..aa76b9e Binary files /dev/null and b/bin/oop/ch04/equality/card2/Card.class differ diff --git a/bin/oop/ch04/equality/card2/Color.class b/bin/oop/ch04/equality/card2/Color.class new file mode 100644 index 0000000..45d5625 Binary files /dev/null and b/bin/oop/ch04/equality/card2/Color.class differ diff --git a/bin/oop/ch04/equality/card2/ColoredCard.class b/bin/oop/ch04/equality/card2/ColoredCard.class new file mode 100644 index 0000000..485cac7 Binary files /dev/null and b/bin/oop/ch04/equality/card2/ColoredCard.class differ diff --git a/bin/oop/ch04/equality/card2/Example.class b/bin/oop/ch04/equality/card2/Example.class new file mode 100644 index 0000000..7d38d7b Binary files /dev/null and b/bin/oop/ch04/equality/card2/Example.class differ diff --git a/bin/oop/ch04/equality/card3/Card.class b/bin/oop/ch04/equality/card3/Card.class new file mode 100644 index 0000000..7a17a0a Binary files /dev/null and b/bin/oop/ch04/equality/card3/Card.class differ diff --git a/bin/oop/ch04/equality/card3/Color.class b/bin/oop/ch04/equality/card3/Color.class new file mode 100644 index 0000000..19ad9cd Binary files /dev/null and b/bin/oop/ch04/equality/card3/Color.class differ diff --git a/bin/oop/ch04/equality/card3/ColoredCard.class b/bin/oop/ch04/equality/card3/ColoredCard.class new file mode 100644 index 0000000..82dd6db Binary files /dev/null and b/bin/oop/ch04/equality/card3/ColoredCard.class differ diff --git a/bin/oop/ch04/equality/card3/Example.class b/bin/oop/ch04/equality/card3/Example.class new file mode 100644 index 0000000..f0ef12a Binary files /dev/null and b/bin/oop/ch04/equality/card3/Example.class differ diff --git a/bin/oop/ch04/equality/card4/Card.class b/bin/oop/ch04/equality/card4/Card.class new file mode 100644 index 0000000..51bbe8a Binary files /dev/null and b/bin/oop/ch04/equality/card4/Card.class differ diff --git a/bin/oop/ch04/equality/card4/Color.class b/bin/oop/ch04/equality/card4/Color.class new file mode 100644 index 0000000..507010c Binary files /dev/null and b/bin/oop/ch04/equality/card4/Color.class differ diff --git a/bin/oop/ch04/equality/card4/ColoredCard.class b/bin/oop/ch04/equality/card4/ColoredCard.class new file mode 100644 index 0000000..32300eb Binary files /dev/null and b/bin/oop/ch04/equality/card4/ColoredCard.class differ diff --git a/bin/oop/ch04/equality/card4/Example.class b/bin/oop/ch04/equality/card4/Example.class new file mode 100644 index 0000000..cf2d0a8 Binary files /dev/null and b/bin/oop/ch04/equality/card4/Example.class differ diff --git a/bin/oop/ch04/equality/rational1/Example.class b/bin/oop/ch04/equality/rational1/Example.class new file mode 100644 index 0000000..baa940b Binary files /dev/null and b/bin/oop/ch04/equality/rational1/Example.class differ diff --git a/bin/oop/ch04/equality/rational1/Rational.class b/bin/oop/ch04/equality/rational1/Rational.class new file mode 100644 index 0000000..feda206 Binary files /dev/null and b/bin/oop/ch04/equality/rational1/Rational.class differ diff --git a/bin/oop/ch04/equality/rational2/Example.class b/bin/oop/ch04/equality/rational2/Example.class new file mode 100644 index 0000000..a4a2862 Binary files /dev/null and b/bin/oop/ch04/equality/rational2/Example.class differ diff --git a/bin/oop/ch04/equality/rational2/Rational.class b/bin/oop/ch04/equality/rational2/Rational.class new file mode 100644 index 0000000..503d4c9 Binary files /dev/null and b/bin/oop/ch04/equality/rational2/Rational.class differ diff --git a/bin/oop/ch05/generic/example/AutoContainer.class b/bin/oop/ch05/generic/example/AutoContainer.class new file mode 100644 index 0000000..87df713 Binary files /dev/null and b/bin/oop/ch05/generic/example/AutoContainer.class differ diff --git a/bin/oop/ch05/generic/example/Example.class b/bin/oop/ch05/generic/example/Example.class new file mode 100644 index 0000000..d10786e Binary files /dev/null and b/bin/oop/ch05/generic/example/Example.class differ diff --git a/bin/oop/ch05/generic/example/GenericContainer.class b/bin/oop/ch05/generic/example/GenericContainer.class new file mode 100644 index 0000000..c62e4a8 Binary files /dev/null and b/bin/oop/ch05/generic/example/GenericContainer.class differ diff --git a/bin/oop/ch05/generic/mensa/AccountCard.class b/bin/oop/ch05/generic/mensa/AccountCard.class new file mode 100644 index 0000000..b4392b6 Binary files /dev/null and b/bin/oop/ch05/generic/mensa/AccountCard.class differ diff --git a/bin/oop/ch05/generic/mensa/CashCard.class b/bin/oop/ch05/generic/mensa/CashCard.class new file mode 100644 index 0000000..168ab5d Binary files /dev/null and b/bin/oop/ch05/generic/mensa/CashCard.class differ diff --git a/bin/oop/ch05/generic/mensa/CashPoint.class b/bin/oop/ch05/generic/mensa/CashPoint.class new file mode 100644 index 0000000..6acac4f Binary files /dev/null and b/bin/oop/ch05/generic/mensa/CashPoint.class differ diff --git a/bin/oop/ch05/generic/mensa/Color.class b/bin/oop/ch05/generic/mensa/Color.class new file mode 100644 index 0000000..d71152e Binary files /dev/null and b/bin/oop/ch05/generic/mensa/Color.class differ diff --git a/bin/oop/ch05/generic/mensa/CountCard.class b/bin/oop/ch05/generic/mensa/CountCard.class new file mode 100644 index 0000000..abafd75 Binary files /dev/null and b/bin/oop/ch05/generic/mensa/CountCard.class differ diff --git a/bin/oop/ch05/generic/mensa/MensaCard.class b/bin/oop/ch05/generic/mensa/MensaCard.class new file mode 100644 index 0000000..8875437 Binary files /dev/null and b/bin/oop/ch05/generic/mensa/MensaCard.class differ diff --git a/bin/oop/ch05/generic/mensa/MensaExample.class b/bin/oop/ch05/generic/mensa/MensaExample.class new file mode 100644 index 0000000..97589be Binary files /dev/null and b/bin/oop/ch05/generic/mensa/MensaExample.class differ diff --git a/bin/oop/ch05/generic/mensa/RejectedException.class b/bin/oop/ch05/generic/mensa/RejectedException.class new file mode 100644 index 0000000..a94d052 Binary files /dev/null and b/bin/oop/ch05/generic/mensa/RejectedException.class differ diff --git a/bin/oop/ch05/generic/mensa/VendingMachine.class b/bin/oop/ch05/generic/mensa/VendingMachine.class new file mode 100644 index 0000000..5063a6f Binary files /dev/null and b/bin/oop/ch05/generic/mensa/VendingMachine.class differ diff --git a/bin/oop/ch05/generic/secured/AuthorizationException.class b/bin/oop/ch05/generic/secured/AuthorizationException.class new file mode 100644 index 0000000..1d5031d Binary files /dev/null and b/bin/oop/ch05/generic/secured/AuthorizationException.class differ diff --git a/bin/oop/ch05/generic/secured/SecuredContent.class b/bin/oop/ch05/generic/secured/SecuredContent.class new file mode 100644 index 0000000..751d0e4 Binary files /dev/null and b/bin/oop/ch05/generic/secured/SecuredContent.class differ diff --git a/bin/oop/ch05/generic/secured/SecurityClient.class b/bin/oop/ch05/generic/secured/SecurityClient.class new file mode 100644 index 0000000..e420f98 Binary files /dev/null and b/bin/oop/ch05/generic/secured/SecurityClient.class differ diff --git a/bin/oop/ch05/mensa/AccountCard.class b/bin/oop/ch05/mensa/AccountCard.class new file mode 100644 index 0000000..f847414 Binary files /dev/null and b/bin/oop/ch05/mensa/AccountCard.class differ diff --git a/bin/oop/ch05/mensa/CashCard.class b/bin/oop/ch05/mensa/CashCard.class new file mode 100644 index 0000000..d888773 Binary files /dev/null and b/bin/oop/ch05/mensa/CashCard.class differ diff --git a/bin/oop/ch05/mensa/CashPoint.class b/bin/oop/ch05/mensa/CashPoint.class new file mode 100644 index 0000000..92d9932 Binary files /dev/null and b/bin/oop/ch05/mensa/CashPoint.class differ diff --git a/bin/oop/ch05/mensa/Color.class b/bin/oop/ch05/mensa/Color.class new file mode 100644 index 0000000..03c8602 Binary files /dev/null and b/bin/oop/ch05/mensa/Color.class differ diff --git a/bin/oop/ch05/mensa/CountCard.class b/bin/oop/ch05/mensa/CountCard.class new file mode 100644 index 0000000..3fb923e Binary files /dev/null and b/bin/oop/ch05/mensa/CountCard.class differ diff --git a/bin/oop/ch05/mensa/MensaCard.class b/bin/oop/ch05/mensa/MensaCard.class new file mode 100644 index 0000000..2d18853 Binary files /dev/null and b/bin/oop/ch05/mensa/MensaCard.class differ diff --git a/bin/oop/ch05/mensa/MensaExample.class b/bin/oop/ch05/mensa/MensaExample.class new file mode 100644 index 0000000..9a52cda Binary files /dev/null and b/bin/oop/ch05/mensa/MensaExample.class differ diff --git a/bin/oop/ch05/mensa/RejectedException.class b/bin/oop/ch05/mensa/RejectedException.class new file mode 100644 index 0000000..b7d1dcb Binary files /dev/null and b/bin/oop/ch05/mensa/RejectedException.class differ diff --git a/bin/oop/ch05/mensa/VendingMachine.class b/bin/oop/ch05/mensa/VendingMachine.class new file mode 100644 index 0000000..ab9cd0f Binary files /dev/null and b/bin/oop/ch05/mensa/VendingMachine.class differ diff --git a/bin/oop/ch05/secured/AuthorizationException.class b/bin/oop/ch05/secured/AuthorizationException.class new file mode 100644 index 0000000..7fbd226 Binary files /dev/null and b/bin/oop/ch05/secured/AuthorizationException.class differ diff --git a/bin/oop/ch05/secured/SecuredContent.class b/bin/oop/ch05/secured/SecuredContent.class new file mode 100644 index 0000000..4c0ea37 Binary files /dev/null and b/bin/oop/ch05/secured/SecuredContent.class differ diff --git a/bin/oop/ch05/secured/SecurityClient.class b/bin/oop/ch05/secured/SecurityClient.class new file mode 100644 index 0000000..aad3c94 Binary files /dev/null and b/bin/oop/ch05/secured/SecurityClient.class differ diff --git a/bin/oop/ch06/calc/AdHocCalculator.class b/bin/oop/ch06/calc/AdHocCalculator.class new file mode 100644 index 0000000..6a5e39e Binary files /dev/null and b/bin/oop/ch06/calc/AdHocCalculator.class differ diff --git a/bin/oop/ch06/calc/BinaryOp.class b/bin/oop/ch06/calc/BinaryOp.class new file mode 100644 index 0000000..2446936 Binary files /dev/null and b/bin/oop/ch06/calc/BinaryOp.class differ diff --git a/bin/oop/ch06/calc/CalculatorError.class b/bin/oop/ch06/calc/CalculatorError.class new file mode 100644 index 0000000..413ef16 Binary files /dev/null and b/bin/oop/ch06/calc/CalculatorError.class differ diff --git a/bin/oop/ch06/calc/Example.class b/bin/oop/ch06/calc/Example.class new file mode 100644 index 0000000..f4e2f3b Binary files /dev/null and b/bin/oop/ch06/calc/Example.class differ diff --git a/bin/oop/ch06/calc/Op.class b/bin/oop/ch06/calc/Op.class new file mode 100644 index 0000000..e1ed32b Binary files /dev/null and b/bin/oop/ch06/calc/Op.class differ diff --git a/bin/oop/ch06/calc/Ops$1.class b/bin/oop/ch06/calc/Ops$1.class new file mode 100644 index 0000000..00c5131 Binary files /dev/null and b/bin/oop/ch06/calc/Ops$1.class differ diff --git a/bin/oop/ch06/calc/Ops$2.class b/bin/oop/ch06/calc/Ops$2.class new file mode 100644 index 0000000..21fc36f Binary files /dev/null and b/bin/oop/ch06/calc/Ops$2.class differ diff --git a/bin/oop/ch06/calc/Ops$3.class b/bin/oop/ch06/calc/Ops$3.class new file mode 100644 index 0000000..14cb4c4 Binary files /dev/null and b/bin/oop/ch06/calc/Ops$3.class differ diff --git a/bin/oop/ch06/calc/Ops$4.class b/bin/oop/ch06/calc/Ops$4.class new file mode 100644 index 0000000..b6c9ead Binary files /dev/null and b/bin/oop/ch06/calc/Ops$4.class differ diff --git a/bin/oop/ch06/calc/Ops$5.class b/bin/oop/ch06/calc/Ops$5.class new file mode 100644 index 0000000..a4f804c Binary files /dev/null and b/bin/oop/ch06/calc/Ops$5.class differ diff --git a/bin/oop/ch06/calc/Ops$6.class b/bin/oop/ch06/calc/Ops$6.class new file mode 100644 index 0000000..d1864e6 Binary files /dev/null and b/bin/oop/ch06/calc/Ops$6.class differ diff --git a/bin/oop/ch06/calc/Ops$7.class b/bin/oop/ch06/calc/Ops$7.class new file mode 100644 index 0000000..792eb97 Binary files /dev/null and b/bin/oop/ch06/calc/Ops$7.class differ diff --git a/bin/oop/ch06/calc/Ops.class b/bin/oop/ch06/calc/Ops.class new file mode 100644 index 0000000..dda7de2 Binary files /dev/null and b/bin/oop/ch06/calc/Ops.class differ diff --git a/bin/oop/ch06/calc/UnaryOp.class b/bin/oop/ch06/calc/UnaryOp.class new file mode 100644 index 0000000..63965e5 Binary files /dev/null and b/bin/oop/ch06/calc/UnaryOp.class differ diff --git a/bin/oop/ch06/observer/Observer.class b/bin/oop/ch06/observer/Observer.class new file mode 100644 index 0000000..4598fb9 Binary files /dev/null and b/bin/oop/ch06/observer/Observer.class differ diff --git a/bin/oop/ch06/observer/ObserverSupport.class b/bin/oop/ch06/observer/ObserverSupport.class new file mode 100644 index 0000000..1ad26b0 Binary files /dev/null and b/bin/oop/ch06/observer/ObserverSupport.class differ diff --git a/bin/oop/ch07/auto/generics/A.class b/bin/oop/ch07/auto/generics/A.class new file mode 100644 index 0000000..aede993 Binary files /dev/null and b/bin/oop/ch07/auto/generics/A.class differ diff --git a/bin/oop/ch07/auto/generics/Automaton.class b/bin/oop/ch07/auto/generics/Automaton.class new file mode 100644 index 0000000..32c0cdb Binary files /dev/null and b/bin/oop/ch07/auto/generics/Automaton.class differ diff --git a/bin/oop/ch07/auto/generics/B.class b/bin/oop/ch07/auto/generics/B.class new file mode 100644 index 0000000..11befdb Binary files /dev/null and b/bin/oop/ch07/auto/generics/B.class differ diff --git a/bin/oop/ch07/auto/generics/C.class b/bin/oop/ch07/auto/generics/C.class new file mode 100644 index 0000000..a7752b7 Binary files /dev/null and b/bin/oop/ch07/auto/generics/C.class differ diff --git a/bin/oop/ch07/auto/generics/D.class b/bin/oop/ch07/auto/generics/D.class new file mode 100644 index 0000000..b66982c Binary files /dev/null and b/bin/oop/ch07/auto/generics/D.class differ diff --git a/bin/oop/ch07/auto/generics/Demo.class b/bin/oop/ch07/auto/generics/Demo.class new file mode 100644 index 0000000..87fd5b8 Binary files /dev/null and b/bin/oop/ch07/auto/generics/Demo.class differ diff --git a/bin/oop/ch07/auto/generics/E.class b/bin/oop/ch07/auto/generics/E.class new file mode 100644 index 0000000..326497b Binary files /dev/null and b/bin/oop/ch07/auto/generics/E.class differ diff --git a/bin/oop/ch07/auto/generics/F.class b/bin/oop/ch07/auto/generics/F.class new file mode 100644 index 0000000..27fb975 Binary files /dev/null and b/bin/oop/ch07/auto/generics/F.class differ diff --git a/bin/oop/ch07/auto/generics/Lower.class b/bin/oop/ch07/auto/generics/Lower.class new file mode 100644 index 0000000..f29f996 Binary files /dev/null and b/bin/oop/ch07/auto/generics/Lower.class differ diff --git a/bin/oop/ch07/auto/generics/State.class b/bin/oop/ch07/auto/generics/State.class new file mode 100644 index 0000000..b872438 Binary files /dev/null and b/bin/oop/ch07/auto/generics/State.class differ diff --git a/bin/oop/ch07/auto/generics/StateMachine.class b/bin/oop/ch07/auto/generics/StateMachine.class new file mode 100644 index 0000000..5a2a439 Binary files /dev/null and b/bin/oop/ch07/auto/generics/StateMachine.class differ diff --git a/bin/oop/ch07/auto/generics/Upper.class b/bin/oop/ch07/auto/generics/Upper.class new file mode 100644 index 0000000..592e26b Binary files /dev/null and b/bin/oop/ch07/auto/generics/Upper.class differ diff --git a/bin/oop/ch07/auto/simple/A.class b/bin/oop/ch07/auto/simple/A.class new file mode 100644 index 0000000..24c6430 Binary files /dev/null and b/bin/oop/ch07/auto/simple/A.class differ diff --git a/bin/oop/ch07/auto/simple/Automaton.class b/bin/oop/ch07/auto/simple/Automaton.class new file mode 100644 index 0000000..2790894 Binary files /dev/null and b/bin/oop/ch07/auto/simple/Automaton.class differ diff --git a/bin/oop/ch07/auto/simple/B.class b/bin/oop/ch07/auto/simple/B.class new file mode 100644 index 0000000..4a317e8 Binary files /dev/null and b/bin/oop/ch07/auto/simple/B.class differ diff --git a/bin/oop/ch07/auto/simple/C.class b/bin/oop/ch07/auto/simple/C.class new file mode 100644 index 0000000..c2b12af Binary files /dev/null and b/bin/oop/ch07/auto/simple/C.class differ diff --git a/bin/oop/ch07/auto/simple/D.class b/bin/oop/ch07/auto/simple/D.class new file mode 100644 index 0000000..7cf7ea8 Binary files /dev/null and b/bin/oop/ch07/auto/simple/D.class differ diff --git a/bin/oop/ch07/auto/simple/Demo.class b/bin/oop/ch07/auto/simple/Demo.class new file mode 100644 index 0000000..d2babc6 Binary files /dev/null and b/bin/oop/ch07/auto/simple/Demo.class differ diff --git a/bin/oop/ch07/auto/simple/E.class b/bin/oop/ch07/auto/simple/E.class new file mode 100644 index 0000000..56140c0 Binary files /dev/null and b/bin/oop/ch07/auto/simple/E.class differ diff --git a/bin/oop/ch07/auto/simple/F.class b/bin/oop/ch07/auto/simple/F.class new file mode 100644 index 0000000..2fbdcc7 Binary files /dev/null and b/bin/oop/ch07/auto/simple/F.class differ diff --git a/bin/oop/ch07/auto/simple/Lower.class b/bin/oop/ch07/auto/simple/Lower.class new file mode 100644 index 0000000..105c48a Binary files /dev/null and b/bin/oop/ch07/auto/simple/Lower.class differ diff --git a/bin/oop/ch07/auto/simple/State.class b/bin/oop/ch07/auto/simple/State.class new file mode 100644 index 0000000..9be5ea2 Binary files /dev/null and b/bin/oop/ch07/auto/simple/State.class differ diff --git a/bin/oop/ch07/auto/simple/StateMachine.class b/bin/oop/ch07/auto/simple/StateMachine.class new file mode 100644 index 0000000..1d6fc09 Binary files /dev/null and b/bin/oop/ch07/auto/simple/StateMachine.class differ diff --git a/bin/oop/ch07/auto/simple/Upper.class b/bin/oop/ch07/auto/simple/Upper.class new file mode 100644 index 0000000..042095b Binary files /dev/null and b/bin/oop/ch07/auto/simple/Upper.class differ diff --git a/bin/oop/ch07/calc/AfterDot.class b/bin/oop/ch07/calc/AfterDot.class new file mode 100644 index 0000000..6a3d477 Binary files /dev/null and b/bin/oop/ch07/calc/AfterDot.class differ diff --git a/bin/oop/ch07/calc/Automaton.class b/bin/oop/ch07/calc/Automaton.class new file mode 100644 index 0000000..273074e Binary files /dev/null and b/bin/oop/ch07/calc/Automaton.class differ diff --git a/bin/oop/ch07/calc/BeforeDot.class b/bin/oop/ch07/calc/BeforeDot.class new file mode 100644 index 0000000..e5c159e Binary files /dev/null and b/bin/oop/ch07/calc/BeforeDot.class differ diff --git a/bin/oop/ch07/calc/BinOp.class b/bin/oop/ch07/calc/BinOp.class new file mode 100644 index 0000000..554f995 Binary files /dev/null and b/bin/oop/ch07/calc/BinOp.class differ diff --git a/bin/oop/ch07/calc/Calculator.class b/bin/oop/ch07/calc/Calculator.class new file mode 100644 index 0000000..245cd84 Binary files /dev/null and b/bin/oop/ch07/calc/Calculator.class differ diff --git a/bin/oop/ch07/calc/Error.class b/bin/oop/ch07/calc/Error.class new file mode 100644 index 0000000..5f2c4d4 Binary files /dev/null and b/bin/oop/ch07/calc/Error.class differ diff --git a/bin/oop/ch07/calc/Example.class b/bin/oop/ch07/calc/Example.class new file mode 100644 index 0000000..dba5ee6 Binary files /dev/null and b/bin/oop/ch07/calc/Example.class differ diff --git a/bin/oop/ch07/calc/HasFirst.class b/bin/oop/ch07/calc/HasFirst.class new file mode 100644 index 0000000..35b2a47 Binary files /dev/null and b/bin/oop/ch07/calc/HasFirst.class differ diff --git a/bin/oop/ch07/calc/HasSecond.class b/bin/oop/ch07/calc/HasSecond.class new file mode 100644 index 0000000..1cea9e7 Binary files /dev/null and b/bin/oop/ch07/calc/HasSecond.class differ diff --git a/bin/oop/ch07/calc/Lower.class b/bin/oop/ch07/calc/Lower.class new file mode 100644 index 0000000..62a8738 Binary files /dev/null and b/bin/oop/ch07/calc/Lower.class differ diff --git a/bin/oop/ch07/calc/NoOp.class b/bin/oop/ch07/calc/NoOp.class new file mode 100644 index 0000000..d7edd1b Binary files /dev/null and b/bin/oop/ch07/calc/NoOp.class differ diff --git a/bin/oop/ch07/calc/State.class b/bin/oop/ch07/calc/State.class new file mode 100644 index 0000000..5c4fb29 Binary files /dev/null and b/bin/oop/ch07/calc/State.class differ diff --git a/bin/oop/ch07/calc/StateMachine.class b/bin/oop/ch07/calc/StateMachine.class new file mode 100644 index 0000000..fd14168 Binary files /dev/null and b/bin/oop/ch07/calc/StateMachine.class differ diff --git a/bin/oop/ch07/calc/Unchanged.class b/bin/oop/ch07/calc/Unchanged.class new file mode 100644 index 0000000..f6af537 Binary files /dev/null and b/bin/oop/ch07/calc/Unchanged.class differ diff --git a/bin/oop/ch07/calc/Upper.class b/bin/oop/ch07/calc/Upper.class new file mode 100644 index 0000000..f1f00a5 Binary files /dev/null and b/bin/oop/ch07/calc/Upper.class differ diff --git a/bin/oop/ch07/calc/Working.class b/bin/oop/ch07/calc/Working.class new file mode 100644 index 0000000..a08ce93 Binary files /dev/null and b/bin/oop/ch07/calc/Working.class differ diff --git a/bin/oop/ch07/clock0/Clock.class b/bin/oop/ch07/clock0/Clock.class new file mode 100644 index 0000000..3d3a541 Binary files /dev/null and b/bin/oop/ch07/clock0/Clock.class differ diff --git a/bin/oop/ch07/clock0/Example.class b/bin/oop/ch07/clock0/Example.class new file mode 100644 index 0000000..ffaa8b2 Binary files /dev/null and b/bin/oop/ch07/clock0/Example.class differ diff --git a/bin/oop/ch07/clock0/State.class b/bin/oop/ch07/clock0/State.class new file mode 100644 index 0000000..61bc07c Binary files /dev/null and b/bin/oop/ch07/clock0/State.class differ diff --git a/bin/oop/ch07/clock1/Clock.class b/bin/oop/ch07/clock1/Clock.class new file mode 100644 index 0000000..31af701 Binary files /dev/null and b/bin/oop/ch07/clock1/Clock.class differ diff --git a/bin/oop/ch07/clock1/Example.class b/bin/oop/ch07/clock1/Example.class new file mode 100644 index 0000000..6e4901d Binary files /dev/null and b/bin/oop/ch07/clock1/Example.class differ diff --git a/bin/oop/ch07/clock1/Normal.class b/bin/oop/ch07/clock1/Normal.class new file mode 100644 index 0000000..69cbf91 Binary files /dev/null and b/bin/oop/ch07/clock1/Normal.class differ diff --git a/bin/oop/ch07/clock1/Setting.class b/bin/oop/ch07/clock1/Setting.class new file mode 100644 index 0000000..740717e Binary files /dev/null and b/bin/oop/ch07/clock1/Setting.class differ diff --git a/bin/oop/ch07/clock1/State.class b/bin/oop/ch07/clock1/State.class new file mode 100644 index 0000000..74e3fa8 Binary files /dev/null and b/bin/oop/ch07/clock1/State.class differ diff --git a/bin/oop/ch07/clock2/Clock.class b/bin/oop/ch07/clock2/Clock.class new file mode 100644 index 0000000..eb75e7c Binary files /dev/null and b/bin/oop/ch07/clock2/Clock.class differ diff --git a/bin/oop/ch07/clock2/Example.class b/bin/oop/ch07/clock2/Example.class new file mode 100644 index 0000000..48191d7 Binary files /dev/null and b/bin/oop/ch07/clock2/Example.class differ diff --git a/bin/oop/ch07/clock2/Normal.class b/bin/oop/ch07/clock2/Normal.class new file mode 100644 index 0000000..326f5a6 Binary files /dev/null and b/bin/oop/ch07/clock2/Normal.class differ diff --git a/bin/oop/ch07/clock2/Setting.class b/bin/oop/ch07/clock2/Setting.class new file mode 100644 index 0000000..0fc11cf Binary files /dev/null and b/bin/oop/ch07/clock2/Setting.class differ diff --git a/bin/oop/ch07/clock2/State.class b/bin/oop/ch07/clock2/State.class new file mode 100644 index 0000000..73f0ca6 Binary files /dev/null and b/bin/oop/ch07/clock2/State.class differ diff --git a/bin/oop/ch07/clock3/Clock$1.class b/bin/oop/ch07/clock3/Clock$1.class new file mode 100644 index 0000000..4e21f04 Binary files /dev/null and b/bin/oop/ch07/clock3/Clock$1.class differ diff --git a/bin/oop/ch07/clock3/Clock$2.class b/bin/oop/ch07/clock3/Clock$2.class new file mode 100644 index 0000000..c028b12 Binary files /dev/null and b/bin/oop/ch07/clock3/Clock$2.class differ diff --git a/bin/oop/ch07/clock3/Clock$State.class b/bin/oop/ch07/clock3/Clock$State.class new file mode 100644 index 0000000..d872501 Binary files /dev/null and b/bin/oop/ch07/clock3/Clock$State.class differ diff --git a/bin/oop/ch07/clock3/Clock.class b/bin/oop/ch07/clock3/Clock.class new file mode 100644 index 0000000..a40f3cf Binary files /dev/null and b/bin/oop/ch07/clock3/Clock.class differ diff --git a/bin/oop/ch07/clock3/Example.class b/bin/oop/ch07/clock3/Example.class new file mode 100644 index 0000000..c98c5b7 Binary files /dev/null and b/bin/oop/ch07/clock3/Example.class differ diff --git a/bin/oop/ch07/expr1/BinExpr.class b/bin/oop/ch07/expr1/BinExpr.class new file mode 100644 index 0000000..a6d1491 Binary files /dev/null and b/bin/oop/ch07/expr1/BinExpr.class differ diff --git a/bin/oop/ch07/expr1/Const.class b/bin/oop/ch07/expr1/Const.class new file mode 100644 index 0000000..eecfcea Binary files /dev/null and b/bin/oop/ch07/expr1/Const.class differ diff --git a/bin/oop/ch07/expr1/Example.class b/bin/oop/ch07/expr1/Example.class new file mode 100644 index 0000000..71bf30c Binary files /dev/null and b/bin/oop/ch07/expr1/Example.class differ diff --git a/bin/oop/ch07/expr1/Expr.class b/bin/oop/ch07/expr1/Expr.class new file mode 100644 index 0000000..b96ecea Binary files /dev/null and b/bin/oop/ch07/expr1/Expr.class differ diff --git a/bin/oop/ch07/expr1/Minus.class b/bin/oop/ch07/expr1/Minus.class new file mode 100644 index 0000000..98e3f28 Binary files /dev/null and b/bin/oop/ch07/expr1/Minus.class differ diff --git a/bin/oop/ch07/expr1/Mult.class b/bin/oop/ch07/expr1/Mult.class new file mode 100644 index 0000000..86b29ab Binary files /dev/null and b/bin/oop/ch07/expr1/Mult.class differ diff --git a/bin/oop/ch07/expr1/Neg.class b/bin/oop/ch07/expr1/Neg.class new file mode 100644 index 0000000..dec0d6b Binary files /dev/null and b/bin/oop/ch07/expr1/Neg.class differ diff --git a/bin/oop/ch07/expr1/Plus.class b/bin/oop/ch07/expr1/Plus.class new file mode 100644 index 0000000..2e5c615 Binary files /dev/null and b/bin/oop/ch07/expr1/Plus.class differ diff --git a/bin/oop/ch07/expr2/BinExpr.class b/bin/oop/ch07/expr2/BinExpr.class new file mode 100644 index 0000000..d4e76ca Binary files /dev/null and b/bin/oop/ch07/expr2/BinExpr.class differ diff --git a/bin/oop/ch07/expr2/Const.class b/bin/oop/ch07/expr2/Const.class new file mode 100644 index 0000000..d3d981d Binary files /dev/null and b/bin/oop/ch07/expr2/Const.class differ diff --git a/bin/oop/ch07/expr2/EvalVisitor.class b/bin/oop/ch07/expr2/EvalVisitor.class new file mode 100644 index 0000000..6f776f2 Binary files /dev/null and b/bin/oop/ch07/expr2/EvalVisitor.class differ diff --git a/bin/oop/ch07/expr2/Example.class b/bin/oop/ch07/expr2/Example.class new file mode 100644 index 0000000..e7a3471 Binary files /dev/null and b/bin/oop/ch07/expr2/Example.class differ diff --git a/bin/oop/ch07/expr2/Expr.class b/bin/oop/ch07/expr2/Expr.class new file mode 100644 index 0000000..d6e08bb Binary files /dev/null and b/bin/oop/ch07/expr2/Expr.class differ diff --git a/bin/oop/ch07/expr2/LukasiewiczVisitor.class b/bin/oop/ch07/expr2/LukasiewiczVisitor.class new file mode 100644 index 0000000..d53c14f Binary files /dev/null and b/bin/oop/ch07/expr2/LukasiewiczVisitor.class differ diff --git a/bin/oop/ch07/expr2/Minus.class b/bin/oop/ch07/expr2/Minus.class new file mode 100644 index 0000000..95f8563 Binary files /dev/null and b/bin/oop/ch07/expr2/Minus.class differ diff --git a/bin/oop/ch07/expr2/Mult.class b/bin/oop/ch07/expr2/Mult.class new file mode 100644 index 0000000..0302cf1 Binary files /dev/null and b/bin/oop/ch07/expr2/Mult.class differ diff --git a/bin/oop/ch07/expr2/Neg.class b/bin/oop/ch07/expr2/Neg.class new file mode 100644 index 0000000..2f30f97 Binary files /dev/null and b/bin/oop/ch07/expr2/Neg.class differ diff --git a/bin/oop/ch07/expr2/Plus.class b/bin/oop/ch07/expr2/Plus.class new file mode 100644 index 0000000..0ca1ccd Binary files /dev/null and b/bin/oop/ch07/expr2/Plus.class differ diff --git a/bin/oop/ch07/expr2/Visitor.class b/bin/oop/ch07/expr2/Visitor.class new file mode 100644 index 0000000..8b5da9c Binary files /dev/null and b/bin/oop/ch07/expr2/Visitor.class differ diff --git a/bin/oop/ch07/trace/ConsoleTrace.class b/bin/oop/ch07/trace/ConsoleTrace.class new file mode 100644 index 0000000..5b2deb2 Binary files /dev/null and b/bin/oop/ch07/trace/ConsoleTrace.class differ diff --git a/bin/oop/ch07/trace/FileTrace.class b/bin/oop/ch07/trace/FileTrace.class new file mode 100644 index 0000000..50cb18e Binary files /dev/null and b/bin/oop/ch07/trace/FileTrace.class differ diff --git a/bin/oop/ch07/trace/Trace.class b/bin/oop/ch07/trace/Trace.class new file mode 100644 index 0000000..c49eb39 Binary files /dev/null and b/bin/oop/ch07/trace/Trace.class differ diff --git a/bin/oop/ch07/tree/Example.class b/bin/oop/ch07/tree/Example.class new file mode 100644 index 0000000..cf785ac Binary files /dev/null and b/bin/oop/ch07/tree/Example.class differ diff --git a/bin/oop/ch07/tree/Example2.class b/bin/oop/ch07/tree/Example2.class new file mode 100644 index 0000000..e31b9fe Binary files /dev/null and b/bin/oop/ch07/tree/Example2.class differ diff --git a/bin/oop/ch07/tree/IterablesIterator.class b/bin/oop/ch07/tree/IterablesIterator.class new file mode 100644 index 0000000..e5c6cb9 Binary files /dev/null and b/bin/oop/ch07/tree/IterablesIterator.class differ diff --git a/bin/oop/ch07/tree/Tree.class b/bin/oop/ch07/tree/Tree.class new file mode 100644 index 0000000..706fb95 Binary files /dev/null and b/bin/oop/ch07/tree/Tree.class differ diff --git a/bin/oop/ch08/gui/calc/CalcApp.class b/bin/oop/ch08/gui/calc/CalcApp.class new file mode 100644 index 0000000..110acd2 Binary files /dev/null and b/bin/oop/ch08/gui/calc/CalcApp.class differ diff --git a/bin/oop/ch08/gui/calc/CalcGUI.class b/bin/oop/ch08/gui/calc/CalcGUI.class new file mode 100644 index 0000000..8512184 Binary files /dev/null and b/bin/oop/ch08/gui/calc/CalcGUI.class differ diff --git a/bin/oop/ch08/gui/mvc/model/CounterModel.class b/bin/oop/ch08/gui/mvc/model/CounterModel.class new file mode 100644 index 0000000..f2cd1ea Binary files /dev/null and b/bin/oop/ch08/gui/mvc/model/CounterModel.class differ diff --git a/bin/oop/ch08/gui/mvc/v1/CounterApp.class b/bin/oop/ch08/gui/mvc/v1/CounterApp.class new file mode 100644 index 0000000..0d96f7a Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v1/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v1/CounterController.class b/bin/oop/ch08/gui/mvc/v1/CounterController.class new file mode 100644 index 0000000..db4493f Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v1/CounterController.class differ diff --git a/bin/oop/ch08/gui/mvc/v1/CounterModel.class b/bin/oop/ch08/gui/mvc/v1/CounterModel.class new file mode 100644 index 0000000..c3c2687 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v1/CounterModel.class differ diff --git a/bin/oop/ch08/gui/mvc/v1/CounterView.class b/bin/oop/ch08/gui/mvc/v1/CounterView.class new file mode 100644 index 0000000..998e58e Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v1/CounterView.class differ diff --git a/bin/oop/ch08/gui/mvc/v2/CounterApp.class b/bin/oop/ch08/gui/mvc/v2/CounterApp.class new file mode 100644 index 0000000..b9cd569 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v2/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v2/CounterController.class b/bin/oop/ch08/gui/mvc/v2/CounterController.class new file mode 100644 index 0000000..7ed18df Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v2/CounterController.class differ diff --git a/bin/oop/ch08/gui/mvc/v2/CounterModel.class b/bin/oop/ch08/gui/mvc/v2/CounterModel.class new file mode 100644 index 0000000..8cf0822 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v2/CounterModel.class differ diff --git a/bin/oop/ch08/gui/mvc/v2/CounterView.class b/bin/oop/ch08/gui/mvc/v2/CounterView.class new file mode 100644 index 0000000..f5e9e26 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v2/CounterView.class differ diff --git a/bin/oop/ch08/gui/mvc/v3/CounterApp.class b/bin/oop/ch08/gui/mvc/v3/CounterApp.class new file mode 100644 index 0000000..2b10b86 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v3/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v3/CounterController.class b/bin/oop/ch08/gui/mvc/v3/CounterController.class new file mode 100644 index 0000000..2f7ddad Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v3/CounterController.class differ diff --git a/bin/oop/ch08/gui/mvc/v3/CounterView.class b/bin/oop/ch08/gui/mvc/v3/CounterView.class new file mode 100644 index 0000000..f8049e0 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v3/CounterView.class differ diff --git a/bin/oop/ch08/gui/mvc/v4/CounterApp.class b/bin/oop/ch08/gui/mvc/v4/CounterApp.class new file mode 100644 index 0000000..e1e1395 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v4/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v4/CounterController.class b/bin/oop/ch08/gui/mvc/v4/CounterController.class new file mode 100644 index 0000000..e3817eb Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v4/CounterController.class differ diff --git a/bin/oop/ch08/gui/mvc/v4/CounterGUI.class b/bin/oop/ch08/gui/mvc/v4/CounterGUI.class new file mode 100644 index 0000000..a94fa68 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v4/CounterGUI.class differ diff --git a/bin/oop/ch08/gui/mvc/v5/CounterApp.class b/bin/oop/ch08/gui/mvc/v5/CounterApp.class new file mode 100644 index 0000000..2d1301c Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v5/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v5/CounterGUI$1.class b/bin/oop/ch08/gui/mvc/v5/CounterGUI$1.class new file mode 100644 index 0000000..eeec987 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v5/CounterGUI$1.class differ diff --git a/bin/oop/ch08/gui/mvc/v5/CounterGUI.class b/bin/oop/ch08/gui/mvc/v5/CounterGUI.class new file mode 100644 index 0000000..7161c51 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v5/CounterGUI.class differ diff --git a/bin/oop/ch08/gui/mvc/v6/CounterApp.class b/bin/oop/ch08/gui/mvc/v6/CounterApp.class new file mode 100644 index 0000000..777979b Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v6/CounterApp.class differ diff --git a/bin/oop/ch08/gui/mvc/v6/CounterGUI.class b/bin/oop/ch08/gui/mvc/v6/CounterGUI.class new file mode 100644 index 0000000..f5986d9 Binary files /dev/null and b/bin/oop/ch08/gui/mvc/v6/CounterGUI.class differ diff --git a/bin/oop/ch08/gui/simple/HelloWorld.class b/bin/oop/ch08/gui/simple/HelloWorld.class new file mode 100644 index 0000000..ee80d82 Binary files /dev/null and b/bin/oop/ch08/gui/simple/HelloWorld.class differ diff --git a/bin/oop/ch09/testing/Example.class b/bin/oop/ch09/testing/Example.class new file mode 100644 index 0000000..401f257 Binary files /dev/null and b/bin/oop/ch09/testing/Example.class differ diff --git a/bin/uebung03/polynomial/POLNYOMIAL.class b/bin/uebung03/polynomial/POLNYOMIAL.class new file mode 100644 index 0000000..6060e2c Binary files /dev/null and b/bin/uebung03/polynomial/POLNYOMIAL.class differ diff --git a/bin/uebung04/ByeGame.class b/bin/uebung04/ByeGame.class new file mode 100644 index 0000000..5ffee63 Binary files /dev/null and b/bin/uebung04/ByeGame.class differ diff --git a/bin/uebung04/Game.class b/bin/uebung04/Game.class new file mode 100644 index 0000000..23188ba Binary files /dev/null and b/bin/uebung04/Game.class differ diff --git a/bin/uebung04/OrdinaryGame.class b/bin/uebung04/OrdinaryGame.class new file mode 100644 index 0000000..3f6c366 Binary files /dev/null and b/bin/uebung04/OrdinaryGame.class differ diff --git a/bin/uebung04/SeededGame.class b/bin/uebung04/SeededGame.class new file mode 100644 index 0000000..654c041 Binary files /dev/null and b/bin/uebung04/SeededGame.class differ diff --git a/src/oop/ch01/chess1/Board.java b/src/oop/ch01/chess1/Board.java new file mode 100644 index 0000000..7060164 --- /dev/null +++ b/src/oop/ch01/chess1/Board.java @@ -0,0 +1,59 @@ +package oop.ch01.chess1; + +import java.util.ArrayList; + +class Board { + ArrayList pieces; + + Board() { + this.pieces = new ArrayList(); + } + + void add(Piece piece) { + if (piece.board != this) + throw new IllegalArgumentException("wrong board"); + final Piece existing = this.pieceAt(piece.row, piece.col); + if (existing != null) + throw new IllegalArgumentException("already occupied by " + + existing.toString()); + this.pieces.add(piece); + } + + 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 = this.pieceAt(row, col); + final char c = p == null ? ' ' : p.charRep(); + System.out.print("| " + c + " "); + } + System.out.println("|"); + System.out.println(" +---+---+---+---+---+---+---+---+"); + } + } + + Piece pieceAt(int row, int col) { + for (Piece p : this.pieces) + if (p.row == row && p.col == col) + return p; + return null; + } + + void check() { + for (Piece p1 : this.pieces) { + System.out.println(p1.toString()); + for (Piece p2 : this.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: " + this.pieces; + } +} diff --git a/src/oop/ch01/chess1/Chess.java b/src/oop/ch01/chess1/Chess.java new file mode 100644 index 0000000..4e73286 --- /dev/null +++ b/src/oop/ch01/chess1/Chess.java @@ -0,0 +1,15 @@ +package oop.ch01.chess1; + +class Chess { + public static void main(String[] args) { + final Board board = new Board(); + final Piece p1 = new Piece(Kind.queen, Color.black, board, 1, 4); + final Piece p2 = new Piece(Kind.queen, Color.white, board, 8, 4); + final Piece p3 = new Piece(Kind.knight, Color.white, board, 3, 3); + final Piece p4 = new Piece(Kind.knight, Color.black, board, 6, 4); + System.out.println(board); + System.out.println(board.toString()); + board.printBoard(); + board.check(); + } +} diff --git a/src/oop/ch01/chess1/Color.java b/src/oop/ch01/chess1/Color.java new file mode 100644 index 0000000..262e8c7 --- /dev/null +++ b/src/oop/ch01/chess1/Color.java @@ -0,0 +1,5 @@ +package oop.ch01.chess1; + +enum Color { + black, white +} diff --git a/src/oop/ch01/chess1/Kind.java b/src/oop/ch01/chess1/Kind.java new file mode 100644 index 0000000..a047b75 --- /dev/null +++ b/src/oop/ch01/chess1/Kind.java @@ -0,0 +1,5 @@ +package oop.ch01.chess1; + +enum Kind { + queen, knight +} diff --git a/src/oop/ch01/chess1/Piece.java b/src/oop/ch01/chess1/Piece.java new file mode 100644 index 0000000..31e7779 --- /dev/null +++ b/src/oop/ch01/chess1/Piece.java @@ -0,0 +1,87 @@ +package oop.ch01.chess1; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +class Piece { + Kind kind; + Color color; + Board board; + int row; + int col; + + Piece(Kind kind, 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.kind = kind; + this.color = color; + this.board = board; + this.row = row; + this.col = col; + board.add(this); + } + + char charRep() { + return switch (this.kind) { + case queen -> this.queenCharRep(); + case knight -> this.knightCharRep(); + }; + } + + public String toString() { + return switch (this.kind) { + case queen -> this.queenToString(); + case knight -> this.knightToString(); + }; + } + + boolean canCapture(Piece other) { + return switch (this.kind) { + case queen -> this.queenCanCapture(other); + case knight -> this.knightCanCapture(other); + }; + } + + char queenCharRep() { + return this.color == Color.white ? 'q' : 'Q'; + } + + char knightCharRep() { + return this.color == Color.white ? 'n' : 'N'; + } + + String queenToString() { + return "" + this.color + " queen at (" + this.row + ", " + this.col + ")"; + } + + String knightToString() { + return "" + this.color + " knight at (" + this.row + ", " + this.col + ")"; + } + + boolean queenCanCapture(Piece other) { + if (this.board != other.board || this.color == other.color) + return false; + if (other.row != this.row && + other.col != this.col && + abs(other.row - this.row) != abs(other.col - this.col)) + return false; + final int dr = signum(other.row - this.row); + final int dc = signum(other.col - this.col); + int r = this.row + dr; + int c = this.col + dc; + while (r != other.row || c != other.col) { + if (this.board.pieceAt(r, c) != null) return false; + r += dr; + c += dc; + } + return true; + } + + boolean knightCanCapture(Piece other) { + if (this.board != other.board || this.color == other.color) + return false; + final int dr = abs(this.row - other.row); + final int dc = abs(this.col - other.col); + return dr == 2 && dc == 1 || dr == 1 && dc == 2; + } +} diff --git a/src/oop/ch01/chess1/package-info.java b/src/oop/ch01/chess1/package-info.java new file mode 100644 index 0000000..ca7751f --- /dev/null +++ b/src/oop/ch01/chess1/package-info.java @@ -0,0 +1,4 @@ +/** + * Using objects with methods for the Java solution of the chess problem. + */ +package oop.ch01.chess1; \ No newline at end of file diff --git a/src/oop/ch01/chess2/Board.java b/src/oop/ch01/chess2/Board.java new file mode 100644 index 0000000..6d2b483 --- /dev/null +++ b/src/oop/ch01/chess2/Board.java @@ -0,0 +1,59 @@ +package oop.ch01.chess2; + +import java.util.ArrayList; + +class Board { + ArrayList pieces; + + Board() { + this.pieces = new ArrayList(); + } + + void add(Piece piece) { + if (piece.board != this) + throw new IllegalArgumentException("wrong board"); + final Piece existing = pieceAt(piece.row, piece.col); + if (existing != null) + throw new IllegalArgumentException("already occupied by " + + existing.toString()); + pieces.add(piece); + } + + 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(" +---+---+---+---+---+---+---+---+"); + } + } + + Piece pieceAt(int row, int col) { + for (Piece p : pieces) + if (p.row == row && p.col == col) + return p; + return null; + } + + 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/oop/ch01/chess2/Chess.java b/src/oop/ch01/chess2/Chess.java new file mode 100644 index 0000000..3a7964e --- /dev/null +++ b/src/oop/ch01/chess2/Chess.java @@ -0,0 +1,15 @@ +package oop.ch01.chess2; + +class Chess { + public static void main(String[] args) { + final Board board = new Board(); + final Piece p1 = new Piece(Kind.queen, Color.black, board, 1, 4); + final Piece p2 = new Piece(Kind.queen, Color.white, board, 8, 4); + final Piece p3 = new Piece(Kind.knight, Color.white, board, 3, 3); + final Piece p4 = new Piece(Kind.knight, Color.black, board, 6, 4); + System.out.println(board); + System.out.println(board.toString()); + board.printBoard(); + board.check(); + } +} diff --git a/src/oop/ch01/chess2/Color.java b/src/oop/ch01/chess2/Color.java new file mode 100644 index 0000000..94180cb --- /dev/null +++ b/src/oop/ch01/chess2/Color.java @@ -0,0 +1,5 @@ +package oop.ch01.chess2; + +enum Color { + black, white +} diff --git a/src/oop/ch01/chess2/Kind.java b/src/oop/ch01/chess2/Kind.java new file mode 100644 index 0000000..acaf4be --- /dev/null +++ b/src/oop/ch01/chess2/Kind.java @@ -0,0 +1,5 @@ +package oop.ch01.chess2; + +enum Kind { + queen, knight +} diff --git a/src/oop/ch01/chess2/Piece.java b/src/oop/ch01/chess2/Piece.java new file mode 100644 index 0000000..6ebbecc --- /dev/null +++ b/src/oop/ch01/chess2/Piece.java @@ -0,0 +1,87 @@ +package oop.ch01.chess2; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +class Piece { + Kind kind; + Color color; + Board board; + int row; + int col; + + Piece(Kind kind, 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.kind = kind; + this.color = color; + this.board = board; + this.row = row; + this.col = col; + board.add(this); + } + + char charRep() { + return switch (kind) { + case queen -> queenCharRep(); + case knight -> knightCharRep(); + }; + } + + public String toString() { + return switch (kind) { + case queen -> queenToString(); + case knight -> knightToString(); + }; + } + + boolean canCapture(Piece other) { + return switch (kind) { + case queen -> queenCanCapture(other); + case knight -> knightCanCapture(other); + }; + } + + char queenCharRep() { + return color == Color.white ? 'q' : 'Q'; + } + + char knightCharRep() { + return color == Color.white ? 'n' : 'N'; + } + + String queenToString() { + return "" + color + " queen at (" + row + ", " + col + ")"; + } + + String knightToString() { + return "" + color + " knight at (" + row + ", " + col + ")"; + } + + boolean queenCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + if (other.row != row && + other.col != col && + abs(other.row - row) != abs(other.col - col)) + return false; + final int dr = signum(other.row - row); + final int dc = signum(other.col - col); + int r = row + dr; + int c = col + dc; + while (r != other.row || c != other.col) { + if (board.pieceAt(r, c) != null) return false; + r += dr; + c += dc; + } + return true; + } + + boolean knightCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + final int dr = abs(row - other.row); + final int dc = abs(col - other.col); + return dr == 2 && dc == 1 || dr == 1 && dc == 2; + } +} diff --git a/src/oop/ch01/chess2/package-info.java b/src/oop/ch01/chess2/package-info.java new file mode 100644 index 0000000..fda7a58 --- /dev/null +++ b/src/oop/ch01/chess2/package-info.java @@ -0,0 +1,5 @@ +/** + * Using objects with methods for the Java solution of the chess problem. + * Qualification of members with "this." is avoided where possible. + */ +package oop.ch01.chess2; \ No newline at end of file diff --git a/src/oop/ch02/cards/Card.java b/src/oop/ch02/cards/Card.java new file mode 100644 index 0000000..c903159 --- /dev/null +++ b/src/oop/ch02/cards/Card.java @@ -0,0 +1,79 @@ +package oop.ch02.cards; + +/** + * Represents any card with a name and a unique serial number. + * + * @author Mark Minas + */ +public class Card { + /** + * The name of this card. + */ + private final String name; + + /** + * Counts the number of instantiations of this class. + */ + private static int counter = 0; + + /** + * The unique serial number of this card. + */ + private final int number; + + /** + * Creates a new card with the specified name. + * + * @param name the name of this card + */ + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++counter; + } + + /** + * Creates a new card for a student with the specified number. + * + * @param num the student's number + */ + public Card(int num) { + this("MatrNr. " + num); + } + + /** + * Returns the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Returns the serial number of this card. + * + * @return the serial number + */ + public int getNumber() { + return number; + } + + /** + * Returns the number of instantiations of this class. + * + * @return the number of instantiations. + */ + public static int getCounter() { + return counter; + } + + /** + * Returns a string representation of this card. + */ + @Override + public String toString() { + return "Card " + name + " (no " + number + ")"; + } +} diff --git a/src/oop/ch02/cards/CardExample.java b/src/oop/ch02/cards/CardExample.java new file mode 100644 index 0000000..1d1a094 --- /dev/null +++ b/src/oop/ch02/cards/CardExample.java @@ -0,0 +1,20 @@ +package oop.ch02.cards; + +import java.util.Arrays; + +public class CardExample { + public static void main(String[] args) { + Card c1 = new Card(12345); + System.out.println(c1 + " erstellt."); + + String[] names = {"Franz Mueller", "Hans Mustermann", "Peter Meier"}; + Card[] cards = new Card[names.length]; + for (int i = 0; i < names.length; i++) { + cards[i] = new Card(names[i]); + System.out.println(cards[i] + " erstellt."); + } + System.out.println("cards = " + cards); + System.out.println("cards = " + Arrays.deepToString(cards)); + System.out.println("#Card = " + Card.getCounter()); + } +} diff --git a/src/oop/ch02/cards0/Card.java b/src/oop/ch02/cards0/Card.java new file mode 100644 index 0000000..474fef9 --- /dev/null +++ b/src/oop/ch02/cards0/Card.java @@ -0,0 +1,34 @@ +package oop.ch02.cards0; + +public class Card { + private final String name; + private static int counter = 0; + private final int number; + + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++Card.counter; + } + + public Card(int num) { + this("MatrNr. " + num); + } + + public String getName() { + return name; + } + + public int getNumber() { + return number; + } + + public static int getCounter() { + return Card.counter; + } + + public String toString() { + return "Card " + name + " (no " + number + ")"; + } +} diff --git a/src/oop/ch02/chess1/Board.java b/src/oop/ch02/chess1/Board.java new file mode 100644 index 0000000..65bfeb1 --- /dev/null +++ b/src/oop/ch02/chess1/Board.java @@ -0,0 +1,59 @@ +package oop.ch02.chess1; + +import java.util.ArrayList; + +public class Board { + private ArrayList pieces; + + public Board() { + this.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.toString()); + 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) { + for (Piece p : pieces) + if (p.getRow() == row && p.getCol() == col) + return p; + return null; + } + + 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/oop/ch02/chess1/Chess.java b/src/oop/ch02/chess1/Chess.java new file mode 100644 index 0000000..555b382 --- /dev/null +++ b/src/oop/ch02/chess1/Chess.java @@ -0,0 +1,15 @@ +package oop.ch02.chess1; + +public class Chess { + public static void main(String[] args) { + final Board board = new Board(); + final Piece p1 = new Piece(Kind.queen, Color.black, board, 1, 4); + final Piece p2 = new Piece(Kind.queen, Color.white, board, 8, 4); + final Piece p3 = new Piece(Kind.knight, Color.white, board, 3, 3); + final Piece p4 = new Piece(Kind.knight, Color.black, board, 6, 4); + System.out.println(board); + System.out.println(board.toString()); + board.printBoard(); + board.check(); + } +} diff --git a/src/oop/ch02/chess1/Color.java b/src/oop/ch02/chess1/Color.java new file mode 100644 index 0000000..4cfb647 --- /dev/null +++ b/src/oop/ch02/chess1/Color.java @@ -0,0 +1,5 @@ +package oop.ch02.chess1; + +public enum Color { + black, white +} diff --git a/src/oop/ch02/chess1/Kind.java b/src/oop/ch02/chess1/Kind.java new file mode 100644 index 0000000..f0db5d2 --- /dev/null +++ b/src/oop/ch02/chess1/Kind.java @@ -0,0 +1,5 @@ +package oop.ch02.chess1; + +public enum Kind { + queen, knight +} diff --git a/src/oop/ch02/chess1/Piece.java b/src/oop/ch02/chess1/Piece.java new file mode 100644 index 0000000..9931f99 --- /dev/null +++ b/src/oop/ch02/chess1/Piece.java @@ -0,0 +1,107 @@ +package oop.ch02.chess1; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +public class Piece { + private Kind kind; + private Color color; + private Board board; + private int row; + private int col; + + public Piece(Kind kind, 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.kind = kind; + this.color = color; + this.board = board; + this.row = row; + this.col = col; + board.add(this); + } + + public Kind getKind() { + return kind; + } + + public Color getColor() { + return color; + } + + public Board getBoard() { + return board; + } + + public int getRow() { + return row; + } + + public int getCol() { + return col; + } + + public char charRep() { + return switch (kind) { + case queen -> queenCharRep(); + case knight -> knightCharRep(); + }; + } + + public String toString() { + return switch (kind) { + case queen -> queenToString(); + case knight -> knightToString(); + }; + } + + public boolean canCapture(Piece other) { + return switch (kind) { + case queen -> queenCanCapture(other); + case knight -> knightCanCapture(other); + }; + } + + private char queenCharRep() { + return color == Color.white ? 'q' : 'Q'; + } + + private char knightCharRep() { + return color == Color.white ? 'n' : 'N'; + } + + private String queenToString() { + return "" + color + " queen at (" + row + ", " + col + ")"; + } + + private String knightToString() { + return "" + color + " knight at (" + row + ", " + col + ")"; + } + + private boolean queenCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + if (other.row != row && + other.col != col && + abs(other.row - row) != abs(other.col - col)) + return false; + final int dr = signum(other.row - row); + final int dc = signum(other.col - col); + int r = row + dr; + int c = col + dc; + while (r != other.row || c != other.col) { + if (board.pieceAt(r, c) != null) return false; + r += dr; + c += dc; + } + return true; + } + + private boolean knightCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + final int dr = abs(row - other.row); + final int dc = abs(col - other.col); + return dr == 2 && dc == 1 || dr == 1 && dc == 2; + } +} diff --git a/src/oop/ch02/chess1/package-info.java b/src/oop/ch02/chess1/package-info.java new file mode 100644 index 0000000..1ce45e6 --- /dev/null +++ b/src/oop/ch02/chess1/package-info.java @@ -0,0 +1,4 @@ +/** + * Using access modifiers in the Java solution of the chess problem. + */ +package oop.ch02.chess1; \ No newline at end of file diff --git a/src/oop/ch02/chess2/Board.java b/src/oop/ch02/chess2/Board.java new file mode 100644 index 0000000..8bbcf20 --- /dev/null +++ b/src/oop/ch02/chess2/Board.java @@ -0,0 +1,54 @@ +package oop.ch02.chess2; + +import java.util.ArrayList; + +public class Board { + private final Piece[][] field = new Piece[8][8]; + private final ArrayList 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.toString()); + 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/oop/ch02/chess2/Chess.java b/src/oop/ch02/chess2/Chess.java new file mode 100644 index 0000000..b7dd76b --- /dev/null +++ b/src/oop/ch02/chess2/Chess.java @@ -0,0 +1,15 @@ +package oop.ch02.chess2; + +public class Chess { + public static void main(String[] args) { + final Board board = new Board(); + final Piece p1 = new Piece(Kind.queen, Color.black, board, 1, 4); + final Piece p2 = new Piece(Kind.queen, Color.white, board, 8, 4); + final Piece p3 = new Piece(Kind.knight, Color.white, board, 3, 3); + final Piece p4 = new Piece(Kind.knight, Color.black, board, 6, 4); + System.out.println(board); + System.out.println(board.toString()); + board.printBoard(); + board.check(); + } +} diff --git a/src/oop/ch02/chess2/Color.java b/src/oop/ch02/chess2/Color.java new file mode 100644 index 0000000..17f158f --- /dev/null +++ b/src/oop/ch02/chess2/Color.java @@ -0,0 +1,5 @@ +package oop.ch02.chess2; + +public enum Color { + black, white +} diff --git a/src/oop/ch02/chess2/Kind.java b/src/oop/ch02/chess2/Kind.java new file mode 100644 index 0000000..18341b6 --- /dev/null +++ b/src/oop/ch02/chess2/Kind.java @@ -0,0 +1,5 @@ +package oop.ch02.chess2; + +public enum Kind { + queen, knight +} diff --git a/src/oop/ch02/chess2/Piece.java b/src/oop/ch02/chess2/Piece.java new file mode 100644 index 0000000..6e6d02f --- /dev/null +++ b/src/oop/ch02/chess2/Piece.java @@ -0,0 +1,107 @@ +package oop.ch02.chess2; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +public class Piece { + private Kind kind; + private Color color; + private Board board; + private int row; + private int col; + + public Piece(Kind kind, 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.kind = kind; + this.color = color; + this.board = board; + this.row = row; + this.col = col; + board.add(this); + } + + public Kind getKind() { + return kind; + } + + public Color getColor() { + return color; + } + + public Board getBoard() { + return board; + } + + public int getRow() { + return row; + } + + public int getCol() { + return col; + } + + public char charRep() { + return switch (kind) { + case queen -> queenCharRep(); + case knight -> knightCharRep(); + }; + } + + public String toString() { + return switch (kind) { + case queen -> queenToString(); + case knight -> knightToString(); + }; + } + + public boolean canCapture(Piece other) { + return switch (kind) { + case queen -> queenCanCapture(other); + case knight -> knightCanCapture(other); + }; + } + + private char queenCharRep() { + return color == Color.white ? 'q' : 'Q'; + } + + private char knightCharRep() { + return color == Color.white ? 'n' : 'N'; + } + + private String queenToString() { + return "" + color + " queen at (" + row + ", " + col + ")"; + } + + private String knightToString() { + return "" + color + " knight at (" + row + ", " + col + ")"; + } + + private boolean queenCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + if (other.row != row && + other.col != col && + abs(other.row - row) != abs(other.col - col)) + return false; + final int dr = signum(other.row - row); + final int dc = signum(other.col - col); + int r = row + dr; + int c = col + dc; + while (r != other.row || c != other.col) { + if (board.pieceAt(r, c) != null) return false; + r += dr; + c += dc; + } + return true; + } + + private boolean knightCanCapture(Piece other) { + if (board != other.board || color == other.color) + return false; + final int dr = abs(row - other.row); + final int dc = abs(col - other.col); + return dr == 2 && dc == 1 || dr == 1 && dc == 2; + } +} diff --git a/src/oop/ch02/chess2/package-info.java b/src/oop/ch02/chess2/package-info.java new file mode 100644 index 0000000..7fa0868 --- /dev/null +++ b/src/oop/ch02/chess2/package-info.java @@ -0,0 +1,4 @@ +/** + * Using arrays for speeding up the Java solution of the chess problem. + */ +package oop.ch02.chess2; \ No newline at end of file diff --git a/src/oop/ch02/clock/Clock1.java b/src/oop/ch02/clock/Clock1.java new file mode 100644 index 0000000..7b57fba --- /dev/null +++ b/src/oop/ch02/clock/Clock1.java @@ -0,0 +1,33 @@ +package oop.ch02.clock; + +public class Clock1 { + private int hour; + private int minute; + + public Clock1(int hour, int minute) { + if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) + throw new IllegalArgumentException("Invalid time " + hour + ":" + minute); + this.hour = hour; + this.minute = minute; + } + + public int getHour() { + return hour; + } + + public void setHour(int hour) { + if (hour < 0 || hour >= 24) + throw new IllegalArgumentException("Invalid hour " + hour); + this.hour = hour; + } + + public int getMinute() { + return minute; + } + + public void setMinute(int minute) { + if (minute < 0 || minute >= 60) + throw new IllegalArgumentException("Invalid minute " + minute); + this.minute = minute; + } +} diff --git a/src/oop/ch02/clock/Clock2.java b/src/oop/ch02/clock/Clock2.java new file mode 100644 index 0000000..72063f4 --- /dev/null +++ b/src/oop/ch02/clock/Clock2.java @@ -0,0 +1,27 @@ +package oop.ch02.clock; + +public class Clock2 { + private int hour; + private int minute; + + public Clock2(int hour, int minute) { + if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) + throw new IllegalArgumentException("Invalid time " + hour + ":" + minute); + this.hour = hour; + this.minute = minute; + } + + public int getHour() { + return hour; + } + + public int getMinute() { + return minute; + } + + public void tick() { + minute = (minute + 1) % 60; + if (minute == 0) + hour = (hour + 1) % 24; + } +} diff --git a/src/oop/ch02/clock/Clock3.java b/src/oop/ch02/clock/Clock3.java new file mode 100644 index 0000000..b3a6e4e --- /dev/null +++ b/src/oop/ch02/clock/Clock3.java @@ -0,0 +1,24 @@ +package oop.ch02.clock; + +public class Clock3 { + public static final int MINUTES_PER_DAY = 60 * 24; + private int mins; + + public Clock3(int hour, int minute) { + if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) + throw new IllegalArgumentException("Invalid time " + hour + ":" + minute); + this.mins = 60 * hour + minute; + } + + public int getHour() { + return mins / 60; + } + + public int getMinute() { + return mins % 60; + } + + public void tick() { + mins = (mins + 1) % MINUTES_PER_DAY; + } +} diff --git a/src/oop/ch02/clock/Example.java b/src/oop/ch02/clock/Example.java new file mode 100644 index 0000000..1d9e9d2 --- /dev/null +++ b/src/oop/ch02/clock/Example.java @@ -0,0 +1,13 @@ +package oop.ch02.clock; + +public class Example { + public static void main(String[] args) { + Clock3 clock = new Clock3(22, 55); + for (int i = 0; i < 7; i++) + clock.tick(); + System.out.printf("%02d:%02d%n", clock.getHour(), clock.getMinute()); + for (int i = 0; i < 60; i++) + clock.tick(); + System.out.printf("%02d:%02d%n", clock.getHour(), clock.getMinute()); + } +} diff --git a/src/oop/ch03/chess/Board.java b/src/oop/ch03/chess/Board.java new file mode 100644 index 0000000..ce68e0a --- /dev/null +++ b/src/oop/ch03/chess/Board.java @@ -0,0 +1,54 @@ +package oop.ch03.chess; + +import java.util.ArrayList; + +public class Board { + private final Piece[][] field = new Piece[8][8]; + private final ArrayList 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.toString()); + 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/oop/ch03/chess/Chess.java b/src/oop/ch03/chess/Chess.java new file mode 100644 index 0000000..fbaf04d --- /dev/null +++ b/src/oop/ch03/chess/Chess.java @@ -0,0 +1,17 @@ +package oop.ch03.chess; + +public class Chess { + public static void main(String[] args) { + final Board board = new Board(); + final Piece p1 = new Queen(Color.black, board, 1, 4); + final Piece p2 = new Queen(Color.white, board, 8, 4); + final Piece p3 = new Knight(Color.white, board, 3, 3); + final Piece p4 = new Knight(Color.black, board, 6, 4); + final Piece p5 = new Rook(Color.white, board, 7, 1); + final Piece p6 = new Rook(Color.black, board, 4, 4); + System.out.println(board); + System.out.println(board.toString()); + board.printBoard(); + board.check(); + } +} diff --git a/src/oop/ch03/chess/Color.java b/src/oop/ch03/chess/Color.java new file mode 100644 index 0000000..38e2090 --- /dev/null +++ b/src/oop/ch03/chess/Color.java @@ -0,0 +1,5 @@ +package oop.ch03.chess; + +public enum Color { + black, white +} diff --git a/src/oop/ch03/chess/Knight.java b/src/oop/ch03/chess/Knight.java new file mode 100644 index 0000000..85cc962 --- /dev/null +++ b/src/oop/ch03/chess/Knight.java @@ -0,0 +1,28 @@ +package oop.ch03.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 "" + getColor() + " knight at (" + 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/oop/ch03/chess/Piece.java b/src/oop/ch03/chess/Piece.java new file mode 100644 index 0000000..cd5b1f7 --- /dev/null +++ b/src/oop/ch03/chess/Piece.java @@ -0,0 +1,38 @@ +package oop.ch03.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/oop/ch03/chess/Queen.java b/src/oop/ch03/chess/Queen.java new file mode 100644 index 0000000..eee11c7 --- /dev/null +++ b/src/oop/ch03/chess/Queen.java @@ -0,0 +1,40 @@ +package oop.ch03.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 "" + getColor() + " queen at (" + 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/oop/ch03/chess/Rook.java b/src/oop/ch03/chess/Rook.java new file mode 100644 index 0000000..dfc8b4c --- /dev/null +++ b/src/oop/ch03/chess/Rook.java @@ -0,0 +1,41 @@ +package oop.ch03.chess; + +import static java.lang.Integer.signum; +import static java.lang.Math.abs; + +public class Rook extends Piece{ + + public Rook(Color color, Board board, int row, int col){ + super(color, board, row, col); + } + + @Override + public char charRep(){ + return getColor() == Color.white ? 'r' : 'R'; + } + + @Override + public boolean canCapture(Piece other){ + if (getBoard() != other.getBoard() || getColor() == other.getColor()) { + return false; + } + if(other.getRow() != getRow() && 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/oop/ch03/chess/package-info.java b/src/oop/ch03/chess/package-info.java new file mode 100644 index 0000000..0277055 --- /dev/null +++ b/src/oop/ch03/chess/package-info.java @@ -0,0 +1,4 @@ +/** + * Using inheritance and dynamic binding in the Java solution of the chess problem. + */ +package oop.ch03.chess; \ No newline at end of file diff --git a/src/oop/ch03/vehicles/AmphibienBsp.java b/src/oop/ch03/vehicles/AmphibienBsp.java new file mode 100644 index 0000000..5d83ad6 --- /dev/null +++ b/src/oop/ch03/vehicles/AmphibienBsp.java @@ -0,0 +1,26 @@ +package oop.ch03.vehicles; + +import java.util.List; + +public class AmphibienBsp { + public static void main(String[] args) { + final Auto golf = new AutoImpl(500); + final Schiff titanic = new SchiffImpl(53150E3); + final Amphibienfahrzeug alligator = new AmphibienfahrzeugImpl(750, 8000); + + System.out.println("Fahrzeuge:"); + final List fahrzeuge = List.of(golf, titanic, alligator); + for (Fahrzeug fz : fahrzeuge) + System.out.println(fz + ", zuladung: " + fz.getMaxZuladung()); + + System.out.println("Autos:"); + final List autos = List.of(golf, alligator); + for (Auto auto : autos) + System.out.println(auto + ", zuladung: " + auto.getMaxZuladung()); + + System.out.println("Schiffe:"); + final List schiffe = List.of(titanic, alligator); + for (Schiff schiff : schiffe) + System.out.println(schiff + ", zuladung: " + schiff.getMaxZuladung()); + } +} diff --git a/src/oop/ch03/vehicles/Amphibienfahrzeug.java b/src/oop/ch03/vehicles/Amphibienfahrzeug.java new file mode 100644 index 0000000..425bed6 --- /dev/null +++ b/src/oop/ch03/vehicles/Amphibienfahrzeug.java @@ -0,0 +1,3 @@ +package oop.ch03.vehicles; + +public interface Amphibienfahrzeug extends Auto, Schiff {} diff --git a/src/oop/ch03/vehicles/AmphibienfahrzeugImpl.java b/src/oop/ch03/vehicles/AmphibienfahrzeugImpl.java new file mode 100644 index 0000000..0accef3 --- /dev/null +++ b/src/oop/ch03/vehicles/AmphibienfahrzeugImpl.java @@ -0,0 +1,27 @@ +package oop.ch03.vehicles; + +public class AmphibienfahrzeugImpl extends AutoImpl implements Amphibienfahrzeug { + private final SchiffImpl schiff; + + public AmphibienfahrzeugImpl(double maxAutoZuladung, + double maxSchiffZuladung) { + super(maxAutoZuladung); + this.schiff = new SchiffImpl(maxSchiffZuladung); + } + + @Override + public boolean kannHierFahren(double laenge, double breite) { + return super.kannHierFahren(laenge, breite) || + schiff.kannHierFahren(laenge, breite); + } + + @Override + public double getMaxZuladung() { + return Math.max(super.getMaxZuladung(), schiff.getMaxZuladung()); + } + + @Override + public double getTiefgang() { + return schiff.getTiefgang(); + } +} diff --git a/src/oop/ch03/vehicles/AmphibienfahrzeugImpl2.java b/src/oop/ch03/vehicles/AmphibienfahrzeugImpl2.java new file mode 100644 index 0000000..c9ac8b1 --- /dev/null +++ b/src/oop/ch03/vehicles/AmphibienfahrzeugImpl2.java @@ -0,0 +1,27 @@ +package oop.ch03.vehicles; + +public class AmphibienfahrzeugImpl2 extends SchiffImpl implements Amphibienfahrzeug { + private final AutoImpl auto; + + public AmphibienfahrzeugImpl2(double maxAutoZuladung, + double maxSchiffZuladung) { + super(maxSchiffZuladung); + this.auto = new AutoImpl(maxAutoZuladung); + } + + @Override + public boolean kannHierFahren(double laenge, double breite) { + return super.kannHierFahren(laenge, breite) || + auto.kannHierFahren(laenge, breite); + } + + @Override + public double getMaxZuladung() { + return Math.max(super.getMaxZuladung(), auto.getMaxZuladung()); + } + + @Override + public double getAchslast() { + return auto.getAchslast(); + } +} diff --git a/src/oop/ch03/vehicles/Auto.java b/src/oop/ch03/vehicles/Auto.java new file mode 100644 index 0000000..a14fb18 --- /dev/null +++ b/src/oop/ch03/vehicles/Auto.java @@ -0,0 +1,5 @@ +package oop.ch03.vehicles; + +public interface Auto extends Fahrzeug { + double getAchslast(); +} diff --git a/src/oop/ch03/vehicles/AutoImpl.java b/src/oop/ch03/vehicles/AutoImpl.java new file mode 100644 index 0000000..e1b41e3 --- /dev/null +++ b/src/oop/ch03/vehicles/AutoImpl.java @@ -0,0 +1,22 @@ +package oop.ch03.vehicles; + +public class AutoImpl extends FahrzeugImpl implements Auto { + public AutoImpl(double maxZuladung) { + super(maxZuladung); + } + + @Override + public double getAchslast() { + return maxZuladung; + } + + @Override + public boolean kannHierFahren(double laenge, double breite) { + return aufStrasse(laenge, breite) && getGewicht() < maxZuladung; + } + + private boolean aufStrasse(double laenge, double breite) { + // TODO implement + return false; + } +} diff --git a/src/oop/ch03/vehicles/Fahrzeug.java b/src/oop/ch03/vehicles/Fahrzeug.java new file mode 100644 index 0000000..c66caa2 --- /dev/null +++ b/src/oop/ch03/vehicles/Fahrzeug.java @@ -0,0 +1,9 @@ +package oop.ch03.vehicles; + +public interface Fahrzeug { + double getGewicht(); + + double getMaxZuladung(); + + boolean kannHierFahren(double laenge, double breite); +} diff --git a/src/oop/ch03/vehicles/FahrzeugImpl.java b/src/oop/ch03/vehicles/FahrzeugImpl.java new file mode 100644 index 0000000..07911a0 --- /dev/null +++ b/src/oop/ch03/vehicles/FahrzeugImpl.java @@ -0,0 +1,20 @@ +package oop.ch03.vehicles; + +public abstract class FahrzeugImpl implements Fahrzeug { + protected final double maxZuladung; + + protected FahrzeugImpl(double maxZuladung) { + this.maxZuladung = maxZuladung; + } + + @Override + public double getGewicht() { + // TODO implement + return 1000.; + } + + @Override + public double getMaxZuladung() { + return maxZuladung; + } +} diff --git a/src/oop/ch03/vehicles/Schiff.java b/src/oop/ch03/vehicles/Schiff.java new file mode 100644 index 0000000..7ed16cb --- /dev/null +++ b/src/oop/ch03/vehicles/Schiff.java @@ -0,0 +1,5 @@ +package oop.ch03.vehicles; + +public interface Schiff extends Fahrzeug { + double getTiefgang(); +} diff --git a/src/oop/ch03/vehicles/SchiffImpl.java b/src/oop/ch03/vehicles/SchiffImpl.java new file mode 100644 index 0000000..f53d8c4 --- /dev/null +++ b/src/oop/ch03/vehicles/SchiffImpl.java @@ -0,0 +1,23 @@ +package oop.ch03.vehicles; + +public class SchiffImpl extends FahrzeugImpl implements Schiff { + public SchiffImpl(double maxZuladung) { + super(maxZuladung); + } + + @Override + public double getTiefgang() { + // TODO implement + return 1.; + } + + @Override + public boolean kannHierFahren(double laenge, double breite) { + return aufWasser(laenge, breite) && getGewicht() < maxZuladung; + } + + private boolean aufWasser(double laenge, double breite) { + // TODO implement + return false; + } +} diff --git a/src/oop/ch04/equality/Example.java b/src/oop/ch04/equality/Example.java new file mode 100644 index 0000000..5dddddb --- /dev/null +++ b/src/oop/ch04/equality/Example.java @@ -0,0 +1,27 @@ +package oop.ch04.equality; + +import oop.ch04.equality.card1.Card; + +public class Example { + public static void main(String[] args) { + Card c1 = new Card("foo"); + Card c2 = new Card("foo"); + System.out.println(c1 == c2); + + int i1 = 42; + int i2 = 2 * 21; + System.out.println(i1 == i2); + + double f1 = 0.5; + double f2 = 1.0 / 2.0; + System.out.println(f1 == f2); + + String s1 = "OOP"; + String s2 = "O" + "OP"; + System.out.println(s1 == s2); + + String s3 = "OOP".substring(1); + String s4 = "OP"; + System.out.println(s3 == s4); + } +} diff --git a/src/oop/ch04/equality/card1/Card.java b/src/oop/ch04/equality/card1/Card.java new file mode 100644 index 0000000..d8015b3 --- /dev/null +++ b/src/oop/ch04/equality/card1/Card.java @@ -0,0 +1,89 @@ +package oop.ch04.equality.card1; + +/** + * Represents any card with a name and a unique serial number. + * + * @author Mark Minas + */ +public class Card { + /** + * The name of this card. + */ + private final String name; + + /** + * Counts the number of instantiations of this class. + */ + private static int counter = 0; + + /** + * The unique serial number of this card. + */ + private final int number; + + /** + * Creates a new card with the specified name. + * + * @param name the name of this card + */ + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++counter; + } + + /** + * Creates a new card for a student with the specified number. + * + * @param num the student's number + */ + public Card(int num) { + this("MatrNr. " + num); + } + + /** + * Returns the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Returns the serial number of this card. + * + * @return the serial number + */ + public int getNumber() { + return number; + } + + /** + * Returns the number of instantiations of this class. + * + * @return the number of instantiations. + */ + public static int getCounter() { + return counter; + } + + /** + * Returns a string representation of this card. + */ + @Override + public String toString() { + return "Card " + name + " (no " + number + ")"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof Card) { + final Card that = (Card) o; + return this.name.equals(that.name); + } + return false; + } +} diff --git a/src/oop/ch04/equality/card1/Color.java b/src/oop/ch04/equality/card1/Color.java new file mode 100644 index 0000000..da0403b --- /dev/null +++ b/src/oop/ch04/equality/card1/Color.java @@ -0,0 +1,5 @@ +package oop.ch04.equality.card1; + +public enum Color { + red, green, blue +} diff --git a/src/oop/ch04/equality/card1/ColoredCard.java b/src/oop/ch04/equality/card1/ColoredCard.java new file mode 100644 index 0000000..b387af6 --- /dev/null +++ b/src/oop/ch04/equality/card1/ColoredCard.java @@ -0,0 +1,26 @@ +package oop.ch04.equality.card1; + +public class ColoredCard extends Card { + public final Color color; + + public ColoredCard(String key, Color color) { + super(key); + this.color = color; + } + + @Override + public String toString() { + return color + " " + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof ColoredCard) { + final ColoredCard that = (ColoredCard) o; + return this.color == that.color && + super.equals(that); + } + return false; + } +} diff --git a/src/oop/ch04/equality/card1/Example.java b/src/oop/ch04/equality/card1/Example.java new file mode 100644 index 0000000..ddd868c --- /dev/null +++ b/src/oop/ch04/equality/card1/Example.java @@ -0,0 +1,11 @@ +package oop.ch04.equality.card1; + +public class Example { + public static void main(String[] args) { + Card k1 = new Card("foo"); + Card c1 = new ColoredCard("foo", Color.green); + + System.out.println(c1.equals(k1)); + System.out.println(k1.equals(c1)); + } +} diff --git a/src/oop/ch04/equality/card2/Card.java b/src/oop/ch04/equality/card2/Card.java new file mode 100644 index 0000000..bd45a6b --- /dev/null +++ b/src/oop/ch04/equality/card2/Card.java @@ -0,0 +1,89 @@ +package oop.ch04.equality.card2; + +/** + * Represents any card with a name and a unique serial number. + * + * @author Mark Minas + */ +public class Card { + /** + * The name of this card. + */ + private final String name; + + /** + * Counts the number of instantiations of this class. + */ + private static int counter = 0; + + /** + * The unique serial number of this card. + */ + private final int number; + + /** + * Creates a new card with the specified name. + * + * @param name the name of this card + */ + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++counter; + } + + /** + * Creates a new card for a student with the specified number. + * + * @param num the student's number + */ + public Card(int num) { + this("MatrNr. " + num); + } + + /** + * Returns the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Returns the serial number of this card. + * + * @return the serial number + */ + public int getNumber() { + return number; + } + + /** + * Returns the number of instantiations of this class. + * + * @return the number of instantiations. + */ + public static int getCounter() { + return counter; + } + + /** + * Returns a string representation of this card. + */ + @Override + public String toString() { + return "Card " + name + " (no " + number + ")"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof Card) { + final Card that = (Card) o; + return this.name.equals(that.name); + } + return false; + } +} diff --git a/src/oop/ch04/equality/card2/Color.java b/src/oop/ch04/equality/card2/Color.java new file mode 100644 index 0000000..a18aae2 --- /dev/null +++ b/src/oop/ch04/equality/card2/Color.java @@ -0,0 +1,5 @@ +package oop.ch04.equality.card2; + +public enum Color { + red, green, blue +} diff --git a/src/oop/ch04/equality/card2/ColoredCard.java b/src/oop/ch04/equality/card2/ColoredCard.java new file mode 100644 index 0000000..5945fc1 --- /dev/null +++ b/src/oop/ch04/equality/card2/ColoredCard.java @@ -0,0 +1,28 @@ +package oop.ch04.equality.card2; + +public class ColoredCard extends Card { + public final Color color; + + public ColoredCard(String key, Color color) { + super(key); + this.color = color; + } + + @Override + public String toString() { + return color + " " + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof ColoredCard) { + final ColoredCard that = (ColoredCard) o; + return this.color == that.color && + super.equals(that); + } + if (o instanceof Card) + return o.equals(this); + return false; + } +} diff --git a/src/oop/ch04/equality/card2/Example.java b/src/oop/ch04/equality/card2/Example.java new file mode 100644 index 0000000..96cd3d9 --- /dev/null +++ b/src/oop/ch04/equality/card2/Example.java @@ -0,0 +1,15 @@ +package oop.ch04.equality.card2; + +public class Example { + public static void main(String[] args) { + Card k1 = new Card("foo"); + Card c1 = new ColoredCard("foo", Color.green); + Card c2 = new ColoredCard("foo", Color.red); + + System.out.println(c1.equals(k1)); + System.out.println(k1.equals(c1)); + System.out.println(c2.equals(k1)); + System.out.println(k1.equals(c2)); + System.out.println(c1.equals(c2)); + } +} diff --git a/src/oop/ch04/equality/card3/Card.java b/src/oop/ch04/equality/card3/Card.java new file mode 100644 index 0000000..d5ece1d --- /dev/null +++ b/src/oop/ch04/equality/card3/Card.java @@ -0,0 +1,89 @@ +package oop.ch04.equality.card3; + +/** + * Represents any card with a name and a unique serial number. + * + * @author Mark Minas + */ +public class Card { + /** + * The name of this card. + */ + private final String name; + + /** + * Counts the number of instantiations of this class. + */ + private static int counter = 0; + + /** + * The unique serial number of this card. + */ + private final int number; + + /** + * Creates a new card with the specified name. + * + * @param name the name of this card + */ + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++counter; + } + + /** + * Creates a new card for a student with the specified number. + * + * @param num the student's number + */ + public Card(int num) { + this("MatrNr. " + num); + } + + /** + * Returns the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Returns the serial number of this card. + * + * @return the serial number + */ + public int getNumber() { + return number; + } + + /** + * Returns the number of instantiations of this class. + * + * @return the number of instantiations. + */ + public static int getCounter() { + return counter; + } + + /** + * Returns a string representation of this card. + */ + @Override + public String toString() { + return "Card " + name + " (no " + number + ")"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof Card) { + final Card that = (Card) o; + return this.name.equals(that.name) && this.getClass() == that.getClass(); + } + return false; + } +} diff --git a/src/oop/ch04/equality/card3/Color.java b/src/oop/ch04/equality/card3/Color.java new file mode 100644 index 0000000..290fd9c --- /dev/null +++ b/src/oop/ch04/equality/card3/Color.java @@ -0,0 +1,5 @@ +package oop.ch04.equality.card3; + +public enum Color { + red, green, blue +} diff --git a/src/oop/ch04/equality/card3/ColoredCard.java b/src/oop/ch04/equality/card3/ColoredCard.java new file mode 100644 index 0000000..ed0cb66 --- /dev/null +++ b/src/oop/ch04/equality/card3/ColoredCard.java @@ -0,0 +1,25 @@ +package oop.ch04.equality.card3; + +public class ColoredCard extends Card { + public final Color color; + + public ColoredCard(String key, Color color) { + super(key); + this.color = color; + } + + @Override + public String toString() { + return color + " " + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof ColoredCard) { + final ColoredCard that = (ColoredCard) o; + return this.color == that.color && super.equals(that); + } + return false; + } +} diff --git a/src/oop/ch04/equality/card3/Example.java b/src/oop/ch04/equality/card3/Example.java new file mode 100644 index 0000000..4df9e3b --- /dev/null +++ b/src/oop/ch04/equality/card3/Example.java @@ -0,0 +1,15 @@ +package oop.ch04.equality.card3; + +public class Example { + public static void main(String[] args) { + Card k1 = new Card("foo"); + Card c1 = new ColoredCard("foo", Color.green); + Card c2 = new ColoredCard("foo", Color.red); + + System.out.println(c1.equals(k1)); + System.out.println(k1.equals(c1)); + System.out.println(c2.equals(k1)); + System.out.println(k1.equals(c2)); + System.out.println(c1.equals(c2)); + } +} diff --git a/src/oop/ch04/equality/card4/Card.java b/src/oop/ch04/equality/card4/Card.java new file mode 100644 index 0000000..a1dd8bc --- /dev/null +++ b/src/oop/ch04/equality/card4/Card.java @@ -0,0 +1,93 @@ +package oop.ch04.equality.card4; + +/** + * Represents any card with a name and a unique serial number. + * + * @author Mark Minas + */ +public class Card { + /** + * The name of this card. + */ + private final String name; + + /** + * Counts the number of instantiations of this class. + */ + private static int counter = 0; + + /** + * The unique serial number of this card. + */ + private final int number; + + /** + * Creates a new card with the specified name. + * + * @param name the name of this card + */ + public Card(String name) { + if (name == null) + throw new NullPointerException("name is null"); + this.name = name; + this.number = ++counter; + } + + /** + * Creates a new card for a student with the specified number. + * + * @param num the student's number + */ + public Card(int num) { + this("MatrNr. " + num); + } + + /** + * Returns the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * Returns the serial number of this card. + * + * @return the serial number + */ + public int getNumber() { + return number; + } + + /** + * Returns the number of instantiations of this class. + * + * @return the number of instantiations. + */ + public static int getCounter() { + return counter; + } + + /** + * Returns a string representation of this card. + */ + @Override + public String toString() { + return "Card " + name + " (no " + number + ")"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof Card) { + final Card that = (Card) o; + return that.canEqual(this) && this.name.equals(that.name); + } + return false; + } + + public boolean canEqual(Object other) { + return other instanceof Card; + } +} diff --git a/src/oop/ch04/equality/card4/Color.java b/src/oop/ch04/equality/card4/Color.java new file mode 100644 index 0000000..ea06dea --- /dev/null +++ b/src/oop/ch04/equality/card4/Color.java @@ -0,0 +1,5 @@ +package oop.ch04.equality.card4; + +public enum Color { + red, green, blue +} diff --git a/src/oop/ch04/equality/card4/ColoredCard.java b/src/oop/ch04/equality/card4/ColoredCard.java new file mode 100644 index 0000000..f5a1df8 --- /dev/null +++ b/src/oop/ch04/equality/card4/ColoredCard.java @@ -0,0 +1,32 @@ +package oop.ch04.equality.card4; + +public class ColoredCard extends Card { + public final Color color; + + public ColoredCard(String key, Color color) { + super(key); + this.color = color; + } + + @Override + public String toString() { + return color + " " + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof ColoredCard) { + final ColoredCard that = (ColoredCard) o; + return that.canEqual(this) && + this.color == that.color && + super.equals(that); + } + return false; + } + + @Override + public boolean canEqual(Object other) { + return other instanceof ColoredCard; + } +} diff --git a/src/oop/ch04/equality/card4/Example.java b/src/oop/ch04/equality/card4/Example.java new file mode 100644 index 0000000..1dce1d6 --- /dev/null +++ b/src/oop/ch04/equality/card4/Example.java @@ -0,0 +1,29 @@ +package oop.ch04.equality.card4; + +import java.util.List; + +import static oop.ch04.equality.card4.Color.green; +import static oop.ch04.equality.card4.Color.red; + +public class Example { + public static void main(String[] args) { + final Card k1 = new Card("foo"); + final Card k2 = new Card("foo"); + final Card k3 = new Card("gnat"); + final Card c1 = new ColoredCard("foo", green); + final Card c2 = new ColoredCard("foo", red); + final Card c3 = new ColoredCard("foo", red); + final Card c4 = new ColoredCard("gnat", red); + + List cards = List.of(k1, k2, k3, c1, c2, c3, c4); + + for (Card x1 : cards) { + System.out.println(x1); + for (Card x2 : cards) + if (x1.equals(x2)) + System.out.println(" equal to " + x2); + else + System.out.println(" NOT equal to " + x2); + } + } +} diff --git a/src/oop/ch04/equality/rational1/Example.java b/src/oop/ch04/equality/rational1/Example.java new file mode 100644 index 0000000..8ed0920 --- /dev/null +++ b/src/oop/ch04/equality/rational1/Example.java @@ -0,0 +1,23 @@ +package oop.ch04.equality.rational1; + +import java.util.HashSet; +import java.util.Set; + +public class Example { + public static void main(String[] args) { + Rational r1 = new Rational(1, 2); + Rational r2 = new Rational(2, 4); + System.out.println(r1 == r2); + System.out.println(r1.equals(r2)); + + Set s1 = new HashSet<>(); + s1.add(new Rational(1, 2)); + s1.add(new Rational(1, 2)); + System.out.println(s1); + + Set s2 = new HashSet<>(); + s2.add(new Rational(1, 2)); + s2.add(new Rational(1, 3)); + System.out.println(s2.contains(new Rational(1, 3))); + } +} diff --git a/src/oop/ch04/equality/rational1/Rational.java b/src/oop/ch04/equality/rational1/Rational.java new file mode 100644 index 0000000..353e05e --- /dev/null +++ b/src/oop/ch04/equality/rational1/Rational.java @@ -0,0 +1,117 @@ +package oop.ch04.equality.rational1; + +public class Rational { + private final int num; + private final int denom; + + public Rational(int num, int denom) { + int gcd = gcDivider(Math.abs(num), Math.abs(denom)); + if (denom < 0) { + this.num = -num / gcd; + this.denom = -denom / gcd; + } + else { + this.num = num / gcd; + this.denom = denom / gcd; + } + } + + public Rational(int num) { + this(num, 1); + } + + private int gcDivider(int x, int y) { + return x == 0 ? y : gcDivider(y % x, x); + } + + public int getNum() { + return num; + } + + public int getDenom() { + return denom; + } + + public Rational add(Rational r) { + return new Rational(num * r.denom + denom * r.num, denom * r.denom); + } + + public Rational add(int i) { + return this.add(new Rational(i)); + } + + public Rational sub(Rational r) { + return new Rational(num * r.denom - denom * r.num, denom * r.denom); + } + + public Rational sub(int i) { + return this.sub(new Rational(i)); + } + + public Rational mult(Rational r) { + return new Rational(num * r.num, denom * r.denom); + } + + public Rational mult(int i) { + return this.mult(new Rational(i)); + } + + public Rational div(Rational r) { + return new Rational(num * r.denom, denom * r.num); + } + + public Rational div(int i) { + return this.div(new Rational(i)); + } + + public boolean lessThan(Rational r) { + return (this.sub(r).num < 0); + } + + public boolean lessThan(int i) { + return (this.sub(i).num < 0); + } + + public boolean greaterThan(Rational r) { + return (this.sub(r).num > 0); + } + + public boolean greaterThan(int i) { + return (this.sub(i).num > 0); + } + + public boolean lessThanOrEqual(Rational r) { + return (this.sub(r).num <= 0); + } + + public boolean lessThanOrEqual(int i) { + return (this.sub(i).num <= 0); + } + + public boolean greaterThanOrEqual(Rational r) { + return (this.sub(r).num <= 0); + } + + public boolean greaterThanOrEqual(int i) { + return (this.sub(i).num <= 0); + } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (o instanceof Rational) { + final Rational that = (Rational) o; + return this.denom == that.denom && this.num == that.num; + } + return false; + } + + public boolean equals(int i) { + return this.equals(new Rational(i)); + } + + @Override + public String toString() { + return denom == 1 ? "" + num : num + "/" + denom; + } +} diff --git a/src/oop/ch04/equality/rational2/Example.java b/src/oop/ch04/equality/rational2/Example.java new file mode 100644 index 0000000..e51bffd --- /dev/null +++ b/src/oop/ch04/equality/rational2/Example.java @@ -0,0 +1,23 @@ +package oop.ch04.equality.rational2; + +import java.util.HashSet; +import java.util.Set; + +public class Example { + public static void main(String[] args) { + Rational r1 = new Rational(1, 2); + Rational r2 = new Rational(2, 4); + System.out.println(r1 == r2); + System.out.println(r1.equals(r2)); + + Set s1 = new HashSet<>(); + s1.add(new Rational(1, 2)); + s1.add(new Rational(1, 2)); + System.out.println(s1); + + Set s2 = new HashSet<>(); + s2.add(new Rational(1, 2)); + s2.add(new Rational(1, 3)); + System.out.println(s2.contains(new Rational(1, 3))); + } +} diff --git a/src/oop/ch04/equality/rational2/Rational.java b/src/oop/ch04/equality/rational2/Rational.java new file mode 100644 index 0000000..ad9d381 --- /dev/null +++ b/src/oop/ch04/equality/rational2/Rational.java @@ -0,0 +1,122 @@ +package oop.ch04.equality.rational2; + +public class Rational { + private final int num; + private final int denom; + + public Rational(int num, int denom) { + int gcd = gcDivider(Math.abs(num), Math.abs(denom)); + if (denom < 0) { + this.num = -num / gcd; + this.denom = -denom / gcd; + } + else { + this.num = num / gcd; + this.denom = denom / gcd; + } + } + + public Rational(int num) { + this(num, 1); + } + + private int gcDivider(int x, int y) { + return x == 0 ? y : gcDivider(y % x, x); + } + + public int getNum() { + return num; + } + + public int getDenom() { + return denom; + } + + public Rational add(Rational r) { + return new Rational(num * r.denom + denom * r.num, denom * r.denom); + } + + public Rational add(int i) { + return this.add(new Rational(i)); + } + + public Rational sub(Rational r) { + return new Rational(num * r.denom - denom * r.num, denom * r.denom); + } + + public Rational sub(int i) { + return this.sub(new Rational(i)); + } + + public Rational mult(Rational r) { + return new Rational(num * r.num, denom * r.denom); + } + + public Rational mult(int i) { + return this.mult(new Rational(i)); + } + + public Rational div(Rational r) { + return new Rational(num * r.denom, denom * r.num); + } + + public Rational div(int i) { + return this.div(new Rational(i)); + } + + public boolean lessThan(Rational r) { + return (this.sub(r).num < 0); + } + + public boolean lessThan(int i) { + return (this.sub(i).num < 0); + } + + public boolean greaterThan(Rational r) { + return (this.sub(r).num > 0); + } + + public boolean greaterThan(int i) { + return (this.sub(i).num > 0); + } + + public boolean lessThanOrEqual(Rational r) { + return (this.sub(r).num <= 0); + } + + public boolean lessThanOrEqual(int i) { + return (this.sub(i).num <= 0); + } + + public boolean greaterThanOrEqual(Rational r) { + return (this.sub(r).num <= 0); + } + + public boolean greaterThanOrEqual(int i) { + return (this.sub(i).num <= 0); + } + + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (o instanceof Rational) { + final Rational that = (Rational) o; + return this.denom == that.denom && this.num == that.num; + } + return false; + } + + @Override + public int hashCode() { + return num + denom; + } + + public boolean equals(int i) { + return this.equals(new Rational(i)); + } + + @Override + public String toString() { + return denom == 1 ? "" + num : num + "/" + denom; + } +} diff --git a/src/oop/ch05/generic/example/AutoContainer.java b/src/oop/ch05/generic/example/AutoContainer.java new file mode 100644 index 0000000..90b4c34 --- /dev/null +++ b/src/oop/ch05/generic/example/AutoContainer.java @@ -0,0 +1,22 @@ +package oop.ch05.generic.example; + +import oop.ch03.vehicles.Auto; + +public class AutoContainer { + private final double maxAchsLast; + private Auto contained; + + public AutoContainer(double maxAchsLast) { + this.maxAchsLast = maxAchsLast; + } + + public Auto getContained() { + return contained; + } + + public void setContained(Auto auto) { + if (auto.getAchslast() > maxAchsLast) + throw new IllegalArgumentException("Too heavy"); + this.contained = auto; + } +} diff --git a/src/oop/ch05/generic/example/Example.java b/src/oop/ch05/generic/example/Example.java new file mode 100644 index 0000000..bccba73 --- /dev/null +++ b/src/oop/ch05/generic/example/Example.java @@ -0,0 +1,54 @@ +package oop.ch05.generic.example; + +import oop.ch03.vehicles.Amphibienfahrzeug; +import oop.ch03.vehicles.AmphibienfahrzeugImpl; +import oop.ch03.vehicles.Auto; +import oop.ch03.vehicles.AutoImpl; + +public class Example { + public static void main(String[] args) { + plainTest(); + genericTest(); + } + + public static void plainTest() { + final Amphibienfahrzeug alligator = new AmphibienfahrzeugImpl(750, 8000); + final Auto golf = new AutoImpl(500); + + final AutoContainer c = new AutoContainer(1000); + + c.setContained(alligator); + System.out.println("Tiefgang: " + getTiefgang(c)); + + c.setContained(golf); + System.out.println("Tiefgang: " + getTiefgang(c)); + } + + public static void genericTest() { + final Amphibienfahrzeug alligator = new AmphibienfahrzeugImpl(750, 8000); + final Auto golf = new AutoImpl(500); + + final GenericContainer c1 = new GenericContainer<>(1000); + + c1.setContained(alligator); + System.out.println("Tiefgang: " + getTiefgang(c1)); + + // incompatible types: oop.ch04.vehicles.Auto cannot be converted to oop.ch04.vehicles.Amphibienfahrzeug + // c1.setContained(golf); + System.out.println("Tiefgang: " + getTiefgang(c1)); + + final GenericContainer c2 = new GenericContainer<>(1000); + c2.setContained(golf); + // no suitable method found for getTiefgang(oop.ch05.generic.example.GenericContainer) + // System.out.println("Tiefgang: " + getTiefgang(c2)); + } + + public static double getTiefgang(AutoContainer container) { + final Amphibienfahrzeug contained = (Amphibienfahrzeug) container.getContained(); + return contained.getTiefgang(); + } + + public static double getTiefgang(GenericContainer container) { + return container.getContained().getTiefgang(); + } +} diff --git a/src/oop/ch05/generic/example/GenericContainer.java b/src/oop/ch05/generic/example/GenericContainer.java new file mode 100644 index 0000000..9790991 --- /dev/null +++ b/src/oop/ch05/generic/example/GenericContainer.java @@ -0,0 +1,22 @@ +package oop.ch05.generic.example; + +import oop.ch03.vehicles.Auto; + +public class GenericContainer { + private final double maxAchsLast; + private A contained; + + public GenericContainer(double maxAchsLast) { + this.maxAchsLast = maxAchsLast; + } + + public A getContained() { + return contained; + } + + public void setContained(A auto) { + if (auto.getAchslast() > maxAchsLast) + throw new IllegalArgumentException("Too heavy"); + this.contained = auto; + } +} diff --git a/src/oop/ch05/generic/mensa/AccountCard.java b/src/oop/ch05/generic/mensa/AccountCard.java new file mode 100644 index 0000000..07e7968 --- /dev/null +++ b/src/oop/ch05/generic/mensa/AccountCard.java @@ -0,0 +1,34 @@ +package oop.ch05.generic.mensa; + +import oop.ch05.generic.secured.AuthorizationException; +import oop.ch05.generic.secured.SecuredContent; +import oop.ch05.generic.secured.SecurityClient; + +public class AccountCard extends MensaCard { + private final SecuredContent account; + + public AccountCard(String key, String account, int password) { + super(key, Color.white); + this.account = new SecuredContent<>(password, account); + } + + public String getAccount() { + return account.getContent(); + } + + public void setAccount(SecurityClient client, String account) throws AuthorizationException { + if (account == null || account.trim().length() == 0) + throw new IllegalArgumentException("Invalid account " + account); + this.account.setContent(client, account); + } + + @Override + public String toString() { + return super.toString() + " for account " + account.getContent(); + } + + @Override + public void pass(CashPoint cashPoint) { + cashPoint.charge(this); + } +} diff --git a/src/oop/ch05/generic/mensa/CashCard.java b/src/oop/ch05/generic/mensa/CashCard.java new file mode 100644 index 0000000..ec5b130 --- /dev/null +++ b/src/oop/ch05/generic/mensa/CashCard.java @@ -0,0 +1,43 @@ +package oop.ch05.generic.mensa; + +import oop.ch05.generic.secured.AuthorizationException; +import oop.ch05.generic.secured.SecuredContent; + +public class CashCard extends MensaCard { + private final SecuredContent balance; + + public CashCard(String key, Color color, int password) { + super(key, color); + balance = new SecuredContent<>(password, 0); + if (color != Color.blue && color != Color.green) + throw new IllegalArgumentException("Invalid CashCard color " + color); + } + + public int getBalance() { + return balance.getContent(); + } + + void deposit(VendingMachine client, int cents) + throws AuthorizationException { + if (cents <= 0) + throw new IllegalArgumentException("Non-positive deposit"); + final int newBalance = getBalance() + cents; + balance.setContent(client, newBalance); + } + + void charge(CashPoint client, int cents) throws AuthorizationException { + if (cents < 0) + throw new IllegalArgumentException("Negative charge"); + balance.setContent(client, getBalance() - cents); + } + + @Override + public String toString() { + return super.toString() + " with " + balance.getContent() + " cents"; + } + + @Override + public void pass(CashPoint cashPoint) throws AuthorizationException, RejectedException { + cashPoint.charge(this); + } +} diff --git a/src/oop/ch05/generic/mensa/CashPoint.java b/src/oop/ch05/generic/mensa/CashPoint.java new file mode 100644 index 0000000..7d77448 --- /dev/null +++ b/src/oop/ch05/generic/mensa/CashPoint.java @@ -0,0 +1,64 @@ +package oop.ch05.generic.mensa; + +import oop.ch05.generic.secured.AuthorizationException; +import oop.ch05.generic.secured.SecurityClient; + +public class CashPoint implements SecurityClient { + private final int password; + public final String name; + private int counter; + private int cents; + + public CashPoint(String name, int password) { + this.name = name; + this.password = password; + this.counter = 0; + this.cents = 0; + } + + @Override + public int challengeResponse(int challenge) { + return challenge ^ password; + } + + @Override + public String toString() { + return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)"; + } + + public int getCounter() { + return counter; + } + + public int getCents() { + return cents; + } + + private int getPrice(Color color) { + return switch (color) { + case green -> 267; + case blue -> 357; + case white -> 495; + default -> 0; + }; + } + + void count(MensaCard card) { + counter++; + } + + void charge(CashCard cashCard) throws AuthorizationException, RejectedException { + final int price = getPrice(cashCard.color); + if (cashCard.getBalance() < price) + throw new RejectedException("insufficient payment"); + cashCard.charge(this, price); + count(cashCard); + cents += price; + } + + void charge(AccountCard accountCard) { + final int price = getPrice(accountCard.color); + System.out.println("Charging " + price + " cents on account " + accountCard.getAccount()); + cents += price; + } +} diff --git a/src/oop/ch05/generic/mensa/Color.java b/src/oop/ch05/generic/mensa/Color.java new file mode 100644 index 0000000..51060be --- /dev/null +++ b/src/oop/ch05/generic/mensa/Color.java @@ -0,0 +1,5 @@ +package oop.ch05.generic.mensa; + +public enum Color { + green, red, blue, white, gray +} diff --git a/src/oop/ch05/generic/mensa/CountCard.java b/src/oop/ch05/generic/mensa/CountCard.java new file mode 100644 index 0000000..80d4107 --- /dev/null +++ b/src/oop/ch05/generic/mensa/CountCard.java @@ -0,0 +1,15 @@ +package oop.ch05.generic.mensa; + +public class CountCard extends MensaCard { + + public CountCard(String key, Color color) { + super(key, color); + if (color != Color.red && color != Color.gray) + throw new IllegalArgumentException("Invalid CountCard color " + color); + } + + @Override + public void pass(CashPoint cashPoint) { + cashPoint.count(this); + } +} diff --git a/src/oop/ch05/generic/mensa/MensaCard.java b/src/oop/ch05/generic/mensa/MensaCard.java new file mode 100644 index 0000000..f8c97ac --- /dev/null +++ b/src/oop/ch05/generic/mensa/MensaCard.java @@ -0,0 +1,20 @@ +package oop.ch05.generic.mensa; + +import oop.ch02.cards.Card; +import oop.ch05.generic.secured.AuthorizationException; + +public abstract class MensaCard extends Card { + public final Color color; + + protected MensaCard(String name, Color color) { + super(name); + this.color = color; + } + + @Override + public String toString() { + return color + " card " + getNumber() + " (" + getName() + ")"; + } + + public abstract void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException; +} \ No newline at end of file diff --git a/src/oop/ch05/generic/mensa/MensaExample.java b/src/oop/ch05/generic/mensa/MensaExample.java new file mode 100644 index 0000000..99efb0b --- /dev/null +++ b/src/oop/ch05/generic/mensa/MensaExample.java @@ -0,0 +1,68 @@ +package oop.ch05.generic.mensa; + +import oop.ch05.generic.secured.AuthorizationException; + +public class MensaExample { + public static void main(String[] args) { + VendingMachine vm1 = new VendingMachine("left", 4711); + VendingMachine vm2 = new VendingMachine("right", 4711); + VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141); + CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711); + + AccountCard conf = new AccountCard("conference", "33-1298", 42); + MensaCard frankSmith = new CountCard("Frank Smith", Color.gray); + CashCard hansMueller = new CashCard("Hans Müller", Color.green, 4711); + CashCard peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711); + CashCard thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711); + + deposit(vm1, hansMueller, 10); + deposit(vm1, peterSchmidt, 5); + deposit(vm2, thomasMayer, 2); + deposit(tumVM, hansMueller, 10); + + System.out.println(vm1); + System.out.println(vm2); + System.out.println(tumVM); + System.out.println(hansMueller); + System.out.println(peterSchmidt); + System.out.println(thomasMayer); + System.out.println(); + + pass(hansMueller, unibwMensa); + System.out.println(hansMueller); + System.out.println(unibwMensa); + + pass(frankSmith, unibwMensa); + pass(conf, unibwMensa); + pass(thomasMayer, unibwMensa); + pass(hansMueller, unibwMensa); + pass(hansMueller, unibwMensa); + pass(hansMueller, unibwMensa); + + System.out.println(unibwMensa); + System.out.println(hansMueller); + System.out.println(peterSchmidt); + System.out.println(thomasMayer); + } + + private static void pass(MensaCard mensaCard, CashPoint cashPoint) { + try { + mensaCard.pass(cashPoint); + } + catch (RejectedException e) { + System.out.println("rejected: " + e.getMessage()); + } + catch (AuthorizationException e) { + System.out.println("authrozation failed: " + e.getMessage()); + } + } + + private static void deposit(VendingMachine vm, CashCard cashCard, int euros) { + try { + vm.deposit(cashCard, euros); + } + catch (AuthorizationException e) { + System.out.println("authorization failed: " + e.getMessage()); + } + } +} diff --git a/src/oop/ch05/generic/mensa/RejectedException.java b/src/oop/ch05/generic/mensa/RejectedException.java new file mode 100644 index 0000000..214f97d --- /dev/null +++ b/src/oop/ch05/generic/mensa/RejectedException.java @@ -0,0 +1,7 @@ +package oop.ch05.generic.mensa; + +public class RejectedException extends Exception { + public RejectedException(String message) { + super(message); + } +} diff --git a/src/oop/ch05/generic/mensa/VendingMachine.java b/src/oop/ch05/generic/mensa/VendingMachine.java new file mode 100644 index 0000000..64326b0 --- /dev/null +++ b/src/oop/ch05/generic/mensa/VendingMachine.java @@ -0,0 +1,37 @@ +package oop.ch05.generic.mensa; + +import oop.ch05.generic.secured.AuthorizationException; +import oop.ch05.generic.secured.SecurityClient; + +public class VendingMachine implements SecurityClient { + private final int password; + public final String name; + private int euros; + + public VendingMachine(String name, int password) { + this.name = name; + this.password = password; + this.euros = 0; + } + + @Override + public int challengeResponse(int challenge) { + return challenge ^ password; + } + + @Override + public String toString() { + return "Vending machine " + name + " (contains EUR " + euros + ")"; + } + + public int getEuros() { + return euros; + } + + public void deposit(CashCard card, int euros) throws AuthorizationException { + if (euros <= 0) + throw new IllegalArgumentException("Non-positive deposit"); + card.deposit(this, euros * 100); + this.euros += euros; + } +} diff --git a/src/oop/ch05/generic/secured/AuthorizationException.java b/src/oop/ch05/generic/secured/AuthorizationException.java new file mode 100644 index 0000000..95aa652 --- /dev/null +++ b/src/oop/ch05/generic/secured/AuthorizationException.java @@ -0,0 +1,20 @@ +package oop.ch05.generic.secured; + +/** + * This class represents exceptions that are thrown whenever an authorization + * fails. + * + * @author Mark Minas + */ +public class AuthorizationException extends Exception { + /** + * Constructs a new authorization exception with the specified detail message + * and cause. + * + * @param message the detail message (which is saved for later retrieval by the + * {@link #getMessage()} method). + */ + public AuthorizationException(String message) { + super(message); + } +} diff --git a/src/oop/ch05/generic/secured/SecuredContent.java b/src/oop/ch05/generic/secured/SecuredContent.java new file mode 100644 index 0000000..c5ea118 --- /dev/null +++ b/src/oop/ch05/generic/secured/SecuredContent.java @@ -0,0 +1,80 @@ +package oop.ch05.generic.secured; + +import java.util.Random; + +/** + * Represents containers for some content that is secured against modifications + * by a challenge-response approach. The content may be read at any time, but + * only authorized clients are allowed to modify the contents. + * + * @param the type of the actual secured content + * @author Mark Minas + */ +public class SecuredContent { + /** + * The actual, secured content + */ + private E content; + /** + * A random number generator used for generating challenges. + */ + private final Random random = new Random(); + /** + * The password used for encryption. + */ + private final int password; + + /** + * Creates a new container containing the specified contents. + * + * @param password this password is used for computing the expected response for a + * challenge. + * @param content the contained content + */ + public SecuredContent(int password, E content) { + this.password = password; + this.content = content; + } + + /** + * Returns the contained contents. There is no authorization necessary for + * reading the contents. + */ + public E getContent() { + return content; + } + + /** + * Sets the new contained contents. Only authorized clients are allowed to do + * so. + * + * @param client The accessing client. Authorization is checked prior to really + * modifying he contained content. + * @param content The new contained contents + * @throws AuthorizationException if the specified client cannot authorize himself + */ + public void setContent(SecurityClient client, E content) + throws AuthorizationException { + final int challenge = nextChallenge(); + if (client.challengeResponse(challenge) != requiredResponse(challenge)) + throw new AuthorizationException(client + + " is not authorized to access contents."); + this.content = content; + } + + /** + * Computes the next random challenge. + */ + private int nextChallenge() { + return random.nextInt(); + } + + /** + * Returns the expected response for the specified challenge. + * + * @param challenge an arbitrary integer used as a challenge + */ + private int requiredResponse(int challenge) { + return challenge ^ password; + } +} diff --git a/src/oop/ch05/generic/secured/SecurityClient.java b/src/oop/ch05/generic/secured/SecurityClient.java new file mode 100644 index 0000000..777ee67 --- /dev/null +++ b/src/oop/ch05/generic/secured/SecurityClient.java @@ -0,0 +1,18 @@ +package oop.ch05.generic.secured; + +/** + * This interface must be implemented by any client that is going to be + * authorized by a challenge-response approach. + * + * @author Mark Minas + */ +public interface SecurityClient { + /** + * Returns the response for the specified challenge. Authorization succeeds + * if this response is the same as the one expected by the server. + * + * @param challenge the integer number for which the correct response has to be + * computed. + */ + int challengeResponse(int challenge); +} diff --git a/src/oop/ch05/mensa/AccountCard.java b/src/oop/ch05/mensa/AccountCard.java new file mode 100644 index 0000000..b74f3a4 --- /dev/null +++ b/src/oop/ch05/mensa/AccountCard.java @@ -0,0 +1,34 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; +import oop.ch05.secured.SecuredContent; +import oop.ch05.secured.SecurityClient; + +public class AccountCard extends MensaCard { + private final SecuredContent account; + + public AccountCard(String key, String account, int password) { + super(key, Color.white); + this.account = new SecuredContent(password, account); + } + + public String getAccount() { + return (String) account.getContent(); + } + + public void setAccount(SecurityClient client, String account) throws AuthorizationException { + if (account == null || account.trim().length() == 0) + throw new IllegalArgumentException("Invalid account " + account); + this.account.setContent(client, account); + } + + @Override + public String toString() { + return super.toString() + " for account " + account.getContent(); + } + + @Override + public void pass(CashPoint cashPoint) { + cashPoint.charge(this); + } +} diff --git a/src/oop/ch05/mensa/CashCard.java b/src/oop/ch05/mensa/CashCard.java new file mode 100644 index 0000000..810f6d0 --- /dev/null +++ b/src/oop/ch05/mensa/CashCard.java @@ -0,0 +1,43 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; +import oop.ch05.secured.SecuredContent; + +public class CashCard extends MensaCard { + private final SecuredContent balance; + + public CashCard(String key, Color color, int password) { + super(key, color); + balance = new SecuredContent(password, 0); + if (color != Color.blue && color != Color.green) + throw new IllegalArgumentException("Invalid CashCard color " + color); + } + + public int getBalance() { + return (Integer) balance.getContent(); + } + + void deposit(VendingMachine client, int cents) + throws AuthorizationException { + if (cents <= 0) + throw new IllegalArgumentException("Non-positive deposit"); + final int newBalance = getBalance() + cents; + balance.setContent(client, newBalance); + } + + void charge(CashPoint client, int cents) throws AuthorizationException { + if (cents < 0) + throw new IllegalArgumentException("Negative charge"); + balance.setContent(client, getBalance() - cents); + } + + @Override + public String toString() { + return super.toString() + " with " + balance.getContent() + " cents"; + } + + @Override + public void pass(CashPoint cashPoint) throws AuthorizationException, RejectedException { + cashPoint.charge(this); + } +} diff --git a/src/oop/ch05/mensa/CashPoint.java b/src/oop/ch05/mensa/CashPoint.java new file mode 100644 index 0000000..832899b --- /dev/null +++ b/src/oop/ch05/mensa/CashPoint.java @@ -0,0 +1,64 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; +import oop.ch05.secured.SecurityClient; + +public class CashPoint implements SecurityClient { + private final int password; + public final String name; + private int counter; + private int cents; + + public CashPoint(String name, int password) { + this.name = name; + this.password = password; + this.counter = 0; + this.cents = 0; + } + + @Override + public int challengeResponse(int challenge) { + return challenge ^ password; + } + + @Override + public String toString() { + return "Cash point " + name + " (" + getCounter() + " meals, " + getCents() + " cents charged)"; + } + + public int getCounter() { + return counter; + } + + public int getCents() { + return cents; + } + + private int getPrice(Color color) { + return switch (color) { + case green -> 267; + case blue -> 357; + case white -> 495; + default -> 0; + }; + } + + void count(MensaCard card) { + counter++; + } + + void charge(CashCard cashCard) throws AuthorizationException, RejectedException { + final int price = getPrice(cashCard.color); + if (cashCard.getBalance() < price) + throw new RejectedException("insufficient payment"); + cashCard.charge(this, price); + count(cashCard); + cents += price; + } + + void charge(AccountCard accountCard) { + final int price = getPrice(accountCard.color); + System.out.println("Charging " + price + " cents on account " + accountCard.getAccount()); + cents += price; + } +} diff --git a/src/oop/ch05/mensa/Color.java b/src/oop/ch05/mensa/Color.java new file mode 100644 index 0000000..13945c7 --- /dev/null +++ b/src/oop/ch05/mensa/Color.java @@ -0,0 +1,5 @@ +package oop.ch05.mensa; + +public enum Color { + green, red, blue, white, gray +} diff --git a/src/oop/ch05/mensa/CountCard.java b/src/oop/ch05/mensa/CountCard.java new file mode 100644 index 0000000..f0c32d3 --- /dev/null +++ b/src/oop/ch05/mensa/CountCard.java @@ -0,0 +1,15 @@ +package oop.ch05.mensa; + +public class CountCard extends MensaCard { + + public CountCard(String key, Color color) { + super(key, color); + if (color != Color.red && color != Color.gray) + throw new IllegalArgumentException("Invalid CountCard color " + color); + } + + @Override + public void pass(CashPoint cashPoint) { + cashPoint.count(this); + } +} diff --git a/src/oop/ch05/mensa/MensaCard.java b/src/oop/ch05/mensa/MensaCard.java new file mode 100644 index 0000000..cf0be76 --- /dev/null +++ b/src/oop/ch05/mensa/MensaCard.java @@ -0,0 +1,20 @@ +package oop.ch05.mensa; + +import oop.ch02.cards.Card; +import oop.ch05.secured.AuthorizationException; + +public abstract class MensaCard extends Card { + public final Color color; + + protected MensaCard(String name, Color color) { + super(name); + this.color = color; + } + + @Override + public String toString() { + return color + " card " + getNumber() + " (" + getName() + ")"; + } + + public abstract void pass(CashPoint cashPoint) throws RejectedException, AuthorizationException; +} \ No newline at end of file diff --git a/src/oop/ch05/mensa/MensaExample.java b/src/oop/ch05/mensa/MensaExample.java new file mode 100644 index 0000000..478c549 --- /dev/null +++ b/src/oop/ch05/mensa/MensaExample.java @@ -0,0 +1,68 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; + +public class MensaExample { + public static void main(String[] args) { + VendingMachine vm1 = new VendingMachine("left", 4711); + VendingMachine vm2 = new VendingMachine("right", 4711); + VendingMachine tumVM = new VendingMachine("TUM Mensa", 3141); + CashPoint unibwMensa = new CashPoint("UniBw Mensa", 4711); + + AccountCard conf = new AccountCard("conference", "33-1298", 42); + MensaCard frankSmith = new CountCard("Frank Smith", Color.gray); + CashCard hansMueller = new CashCard("Hans Müller", Color.green, 4711); + CashCard peterSchmidt = new CashCard("Peter Schmidt", Color.green, 4711); + CashCard thomasMayer = new CashCard("Thomas Mayer", Color.blue, 4711); + + deposit(vm1, hansMueller, 10); + deposit(vm1, peterSchmidt, 5); + deposit(vm2, thomasMayer, 2); + deposit(tumVM, hansMueller, 10); + + System.out.println(vm1); + System.out.println(vm2); + System.out.println(tumVM); + System.out.println(hansMueller); + System.out.println(peterSchmidt); + System.out.println(thomasMayer); + System.out.println(); + + pass(hansMueller, unibwMensa); + System.out.println(hansMueller); + System.out.println(unibwMensa); + + pass(frankSmith, unibwMensa); + pass(conf, unibwMensa); + pass(thomasMayer, unibwMensa); + pass(hansMueller, unibwMensa); + pass(hansMueller, unibwMensa); + pass(hansMueller, unibwMensa); + + System.out.println(unibwMensa); + System.out.println(hansMueller); + System.out.println(peterSchmidt); + System.out.println(thomasMayer); + } + + private static void pass(MensaCard mensaCard, CashPoint cashPoint) { + try { + mensaCard.pass(cashPoint); + } + catch (RejectedException e) { + System.out.println("rejected: " + e.getMessage()); + } + catch (AuthorizationException e) { + System.out.println("authrozation failed: " + e.getMessage()); + } + } + + private static void deposit(VendingMachine vm, CashCard cashCard, int euros) { + try { + vm.deposit(cashCard, euros); + } + catch (AuthorizationException e) { + System.out.println("authorization failed: " + e.getMessage()); + } + } +} diff --git a/src/oop/ch05/mensa/RejectedException.java b/src/oop/ch05/mensa/RejectedException.java new file mode 100644 index 0000000..c51c48b --- /dev/null +++ b/src/oop/ch05/mensa/RejectedException.java @@ -0,0 +1,7 @@ +package oop.ch05.mensa; + +public class RejectedException extends Exception { + public RejectedException(String message) { + super(message); + } +} diff --git a/src/oop/ch05/mensa/VendingMachine.java b/src/oop/ch05/mensa/VendingMachine.java new file mode 100644 index 0000000..b2e80ec --- /dev/null +++ b/src/oop/ch05/mensa/VendingMachine.java @@ -0,0 +1,37 @@ +package oop.ch05.mensa; + +import oop.ch05.secured.AuthorizationException; +import oop.ch05.secured.SecurityClient; + +public class VendingMachine implements SecurityClient { + private final int password; + public final String name; + private int euros; + + public VendingMachine(String name, int password) { + this.name = name; + this.password = password; + this.euros = 0; + } + + @Override + public int challengeResponse(int challenge) { + return challenge ^ password; + } + + @Override + public String toString() { + return "Vending machine " + name + " (contains EUR " + euros + ")"; + } + + public int getEuros() { + return euros; + } + + public void deposit(CashCard card, int euros) throws AuthorizationException { + if (euros <= 0) + throw new IllegalArgumentException("Non-positive deposit"); + card.deposit(this, euros * 100); + this.euros += euros; + } +} diff --git a/src/oop/ch05/secured/AuthorizationException.java b/src/oop/ch05/secured/AuthorizationException.java new file mode 100644 index 0000000..325c201 --- /dev/null +++ b/src/oop/ch05/secured/AuthorizationException.java @@ -0,0 +1,20 @@ +package oop.ch05.secured; + +/** + * This class represents exceptions that are thrown whenever an authorization + * fails. + * + * @author Mark Minas + */ +public class AuthorizationException extends Exception { + /** + * Constructs a new authorization exception with the specified detail message + * and cause. + * + * @param message the detail message (which is saved for later retrieval by the + * {@link #getMessage()} method). + */ + public AuthorizationException(String message) { + super(message); + } +} diff --git a/src/oop/ch05/secured/SecuredContent.java b/src/oop/ch05/secured/SecuredContent.java new file mode 100644 index 0000000..1426aaf --- /dev/null +++ b/src/oop/ch05/secured/SecuredContent.java @@ -0,0 +1,79 @@ +package oop.ch05.secured; + +import java.util.Random; + +/** + * Represents containers for some content that is secured against modifications + * by a challenge-response approach. The content may be read at any time, but + * only authorized clients are allowed to modify the contents. + * + * @author Mark Minas + */ +public class SecuredContent { + /** + * The actual, secured content + */ + private Object content; + /** + * A random number generator used for generating challenges. + */ + private final Random random = new Random(); + /** + * The password used for encryption. + */ + private final int password; + + /** + * Creates a new container containing the specified contents. + * + * @param password this password is used for computing the expected response for a + * challenge. + * @param content the contained content + */ + public SecuredContent(int password, Object content) { + this.password = password; + this.content = content; + } + + /** + * Returns the contained contents. There is no authorization necessary for + * reading the contents. + */ + public Object getContent() { + return content; + } + + /** + * Sets the new contained contents. Only authorized clients are allowed to do + * so. + * + * @param client The accessing client. Authorization is checked prior to really + * modifying he contained content. + * @param content The new contained contents + * @throws AuthorizationException if the specified client cannot authorize himself + */ + public void setContent(SecurityClient client, Object content) + throws AuthorizationException { + final int challenge = nextChallenge(); + if (client.challengeResponse(challenge) != requiredResponse(challenge)) + throw new AuthorizationException(client + + " is not authorized to access contents."); + this.content = content; + } + + /** + * Computes the next random challenge. + */ + private int nextChallenge() { + return random.nextInt(); + } + + /** + * Returns the expected response for the specified challenge. + * + * @param challenge an arbitrary integer used as a challenge + */ + private int requiredResponse(int challenge) { + return challenge ^ password; + } +} diff --git a/src/oop/ch05/secured/SecurityClient.java b/src/oop/ch05/secured/SecurityClient.java new file mode 100644 index 0000000..760bfd9 --- /dev/null +++ b/src/oop/ch05/secured/SecurityClient.java @@ -0,0 +1,18 @@ +package oop.ch05.secured; + +/** + * This interface must be implemented by any client that is going to be + * authorized by a challenge-response approach. + * + * @author Mark Minas + */ +public interface SecurityClient { + /** + * Returns the response for the specified challenge. Authorization succeeds + * if this response is the same as the one expected by the server. + * + * @param challenge the integer number for which the correct response has to be + * computed. + */ + int challengeResponse(int challenge); +} diff --git a/src/oop/ch06/calc/AdHocCalculator.java b/src/oop/ch06/calc/AdHocCalculator.java new file mode 100644 index 0000000..3bf913b --- /dev/null +++ b/src/oop/ch06/calc/AdHocCalculator.java @@ -0,0 +1,113 @@ +package oop.ch06.calc; + +import oop.ch06.observer.ObserverSupport; + +public class AdHocCalculator extends ObserverSupport { + private double value; + private double opnd1; + private BinaryOp lastOp; + private double factor; + private boolean startedNumTyping; + private boolean hasSecondOpnd; + + public double getValue() {return value;} + + private void setValue(double n) { + value = n; + notifyObservers(); + } + + public void digit(int d) { + if (!Double.isNaN(value)) { + if (!startedNumTyping) { + startedNumTyping = true; + setValue(d); + factor = 1.; + } + else if (factor < 1.) { + setValue(value + factor * d); + factor *= 0.1; + } + else + setValue(10. * value + d); + if (lastOp != null) + hasSecondOpnd = true; + } + } + + public void dot() { + if (!Double.isNaN(value)) { + if (!startedNumTyping) { + startedNumTyping = true; + setValue(0); + factor = 0.1; + } + else if (factor > 0.1) + factor = 0.1; + if (lastOp != null) + hasSecondOpnd = true; + } + } + + public void unOp(UnaryOp op) { + try { + if (!Double.isNaN(value)) { + startedNumTyping = false; + setValue(op.eval(value)); + if (lastOp != null) + hasSecondOpnd = true; + } + } + catch (CalculatorError ex) { + setValue(Double.NaN); + } + } + + public void binOp(BinaryOp op) { + try { + if (!Double.isNaN(value)) { + startedNumTyping = false; + if (lastOp == null) { + opnd1 = value; + hasSecondOpnd = false; + } + else if (hasSecondOpnd) { + setValue(lastOp.eval(opnd1, value)); + opnd1 = value; + } + lastOp = op; + } + } + catch (CalculatorError ex) { + setValue(Double.NaN); + } + } + + public void eval() { + try { + if (!Double.isNaN(value) && lastOp != null) { + startedNumTyping = false; + setValue(lastOp.eval(opnd1, value)); + lastOp = null; + } + } + catch (CalculatorError ex) { + setValue(Double.NaN); + } + } + + public void clear() { + if (!Double.isNaN(value)) { + setValue(0); + startedNumTyping = false; + hasSecondOpnd = false; + } + } + + public void allClear() { + setValue(0); + startedNumTyping = false; + hasSecondOpnd = false; + lastOp = null; + } +} diff --git a/src/oop/ch06/calc/BinaryOp.java b/src/oop/ch06/calc/BinaryOp.java new file mode 100644 index 0000000..878cd9a --- /dev/null +++ b/src/oop/ch06/calc/BinaryOp.java @@ -0,0 +1,9 @@ +package oop.ch06.calc; + +public abstract class BinaryOp extends Op { + protected BinaryOp(String name) { + super(name); + } + + public abstract double eval(double opnd1, double opnd2); +} diff --git a/src/oop/ch06/calc/CalculatorError.java b/src/oop/ch06/calc/CalculatorError.java new file mode 100644 index 0000000..b541234 --- /dev/null +++ b/src/oop/ch06/calc/CalculatorError.java @@ -0,0 +1,7 @@ +package oop.ch06.calc; + +public class CalculatorError extends RuntimeException { + public CalculatorError(String message) { + super(message); + } +} diff --git a/src/oop/ch06/calc/Example.java b/src/oop/ch06/calc/Example.java new file mode 100644 index 0000000..7eda091 --- /dev/null +++ b/src/oop/ch06/calc/Example.java @@ -0,0 +1,27 @@ +package oop.ch06.calc; + +class Example { + public static void main(String[] args) { + final AdHocCalculator calc = new AdHocCalculator(); + calc.digit(2); + calc.digit(4); + calc.dot(); + calc.digit(5); + System.out.println(calc.getValue()); + calc.binOp(Ops.PLUS); + calc.digit(3); + calc.clear(); + calc.binOp(Ops.MULT); + calc.digit(1); + calc.digit(2); + System.out.println(calc.getValue()); + calc.eval(); + System.out.println(calc.getValue()); + calc.unOp(Ops.CHANGE_SIGN); + System.out.println(calc.getValue()); + calc.unOp(Ops.SQRT); + System.out.println(calc.getValue()); + calc.allClear(); + System.out.println(calc.getValue()); + } +} diff --git a/src/oop/ch06/calc/Op.java b/src/oop/ch06/calc/Op.java new file mode 100644 index 0000000..3c9a283 --- /dev/null +++ b/src/oop/ch06/calc/Op.java @@ -0,0 +1,22 @@ +package oop.ch06.calc; + +abstract class Op { + private final String name; + + protected Op(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } + + protected double check(double v) { + if (Double.isNaN(v)) + throw new CalculatorError("value is not a number"); + if (Double.isInfinite(v)) + throw new CalculatorError("infinite value"); + return v; + } +} diff --git a/src/oop/ch06/calc/Ops.java b/src/oop/ch06/calc/Ops.java new file mode 100644 index 0000000..15a1d98 --- /dev/null +++ b/src/oop/ch06/calc/Ops.java @@ -0,0 +1,47 @@ +package oop.ch06.calc; + +public class Ops { + private Ops() { /* do not instantiate */ } + + public static final BinaryOp PLUS = new BinaryOp("+") { + public double eval(double opnd1, double opnd2) { + return check(opnd1 + opnd2); + } + }; + + public static final BinaryOp MULT = new BinaryOp("*") { + public double eval(double opnd1, double opnd2) { + return check(opnd1 * opnd2); + } + }; + + public static final BinaryOp MINUS = new BinaryOp("-") { + public double eval(double opnd1, double opnd2) { + return check(opnd1 - opnd2); + } + }; + + public static final BinaryOp DIV = new BinaryOp("/") { + public double eval(double opnd1, double opnd2) { + return check(opnd1 / opnd2); + } + }; + + public static final UnaryOp CHANGE_SIGN = new UnaryOp("+/-") { + public double eval(double opnd) { + return check(-opnd); + } + }; + + public static final UnaryOp SQRT = new UnaryOp("sqrt") { + public double eval(double opnd) { + return check(Math.sqrt(opnd)); + } + }; + + public static final UnaryOp EXP = new UnaryOp("exp") { + public double eval(double opnd) { + return check(Math.exp(opnd)); + } + }; +} diff --git a/src/oop/ch06/calc/UnaryOp.java b/src/oop/ch06/calc/UnaryOp.java new file mode 100644 index 0000000..a3c79ec --- /dev/null +++ b/src/oop/ch06/calc/UnaryOp.java @@ -0,0 +1,9 @@ +package oop.ch06.calc; + +public abstract class UnaryOp extends Op { + protected UnaryOp(String name) { + super(name); + } + + public abstract double eval(double opnd); +} diff --git a/src/oop/ch06/observer/Observer.java b/src/oop/ch06/observer/Observer.java new file mode 100644 index 0000000..112dcca --- /dev/null +++ b/src/oop/ch06/observer/Observer.java @@ -0,0 +1,5 @@ +package oop.ch06.observer; + +public interface Observer { + void update(); +} diff --git a/src/oop/ch06/observer/ObserverSupport.java b/src/oop/ch06/observer/ObserverSupport.java new file mode 100644 index 0000000..8d9df4f --- /dev/null +++ b/src/oop/ch06/observer/ObserverSupport.java @@ -0,0 +1,17 @@ +package oop.ch06.observer; + +import java.util.ArrayList; +import java.util.List; + +public class ObserverSupport { + private final List observers = new ArrayList<>(); + + public void notifyObservers() { + for (Observer o : observers) + o.update(); + } + + public void addObserver(Observer o) { + observers.add(o); + } +} diff --git a/src/oop/ch07/auto/generics/A.java b/src/oop/ch07/auto/generics/A.java new file mode 100644 index 0000000..6dbac1c --- /dev/null +++ b/src/oop/ch07/auto/generics/A.java @@ -0,0 +1,40 @@ +package oop.ch07.auto.generics; + +class A extends State { + private final Upper upper = new Upper(this); + private final Lower lower = new Lower(this); + + A(Automaton parent) { + super(parent); + } + + @Override + void entry() { + upper.entry(); + lower.entry(); + } + + @Override + void a() { + upper.a(); + lower.a(); + } + + @Override + void b() { + upper.b(); + lower.b(); + } + + @Override + void c() { + upper.c(); + lower.c(); + } + + @Override + void d() { + System.out.println("A->F"); + parent.gotoState(new F(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/Automaton.java b/src/oop/ch07/auto/generics/Automaton.java new file mode 100644 index 0000000..19d61de --- /dev/null +++ b/src/oop/ch07/auto/generics/Automaton.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class Automaton extends StateMachine { + Automaton() { + super(null); + entry(); + } + + @Override + A initialState() { + return new A(this); + } +} diff --git a/src/oop/ch07/auto/generics/B.java b/src/oop/ch07/auto/generics/B.java new file mode 100644 index 0000000..4e47be8 --- /dev/null +++ b/src/oop/ch07/auto/generics/B.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class B extends State { + B(Upper parent) { + super(parent); + } + + @Override + void a() { + System.out.println("B->C"); + parent.gotoState(new C(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/C.java b/src/oop/ch07/auto/generics/C.java new file mode 100644 index 0000000..71b320a --- /dev/null +++ b/src/oop/ch07/auto/generics/C.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class C extends State { + C(Upper parent) { + super(parent); + } + + @Override + void b() { + System.out.println("C->B"); + parent.gotoState(new B(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/D.java b/src/oop/ch07/auto/generics/D.java new file mode 100644 index 0000000..11c69ca --- /dev/null +++ b/src/oop/ch07/auto/generics/D.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class D extends State { + D(Lower parent) { + super(parent); + } + + @Override + void a() { + System.out.println("D->E"); + parent.gotoState(new E(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/Demo.java b/src/oop/ch07/auto/generics/Demo.java new file mode 100644 index 0000000..0e40f74 --- /dev/null +++ b/src/oop/ch07/auto/generics/Demo.java @@ -0,0 +1,25 @@ +package oop.ch07.auto.generics; + +public class Demo { + public static void main(String[] args) { + final Automaton auto = new Automaton(); + System.out.println("a:"); + auto.a(); + System.out.println("---------\nb:"); + auto.b(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\nd:"); + auto.d(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\nd:"); + auto.d(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\na:"); + auto.a(); + System.out.println("---------\nb:"); + auto.b(); + } +} diff --git a/src/oop/ch07/auto/generics/E.java b/src/oop/ch07/auto/generics/E.java new file mode 100644 index 0000000..68771cd --- /dev/null +++ b/src/oop/ch07/auto/generics/E.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class E extends State { + E(Lower parent) { + super(parent); + } + + @Override + void c() { + System.out.println("E->D"); + parent.gotoState(new D(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/F.java b/src/oop/ch07/auto/generics/F.java new file mode 100644 index 0000000..03fd2f5 --- /dev/null +++ b/src/oop/ch07/auto/generics/F.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.generics; + +class F extends State { + F(Automaton parent) { + super(parent); + } + + @Override + void d() { + System.out.println("F->A"); + parent.gotoState(new A(parent)); + } +} diff --git a/src/oop/ch07/auto/generics/Lower.java b/src/oop/ch07/auto/generics/Lower.java new file mode 100644 index 0000000..784616d --- /dev/null +++ b/src/oop/ch07/auto/generics/Lower.java @@ -0,0 +1,18 @@ +package oop.ch07.auto.generics; + +class Lower extends StateMachine { + Lower(A parent) { + super(parent); + } + + @Override + D initialState() { + return new D(this); + } + + @Override + void b() { + System.out.println("A->E"); + gotoState(new E(this)); + } +} diff --git a/src/oop/ch07/auto/generics/State.java b/src/oop/ch07/auto/generics/State.java new file mode 100644 index 0000000..e3948e8 --- /dev/null +++ b/src/oop/ch07/auto/generics/State.java @@ -0,0 +1,47 @@ +package oop.ch07.auto.generics; + +import java.lang.System.Logger; + +/** + * An abstract class representing a state in a state machine. + * + * @param

the type of the containing state. + */ +abstract class State

> { + /** + * The logger for logging messages. + */ + static final Logger LOGGER = System.getLogger(State.class.getName()); + + /** + * The parent state containing this state. + */ + final P parent; + + /** + * Constructs a state with the given parent. + * + * @param parent The state containing this state. + */ + State(P parent) { + this.parent = parent; + } + + /** + * Performs entry actions when entering this state. + */ + void entry() {} + + void a() { /* do nothing */} + + void b() { /* do nothing */} + + void c() { /* do nothing */} + + void d() { /* do nothing */} + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/src/oop/ch07/auto/generics/StateMachine.java b/src/oop/ch07/auto/generics/StateMachine.java new file mode 100644 index 0000000..7caffa2 --- /dev/null +++ b/src/oop/ch07/auto/generics/StateMachine.java @@ -0,0 +1,88 @@ +package oop.ch07.auto.generics; + +import java.lang.System.Logger.Level; + +/** + * An abstract class representing a state machine. It maintains the current state and provides methods to transition + * between states. + * + * @param the type of the concrete state machine. + * @param

the type of the containing state. + */ +abstract class StateMachine, P extends State> extends State

{ + /** + * The current state of the state machine. + */ + private State state; + + /** + * Constructs a state machine with the given parent. + * + * @param parent The parent state machine. + */ + StateMachine(P parent) { + super(parent); + } + + /** + * Gets the initial state of the state machine. + */ + abstract State initialState(); + + /** + * Transitions the state machine to the specified state. + * + * @param newState The state to transition to. + */ + void gotoState(State newState) { + LOGGER.log(Level.DEBUG, "{0}: {1} --> {2}", this, state, newState); + enter(newState); + } + + /** + * Performs the entry action of the state machine. + */ + @Override + void entry() { + final State newState = initialState(); + LOGGER.log(Level.DEBUG, "{0}: initial state={1}", this, newState); + enter(newState); + } + + /** + * Enters a new state. + * + * @param newState The new state to enter. + */ + private void enter(State newState) { + if (newState.parent != this) + throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this); + state = newState; + state.entry(); + } + + @Override + void a() { + state.a(); + } + + @Override + void b() { + state.b(); + } + + @Override + void c() { + state.c(); + } + + @Override + void d() { + state.d(); + } + + @Override + public String toString() { + return super.toString() + "(in " + state + ")"; + } +} diff --git a/src/oop/ch07/auto/generics/Upper.java b/src/oop/ch07/auto/generics/Upper.java new file mode 100644 index 0000000..1a23c3c --- /dev/null +++ b/src/oop/ch07/auto/generics/Upper.java @@ -0,0 +1,12 @@ +package oop.ch07.auto.generics; + +class Upper extends StateMachine { + Upper(A parent) { + super(parent); + } + + @Override + B initialState() { + return new B(this); + } +} diff --git a/src/oop/ch07/auto/simple/A.java b/src/oop/ch07/auto/simple/A.java new file mode 100644 index 0000000..3fed093 --- /dev/null +++ b/src/oop/ch07/auto/simple/A.java @@ -0,0 +1,40 @@ +package oop.ch07.auto.simple; + +class A extends State { + private final Upper upper = new Upper(this); + private final Lower lower = new Lower(this); + + A(State parent) { + super(parent); + } + + @Override + void entry() { + upper.entry(); + lower.entry(); + } + + @Override + void a() { + upper.a(); + lower.a(); + } + + @Override + void b() { + upper.b(); + lower.b(); + } + + @Override + void c() { + upper.c(); + lower.c(); + } + + @Override + void d() { + System.out.println("A->F"); + parent.gotoState(new F(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/Automaton.java b/src/oop/ch07/auto/simple/Automaton.java new file mode 100644 index 0000000..bb46c94 --- /dev/null +++ b/src/oop/ch07/auto/simple/Automaton.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class Automaton extends StateMachine { + Automaton() { + super(null); + entry(); + } + + @Override + A initialState() { + return new A(this); + } +} diff --git a/src/oop/ch07/auto/simple/B.java b/src/oop/ch07/auto/simple/B.java new file mode 100644 index 0000000..f00bf01 --- /dev/null +++ b/src/oop/ch07/auto/simple/B.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class B extends State { + B(State parent) { + super(parent); + } + + @Override + void a() { + System.out.println("B->C"); + parent.gotoState(new C(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/C.java b/src/oop/ch07/auto/simple/C.java new file mode 100644 index 0000000..c222e18 --- /dev/null +++ b/src/oop/ch07/auto/simple/C.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class C extends State { + C(State parent) { + super(parent); + } + + @Override + void b() { + System.out.println("C->B"); + parent.gotoState(new B(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/D.java b/src/oop/ch07/auto/simple/D.java new file mode 100644 index 0000000..eec583f --- /dev/null +++ b/src/oop/ch07/auto/simple/D.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class D extends State { + D(State parent) { + super(parent); + } + + @Override + void a() { + System.out.println("D->E"); + parent.gotoState(new E(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/Demo.java b/src/oop/ch07/auto/simple/Demo.java new file mode 100644 index 0000000..a019ed1 --- /dev/null +++ b/src/oop/ch07/auto/simple/Demo.java @@ -0,0 +1,25 @@ +package oop.ch07.auto.simple; + +public class Demo { + public static void main(String[] args) { + final Automaton auto = new Automaton(); + System.out.println("a:"); + auto.a(); + System.out.println("---------\nb:"); + auto.b(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\nd:"); + auto.d(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\nd:"); + auto.d(); + System.out.println("---------\nc:"); + auto.c(); + System.out.println("---------\na:"); + auto.a(); + System.out.println("---------\nb:"); + auto.b(); + } +} diff --git a/src/oop/ch07/auto/simple/E.java b/src/oop/ch07/auto/simple/E.java new file mode 100644 index 0000000..e2809af --- /dev/null +++ b/src/oop/ch07/auto/simple/E.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class E extends State { + E(State parent) { + super(parent); + } + + @Override + void c() { + System.out.println("E->D"); + parent.gotoState(new D(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/F.java b/src/oop/ch07/auto/simple/F.java new file mode 100644 index 0000000..17ca5a3 --- /dev/null +++ b/src/oop/ch07/auto/simple/F.java @@ -0,0 +1,13 @@ +package oop.ch07.auto.simple; + +class F extends State { + F(State parent) { + super(parent); + } + + @Override + void d() { + System.out.println("F->A"); + parent.gotoState(new A(parent)); + } +} diff --git a/src/oop/ch07/auto/simple/Lower.java b/src/oop/ch07/auto/simple/Lower.java new file mode 100644 index 0000000..e34795e --- /dev/null +++ b/src/oop/ch07/auto/simple/Lower.java @@ -0,0 +1,18 @@ +package oop.ch07.auto.simple; + +class Lower extends StateMachine { + Lower(State parent) { + super(parent); + } + + @Override + D initialState() { + return new D(this); + } + + @Override + void b() { + System.out.println("A->E"); + gotoState(new E(this)); + } +} diff --git a/src/oop/ch07/auto/simple/State.java b/src/oop/ch07/auto/simple/State.java new file mode 100644 index 0000000..d1ad84a --- /dev/null +++ b/src/oop/ch07/auto/simple/State.java @@ -0,0 +1,49 @@ +package oop.ch07.auto.simple; + +import java.lang.System.Logger; + +/** + * An abstract class representing a state in a state machine. + */ +abstract class State { + /** + * The logger for logging messages. + */ + static final Logger LOGGER = System.getLogger(State.class.getName()); + + /** + * The parent state containing this state. + */ + final State parent; + + /** + * Constructs a state with the given parent. + * + * @param parent The state containing this state. + */ + State(State parent) { + this.parent = parent; + } + + void gotoState(State newState) { + throw new IllegalStateException("not in a statemachine"); + } + + /** + * Performs entry actions when entering this state. + */ + void entry() { /* do nothing */} + + void a() { /* do nothing */} + + void b() { /* do nothing */} + + void c() { /* do nothing */} + + void d() { /* do nothing */} + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/src/oop/ch07/auto/simple/StateMachine.java b/src/oop/ch07/auto/simple/StateMachine.java new file mode 100644 index 0000000..2d4f700 --- /dev/null +++ b/src/oop/ch07/auto/simple/StateMachine.java @@ -0,0 +1,86 @@ +package oop.ch07.auto.simple; + +import java.lang.System.Logger.Level; + +/** + * An abstract class representing a state machine. It maintains the current state and provides methods to transition + * between states. + */ +abstract class StateMachine extends State { + /** + * The current state of the state machine. + */ + private State state; + + /** + * Constructs a state machine with the given parent. + * + * @param parent The parent state machine. + */ + StateMachine(State parent) { + super(parent); + } + + /** + * Gets the initial state of the state machine. + */ + abstract State initialState(); + + /** + * Transitions the state machine to the specified state. + * + * @param newState The state to transition to. + */ + @Override + void gotoState(State newState) { + LOGGER.log(Level.DEBUG, "{0}: {1} --> {2}", this, state, newState); + enter(newState); + } + + /** + * Performs the entry action of the state machine. + */ + @Override + void entry() { + final State newState = initialState(); + LOGGER.log(Level.DEBUG, "{0}: initial state={1}", this, newState); + enter(newState); + } + + /** + * Enters a new state. + * + * @param newState The new state to enter. + */ + private void enter(State newState) { + if (newState.parent != this) + throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this); + state = newState; + state.entry(); + } + + @Override + void a() { + state.a(); + } + + @Override + void b() { + state.b(); + } + + @Override + void c() { + state.c(); + } + + @Override + void d() { + state.d(); + } + + @Override + public String toString() { + return super.toString() + "(in " + state + ")"; + } +} diff --git a/src/oop/ch07/auto/simple/Upper.java b/src/oop/ch07/auto/simple/Upper.java new file mode 100644 index 0000000..a1726cd --- /dev/null +++ b/src/oop/ch07/auto/simple/Upper.java @@ -0,0 +1,12 @@ +package oop.ch07.auto.simple; + +class Upper extends StateMachine { + Upper(State parent) { + super(parent); + } + + @Override + B initialState() { + return new B(this); + } +} diff --git a/src/oop/ch07/calc/AfterDot.java b/src/oop/ch07/calc/AfterDot.java new file mode 100644 index 0000000..e1d76bc --- /dev/null +++ b/src/oop/ch07/calc/AfterDot.java @@ -0,0 +1,12 @@ +package oop.ch07.calc; + +class AfterDot extends State { + AfterDot(Lower parent) {super(parent, "after_dot");} + + @Override + void digit(int d) { + final Calculator c = getCalculator(); + c.setValue(c.getValue() + d * c.getFactor()); + c.setFactor(0.1 * c.getFactor()); + } +} diff --git a/src/oop/ch07/calc/Automaton.java b/src/oop/ch07/calc/Automaton.java new file mode 100644 index 0000000..a7d7141 --- /dev/null +++ b/src/oop/ch07/calc/Automaton.java @@ -0,0 +1,55 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.CalculatorError; +import oop.ch06.calc.UnaryOp; + +class Automaton extends StateMachine { + private final Calculator calculator; + + Automaton(Calculator calculator) { + super(null, "Automaton"); + this.calculator = calculator; + entry(); + } + + @Override + Calculator getCalculator() { + return calculator; + } + + @Override + Working initialState() { + return new Working(this); + } + + @Override + void unOp(UnaryOp op) { + try { + super.unOp(op); + } + catch (CalculatorError ex) { + error(); + } + } + + @Override + void binOp(BinaryOp op) { + try { + super.binOp(op); + } + catch (CalculatorError ex) { + error(); + } + } + + @Override + void eval() { + try { + super.eval(); + } + catch (CalculatorError ex) { + error(); + } + } +} diff --git a/src/oop/ch07/calc/BeforeDot.java b/src/oop/ch07/calc/BeforeDot.java new file mode 100644 index 0000000..02fa3d2 --- /dev/null +++ b/src/oop/ch07/calc/BeforeDot.java @@ -0,0 +1,19 @@ +package oop.ch07.calc; + +class BeforeDot extends State { + BeforeDot(Lower parent) { + super(parent, "before_dot"); + } + + @Override + void digit(int d) { + final Calculator c = getCalculator(); + c.setValue(10.0 * c.getValue() + d); + } + + @Override + void dot() { + getCalculator().setFactor(0.1); + parent.gotoState(new AfterDot(parent)); + } +} diff --git a/src/oop/ch07/calc/BinOp.java b/src/oop/ch07/calc/BinOp.java new file mode 100644 index 0000000..d1c0e81 --- /dev/null +++ b/src/oop/ch07/calc/BinOp.java @@ -0,0 +1,28 @@ +package oop.ch07.calc; + +import oop.ch06.calc.UnaryOp; + +class BinOp extends StateMachine { + BinOp(Upper parent) { + super(parent, "bin_op"); + } + + @Override + State initialState() { + return new HasFirst(this); + } + + @Override + void unOp(UnaryOp op) { + final Calculator c = getCalculator(); + c.setValue(op.eval(c.getValue())); + gotoState(new HasSecond(this)); + } + + @Override + void eval() { + final Calculator c = getCalculator(); + c.setValue(c.getLastOp().eval(c.getOpnd1(), c.getValue())); + parent.gotoState(new NoOp(parent)); + } +} diff --git a/src/oop/ch07/calc/Calculator.java b/src/oop/ch07/calc/Calculator.java new file mode 100644 index 0000000..714f985 --- /dev/null +++ b/src/oop/ch07/calc/Calculator.java @@ -0,0 +1,96 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.CalculatorError; +import oop.ch06.calc.UnaryOp; +import oop.ch06.observer.Observer; +import oop.ch06.observer.ObserverSupport; + +public class Calculator { + private double value; + private double opnd1; + private BinaryOp lastOp; + private double factor; + private final ObserverSupport observerSupport = new ObserverSupport(); + private final Automaton auto = new Automaton(this); + + public double getValue() { + return value; + } + + void setValue(double n) { + value = n; + observerSupport.notifyObservers(); + } + + double getOpnd1() { + return opnd1; + } + + void setOpnd1(double opnd1) { + this.opnd1 = opnd1; + } + + BinaryOp getLastOp() { + return lastOp; + } + + void setLastOp(BinaryOp lastOp) { + this.lastOp = lastOp; + } + + double getFactor() { + return factor; + } + + void setFactor(double factor) { + this.factor = factor; + } + + public void addObserver(Observer o) { + observerSupport.addObserver(o); + } + + public void unOp(UnaryOp op) { + try { + auto.unOp(op); + } + catch (CalculatorError ex) { + auto.error(); + } + } + + public void binOp(BinaryOp op) { + try { + auto.binOp(op); + } + catch (CalculatorError ex) { + auto.error(); + } + } + + public void eval() { + try { + auto.eval(); + } + catch (CalculatorError ex) { + auto.error(); + } + } + + public void digit(int d) { + auto.digit(d); + } + + public void dot() { + auto.dot(); + } + + public void clear() { + auto.clear(); + } + + public void allClear() { + auto.allClear(); + } +} diff --git a/src/oop/ch07/calc/Error.java b/src/oop/ch07/calc/Error.java new file mode 100644 index 0000000..4399d3c --- /dev/null +++ b/src/oop/ch07/calc/Error.java @@ -0,0 +1,12 @@ +package oop.ch07.calc; + +class Error extends State { + Error(Automaton parent) { + super(parent, "error"); + } + + @Override + void allClear() { + parent.gotoState(new Working(parent)); + } +} diff --git a/src/oop/ch07/calc/Example.java b/src/oop/ch07/calc/Example.java new file mode 100644 index 0000000..c9aacf6 --- /dev/null +++ b/src/oop/ch07/calc/Example.java @@ -0,0 +1,29 @@ +package oop.ch07.calc; + +import oop.ch06.calc.Ops; + +class Example { + public static void main(String[] args) { + final Calculator calc = new Calculator(); + calc.digit(2); + calc.digit(4); + calc.dot(); + calc.digit(5); + System.out.println(calc.getValue()); + calc.binOp(Ops.PLUS); + calc.digit(3); + calc.clear(); + calc.binOp(Ops.MULT); + calc.digit(1); + calc.digit(2); + System.out.println(calc.getValue()); + calc.eval(); + System.out.println(calc.getValue()); + calc.unOp(Ops.CHANGE_SIGN); + System.out.println(calc.getValue()); + calc.unOp(Ops.SQRT); + System.out.println(calc.getValue()); + calc.allClear(); + System.out.println(calc.getValue()); + } +} diff --git a/src/oop/ch07/calc/HasFirst.java b/src/oop/ch07/calc/HasFirst.java new file mode 100644 index 0000000..ede36f5 --- /dev/null +++ b/src/oop/ch07/calc/HasFirst.java @@ -0,0 +1,24 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; + +class HasFirst extends State { + HasFirst(BinOp parent) { + super(parent, "has_first"); + } + + @Override + void digit(int d) { + parent.gotoState(new HasSecond(parent)); + } + + @Override + void dot() { + parent.gotoState(new HasSecond(parent)); + } + + @Override + void binOp(BinaryOp op) { + getCalculator().setLastOp(op); + } +} diff --git a/src/oop/ch07/calc/HasSecond.java b/src/oop/ch07/calc/HasSecond.java new file mode 100644 index 0000000..40158ef --- /dev/null +++ b/src/oop/ch07/calc/HasSecond.java @@ -0,0 +1,23 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; + +class HasSecond extends State { + HasSecond(BinOp parent) { + super(parent, "has_second"); + } + + @Override + void binOp(BinaryOp op) { + final Calculator c = getCalculator(); + c.setValue(c.getLastOp().eval(c.getOpnd1(), c.getValue())); + c.setOpnd1(c.getValue()); + c.setLastOp(op); + parent.gotoState(new HasFirst(parent)); + } + + @Override + void clear() { + parent.gotoState(new HasFirst(parent)); + } +} diff --git a/src/oop/ch07/calc/Lower.java b/src/oop/ch07/calc/Lower.java new file mode 100644 index 0000000..04c46b9 --- /dev/null +++ b/src/oop/ch07/calc/Lower.java @@ -0,0 +1,36 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.UnaryOp; + +class Lower extends StateMachine { + Lower(Working parent) { + super(parent, "lower"); + } + + @Override + Unchanged initialState() { + return new Unchanged(this); + } + + @Override + void unOp(UnaryOp op) { + gotoState(new Unchanged(this)); + } + + @Override + void binOp(BinaryOp op) { + gotoState(new Unchanged(this)); + } + + @Override + void eval() { + gotoState(new Unchanged(this)); + } + + @Override + void clear() { + getCalculator().setValue(0.0); + gotoState(new Unchanged(this)); + } +} diff --git a/src/oop/ch07/calc/NoOp.java b/src/oop/ch07/calc/NoOp.java new file mode 100644 index 0000000..1e0fba1 --- /dev/null +++ b/src/oop/ch07/calc/NoOp.java @@ -0,0 +1,24 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.UnaryOp; + +class NoOp extends State { + NoOp(Upper parent) { + super(parent, "no_op"); + } + + @Override + void unOp(UnaryOp op) { + final Calculator c = getCalculator(); + c.setValue(op.eval(c.getValue())); + } + + @Override + void binOp(BinaryOp op) { + final Calculator c = getCalculator(); + c.setLastOp(op); + c.setOpnd1(c.getValue()); + parent.gotoState(new BinOp(parent)); + } +} diff --git a/src/oop/ch07/calc/State.java b/src/oop/ch07/calc/State.java new file mode 100644 index 0000000..1122da9 --- /dev/null +++ b/src/oop/ch07/calc/State.java @@ -0,0 +1,114 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.UnaryOp; + +import java.lang.System.Logger; + +/** + * An abstract class representing a state in a calculator state machine. + * This class provides basic functionalities that all states in the calculator state machine should implement. + * + * @param

the type of the containing state. + */ +abstract class State

> { + /** + * The logger for logging messages. + */ + static final Logger LOGGER = System.getLogger(State.class.getName()); + + /** + * The name of the state. + */ + final String name; + + /** + * The parent state containing this state. + */ + final P parent; + + /** + * Constructs a state with the given parent. + * + * @param parent The state containing this state. + */ + State(P parent) { + this(parent, null); + } + + /** + * Constructs a state with the given parent and name. + * + * @param parent The state containing this state. + * @param name The name of the state. + */ + State(P parent, String name) { + this.parent = parent; + this.name = name; + } + + /** + * Performs entry actions when entering this state. + */ + void entry() {} + + /** + * Handles the input of a digit. + * + * @param d The digit to be handled. + */ + void digit(int d) {} + + /** + * Handles the input of a dot. + */ + void dot() {} + + /** + * Handles the application of a unary operation. + * + * @param op The unary operation to be applied. + */ + void unOp(UnaryOp op) {} + + /** + * Handles the application of a binary operation. + * + * @param op The binary operation to be applied. + */ + void binOp(BinaryOp op) {} + + /** + * Evaluates the current expression. + */ + void eval() {} + + /** + * Clears the current input. + */ + void clear() {} + + /** + * Clears all stored values and resets the calculator. + */ + void allClear() {} + + /** + * Handles error conditions. + */ + void error() {} + + /** + * Gets the calculator associated with this state. + * + * @return The calculator object. + */ + Calculator getCalculator() { + return parent.getCalculator(); + } + + @Override + public String toString() { + return name == null ? getClass().getName() : name; + } +} diff --git a/src/oop/ch07/calc/StateMachine.java b/src/oop/ch07/calc/StateMachine.java new file mode 100644 index 0000000..a5d311e --- /dev/null +++ b/src/oop/ch07/calc/StateMachine.java @@ -0,0 +1,122 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.UnaryOp; + +import java.lang.System.Logger.Level; + +/** + * An abstract class representing a state machine in a calculator system. + * This class extends the CompositeState class and defines the basic behavior of a state machine. + * It maintains the current state and provides methods to transition between states and handle state changes. + * + * @param the type of the concrete state machine. + * @param

the type of the containing state. + */ +abstract class StateMachine, P extends State> extends State

{ + /** + * The current state of the state machine. + */ + private State state; + + /** + * Constructs a state machine with the given parent. + * + * @param parent The parent state machine. + */ + StateMachine(P parent) { + super(parent); + } + + /** + * Constructs a state machine with the given parent and name. + * + * @param parent The parent state machine. + * @param name The name of the state machine. + */ + StateMachine(P parent, String name) { + super(parent, name); + } + + /** + * Gets the initial state of the state machine. + */ + abstract State initialState(); + + /** + * Transitions the state machine to the specified state. + * + * @param newState The state to transition to. + */ + void gotoState(State newState) { + LOGGER.log(Level.DEBUG, "{0}: {1} --> {2}", this, state, newState); + enter(newState); + } + + /** + * Performs the entry action of the state machine. + */ + @Override + void entry() { + final State newState = initialState(); + LOGGER.log(Level.DEBUG, "{0}: initial state={1}", this, newState); + enter(newState); + } + + /** + * Enters a new state. + * + * @param newState The new state to enter. + */ + private void enter(State newState) { + if (newState.parent != this) + throw new IllegalArgumentException("Wrong state: " + newState + " belongs to " + newState.parent + " instead of " + this); + state = newState; + state.entry(); + } + + @Override + void digit(int d) { + state.digit(d); + } + + @Override + void dot() { + state.dot(); + } + + @Override + void unOp(UnaryOp op) { + state.unOp(op); + } + + @Override + void binOp(BinaryOp op) { + state.binOp(op); + } + + @Override + void eval() { + state.eval(); + } + + @Override + void clear() { + state.clear(); + } + + @Override + void allClear() { + state.allClear(); + } + + @Override + void error() { + state.error(); + } + + @Override + public String toString() { + return super.toString() + "(in " + state + ")"; + } +} diff --git a/src/oop/ch07/calc/Unchanged.java b/src/oop/ch07/calc/Unchanged.java new file mode 100644 index 0000000..001bad1 --- /dev/null +++ b/src/oop/ch07/calc/Unchanged.java @@ -0,0 +1,21 @@ +package oop.ch07.calc; + +class Unchanged extends State { + Unchanged(Lower parent) { + super(parent, "unchanged"); + } + + @Override + void digit(int d) { + getCalculator().setValue(d); + parent.gotoState(new BeforeDot(parent)); + } + + @Override + void dot() { + final Calculator c = getCalculator(); + c.setValue(0.0); + c.setFactor(0.1); + parent.gotoState(new AfterDot(parent)); + } +} diff --git a/src/oop/ch07/calc/Upper.java b/src/oop/ch07/calc/Upper.java new file mode 100644 index 0000000..27dbf01 --- /dev/null +++ b/src/oop/ch07/calc/Upper.java @@ -0,0 +1,12 @@ +package oop.ch07.calc; + +class Upper extends StateMachine { + Upper(Working parent) { + super(parent, "upper"); + } + + @Override + NoOp initialState() { + return new NoOp(this); + } +} diff --git a/src/oop/ch07/calc/Working.java b/src/oop/ch07/calc/Working.java new file mode 100644 index 0000000..107bde2 --- /dev/null +++ b/src/oop/ch07/calc/Working.java @@ -0,0 +1,69 @@ +package oop.ch07.calc; + +import oop.ch06.calc.BinaryOp; +import oop.ch06.calc.UnaryOp; + +class Working extends State { + private final Upper upper; + private final Lower lower; + + Working(Automaton parent) { + super(parent, "working"); + upper = new Upper(this); + lower = new Lower(this); + } + + @Override + void entry() { + getCalculator().setValue(0.0); + upper.entry(); + lower.entry(); + } + + @Override + void digit(int d) { + upper.digit(d); + lower.digit(d); + } + + @Override + void dot() { + upper.dot(); + lower.dot(); + } + + @Override + void unOp(UnaryOp op) { + upper.unOp(op); + lower.unOp(op); + } + + @Override + void binOp(BinaryOp op) { + upper.binOp(op); + lower.binOp(op); + } + + @Override + void eval() { + upper.eval(); + lower.eval(); + } + + @Override + void clear() { + upper.clear(); + lower.clear(); + } + + @Override + void allClear() { + parent.gotoState(new Working(parent)); + } + + @Override + void error() { + getCalculator().setValue(Double.NaN); + parent.gotoState(new Error(parent)); + } +} diff --git a/src/oop/ch07/clock0/Clock.java b/src/oop/ch07/clock0/Clock.java new file mode 100644 index 0000000..06bed35 --- /dev/null +++ b/src/oop/ch07/clock0/Clock.java @@ -0,0 +1,41 @@ +package oop.ch07.clock0; + +public class Clock { + private State state = State.Normal; + private int time = 0; + + public int getTime() { + return time; + } + + public void tick() { + switch (state) { + case Normal: + time += 1; + break; + case Setting: + break; + } + } + + public void set() { + switch (state) { + case Normal: + break; + case Setting: + time += 60; + break; + } + } + + public void mode() { + switch (state) { + case Normal: + state = State.Setting; + break; + case Setting: + state = State.Normal; + break; + } + } +} \ No newline at end of file diff --git a/src/oop/ch07/clock0/Example.java b/src/oop/ch07/clock0/Example.java new file mode 100644 index 0000000..7b5768b --- /dev/null +++ b/src/oop/ch07/clock0/Example.java @@ -0,0 +1,33 @@ +package oop.ch07.clock0; + +public class Example { + public static void main(String[] args) { + final Clock clock = new Clock(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + } +} diff --git a/src/oop/ch07/clock0/State.java b/src/oop/ch07/clock0/State.java new file mode 100644 index 0000000..5cfa69d --- /dev/null +++ b/src/oop/ch07/clock0/State.java @@ -0,0 +1,5 @@ +package oop.ch07.clock0; + +enum State { + Normal, Setting +} diff --git a/src/oop/ch07/clock1/Clock.java b/src/oop/ch07/clock1/Clock.java new file mode 100644 index 0000000..0d77ce2 --- /dev/null +++ b/src/oop/ch07/clock1/Clock.java @@ -0,0 +1,23 @@ +package oop.ch07.clock1; + +public class Clock { + private State state = new Normal(this); + private int t = 0; + + void setState(State s) { state = s; } + + public int getTime() { + return t; + } + + void setTime(int t) { + this.t = t; + } + + public void tick() {state.tick();} + + public void set() {state.set();} + + public void mode() {state.mode();} +} + diff --git a/src/oop/ch07/clock1/Example.java b/src/oop/ch07/clock1/Example.java new file mode 100644 index 0000000..b259660 --- /dev/null +++ b/src/oop/ch07/clock1/Example.java @@ -0,0 +1,33 @@ +package oop.ch07.clock1; + +public class Example { + public static void main(String[] args) { + final Clock clock = new Clock(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + } +} diff --git a/src/oop/ch07/clock1/Normal.java b/src/oop/ch07/clock1/Normal.java new file mode 100644 index 0000000..bb324f5 --- /dev/null +++ b/src/oop/ch07/clock1/Normal.java @@ -0,0 +1,13 @@ +package oop.ch07.clock1; + +class Normal extends State { + Normal(Clock clock) { + super(clock); + } + + public void tick() {clock.setTime(clock.getTime() + 1);} + + public void set() {} + + public void mode() {clock.setState(new Setting(clock));} +} diff --git a/src/oop/ch07/clock1/Setting.java b/src/oop/ch07/clock1/Setting.java new file mode 100644 index 0000000..ccaf884 --- /dev/null +++ b/src/oop/ch07/clock1/Setting.java @@ -0,0 +1,13 @@ +package oop.ch07.clock1; + +class Setting extends State { + Setting(Clock clock) { + super(clock); + } + + public void tick() {} + + public void set() {clock.setTime(clock.getTime() + 60);} + + public void mode() {clock.setState(new Normal(clock));} +} diff --git a/src/oop/ch07/clock1/State.java b/src/oop/ch07/clock1/State.java new file mode 100644 index 0000000..fc8e7b2 --- /dev/null +++ b/src/oop/ch07/clock1/State.java @@ -0,0 +1,15 @@ +package oop.ch07.clock1; + +abstract class State { + final Clock clock; + + State(Clock clock) { + this.clock = clock; + } + + abstract void tick(); + + abstract void set(); + + abstract void mode(); +} diff --git a/src/oop/ch07/clock2/Clock.java b/src/oop/ch07/clock2/Clock.java new file mode 100644 index 0000000..d7f7f9b --- /dev/null +++ b/src/oop/ch07/clock2/Clock.java @@ -0,0 +1,25 @@ +package oop.ch07.clock2; + +public class Clock { + final State normal = new Normal(this); + final State setting = new Setting(this); + private State state = normal; + private int t = 0; + + void setState(State s) { state = s; } + + public int getTime() { + return t; + } + + void setTime(int t) { + this.t = t; + } + + public void tick() { state.tick(); } + + public void set() { state.set(); } + + public void mode() { state.mode(); } +} + diff --git a/src/oop/ch07/clock2/Example.java b/src/oop/ch07/clock2/Example.java new file mode 100644 index 0000000..fa2e216 --- /dev/null +++ b/src/oop/ch07/clock2/Example.java @@ -0,0 +1,33 @@ +package oop.ch07.clock2; + +public class Example { + public static void main(String[] args) { + final Clock clock = new Clock(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + } +} diff --git a/src/oop/ch07/clock2/Normal.java b/src/oop/ch07/clock2/Normal.java new file mode 100644 index 0000000..50ffc9a --- /dev/null +++ b/src/oop/ch07/clock2/Normal.java @@ -0,0 +1,13 @@ +package oop.ch07.clock2; + +class Normal extends State { + Normal(Clock clock) { + super(clock); + } + + public void tick() { clock.setTime(clock.getTime() + 1); } + + public void set() {} + + public void mode() { clock.setState(clock.setting); } +} diff --git a/src/oop/ch07/clock2/Setting.java b/src/oop/ch07/clock2/Setting.java new file mode 100644 index 0000000..516ba7c --- /dev/null +++ b/src/oop/ch07/clock2/Setting.java @@ -0,0 +1,13 @@ +package oop.ch07.clock2; + +class Setting extends State { + Setting(Clock clock) { + super(clock); + } + + public void tick() {} + + public void set() { clock.setTime(clock.getTime() + 60); } + + public void mode() { clock.setState(clock.normal); } +} diff --git a/src/oop/ch07/clock2/State.java b/src/oop/ch07/clock2/State.java new file mode 100644 index 0000000..4abe17d --- /dev/null +++ b/src/oop/ch07/clock2/State.java @@ -0,0 +1,15 @@ +package oop.ch07.clock2; + +abstract class State { + final Clock clock; + + State(Clock clock) { + this.clock = clock; + } + + abstract void tick(); + + abstract void set(); + + abstract void mode(); +} diff --git a/src/oop/ch07/clock3/Clock.java b/src/oop/ch07/clock3/Clock.java new file mode 100644 index 0000000..3c0ab86 --- /dev/null +++ b/src/oop/ch07/clock3/Clock.java @@ -0,0 +1,51 @@ +package oop.ch07.clock3; + +public class Clock { + private abstract class State { + abstract void tick(); + + abstract void set(); + + abstract void mode(); + } + + private final State normal = new State() { + @Override + void tick() { setTime(getTime() + 1); } + + @Override + void set() {} + + @Override + void mode() { state = setting; } + }; + + private final State setting = new State() { + @Override + void tick() {} + + @Override + void set() { setTime(getTime() + 60); } + + @Override + void mode() { state = normal; } + }; + + private State state = normal; + private int t = 0; + + public int getTime() { + return t; + } + + private void setTime(int t) { + this.t = t; + } + + public void tick() { state.tick(); } + + public void set() { state.set(); } + + public void mode() { state.mode(); } +} + diff --git a/src/oop/ch07/clock3/Example.java b/src/oop/ch07/clock3/Example.java new file mode 100644 index 0000000..293d9f7 --- /dev/null +++ b/src/oop/ch07/clock3/Example.java @@ -0,0 +1,33 @@ +package oop.ch07.clock3; + +public class Example { + public static void main(String[] args) { + final Clock clock = new Clock(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + clock.mode(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + clock.tick(); + clock.set(); + System.out.println(clock.getTime()); + } +} diff --git a/src/oop/ch07/expr1/BinExpr.java b/src/oop/ch07/expr1/BinExpr.java new file mode 100644 index 0000000..274ddf4 --- /dev/null +++ b/src/oop/ch07/expr1/BinExpr.java @@ -0,0 +1,16 @@ +package oop.ch07.expr1; + +public abstract class BinExpr implements Expr { + public final Expr op1; + public final Expr op2; + private final char op; + + protected BinExpr(Expr op1, char op, Expr op2) { + this.op1 = op1; + this.op2 = op2; + this.op = op; + } + + @Override + public String toString() {return "(" + op1 + op + op2 + ")";} +} diff --git a/src/oop/ch07/expr1/Const.java b/src/oop/ch07/expr1/Const.java new file mode 100644 index 0000000..3fa3b0a --- /dev/null +++ b/src/oop/ch07/expr1/Const.java @@ -0,0 +1,15 @@ +package oop.ch07.expr1; + +public class Const implements Expr { + public final int value; + + public Const(int v) { + this.value = v; + } + + @Override + public int eval() { return value; } + + @Override + public String toString() { return String.valueOf(value); } +} diff --git a/src/oop/ch07/expr1/Example.java b/src/oop/ch07/expr1/Example.java new file mode 100644 index 0000000..2164186 --- /dev/null +++ b/src/oop/ch07/expr1/Example.java @@ -0,0 +1,8 @@ +package oop.ch07.expr1; + +public class Example { + public static void main(String[] args) { + Expr e = new Plus(new Neg(new Minus(new Const(1), new Const(2))), new Const(12)); + System.out.println(e + " = " + e.eval()); + } +} diff --git a/src/oop/ch07/expr1/Expr.java b/src/oop/ch07/expr1/Expr.java new file mode 100644 index 0000000..f347f9a --- /dev/null +++ b/src/oop/ch07/expr1/Expr.java @@ -0,0 +1,6 @@ +package oop.ch07.expr1; + +public interface Expr { + int eval(); +} + diff --git a/src/oop/ch07/expr1/Minus.java b/src/oop/ch07/expr1/Minus.java new file mode 100644 index 0000000..8d8f568 --- /dev/null +++ b/src/oop/ch07/expr1/Minus.java @@ -0,0 +1,10 @@ +package oop.ch07.expr1; + +public class Minus extends BinExpr { + public Minus(Expr op1, Expr op2) { + super(op1, '-', op2); + } + + @Override + public int eval() { return op1.eval() - op2.eval(); } +} diff --git a/src/oop/ch07/expr1/Mult.java b/src/oop/ch07/expr1/Mult.java new file mode 100644 index 0000000..d1936a3 --- /dev/null +++ b/src/oop/ch07/expr1/Mult.java @@ -0,0 +1,10 @@ +package oop.ch07.expr1; + +public class Mult extends BinExpr { + public Mult(Expr op1, Expr op2) { + super(op1, '*', op2); + } + + @Override + public int eval() { return op1.eval() * op2.eval(); } +} diff --git a/src/oop/ch07/expr1/Neg.java b/src/oop/ch07/expr1/Neg.java new file mode 100644 index 0000000..bf9567a --- /dev/null +++ b/src/oop/ch07/expr1/Neg.java @@ -0,0 +1,15 @@ +package oop.ch07.expr1; + +public class Neg implements Expr { + public final Expr op; + + public Neg(Expr op) { + this.op = op; + } + + @Override + public int eval() {return -op.eval();} + + @Override + public String toString() { return "(-" + op + ")"; } +} diff --git a/src/oop/ch07/expr1/Plus.java b/src/oop/ch07/expr1/Plus.java new file mode 100644 index 0000000..f553b8f --- /dev/null +++ b/src/oop/ch07/expr1/Plus.java @@ -0,0 +1,10 @@ +package oop.ch07.expr1; + +public class Plus extends BinExpr { + public Plus(Expr op1, Expr op2) { + super(op1, '+', op2); + } + + @Override + public int eval() { return op1.eval() + op2.eval(); } +} diff --git a/src/oop/ch07/expr2/BinExpr.java b/src/oop/ch07/expr2/BinExpr.java new file mode 100644 index 0000000..d0dfad5 --- /dev/null +++ b/src/oop/ch07/expr2/BinExpr.java @@ -0,0 +1,16 @@ +package oop.ch07.expr2; + +public abstract class BinExpr implements Expr { + public final Expr op1; + public final Expr op2; + private final char op; + + protected BinExpr(Expr op1, char op, Expr op2) { + this.op1 = op1; + this.op2 = op2; + this.op = op; + } + + @Override + public String toString() {return "(" + op1 + op + op2 + ")";} +} diff --git a/src/oop/ch07/expr2/Const.java b/src/oop/ch07/expr2/Const.java new file mode 100644 index 0000000..ba0b56d --- /dev/null +++ b/src/oop/ch07/expr2/Const.java @@ -0,0 +1,15 @@ +package oop.ch07.expr2; + +public class Const implements Expr { + public final int value; + + public Const(int v) { + this.value = v; + } + + @Override + public T accept(Visitor visitor) {return visitor.visit(this);} + + @Override + public String toString() {return String.valueOf(value);} +} diff --git a/src/oop/ch07/expr2/EvalVisitor.java b/src/oop/ch07/expr2/EvalVisitor.java new file mode 100644 index 0000000..83f4e3b --- /dev/null +++ b/src/oop/ch07/expr2/EvalVisitor.java @@ -0,0 +1,23 @@ +package oop.ch07.expr2; + +public class EvalVisitor implements Visitor { + public Integer visit(Const e) { + return e.value; + } + + public Integer visit(Plus e) { + return e.op1.accept(this) + e.op2.accept(this); + } + + public Integer visit(Minus e) { + return e.op1.accept(this) - e.op2.accept(this); + } + + public Integer visit(Mult e) { + return e.op1.accept(this) * e.op2.accept(this); + } + + public Integer visit(Neg e) { + return -e.op.accept(this); + } +} diff --git a/src/oop/ch07/expr2/Example.java b/src/oop/ch07/expr2/Example.java new file mode 100644 index 0000000..b65b297 --- /dev/null +++ b/src/oop/ch07/expr2/Example.java @@ -0,0 +1,9 @@ +package oop.ch07.expr2; + +public class Example { + public static void main(String[] args) { + Expr e = new Plus(new Neg(new Minus(new Const(1), new Const(2))), new Const(12)); + System.out.println(e + " = " + e.accept(new EvalVisitor())); + System.out.println(e + " = " + e.accept(new LukasiewiczVisitor())); + } +} diff --git a/src/oop/ch07/expr2/Expr.java b/src/oop/ch07/expr2/Expr.java new file mode 100644 index 0000000..92c68e8 --- /dev/null +++ b/src/oop/ch07/expr2/Expr.java @@ -0,0 +1,6 @@ +package oop.ch07.expr2; + +public interface Expr { + T accept(Visitor visitor); +} + diff --git a/src/oop/ch07/expr2/LukasiewiczVisitor.java b/src/oop/ch07/expr2/LukasiewiczVisitor.java new file mode 100644 index 0000000..c9a1bbf --- /dev/null +++ b/src/oop/ch07/expr2/LukasiewiczVisitor.java @@ -0,0 +1,28 @@ +package oop.ch07.expr2; + +public class LukasiewiczVisitor implements Visitor { + @Override + public String visit(Const e) { + return String.valueOf(e.value); + } + + @Override + public String visit(Plus e) { + return "+ " + e.op1.accept(this) + " " + e.op2.accept(this); + } + + @Override + public String visit(Minus e) { + return "- " + e.op1.accept(this) + " " + e.op2.accept(this); + } + + @Override + public String visit(Mult e) { + return "* " + e.op1.accept(this) + " " + e.op2.accept(this); + } + + @Override + public String visit(Neg e) { + return "~ " + e.op.accept(this); + } +} diff --git a/src/oop/ch07/expr2/Minus.java b/src/oop/ch07/expr2/Minus.java new file mode 100644 index 0000000..f4d9bae --- /dev/null +++ b/src/oop/ch07/expr2/Minus.java @@ -0,0 +1,10 @@ +package oop.ch07.expr2; + +public class Minus extends BinExpr { + public Minus(Expr op1, Expr op2) { + super(op1, '-', op2); + } + + @Override + public T accept(Visitor visitor) {return visitor.visit(this);} +} diff --git a/src/oop/ch07/expr2/Mult.java b/src/oop/ch07/expr2/Mult.java new file mode 100644 index 0000000..c6ae24a --- /dev/null +++ b/src/oop/ch07/expr2/Mult.java @@ -0,0 +1,10 @@ +package oop.ch07.expr2; + +public class Mult extends BinExpr { + public Mult(Expr op1, Expr op2) { + super(op1, '*', op2); + } + + @Override + public T accept(Visitor visitor) { return visitor.visit(this); } +} diff --git a/src/oop/ch07/expr2/Neg.java b/src/oop/ch07/expr2/Neg.java new file mode 100644 index 0000000..400752f --- /dev/null +++ b/src/oop/ch07/expr2/Neg.java @@ -0,0 +1,15 @@ +package oop.ch07.expr2; + +public class Neg implements Expr { + public final Expr op; + + public Neg(Expr op) { + this.op = op; + } + + @Override + public T accept(Visitor visitor) { return visitor.visit(this); } + + @Override + public String toString() { return "(-" + op + ")"; } +} diff --git a/src/oop/ch07/expr2/Plus.java b/src/oop/ch07/expr2/Plus.java new file mode 100644 index 0000000..aec3e9f --- /dev/null +++ b/src/oop/ch07/expr2/Plus.java @@ -0,0 +1,8 @@ +package oop.ch07.expr2; + +public class Plus extends BinExpr { + public Plus(Expr op1, Expr op2) { super(op1, '+', op2); } + + @Override + public T accept(Visitor visitor) { return visitor.visit(this); } +} diff --git a/src/oop/ch07/expr2/Visitor.java b/src/oop/ch07/expr2/Visitor.java new file mode 100644 index 0000000..49d01a8 --- /dev/null +++ b/src/oop/ch07/expr2/Visitor.java @@ -0,0 +1,13 @@ +package oop.ch07.expr2; + +public interface Visitor { + T visit(Const e); + + T visit(Plus e); + + T visit(Minus e); + + T visit(Mult e); + + T visit(Neg e); +} diff --git a/src/oop/ch07/trace/ConsoleTrace.java b/src/oop/ch07/trace/ConsoleTrace.java new file mode 100644 index 0000000..88d2aca --- /dev/null +++ b/src/oop/ch07/trace/ConsoleTrace.java @@ -0,0 +1,19 @@ +package oop.ch07.trace; + +public class ConsoleTrace extends Trace { + @Override + public void debug(int level, String message) { + if (level >= getDebugLevel()) + System.out.println("DEBUG: " + message); + } + + @Override + public void error(String message) { + System.out.println("ERROR: " + message); + } + + @Override + public void close() { + // nothing to do here + } +} diff --git a/src/oop/ch07/trace/FileTrace.java b/src/oop/ch07/trace/FileTrace.java new file mode 100644 index 0000000..0e0685a --- /dev/null +++ b/src/oop/ch07/trace/FileTrace.java @@ -0,0 +1,28 @@ +package oop.ch07.trace; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +public class FileTrace extends Trace { + private final PrintWriter pw; + + public FileTrace(String path) throws IOException { + pw = new PrintWriter(new FileWriter(path), true); + } + + @Override + public void debug(int level, String message) { + if (level >= getDebugLevel()) pw.println("DEBUG: " + message); + } + + @Override + public void error(String message) { + pw.println("ERROR: " + message); + } + + @Override + public void close() { + pw.close(); + } +} diff --git a/src/oop/ch07/trace/Trace.java b/src/oop/ch07/trace/Trace.java new file mode 100644 index 0000000..5888ad1 --- /dev/null +++ b/src/oop/ch07/trace/Trace.java @@ -0,0 +1,28 @@ +package oop.ch07.trace; + +import java.io.IOException; + +public abstract class Trace { + private int debugLevel = 0; + + public void setDebugLevel(int debugLevel) { + this.debugLevel = debugLevel; + } + + public int getDebugLevel() {return debugLevel;} + + public abstract void debug(int level, String message); + + public abstract void error(String message); + + public abstract void close(); + + public static Trace intoFile(String path) { + try { return new FileTrace(path); } + catch (IOException ex) { + final Trace trace = new ConsoleTrace(); + trace.error(ex.getMessage() + " - falling back to system trace."); + return trace; + } + } +} diff --git a/src/oop/ch07/tree/Example.java b/src/oop/ch07/tree/Example.java new file mode 100644 index 0000000..a9bfcdd --- /dev/null +++ b/src/oop/ch07/tree/Example.java @@ -0,0 +1,40 @@ +package oop.ch07.tree; + +public class Example { + public static void main(String[] args) { + final Tree t = + new Tree<>(42, + new Tree<>(17, + new Tree<>(5), + null), + new Tree<>(81, + new Tree<>(51), + new Tree<>(97))); + System.out.println("t = " + t); + for (int e : t) System.out.println(e); + + final Tree t1 = + new Tree<>("foo", + new Tree<>("baz", + new Tree<>("abc"), + null), + new Tree<>("google", + new Tree<>("gnat"), + new Tree<>("hex"))); + + System.out.println("t1 = " + t1); + for (String e : t1) System.out.println(e); + + final Tree t2 = new Tree<>("1", new Tree<>("2", new Tree<>("3"), null), null); + System.out.println("t2 = " + t2); + for (String e : t2) System.out.println(e); + + final Tree t3 = new Tree<>("4", null, new Tree<>("5", null, new Tree<>("6"))); + System.out.println("t3 = " + t3); + for (String e : t3) System.out.println(e); + + final Tree t4 = new Tree<>("7", t2, t3); + System.out.println("t4 = " + t4); + for (String e : t4) System.out.println(e); + } +} diff --git a/src/oop/ch07/tree/Example2.java b/src/oop/ch07/tree/Example2.java new file mode 100644 index 0000000..7137dc1 --- /dev/null +++ b/src/oop/ch07/tree/Example2.java @@ -0,0 +1,15 @@ +package oop.ch07.tree; + +import java.util.Iterator; +import java.util.List; + +public class Example2 { + public static void main(String[] args) { + final Iterator it = + new IterablesIterator<>(List.of(List.of(1, 2, 3), + List.of(4, 5, 6), + List.of(7, 8, 9))); + while (it.hasNext()) + System.out.println(it.next()); + } +} diff --git a/src/oop/ch07/tree/IterablesIterator.java b/src/oop/ch07/tree/IterablesIterator.java new file mode 100644 index 0000000..435dcf7 --- /dev/null +++ b/src/oop/ch07/tree/IterablesIterator.java @@ -0,0 +1,32 @@ +package oop.ch07.tree; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class IterablesIterator implements Iterator { + private final Iterator> iterablesIt; + private Iterator currentIt; + + public IterablesIterator(Iterable> iterables) { + iterablesIt = iterables.iterator(); + } + + @Override + public boolean hasNext() { + if (currentIt != null && currentIt.hasNext()) + return true; + while (iterablesIt.hasNext()) { + currentIt = iterablesIt.next().iterator(); + if (currentIt.hasNext()) + return true; + } + return false; + } + + @Override + public E next() { + if (hasNext()) + return currentIt.next(); + throw new NoSuchElementException(); + } +} diff --git a/src/oop/ch07/tree/Tree.java b/src/oop/ch07/tree/Tree.java new file mode 100644 index 0000000..3532230 --- /dev/null +++ b/src/oop/ch07/tree/Tree.java @@ -0,0 +1,33 @@ +package oop.ch07.tree; + +import java.util.Iterator; +import java.util.List; + +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; + +public class Tree implements Iterable { + public final E elem; + public final Tree left; + public final Tree right; + + public Tree(E elem) {this(elem, null, null);} + + public Tree(E elem, Tree left, Tree right) { + this.elem = elem; + this.left = left; + this.right = right; + } + + @Override + public String toString() { + return String.format("%s(%s, %s)", elem, left, right); + } + + @Override + public Iterator iterator() { + return new IterablesIterator<>(List.of(left == null ? emptyList() : left, + singletonList(elem), + right == null ? emptyList() : right)); + } +} diff --git a/src/oop/ch08/gui/calc/CalcApp.java b/src/oop/ch08/gui/calc/CalcApp.java new file mode 100644 index 0000000..816d6e8 --- /dev/null +++ b/src/oop/ch08/gui/calc/CalcApp.java @@ -0,0 +1,21 @@ +package oop.ch08.gui.calc; + +import oop.ch07.calc.Calculator; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CalcApp { + public static void main(String[] args) { + showFrame(new CalcGUI(new Calculator()), "Calculator"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/calc/CalcGUI.java b/src/oop/ch08/gui/calc/CalcGUI.java new file mode 100644 index 0000000..3033aa4 --- /dev/null +++ b/src/oop/ch08/gui/calc/CalcGUI.java @@ -0,0 +1,77 @@ +package oop.ch08.gui.calc; + +import oop.ch06.calc.Ops; +import oop.ch06.observer.Observer; +import oop.ch07.calc.Calculator; + +import javax.swing.JButton; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.SwingConstants; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.event.ActionListener; + +class CalcGUI extends JPanel implements Observer { + private static final int COLUMNS = 4; + private final Calculator calc; + private final JTextField display = new JTextField(); + private int count; + + CalcGUI(Calculator calc) { + this.calc = calc; + calc.addObserver(this); + setLayout(new GridBagLayout()); + addDisplay(); + addButton("±", e -> calc.unOp(Ops.CHANGE_SIGN)); + addButton("√", e -> calc.unOp(Ops.SQRT)); + addButton("C", e -> calc.clear()); + addButton("AC", e -> calc.allClear()); + addButton("7", e -> calc.digit(7)); + addButton("8", e -> calc.digit(8)); + addButton("9", e -> calc.digit(9)); + addButton("+", e -> calc.binOp(Ops.PLUS)); + addButton("4", e -> calc.digit(4)); + addButton("5", e -> calc.digit(5)); + addButton("6", e -> calc.digit(6)); + addButton("-", e -> calc.binOp(Ops.MINUS)); + addButton("1", e -> calc.digit(1)); + addButton("2", e -> calc.digit(2)); + addButton("3", e -> calc.digit(3)); + addButton("*", e -> calc.binOp(Ops.MULT)); + addButton("0", e -> calc.digit(0)); + addButton(".", e -> calc.dot()); + addButton("=", e -> calc.eval()); + addButton("/", e -> calc.binOp(Ops.DIV)); + update(); + } + + private void addDisplay() { + display.setEditable(false); + display.setHorizontalAlignment(SwingConstants.TRAILING); + add(display, makeConstraint(COLUMNS)); + } + + private void addButton(String label, ActionListener listener) { + final JButton button = new JButton(label); + button.addActionListener(listener); + add(button, makeConstraint(1)); + } + + private GridBagConstraints makeConstraint(int span) { + final GridBagConstraints c = new GridBagConstraints(); + c.gridx = count % COLUMNS; + c.gridy = count / COLUMNS; + c.gridwidth = span; + c.fill = GridBagConstraints.BOTH; + c.weightx = 0.5; + c.weighty = 0.5; + count += span; + return c; + } + + @Override + public void update() { + display.setText(String.valueOf(calc.getValue())); + } +} diff --git a/src/oop/ch08/gui/mvc/model/CounterModel.java b/src/oop/ch08/gui/mvc/model/CounterModel.java new file mode 100644 index 0000000..efc4f01 --- /dev/null +++ b/src/oop/ch08/gui/mvc/model/CounterModel.java @@ -0,0 +1,22 @@ +package oop.ch08.gui.mvc.model; + +import oop.ch06.observer.Observer; +import oop.ch06.observer.ObserverSupport; + +public class CounterModel { + private final ObserverSupport observerSupport = new ObserverSupport(); + private int counter = 0; + + public void addObserver(Observer observer) { + observerSupport.addObserver(observer); + } + + public int getCounter() { + return counter; + } + + public void increment() { + counter++; + observerSupport.notifyObservers(); + } +} diff --git a/src/oop/ch08/gui/mvc/v1/CounterApp.java b/src/oop/ch08/gui/mvc/v1/CounterApp.java new file mode 100644 index 0000000..5af64af --- /dev/null +++ b/src/oop/ch08/gui/mvc/v1/CounterApp.java @@ -0,0 +1,24 @@ +package oop.ch08.gui.mvc.v1; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + final CounterModel model = new CounterModel(); + final CounterView view = new CounterView(model); + final CounterController controller = new CounterController(model); + view.addMouseListener(controller); + + showFrame(view, "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v1/CounterController.java b/src/oop/ch08/gui/mvc/v1/CounterController.java new file mode 100644 index 0000000..0521476 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v1/CounterController.java @@ -0,0 +1,18 @@ +package oop.ch08.gui.mvc.v1; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +class CounterController extends MouseAdapter { + private final CounterModel model; + + CounterController(CounterModel model) { + this.model = model; + } + + @Override + public void mouseClicked(MouseEvent e) { + if (10 <= e.getX() && e.getX() <= 110 && 10 <= e.getY() && e.getY() <= 40) + model.increment(); + } +} diff --git a/src/oop/ch08/gui/mvc/v1/CounterModel.java b/src/oop/ch08/gui/mvc/v1/CounterModel.java new file mode 100644 index 0000000..a47a9c9 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v1/CounterModel.java @@ -0,0 +1,21 @@ +package oop.ch08.gui.mvc.v1; + +public class CounterModel { + private CounterView view; + private int counter = 0; + + void setView(CounterView view) { + if (this.view != null && this.view != view) + throw new RuntimeException("Trying to reset view"); + this.view = view; + } + + public int getCounter() { + return counter; + } + + public void increment() { + counter++; + view.update(); + } +} diff --git a/src/oop/ch08/gui/mvc/v1/CounterView.java b/src/oop/ch08/gui/mvc/v1/CounterView.java new file mode 100644 index 0000000..44f9442 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v1/CounterView.java @@ -0,0 +1,29 @@ +package oop.ch08.gui.mvc.v1; + +import javax.swing.JPanel; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; + +class CounterView extends JPanel { + private final CounterModel model; + + CounterView(CounterModel model) { + this.model = model; + model.setView(this); + setPreferredSize(new Dimension(200, 100)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.LIGHT_GRAY); + g.fillRect(10, 10, 100, 30); + g.setColor(Color.BLACK); + g.drawString("Push me! (" + model.getCounter() + ")", 15, 30); + } + + public void update() { + repaint(); + } +} diff --git a/src/oop/ch08/gui/mvc/v2/CounterApp.java b/src/oop/ch08/gui/mvc/v2/CounterApp.java new file mode 100644 index 0000000..68f4ada --- /dev/null +++ b/src/oop/ch08/gui/mvc/v2/CounterApp.java @@ -0,0 +1,24 @@ +package oop.ch08.gui.mvc.v2; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + final CounterModel model = new CounterModel(); + final CounterView view = new CounterView(model); + final CounterController controller = new CounterController(model); + view.addMouseListener(controller); + + showFrame(view, "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v2/CounterController.java b/src/oop/ch08/gui/mvc/v2/CounterController.java new file mode 100644 index 0000000..08df41b --- /dev/null +++ b/src/oop/ch08/gui/mvc/v2/CounterController.java @@ -0,0 +1,18 @@ +package oop.ch08.gui.mvc.v2; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +class CounterController extends MouseAdapter { + private final CounterModel model; + + CounterController(CounterModel model) { + this.model = model; + } + + @Override + public void mouseClicked(MouseEvent e) { + if (10 <= e.getX() && e.getX() <= 110 && 10 <= e.getY() && e.getY() <= 40) + model.increment(); + } +} diff --git a/src/oop/ch08/gui/mvc/v2/CounterModel.java b/src/oop/ch08/gui/mvc/v2/CounterModel.java new file mode 100644 index 0000000..56586de --- /dev/null +++ b/src/oop/ch08/gui/mvc/v2/CounterModel.java @@ -0,0 +1,23 @@ +package oop.ch08.gui.mvc.v2; + +import oop.ch06.observer.Observer; + +public class CounterModel { + private Observer observer; + private int counter = 0; + + void setObserver(Observer observer) { + if (this.observer != null && this.observer != observer) + throw new RuntimeException("Trying to reset observer"); + this.observer = observer; + } + + public int getCounter() { + return counter; + } + + public void increment() { + counter++; + observer.update(); + } +} diff --git a/src/oop/ch08/gui/mvc/v2/CounterView.java b/src/oop/ch08/gui/mvc/v2/CounterView.java new file mode 100644 index 0000000..ce549cb --- /dev/null +++ b/src/oop/ch08/gui/mvc/v2/CounterView.java @@ -0,0 +1,32 @@ +package oop.ch08.gui.mvc.v2; + +import oop.ch06.observer.Observer; + +import javax.swing.JPanel; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; + +class CounterView extends JPanel implements Observer { + private final CounterModel model; + + CounterView(CounterModel model) { + this.model = model; + model.setObserver(this); + setPreferredSize(new Dimension(200, 100)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.LIGHT_GRAY); + g.fillRect(10, 10, 100, 30); + g.setColor(Color.BLACK); + g.drawString("Push me! (" + model.getCounter() + ")", 15, 30); + } + + @Override + public void update() { + repaint(); + } +} diff --git a/src/oop/ch08/gui/mvc/v3/CounterApp.java b/src/oop/ch08/gui/mvc/v3/CounterApp.java new file mode 100644 index 0000000..892a216 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v3/CounterApp.java @@ -0,0 +1,26 @@ +package oop.ch08.gui.mvc.v3; + +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + final CounterModel model = new CounterModel(); + final CounterView view = new CounterView(model); + final CounterController controller = new CounterController(model); + view.addMouseListener(controller); + + showFrame(view, "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v3/CounterController.java b/src/oop/ch08/gui/mvc/v3/CounterController.java new file mode 100644 index 0000000..4242551 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v3/CounterController.java @@ -0,0 +1,20 @@ +package oop.ch08.gui.mvc.v3; + +import oop.ch08.gui.mvc.model.CounterModel; + +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +class CounterController extends MouseAdapter { + private final CounterModel model; + + CounterController(CounterModel model) { + this.model = model; + } + + @Override + public void mouseClicked(MouseEvent e) { + if (10 <= e.getX() && e.getX() <= 110 && 10 <= e.getY() && e.getY() <= 40) + model.increment(); + } +} diff --git a/src/oop/ch08/gui/mvc/v3/CounterView.java b/src/oop/ch08/gui/mvc/v3/CounterView.java new file mode 100644 index 0000000..3946abe --- /dev/null +++ b/src/oop/ch08/gui/mvc/v3/CounterView.java @@ -0,0 +1,33 @@ +package oop.ch08.gui.mvc.v3; + +import oop.ch06.observer.Observer; +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JPanel; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; + +class CounterView extends JPanel implements Observer { + private final CounterModel model; + + CounterView(CounterModel model) { + this.model = model; + model.addObserver(this); + setPreferredSize(new Dimension(200, 100)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.LIGHT_GRAY); + g.fillRect(10, 10, 100, 30); + g.setColor(Color.BLACK); + g.drawString("Push me! (" + model.getCounter() + ")", 15, 30); + } + + @Override + public void update() { + repaint(); + } +} diff --git a/src/oop/ch08/gui/mvc/v4/CounterApp.java b/src/oop/ch08/gui/mvc/v4/CounterApp.java new file mode 100644 index 0000000..1e45b93 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v4/CounterApp.java @@ -0,0 +1,21 @@ +package oop.ch08.gui.mvc.v4; + +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + showFrame(new CounterGUI(new CounterModel()), "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v4/CounterController.java b/src/oop/ch08/gui/mvc/v4/CounterController.java new file mode 100644 index 0000000..2015f7f --- /dev/null +++ b/src/oop/ch08/gui/mvc/v4/CounterController.java @@ -0,0 +1,19 @@ +package oop.ch08.gui.mvc.v4; + +import oop.ch08.gui.mvc.model.CounterModel; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +class CounterController implements ActionListener { + private final CounterModel model; + + CounterController(CounterModel model) { + this.model = model; + } + + @Override + public void actionPerformed(ActionEvent e) { + model.increment(); + } +} diff --git a/src/oop/ch08/gui/mvc/v4/CounterGUI.java b/src/oop/ch08/gui/mvc/v4/CounterGUI.java new file mode 100644 index 0000000..caedbc7 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v4/CounterGUI.java @@ -0,0 +1,25 @@ +package oop.ch08.gui.mvc.v4; + +import oop.ch06.observer.Observer; +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JButton; +import javax.swing.JPanel; + +class CounterGUI extends JPanel implements Observer { + private final CounterModel model; + private final JButton button = new JButton("Push me!"); + + CounterGUI(CounterModel model) { + this.model = model; + model.addObserver(this); + button.addActionListener(new CounterController(model)); + add(button); + update(); + } + + @Override + public void update() { + button.setText("Push me! (" + model.getCounter() + ")"); + } +} diff --git a/src/oop/ch08/gui/mvc/v5/CounterApp.java b/src/oop/ch08/gui/mvc/v5/CounterApp.java new file mode 100644 index 0000000..5cb8a16 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v5/CounterApp.java @@ -0,0 +1,21 @@ +package oop.ch08.gui.mvc.v5; + +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + showFrame(new CounterGUI(new CounterModel()), "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v5/CounterGUI.java b/src/oop/ch08/gui/mvc/v5/CounterGUI.java new file mode 100644 index 0000000..90525e9 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v5/CounterGUI.java @@ -0,0 +1,32 @@ +package oop.ch08.gui.mvc.v5; + +import oop.ch06.observer.Observer; +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JButton; +import javax.swing.JPanel; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +class CounterGUI extends JPanel implements Observer { + private final CounterModel model; + private final JButton button = new JButton("Push me!"); + + CounterGUI(CounterModel model) { + this.model = model; + model.addObserver(this); + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + model.increment(); + } + }); + add(button); + update(); + } + + @Override + public void update() { + button.setText("Push me! (" + model.getCounter() + ")"); + } +} diff --git a/src/oop/ch08/gui/mvc/v6/CounterApp.java b/src/oop/ch08/gui/mvc/v6/CounterApp.java new file mode 100644 index 0000000..398df5c --- /dev/null +++ b/src/oop/ch08/gui/mvc/v6/CounterApp.java @@ -0,0 +1,21 @@ +package oop.ch08.gui.mvc.v6; + +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; +import java.awt.Container; + +public class CounterApp { + public static void main(String[] args) { + showFrame(new CounterGUI(new CounterModel()), "Counter"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/src/oop/ch08/gui/mvc/v6/CounterGUI.java b/src/oop/ch08/gui/mvc/v6/CounterGUI.java new file mode 100644 index 0000000..ac4f931 --- /dev/null +++ b/src/oop/ch08/gui/mvc/v6/CounterGUI.java @@ -0,0 +1,25 @@ +package oop.ch08.gui.mvc.v6; + +import oop.ch06.observer.Observer; +import oop.ch08.gui.mvc.model.CounterModel; + +import javax.swing.JButton; +import javax.swing.JPanel; + +class CounterGUI extends JPanel implements Observer { + private final CounterModel model; + private final JButton button = new JButton("Push me!"); + + CounterGUI(CounterModel model) { + this.model = model; + model.addObserver(this); + button.addActionListener(e -> model.increment()); + add(button); + update(); + } + + @Override + public void update() { + button.setText("Push me! (" + model.getCounter() + ")"); + } +} diff --git a/src/oop/ch08/gui/simple/HelloWorld.java b/src/oop/ch08/gui/simple/HelloWorld.java new file mode 100644 index 0000000..cddf627 --- /dev/null +++ b/src/oop/ch08/gui/simple/HelloWorld.java @@ -0,0 +1,37 @@ +package oop.ch08.gui.simple; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.WindowConstants; +import java.awt.Color; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Graphics; + +public class HelloWorld extends JPanel { + public static void main(String[] args) { + final HelloWorld pane = new HelloWorld(); + showFrame(pane, "Hello World"); + } + + private static void showFrame(Container pane, String title) { + final JFrame frame = new JFrame(title); + frame.setContentPane(pane); + frame.pack(); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } + + private HelloWorld() { + setPreferredSize(new Dimension(200, 200)); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(Color.LIGHT_GRAY); + g.fillRect(10, 10, 90, 30); + g.setColor(Color.BLACK); + g.drawString("Hello World", 15, 30); + } +} diff --git a/src/oop/ch09/testing/Example.java b/src/oop/ch09/testing/Example.java new file mode 100644 index 0000000..555876b --- /dev/null +++ b/src/oop/ch09/testing/Example.java @@ -0,0 +1,46 @@ +package oop.ch09.testing; + +public class Example { + /** + * Counts zeroes in an array. + * + * @param x array to count zeroes in + * @return number of occurrences of 0 in x + * @throws NullPointerException if x is null + */ + public static int numZero(int[] x) { + int count = 0; + for (int i = 1; i < x.length; i++) + if (x[i] == 0) + count++; + return count; + } + + public static int numZero2(int[] x) { + int count = 0; + int i = 1; + while (i < x.length) { + if (x[i] == 0) + count++; + i++; + } + return count; + } + + public static void main(String[] args) { + int g = 5 / 0; + check(2, 7, 0); + check(0, 7, 2); + } + + private static void check(int... x) { + System.out.print("zeroes in ["); + boolean first = true; + for (int v : x) { + if (!first) System.out.print(", "); + first = false; + System.out.print(v); + } + System.out.println("]: " + numZero2(x)); + } +} diff --git a/src/uebung03/polynomial/POLNYOMIAL.java b/src/uebung03/polynomial/POLNYOMIAL.java new file mode 100644 index 0000000..e5cddb5 --- /dev/null +++ b/src/uebung03/polynomial/POLNYOMIAL.java @@ -0,0 +1,30 @@ +package uebung03.polynomial; + +public class POLNYOMIAL { + private int[] koeff; + private static int degree = 0; + + // Kontruktor für den die Koeffizienten übergeben werden + public POLNYOMIAL(int[] koeff){ + koeff = this.koeff; + degree = getDegree(koeff); + } + + //Konstruktor für den immer gilt p(x) = x + public POLNYOMIAL(){ + koeff = new int[1]; + degree = getDegree(koeff); + } + + private int getDegree(int[] koeff) { + int result = 0; + for(int i = 0; i < koeff.length; i++ ){ + if (koeff[i] != 0) { + result = i; + } + } + return result; + } + + +} diff --git a/src/uebung04/ByeGame.java b/src/uebung04/ByeGame.java new file mode 100644 index 0000000..8e3d079 --- /dev/null +++ b/src/uebung04/ByeGame.java @@ -0,0 +1,5 @@ +package uebung04; + +public class ByeGame extends Game{ + +} diff --git a/src/uebung04/Game.java b/src/uebung04/Game.java new file mode 100644 index 0000000..6754036 --- /dev/null +++ b/src/uebung04/Game.java @@ -0,0 +1,34 @@ +package uebung04; + +import java.util.List; + +public abstract class Game { + + protected static int counter = 0; + protected final int id; + protected String player1; + protected String player2; + protected String winner; + + public Game(){ + id = counter; + counter++; + } + + public int getId() {return id;} + + public String getWinner(){return winner;} + + public void setWinner(int player){ + if (player == 1) { + winner = player1; + } else if(player == 2){ + winner = player2; + } + } + + public abstract String getPlayer1(); + public abstract String getPlayer2(); + + public abstract List getAllPlayers(); +} diff --git a/src/uebung04/OrdinaryGame.java b/src/uebung04/OrdinaryGame.java new file mode 100644 index 0000000..f7d6b3e --- /dev/null +++ b/src/uebung04/OrdinaryGame.java @@ -0,0 +1,5 @@ +package uebung04; + +public class OrdinaryGame extends Game{ + +} diff --git a/src/uebung04/SeededGame.java b/src/uebung04/SeededGame.java new file mode 100644 index 0000000..89852d4 --- /dev/null +++ b/src/uebung04/SeededGame.java @@ -0,0 +1,5 @@ +package uebung04; + +public class SeededGame extends Game{ + +} diff --git a/test/oop/ch03/chess/ChessTest.java b/test/oop/ch03/chess/ChessTest.java new file mode 100644 index 0000000..4b46c23 --- /dev/null +++ b/test/oop/ch03/chess/ChessTest.java @@ -0,0 +1,168 @@ +package oop.ch03.chess; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; + +public class ChessTest { + // Das Zeilenende wird auf Windows-Systemen anders codiert als auf Nicht-Windows-Systemen. + // Die folgende Zeile ermittelt die aktuelle Codierung. + // EOL = "End of Line" + private static final String LS = System.lineSeparator(); + private static final String HE = " 1 2 3 4 5 6 7 8" + LS; + private static final String LI = " +---+---+---+---+---+---+---+---+" + LS; + private static final String EMP = " | | | | | | | | |" + LS; + private static final String WK1 = " | n | | | | | | | |" + LS; + private static final String WQ3 = " | | | q | | | | | |" + LS; + private static final String CO1 = " | | | n | | N | Q | | |" + LS; + private static final String INIT_BOARD = board(EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP); + private static final String WK61_BOARD = board(EMP, EMP, EMP, EMP, EMP, WK1, EMP, EMP); + private static final String APP1_BOARD = board(EMP, EMP, EMP, EMP, WQ3, CO1, EMP, EMP); + private static final String CHECK_TEXT1 = "white queen at (4, 2)" + LS + + " cannot capture black queen at (8, 6)" + LS + + " cannot capture white knight at (6, 4)" + LS + + "black queen at (8, 6)" + LS + + " cannot capture white queen at (4, 2)" + LS + + " can capture white knight at (6, 4)" + LS + + "white knight at (6, 4)" + LS + + " cannot capture white queen at (4, 2)" + LS + + " cannot capture black queen at (8, 6)" + LS; + private static final String CHECK_TEXT2 = "white knight at (6, 3)" + LS + + " cannot capture black knight at (6, 5)" + LS + + " cannot capture black queen at (6, 6)" + LS + + " cannot capture white queen at (5, 3)" + LS + + "black knight at (6, 5)" + LS + + " cannot capture white knight at (6, 3)" + LS + + " cannot capture black queen at (6, 6)" + LS + + " can capture white queen at (5, 3)" + LS + + "black queen at (6, 6)" + LS + + " cannot capture white knight at (6, 3)" + LS + + " cannot capture black knight at (6, 5)" + LS + + " cannot capture white queen at (5, 3)" + LS + + "white queen at (5, 3)" + LS + + " cannot capture white knight at (6, 3)" + LS + + " cannot capture black knight at (6, 5)" + LS + + " cannot capture black queen at (6, 6)" + LS; + + private static String board(String... s) { + if (s.length != 8) + throw new IllegalArgumentException("Expected 8, but got " + s.length + " arguments"); + StringBuilder sb = new StringBuilder(); + sb.append(HE).append(LI); + for (int i = 1; i <= 8; i++) + sb.append(i).append(s[i - 1]).append(LI); + return sb.toString(); + } + + private Board board; + + private final ByteArrayOutputStream printed = new ByteArrayOutputStream(); + private final PrintStream printStream = new PrintStream(printed, true); + + @Before + public void setup() { + board = new Board(); + printed.reset(); + System.setOut(printStream); + } + + @After + public void tearDown() { + printStream.close(); + } + + @Test + public void testEmpty() { + board.printBoard(); + assertEquals(INIT_BOARD, printed.toString()); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidPos1() { + new Knight(Color.white, board, 0, 4); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidPos2() { + new Queen(Color.white, board, 0, 4); + } + + @Test + public void testWhiteknight() { + final Piece knight = new Knight(Color.white, board, 6, 1); + checkPieces(1); + assertEquals(knight, board.pieceAt(6, 1)); + board.printBoard(); + assertEquals(WK61_BOARD, printed.toString()); + } + + @Test + public void test3Pieces() { + final Piece queen1 = new Queen(Color.white, board, 4, 2); + final Piece queen2 = new Queen(Color.black, board, 8, 6); + final Piece knight = new Knight(Color.white, board, 6, 4); + Assert.assertFalse(queen1.canCapture(queen2)); + Assert.assertFalse(queen1.canCapture(knight)); + Assert.assertFalse(queen2.canCapture(queen1)); + Assert.assertTrue(queen2.canCapture(knight)); + Assert.assertFalse(knight.canCapture(queen1)); + Assert.assertFalse(knight.canCapture(queen2)); + checkPieces(3); + assertEquals(queen1, board.pieceAt(4, 2)); + assertEquals(queen2, board.pieceAt(8, 6)); + assertEquals(knight, board.pieceAt(6, 4)); + board.check(); + assertEquals(CHECK_TEXT1, printed.toString()); + } + + @Test + public void test4Pieces() { + final Piece knight1 = new Knight(Color.white, board, 6, 3); + final Piece knight2 = new Knight(Color.black, board, 6, 5); + final Piece queen1 = new Queen(Color.black, board, 6, 6); + final Piece queen2 = new Queen(Color.white, board, 5, 3); + checkPieces(4); + assertEquals(knight1, board.pieceAt(6, 3)); + assertEquals(knight2, board.pieceAt(6, 5)); + assertEquals(queen1, board.pieceAt(6, 6)); + assertEquals(queen2, board.pieceAt(5, 3)); + board.printBoard(); + assertEquals(APP1_BOARD, printed.toString()); + Assert.assertFalse(knight1.canCapture(knight2)); + Assert.assertFalse(knight1.canCapture(queen1)); + Assert.assertFalse(knight1.canCapture(queen2)); + Assert.assertFalse(knight2.canCapture(knight1)); + Assert.assertFalse(knight2.canCapture(queen1)); + Assert.assertTrue(knight2.canCapture(queen2)); + Assert.assertFalse(queen1.canCapture(knight1)); + Assert.assertFalse(queen1.canCapture(knight2)); + Assert.assertFalse(queen1.canCapture(queen2)); + Assert.assertFalse(queen2.canCapture(knight1)); + Assert.assertFalse(queen2.canCapture(knight2)); + Assert.assertFalse(queen2.canCapture(queen1)); + printed.reset(); + board.check(); + assertEquals(CHECK_TEXT2, printed.toString()); + } + + private void checkPieces(int expectedNumPieces) { + int numPieces = 0; + for (int row = 1; row < 9; row++) + for (int col = 1; col < 9; col++) + if (board.pieceAt(row, col) != null) + numPieces++; + assertEquals(expectedNumPieces, numPieces); + } + + @Test + public void testRunWithErrors() { + final Piece rook = new Rook(Color.white, board, 1, 1); + assertEquals(rook, board.pieceAt(1, 1)); + } +} \ No newline at end of file diff --git a/test/oop/ch08/gui/mvc/TestModel.java b/test/oop/ch08/gui/mvc/TestModel.java new file mode 100644 index 0000000..2275ed6 --- /dev/null +++ b/test/oop/ch08/gui/mvc/TestModel.java @@ -0,0 +1,24 @@ +package oop.ch08.gui.mvc; + +import oop.ch08.gui.mvc.model.CounterModel; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestModel { + @Test + public void testInitWithZero() { + final CounterModel model = new CounterModel(); + assertEquals(0, model.getCounter()); + } + + @Test + public void testInc() { + final CounterModel model = new CounterModel(); + int counter = model.getCounter(); + model.increment(); + assertEquals(++counter, model.getCounter()); + model.increment(); + assertEquals(++counter, model.getCounter()); + } +} diff --git a/test/oop/ch09/calc/test/AdHocCalculatorEdgePairTest.java b/test/oop/ch09/calc/test/AdHocCalculatorEdgePairTest.java new file mode 100644 index 0000000..df6d4ff --- /dev/null +++ b/test/oop/ch09/calc/test/AdHocCalculatorEdgePairTest.java @@ -0,0 +1,2040 @@ +package oop.ch09.calc.test; + +import oop.ch06.calc.AdHocCalculator; +import oop.ch06.calc.Ops; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class AdHocCalculatorEdgePairTest { + private AdHocCalculator calc; + + @Before + public void setup() { + calc = new AdHocCalculator(); + calc.digit(6); + calc.binOp(Ops.MULT); + calc.digit(7); + calc.eval(); + } + + @Test + public void test0() { + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(45.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(45.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-45.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(-45.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-102.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(5.0345753587649823E-45, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-5.0345753587649823E-45, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(148.4131591025766, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.03368973499542734, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.03368973499542734, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.1835476368560144, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test1() { + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(0.6, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1096.6331584284585, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-1093.6331584284585, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1196033.485214206, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test2() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test3() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test4() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test5() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(51.0, calc.getValue(), 1e-10); + } + + @Test + public void test6() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + } + + @Test + public void test7() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test8() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test9() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test10() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(35.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test11() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test12() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(93.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.451245542920086E40, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test13() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test14() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test15() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test16() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + } + + @Test + public void test17() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1764.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(1764.0, calc.getValue(), 1e-10); + } + + @Test + public void test18() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test19() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test20() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test21() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(20.085536923187668, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test22() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test23() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-6.0, calc.getValue(), 1e-10); + } + + @Test + public void test24() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test25() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test26() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test27() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test28() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test29() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test30() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test31() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test32() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test33() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test34() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(69.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(69.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-69.0, calc.getValue(), 1e-10); + } + + @Test + public void test35() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test36() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test37() { + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test38() { + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(2.3, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.3, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(2.3, calc.getValue(), 1e-10); + } + + @Test + public void test39() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test40() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test41() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test42() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test43() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test44() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test45() { + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test46() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(12.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(12.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(3.4641016151377544, calc.getValue(), 1e-10); + } + + @Test + public void test47() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test48() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test49() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(8103.083927575384, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test50() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(78.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(8.831760866327848, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test51() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test52() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(72.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test53() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(64.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test54() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(421.0, calc.getValue(), 1e-10); + } + + @Test + public void test62() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test63() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test64() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test65() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test66() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test67() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test68() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test69() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test70() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test71() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.33, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test72() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test73() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test74() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test75() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(0.7, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.7, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test76() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test77() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test78() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(0.9, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.45960311115695, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test79() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test80() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test81() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test82() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test83() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test84() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test85() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(0.01831563888873418, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(4.7, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(109.94717245212352, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test86() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(652.4540390039687, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test87() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test88() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(12.752532700090239, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(29.24746729990976, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test89() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test90() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(35.51925930159214, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test91() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test92() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test93() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-1.73927494152050099E18, calc.getValue(), 1e-10); + } + + @Test + public void test94() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + calc.digit(6); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test95() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test96() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test97() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-1764.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test98() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + } + + @Test + public void test99() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.4147993510032952E-17, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(2.4147993510032952E-17, calc.getValue(), 1e-10); + } + + @Test + public void test100() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test101() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(48.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.1347969605334383E21, calc.getValue(), 1e-10); + } + + @Test + public void test102() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test103() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(515.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-473.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-473.0, calc.getValue(), 1e-10); + } + + @Test + public void test104() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(272.19110933313016, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test105() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test106() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test107() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(7.304954754386104E19, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test108() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test109() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test110() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(168.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test111() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test112() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(44.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test113() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test114() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test115() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(252.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.7682763318657856E109, calc.getValue(), 1e-10); + } + + @Test + public void test116() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test117() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test118() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test119() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(252.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test120() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test121() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(43.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(43.0, calc.getValue(), 1e-10); + } + + @Test + public void test122() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test123() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test124() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(36.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test125() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test126() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test127() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(41.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.4031242374328485, calc.getValue(), 1e-10); + } + + @Test + public void test128() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(54.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test129() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(126.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test130() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.8284271247461903, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(44.82842712474619, calc.getValue(), 1e-10); + } + + @Test + public void test131() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(33.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test132() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-6.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test133() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2980.9579870417283, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test134() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test135() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test136() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test137() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(27.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(27.0, calc.getValue(), 1e-10); + } + + @Test + public void test138() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(31.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(73.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test139() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(43.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-43.0, calc.getValue(), 1e-10); + } + + @Test + public void test140() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(20.085536923187668, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test141() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(75.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test142() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test143() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test144() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test145() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test146() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test147() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + calc.binOp(Ops.MINUS); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test148() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test149() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test150() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(0.6, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test151() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test152() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test153() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test154() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-1.0, calc.getValue(), 1e-10); + } + + @Test + public void test155() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(0.4, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(41.6, calc.getValue(), 1e-10); + } + + @Test + public void test156() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test157() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test158() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test159() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test160() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(0.9, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(0.97, calc.getValue(), 1e-10); + } +} diff --git a/test/oop/ch09/calc/test/AdHocCalculatorEdgeTest.java b/test/oop/ch09/calc/test/AdHocCalculatorEdgeTest.java new file mode 100644 index 0000000..3d7efc8 --- /dev/null +++ b/test/oop/ch09/calc/test/AdHocCalculatorEdgeTest.java @@ -0,0 +1,280 @@ +package oop.ch09.calc.test; + +import oop.ch06.calc.AdHocCalculator; +import oop.ch06.calc.Ops; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class AdHocCalculatorEdgeTest { + private AdHocCalculator calc; + + @Before + public void setup() { + calc = new AdHocCalculator(); + calc.digit(6); + calc.binOp(Ops.MULT); + calc.digit(7); + calc.eval(); + } + + @Test + public void test0() { + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(62.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(62.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test1() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(5.25, calc.getValue(), 1e-10); + } + + @Test + public void test2() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test3() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test4() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test5() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test6() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test7() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test8() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(9.0, calc.getValue(), 1e-10); + } + + @Test + public void test9() { + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-5.0, calc.getValue(), 1e-10); + } + + @Test + public void test11() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test12() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(0.2, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.2214027581601699, calc.getValue(), 1e-10); + } + + @Test + public void test13() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test14() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test15() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-1.73927494152050099E18, calc.getValue(), 1e-10); + } + + @Test + public void test16() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(-1.73927494152050099E18, calc.getValue(), 1e-10); + } + + @Test + public void test17() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test18() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test19() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(25.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-25.0, calc.getValue(), 1e-10); + } + + @Test + public void test20() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(51.0, calc.getValue(), 1e-10); + } + + @Test + public void test21() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.6457513110645907, calc.getValue(), 1e-10); + } + + @Test + public void test22() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test23() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test24() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } +} diff --git a/test/oop/ch09/calc/test/CalculatorEdgePairTest.java b/test/oop/ch09/calc/test/CalculatorEdgePairTest.java new file mode 100644 index 0000000..f3254d6 --- /dev/null +++ b/test/oop/ch09/calc/test/CalculatorEdgePairTest.java @@ -0,0 +1,2040 @@ +package oop.ch09.calc.test; + +import oop.ch06.calc.Ops; +import oop.ch07.calc.Calculator; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CalculatorEdgePairTest { + private Calculator calc; + + @Before + public void setup() { + calc = new Calculator(); + calc.digit(6); + calc.binOp(Ops.MULT); + calc.digit(7); + calc.eval(); + } + + @Test + public void test0() { + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(45.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(45.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-45.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(-45.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(-51.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-102.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(5.0345753587649823E-45, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-5.0345753587649823E-45, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(148.4131591025766, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.03368973499542734, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.03368973499542734, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.1835476368560144, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test1() { + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(0.6, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1096.6331584284585, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-1093.6331584284585, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1196033.485214206, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test2() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test3() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test4() { + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test5() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(51.0, calc.getValue(), 1e-10); + } + + @Test + public void test6() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + } + + @Test + public void test7() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test8() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test9() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test10() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(35.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test11() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test12() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(93.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.451245542920086E40, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test13() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test14() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test15() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test16() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + } + + @Test + public void test17() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1764.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(1764.0, calc.getValue(), 1e-10); + } + + @Test + public void test18() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test19() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test20() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test21() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(20.085536923187668, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test22() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test23() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-6.0, calc.getValue(), 1e-10); + } + + @Test + public void test24() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test25() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test26() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test27() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test28() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test29() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test30() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test31() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test32() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test33() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test34() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(69.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(69.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-69.0, calc.getValue(), 1e-10); + } + + @Test + public void test35() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test36() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test37() { + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test38() { + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(2.3, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.3, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(2.3, calc.getValue(), 1e-10); + } + + @Test + public void test39() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test40() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test41() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test42() { + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test43() { + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test44() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test45() { + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test46() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(12.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(12.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(3.4641016151377544, calc.getValue(), 1e-10); + } + + @Test + public void test47() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test48() { + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test49() { + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(8103.083927575384, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test50() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(78.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(8.831760866327848, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test51() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test52() { + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(72.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test53() { + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(64.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test54() { + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(421.0, calc.getValue(), 1e-10); + } + + @Test + public void test62() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test63() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test64() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test65() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test66() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test67() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test68() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test69() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test70() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test71() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.33, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test72() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test73() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test74() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test75() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(0.7, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.7, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test76() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test77() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test78() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(0.9, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.45960311115695, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test79() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test80() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test81() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test82() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test83() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test84() { + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test85() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-4.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(0.01831563888873418, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(4.7, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(109.94717245212352, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test86() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(652.4540390039687, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test87() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test88() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(12.752532700090239, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(29.24746729990976, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test89() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test90() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(35.51925930159214, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test91() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test92() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(0.3, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test93() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-1.73927494152050099E18, calc.getValue(), 1e-10); + } + + @Test + public void test94() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.5457298950218306, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + calc.digit(6); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test95() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test96() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test97() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(-1764.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test98() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + } + + @Test + public void test99() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(2.4147993510032952E-17, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(2.4147993510032952E-17, calc.getValue(), 1e-10); + } + + @Test + public void test100() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test101() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(48.48074069840786, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.1347969605334383E21, calc.getValue(), 1e-10); + } + + @Test + public void test102() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test103() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(51.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(515.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-473.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(-473.0, calc.getValue(), 1e-10); + } + + @Test + public void test104() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(272.19110933313016, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test105() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test106() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(84.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test107() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(7.304954754386104E19, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test108() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test109() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test110() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.48074069840786, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(168.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test111() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test112() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.73927494152050099E18, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(44.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test113() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test114() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MINUS); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test115() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(252.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2.7682763318657856E109, calc.getValue(), 1e-10); + } + + @Test + public void test116() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test117() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test118() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test119() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(252.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + } + + @Test + public void test120() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test121() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(43.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(43.0, calc.getValue(), 1e-10); + } + + @Test + public void test122() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test123() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test124() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(36.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test125() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test126() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test127() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(41.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(6.4031242374328485, calc.getValue(), 1e-10); + } + + @Test + public void test128() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(5.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(54.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test129() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(126.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test130() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(2.8284271247461903, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(44.82842712474619, calc.getValue(), 1e-10); + } + + @Test + public void test131() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(9.0, calc.getValue(), 1e-10); + calc.binOp(Ops.DIV); + assertEquals(33.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test132() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-6.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test133() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(8); + assertEquals(8.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(2980.9579870417283, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + } + + @Test + public void test134() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test135() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + } + + @Test + public void test136() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(4); + assertEquals(4.0, calc.getValue(), 1e-10); + } + + @Test + public void test137() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(2); + assertEquals(2.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(27.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(27.0, calc.getValue(), 1e-10); + } + + @Test + public void test138() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(31.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(73.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test139() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(1); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(43.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-43.0, calc.getValue(), 1e-10); + } + + @Test + public void test140() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(20.085536923187668, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test141() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + calc.digit(5); + assertEquals(75.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test142() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(3); + assertEquals(3.0, calc.getValue(), 1e-10); + } + + @Test + public void test143() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.PLUS); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test144() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test145() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test146() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test147() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertTrue(Double.isNaN(calc.getValue())); + calc.binOp(Ops.MINUS); + assertTrue(Double.isNaN(calc.getValue())); + } + + @Test + public void test148() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.binOp(Ops.MULT); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test149() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test150() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(6); + assertEquals(0.6, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test151() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + } + + @Test + public void test152() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test153() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.clear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test154() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.EXP); + assertEquals(1.0, calc.getValue(), 1e-10); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-1.0, calc.getValue(), 1e-10); + } + + @Test + public void test155() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(4); + assertEquals(0.4, calc.getValue(), 1e-10); + calc.binOp(Ops.MINUS); + assertEquals(41.6, calc.getValue(), 1e-10); + } + + @Test + public void test156() { + calc.binOp(Ops.PLUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.unOp(Ops.SQRT); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(7.0, calc.getValue(), 1e-10); + } + + @Test + public void test157() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.eval(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + } + + @Test + public void test158() { + calc.binOp(Ops.DIV); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.digit(6); + assertEquals(6.0, calc.getValue(), 1e-10); + } + + @Test + public void test159() { + calc.binOp(Ops.MINUS); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.allClear(); + assertEquals(0.0, calc.getValue(), 1e-20); + calc.unOp(Ops.CHANGE_SIGN); + assertEquals(-0.0, calc.getValue(), 1e-10); + } + + @Test + public void test160() { + calc.binOp(Ops.MULT); + assertEquals(42.0, calc.getValue(), 1e-10); + calc.dot(); + assertEquals(0.0, calc.getValue(), 1e-10); + calc.digit(9); + assertEquals(0.9, calc.getValue(), 1e-10); + calc.digit(7); + assertEquals(0.97, calc.getValue(), 1e-10); + } +} diff --git a/test/oop/ch09/testing/ExceptionTest.java b/test/oop/ch09/testing/ExceptionTest.java new file mode 100644 index 0000000..20533be --- /dev/null +++ b/test/oop/ch09/testing/ExceptionTest.java @@ -0,0 +1,34 @@ +package oop.ch09.testing; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class ExceptionTest { + @Test + public void testDivZero1() { + int zero = 0; + int result = 4 / zero; + assertTrue(true); + } + + @Test + public void testDivZero2() { + int zero = 0; + try { + int result = 4 / zero; + fail("ArithmeticException kam nicht"); + } + catch (ArithmeticException ex) { + assertEquals("/ by zero", ex.getMessage()); + } + } + + @Test(expected = ArithmeticException.class) + public void testDivZero2b() { + int zero = 0; + int result = 4 / zero; + } +} diff --git a/test/uebung/uebung04/tournament/TournamentTest.java b/test/uebung/uebung04/tournament/TournamentTest.java new file mode 100644 index 0000000..5dbff2d --- /dev/null +++ b/test/uebung/uebung04/tournament/TournamentTest.java @@ -0,0 +1,134 @@ +package test.uebung.uebung04.tournament; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class TournamentTest { + + private static final String BORGHOFF = "Prof. Borghoff"; + private static final String HOMMEL = "Prof. Hommel"; + private static final String KOCH = "Prof. Koch"; + private static final String MINAS = "Prof. Minas"; + private static final String BUCHENRIEDER = "Prof. Buchenrieder"; + private static final String DREO = "Prof. Dreo"; + private static final String ROSE = "Prof. Rose"; + private static final String SIEGLE = "Prof. Siegle"; + private static final String TEEGE = "Prof. Teege"; + private static final Set SET1 = Set.of(BORGHOFF, HOMMEL, KOCH, MINAS, BUCHENRIEDER, DREO, ROSE, SIEGLE, TEEGE); + private static final Set SET2 = Set.of(BORGHOFF, MINAS, SIEGLE, HOMMEL, DREO); + private static final Set SET3 = Set.of(BORGHOFF, MINAS, SIEGLE); + private static final Set SET4 = Set.of(MINAS, SIEGLE); + private static final Set SET5 = Set.of(MINAS); + + private Tournament tournament; + private Game r1s1; + private Game r1s2; + private Game r1s3; + private Game r1s4; + private Game r2s1; + private Game r2s2; + private Game r3s1; + private Game r4s1; + + @Before + public void setup() { + r1s1 = new SeededGame(HOMMEL, KOCH); + r1s2 = new SeededGame(MINAS, BUCHENRIEDER); + r1s3 = new SeededGame(DREO, ROSE); + r1s4 = new SeededGame(SIEGLE, TEEGE); + r2s1 = new ByeGame(BORGHOFF, r1s1); + r2s2 = new OrdinaryGame(r1s2, r1s3); + r3s1 = new OrdinaryGame(r2s1, r2s2); + r4s1 = new OrdinaryGame(r3s1, r1s4); + tournament = new Tournament("UniBw Sportschießen", r4s1); + } + + @Test + public void testBeforeFirstRound() { + assertEquals(SET1, asSet(tournament.getAllPlayers())); + assertEquals(SET1, asSet(tournament.getRemainingPlayers())); + } + + @Test + public void testFirstRound() { + r1s1.setWinner(HOMMEL); + r1s2.setWinner(MINAS); + r1s3.setWinner(DREO); + r1s4.setWinner(SIEGLE); + assertEquals(SET1, asSet(tournament.getAllPlayers())); + assertEquals(SET2, asSet(tournament.getRemainingPlayers())); + } + + @Test + public void testSecondRound() { + r1s1.setWinner(HOMMEL); + r1s2.setWinner(MINAS); + r1s3.setWinner(DREO); + r1s4.setWinner(SIEGLE); + r2s1.setWinner(BORGHOFF); + r2s2.setWinner(MINAS); + assertEquals(SET1, asSet(tournament.getAllPlayers())); + assertEquals(SET3, asSet(tournament.getRemainingPlayers())); + } + + @Test + public void testThirdRound() { + r1s1.setWinner(HOMMEL); + r1s2.setWinner(MINAS); + r1s3.setWinner(DREO); + r1s4.setWinner(SIEGLE); + r2s1.setWinner(BORGHOFF); + r2s2.setWinner(MINAS); + r3s1.setWinner(MINAS); + assertEquals(SET1, asSet(tournament.getAllPlayers())); + assertEquals(SET4, asSet(tournament.getRemainingPlayers())); + } + + @Test + public void testCompleteTournament() { + r1s1.setWinner(HOMMEL); + r1s2.setWinner(MINAS); + r1s3.setWinner(DREO); + r1s4.setWinner(SIEGLE); + r2s1.setWinner(BORGHOFF); + r2s2.setWinner(MINAS); + r3s1.setWinner(MINAS); + r4s1.setWinner(MINAS); + assertEquals(SET1, asSet(tournament.getAllPlayers())); + assertEquals(SET5, asSet(tournament.getRemainingPlayers())); + } + + @Test + public void testException() { + r1s1.setWinner(HOMMEL); + r1s2.setWinner(MINAS); + r1s3.setWinner(DREO); + r1s4.setWinner(SIEGLE); + r2s1.setWinner(BORGHOFF); + r2s2.setWinner(MINAS); + r3s1.setWinner(MINAS); + assertThrows(IllegalArgumentException.class, () -> r4s1.setWinner(BUCHENRIEDER)); + } + + @Test(expected = IllegalStateException.class) + public void testException2() { + r4s1.setWinner(MINAS); + } + + @Test + public void testException3() { + r1s1.setWinner(HOMMEL); + assertThrows(IllegalStateException.class, () -> r1s1.setWinner(HOMMEL)); + } + + private static Set asSet(Collection collection) { + return new HashSet<>(collection); + } +}