From 53fb6e1d611a490d4689ec57242f26d257292b4a Mon Sep 17 00:00:00 2001 From: Thomas Smits Date: Mon, 8 Jan 2024 15:32:04 +0100 Subject: [PATCH] Update of exercises --- Assignment_001/readme.md | 2 +- Assignment_001/solution/readme.md | 4 + Assignment_002/readme.md | 2 +- Assignment_002/solution/readme.md | 4 + Assignment_003/readme.md | 2 +- Assignment_003/solution/readme.md | 4 + Assignment_004/readme.md | 2 +- Assignment_004/solution/readme.md | 4 + Assignment_005/readme.md | 2 +- Assignment_005/solution/readme.md | 4 + Assignment_006/readme.md | 2 +- Assignment_006/solution/readme.md | 4 + Assignment_008/readme.md | 2 +- Assignment_008/solution/readme.md | 4 + Assignment_009/readme.md | 2 +- Assignment_009/solution/readme.md | 4 + Assignment_010/readme.md | 2 +- Assignment_010/solution/readme.md | 4 + Assignment_011/readme.md | 2 +- Assignment_011/solution/readme.md | 4 + Assignment_012/readme.md | 2 +- Assignment_013/readme.md | 2 +- Assignment_013/solution/readme.md | 5 + Assignment_014/readme.md | 2 +- Assignment_014/solution/readme.md | 5 + Assignment_015/readme.md | 2 +- Assignment_015/solution/readme.md | 5 + Assignment_016/readme.md | 2 +- Assignment_016/solution/readme.md | 5 + Assignment_017/readme.md | 2 +- Assignment_017/solution/readme.md | 5 + Assignment_018/readme.md | 2 +- Assignment_018/solution/readme.md | 5 + Assignment_019/readme.md | 2 +- Assignment_019/solution/readme.md | 5 + Assignment_020/readme.md | 2 +- Assignment_020/solution/readme.md | 5 + Assignment_021/readme.md | 2 +- Assignment_021/solution/readme.md | 5 + Assignment_022/readme.md | 2 +- Assignment_022/solution/readme.md | 5 + Assignment_023/readme.md | 2 +- Assignment_023/solution/readme.md | 5 + Assignment_024/readme.md | 2 +- Assignment_024/solution/readme.md | 5 + Assignment_025/readme.md | 2 +- Assignment_025/solution/readme.md | 5 + Assignment_026/readme.md | 2 +- Assignment_026/solution/readme.md | 5 + Assignment_027/readme.md | 2 +- Assignment_027/solution/readme.md | 5 + Assignment_028/readme.md | 2 +- Assignment_028/solution/readme.md | 5 + Assignment_029/readme.md | 2 +- Assignment_029/solution/readme.md | 5 + Assignment_030/readme.md | 2 +- Assignment_030/solution/readme.md | 5 + Assignment_031/readme.md | 2 +- Assignment_031/solution/readme.md | 5 + Assignment_032/readme.md | 2 +- Assignment_032/solution/readme.md | 5 + Assignment_033/readme.md | 2 +- Assignment_033/solution/readme.md | 5 + Assignment_034/readme.md | 2 +- Assignment_034/solution/readme.md | 5 + Assignment_035/readme.md | 2 +- Assignment_035/solution/readme.md | 5 + Assignment_036/readme.md | 2 +- Assignment_036/solution/readme.md | 5 + Assignment_037/readme.md | 2 +- Assignment_037/solution/readme.md | 5 + Assignment_038/readme.md | 2 +- Assignment_038/solution/readme.md | 5 + Assignment_039/readme.md | 2 +- Assignment_039/solution/readme.md | 5 + Assignment_040/readme.md | 2 +- Assignment_040/solution/readme.md | 5 + Assignment_041/readme.md | 2 +- Assignment_041/solution/readme.md | 5 + Assignment_042/readme.md | 2 +- Assignment_042/solution/readme.md | 5 + Assignment_043/readme.md | 2 +- Assignment_043/solution/readme.md | 5 + Assignment_044/readme.md | 2 +- Assignment_044/solution/readme.md | 5 + Assignment_045/readme.md | 2 +- Assignment_045/solution/readme.md | 5 + Assignment_046/readme.md | 2 +- Assignment_046/solution/readme.md | 5 + Assignment_047/readme.md | 2 +- Assignment_047/solution/readme.md | 5 + Assignment_048/readme.md | 2 +- Assignment_048/solution/readme.md | 5 + Assignment_049/readme.md | 2 +- Assignment_049/solution/readme.md | 5 + Assignment_050/readme.md | 2 +- Assignment_050/solution/readme.md | 5 + Assignment_051/readme.md | 2 +- Assignment_051/solution/readme.md | 5 + Assignment_052/readme.md | 2 +- Assignment_052/solution/readme.md | 5 + Assignment_053/readme.md | 2 +- Assignment_053/solution/readme.md | 5 + Assignment_054/readme.md | 2 +- Assignment_054/solution/readme.md | 5 + Assignment_055/readme.md | 2 +- Assignment_055/solution/readme.md | 5 + Assignment_056/readme.md | 2 +- Assignment_056/solution/readme.md | 5 + Assignment_057/readme.md | 2 +- Assignment_057/solution/readme.md | 5 + Assignment_058/readme.md | 2 +- Assignment_058/solution/readme.md | 5 + Assignment_059/readme.md | 2 +- Assignment_059/solution/readme.md | 5 + Assignment_060/readme.md | 2 +- Assignment_060/solution/readme.md | 5 + Assignment_061/readme.md | 2 +- Assignment_061/solution/readme.md | 5 + Assignment_062/readme.md | 2 +- Assignment_062/solution/readme.md | 5 + Assignment_063/readme.md | 2 +- Assignment_063/solution/readme.md | 5 + Assignment_064/readme.md | 2 +- Assignment_064/solution/readme.md | 5 + Assignment_065/readme.md | 2 +- Assignment_065/solution/readme.md | 5 + Assignment_066/readme.md | 2 +- Assignment_066/solution/readme.md | 5 + Assignment_067/readme.md | 2 +- Assignment_067/solution/readme.md | 5 + Assignment_068/readme.md | 2 +- Assignment_068/solution/readme.md | 5 + Assignment_069/readme.md | 2 +- Assignment_069/solution/readme.md | 5 + Assignment_070/readme.md | 2 +- Assignment_070/solution/readme.md | 5 + Assignment_071/readme.md | 2 +- Assignment_071/solution/readme.md | 5 + Assignment_072/readme.md | 2 +- Assignment_072/solution/readme.md | 5 + Assignment_073/readme.md | 2 +- Assignment_073/solution/readme.md | 5 + Assignment_074/readme.md | 2 +- Assignment_074/solution/readme.md | 6 + Assignment_075/readme.md | 2 +- Assignment_075/solution/readme.md | 5 + Assignment_076/readme.md | 2 +- Assignment_076/solution/readme.md | 5 + Assignment_077/readme.md | 2 +- Assignment_077/solution/readme.md | 5 + Assignment_078/readme.md | 2 +- Assignment_078/solution/readme.md | 5 + Assignment_079/readme.md | 2 +- Assignment_079/solution/readme.md | 5 + Assignment_080/readme.md | 2 +- Assignment_080/solution/readme.md | 5 + Assignment_081/readme.md | 2 +- Assignment_081/solution/readme.md | 5 + Assignment_082/readme.md | 2 +- Assignment_082/solution/readme.md | 5 + Assignment_083/readme.md | 2 +- Assignment_083/solution/readme.md | 5 + Assignment_084/readme.md | 2 +- Assignment_084/solution/readme.md | 5 + Assignment_085/readme.md | 2 +- Assignment_085/solution/readme.md | 5 + Assignment_086/readme.md | 2 +- Assignment_086/solution/readme.md | 5 + Assignment_087/readme.md | 2 +- Assignment_087/solution/readme.md | 5 + Assignment_088/readme.md | 2 +- Assignment_088/solution/readme.md | 5 + Assignment_089/readme.md | 2 +- Assignment_089/solution/readme.md | 5 + Assignment_090/readme.md | 2 +- Assignment_090/solution/readme.md | 5 + Assignment_091/readme.md | 2 +- Assignment_091/solution/readme.md | 5 + Assignment_092/readme.md | 2 +- Assignment_092/solution/readme.md | 5 + Assignment_093/readme.md | 2 +- Assignment_093/solution/readme.md | 5 + Assignment_094/readme.md | 2 +- Assignment_094/solution/readme.md | 5 + Assignment_095/readme.md | 2 +- Assignment_095/solution/readme.md | 5 + Assignment_096/readme.md | 2 +- Assignment_096/solution/readme.md | 5 + Assignment_097/readme.md | 2 +- Assignment_097/solution/readme.md | 5 + readme.md | 192 ++++++------- .../auffrischung/grossmacher/Grossmacher.java | 16 ++ .../labeled_break/ArraySucher.java | 27 ++ .../password/PasswortChecker.java | 53 ++++ .../auffrischung/printf/DoubleFormatter.java | 18 ++ .../auffrischung/suchemax/GroessteZahl.java | 21 ++ .../taschenrechner/Taschenrechner.java | 25 ++ .../pr2/collections/iterator/SimpleStack.java | 80 ++++++ .../iterator/test/SimpleStackTest.java | 86 ++++++ .../collections/iterieren/CoinGenerator.java | 115 ++++++++ .../pr2/collections/iterieren/GameBoard.java | 66 +++++ .../pr2/collections/iterieren/GameMain.java | 28 ++ .../pr2/collections/list/WordCollector.java | 267 ++++++++++++++++++ .../pr2/collections/list_iterator/Liste.java | 57 ++++ .../java/pr2/collections/map/WordCount.java | 83 ++++++ .../pr2/collections/map/WordFrequency.java | 56 ++++ .../pr2/collections/reverser/Reverser.java | 18 ++ .../reverser/test/ReverserTest.java | 23 ++ .../reverser_generic/Reverser.java | 19 ++ .../reverser_generic/test/ReverserTest.java | 23 ++ .../pr2/collections/set/WordCollector.java | 80 ++++++ .../sorter_1/CommandLineSorter.java | 17 ++ .../sorter_2/CommandLineSorter.java | 16 ++ .../pr2/collections/sortieren/Sorter.java | 83 ++++++ .../sortieren/test/SorterTest.java | 75 +++++ .../collections/woerterbuch/Woerterbuch.java | 37 +++ .../main/java/pr2/enums/eigene/Wuerfel.java | 9 + .../java/pr2/enums/eigene/WuerfelTyp.java | 71 +++++ .../pr2/enums/eigene/test/WuerfelTest.java | 24 ++ .../java/pr2/enums/filme/LieblingsFilme.java | 50 ++++ .../src/main/java/pr2/enums/filme/Main.java | 9 + .../src/main/java/pr2/enums/schnick/Move.java | 46 +++ .../enums/schnick/SchnickSchnackSchnuck.java | 89 ++++++ .../main/java/pr2/enums/singleton/MyEnum.java | 7 + .../pr2/enums/singleton/SingletonBeweis.java | 14 + .../java/pr2/enums/verwenden/GameBoard.java | 47 +++ .../java/pr2/enums/verwenden/GameMain.java | 28 ++ .../java/pr2/enums/verwenden/Professor.java | 32 +++ .../ausnahmen_testen/CrypterCaesar.java | 164 +++++++++++ .../ausnahmen_testen/IllegalKeyException.java | 31 ++ .../IllegalMessageException.java | 31 ++ .../ausnahmen_testen/test/CaesarTest.java | 149 ++++++++++ .../pr2/exceptions/eigene_ausnahme/Fuse.java | 58 ++++ .../eigene_ausnahme/FuseTrippedException.java | 37 +++ .../IllegalCurrentException.java | 30 ++ .../eigene_ausnahme/test/FuseTest.java | 158 +++++++++++ .../pr2/exceptions/fakultaet/Fakultaet.java | 18 ++ .../fakultaet/FakultaetException.java | 15 + .../fakultaet/test/FakultaetTest.java | 38 +++ .../handle_or_declare/Buchstabenzaehler.java | 124 ++++++++ .../handle_or_declare/StatistikException.java | 31 ++ .../pr2/exceptions/try_catch/Zahlenraten.java | 67 +++++ .../main/java/pr2/generics/einfach/Liste.java | 90 ++++++ .../java/pr2/generics/einfach/ListeNode.java | 30 ++ .../pr2/generics/einfach/test/ListeTest.java | 61 ++++ .../java/pr2/generics/number_pair/Main.java | 15 + .../pr2/generics/number_pair/NumberPair.java | 8 + .../java/pr2/generics/number_pair/Pair.java | 20 ++ .../src/main/java/pr2/generics/pair/Main.java | 17 ++ .../src/main/java/pr2/generics/pair/Pair.java | 20 ++ .../main/java/pr2/generics/pairlist/Main.java | 19 ++ .../main/java/pr2/generics/pairlist/Pair.java | 20 ++ .../java/pr2/generics/pairlist/PairList.java | 20 ++ .../generics/printer/CollectionPrinter.java | 11 + .../main/java/pr2/generics/printer/Main.java | 23 ++ .../main/java/pr2/generics/queue/Queue.java | 39 +++ .../java/pr2/generics/same_pair/Main.java | 15 + .../java/pr2/generics/same_pair/Pair.java | 20 ++ .../java/pr2/generics/same_pair/SamePair.java | 8 + .../pr2/generics/super_extends/Liste.java | 108 +++++++ .../pr2/generics/super_extends/ListeNode.java | 28 ++ .../super_extends/test/ListeTest.java | 42 +++ .../pr2/generics/verwenden/GameBoard.java | 169 +++++++++++ .../java/pr2/generics/verwenden/GameMain.java | 28 ++ .../pr2/generics/wildcards/ListHelper.java | 35 +++ .../java/pr2/generics/wildcards/Main.java | 39 +++ .../pr2/generics/wildcards/SimpleList.java | 12 + .../pr2/interfaces/abstrakt_form/Form.java | 16 ++ .../abstrakte_klassen_s/Dreieck.java | 39 +++ .../interfaces/abstrakte_klassen_s/Figur.java | 14 + .../abstrakte_klassen_s/Geometer.java | 43 +++ .../interfaces/abstrakte_klassen_s/Kreis.java | 32 +++ .../comparable_student/Student.java | 42 +++ .../interfaces_s/api/AnalogController.java | 44 +++ .../interfaces_s/api/DigitalController.java | 36 +++ .../impl/AnalogControllerImpl.java | 61 ++++ .../impl/DigitalControllerImpl.java | 55 ++++ .../interfaces_s/impl/DualShock4.java | 95 +++++++ .../test/AnalogControllerTest.java | 52 ++++ .../test/DigitalControllerTest.java | 62 ++++ .../interfaces_s/test/DualShock4Test.java | 45 +++ .../main/java/pr2/interfaces/stack/Stack.java | 12 + .../java/pr2/interfaces/stack/StackImpl.java | 43 +++ .../pr2/interfaces/stack/test/StackTest.java | 35 +++ .../interfaces/uebersetzer/Translator.java | 38 +++ .../interfaces/uebersetzer/Uebersetzer.java | 13 + .../UebersetzerDeutschEnglisch.java | 17 ++ .../UebersetzerDeutschSpanisch.java | 17 ++ .../intro/coding_standard/CodingStandard.java | 54 ++++ .../main/java/pr2/intro/javadoc/Waehrung.java | 142 ++++++++++ .../pr2/intro/junit_faculty/Fakultaet.java | 14 + .../junit_faculty/test/FakultaetTest.java | 20 ++ .../io/buffered_reader/LineNumberPrinter.java | 36 +++ .../main/java/pr2/io/data_output/Board.java | 172 +++++++++++ .../main/java/pr2/io/data_output/Color.java | 30 ++ .../pr2/io/data_output/test/BoardTest.java | 63 +++++ .../pr2/io/datainputoutput_1/DateReader.java | 43 +++ .../pr2/io/datainputoutput_1/DateWriter.java | 63 +++++ .../pr2/io/datainputoutput_2/DateReader.java | 45 +++ .../pr2/io/datainputoutput_2/DateWriter.java | 63 +++++ .../java/pr2/io/datei_lesen/ReadData.java | 33 +++ .../pr2/io/datei_schreiben/WriteData.java | 33 +++ .../pr2/io/datei_schreiben/test/DataTest.java | 39 +++ .../java/pr2/io/file/FilesystemWalker.java | 94 ++++++ .../main/java/pr2/io/filetest/FileInfo.java | 40 +++ .../main/java/pr2/io/filter/UCaseReader.java | 36 +++ .../pr2/io/filter/test/UCaseReaderTest.java | 34 +++ .../java/pr2/io/konsole/ConsoleToFile.java | 46 +++ .../java/pr2/io/linecounter/LineCounter.java | 29 ++ .../java/pr2/io/random_access/FileSort.java | 58 ++++ .../io/random_access/test/FileSortTest.java | 63 +++++ .../io/reader_writer/ReadAndPrintFile.java | 34 +++ .../src/main/java/pr2/io/rot13/Rot13.java | 25 ++ .../main/java/pr2/io/rot13/Rot13Reader.java | 24 ++ .../main/java/pr2/io/scrambler/Scrambler.java | 38 +++ .../java/pr2/io/serialisierung/Board.java | 139 +++++++++ .../java/pr2/io/serialisierung/Color.java | 30 ++ .../pr2/io/serialisierung/test/BoardTest.java | 46 +++ .../main/java/pr2/lambda/anonymous/Alien.java | 46 +++ .../java/pr2/lambda/anonymous/GameBoard.java | 73 +++++ .../java/pr2/lambda/anonymous/GameMain.java | 28 ++ .../pr2/lambda/array_sorter/ReverseSort.java | 28 ++ .../main/java/pr2/lambda/callback/Main.java | 46 +++ .../pr2/lambda/callback/NumberSelector.java | 30 ++ .../java/pr2/lambda/callback/Predicate.java | 20 ++ .../pr2/lambda/comparator/SortLambda.java | 12 + .../main/java/pr2/lambda/lambdas/Main.java | 31 ++ .../pr2/lambda/lambdas/NumberSelector.java | 30 ++ .../java/pr2/lambda/lambdas/Predicate.java | 20 ++ .../src/main/java/pr2/lambda/local/Alien.java | 46 +++ .../main/java/pr2/lambda/local/GameBoard.java | 72 +++++ .../main/java/pr2/lambda/local/GameMain.java | 28 ++ .../pr2/lambda/matrixsuche/MatrixSuche.java | 39 +++ .../matrixsuche/test/MatrixSucheTest.java | 32 +++ .../lambda/mogrifier_1/StringFunction.java | 6 + .../mogrifier_1/StringTransmogrifier.java | 14 + .../test/StringTransmogrifierTest.java | 33 +++ .../lambda/mogrifier_2/StringFunction.java | 18 ++ .../mogrifier_2/StringTransmogrifier.java | 14 + .../test/StringTransmogrifierTest.java | 43 +++ .../main/java/pr2/lambda/nonstatic/Alien.java | 54 ++++ .../java/pr2/lambda/nonstatic/GameBoard.java | 108 +++++++ .../java/pr2/lambda/nonstatic/GameMain.java | 27 ++ .../java/pr2/lambda/static_member/Alien.java | 54 ++++ .../pr2/lambda/static_member/GameBoard.java | 111 ++++++++ .../pr2/lambda/static_member/GameMain.java | 27 ++ .../java/pr2/object/clone_alien/Alien.java | 100 +++++++ .../pr2/object/clone_alien/Raumanzug.java | 75 +++++ .../clone_alien/test/AlienCloneTest.java | 37 +++ .../java/pr2/object/clone_person/Person.java | 34 +++ .../java/pr2/object/equals_hashcode/Ork.java | 57 ++++ .../pr2/object/equals_hashcode/Wesen.java | 66 +++++ .../equals_hashcode/test/WesenTest.java | 87 ++++++ .../java/pr2/object/equals_person/Person.java | 54 ++++ .../main/java/pr2/object/tostring/Alien.java | 56 ++++ .../java/pr2/object/tostring/Ausgabe.java | 24 ++ .../java/pr2/object/tostring/Raumanzug.java | 52 ++++ .../object/wrapper_boxsearch/BoxSearcher.java | 18 ++ .../object/wrapper_umwandler/Umwandler.java | 47 +++ .../information_hiding/Rechner.java | 6 + .../information_hiding/test/RechnerTest.java | 50 ++++ .../src/main/java/pr2/vererbung/finals/A.java | 32 +++ .../src/main/java/pr2/vererbung/finals/B.java | 22 ++ .../java/pr2/vererbung/geometrie1/Figur.java | 14 + .../java/pr2/vererbung/geometrie1/Main.java | 12 + .../pr2/vererbung/geometrie1/Rechteck.java | 8 + .../java/pr2/vererbung/geometrie2/Figur.java | 18 ++ .../java/pr2/vererbung/geometrie2/Main.java | 12 + .../pr2/vererbung/geometrie2/Rechteck.java | 13 + .../pr2/vererbung/geometrie3/Dreieck.java | 8 + .../java/pr2/vererbung/geometrie3/Figur.java | 19 ++ .../java/pr2/vererbung/geometrie3/Gerade.java | 17 ++ .../java/pr2/vererbung/geometrie3/Main.java | 12 + .../pr2/vererbung/geometrie3/Quadrat.java | 8 + .../pr2/vererbung/geometrie3/Rechteck.java | 8 + .../pr2/vererbung/konstruktoren/Nachtelf.java | 47 +++ .../pr2/vererbung/konstruktoren/Untoter.java | 47 +++ .../pr2/vererbung/konstruktoren/Wesen.java | 30 ++ .../konstruktoren/test/WesenTest.java | 47 +++ .../java/pr2/vererbung/polymorphie/Affe.java | 16 ++ .../vererbung/polymorphie/Futterstelle.java | 16 ++ .../pr2/vererbung/polymorphie/Giraffe.java | 16 ++ .../pr2/vererbung/polymorphie/Gorilla.java | 16 ++ .../vererbung/polymorphie/ZooSimulation.java | 39 +++ .../pr2/vererbung/polymorphie/ZooTier.java | 41 +++ .../singleton_einfach/Singleton.java | 12 + .../java/pr2/vererbung/statics/Spiel.java | 33 +++ .../java/pr2/vererbung/statics/Wuerfel.java | 52 ++++ .../vererbung/ueberladen_summe/Summator.java | 20 ++ .../ueberladen_summe/test/SummatorTest.java | 22 ++ .../ueberschreiben_lebewesen/Amoebe.java | 9 + .../ueberschreiben_lebewesen/Dackel.java | 13 + .../ueberschreiben_lebewesen/Lebewesen.java | 8 + .../ueberschreiben_lebewesen/Main.java | 16 ++ .../pr2/vererbung/vararg_summe/Summator.java | 22 ++ .../vararg_summe/test/SummatorTest.java | 23 ++ .../vererbung_geometrie/Dreieck.java | 17 ++ .../vererbung/vererbung_geometrie/Figur.java | 37 +++ .../vererbung/vererbung_geometrie/Gerade.java | 31 ++ .../vererbung_geometrie/Quadrat.java | 16 ++ .../vererbung_geometrie/Rechteck.java | 17 ++ .../vererbung_geometrie/test/FigurenTest.java | 67 +++++ 403 files changed, 9504 insertions(+), 192 deletions(-) create mode 100644 Assignment_001/solution/readme.md create mode 100644 Assignment_002/solution/readme.md create mode 100644 Assignment_003/solution/readme.md create mode 100644 Assignment_004/solution/readme.md create mode 100644 Assignment_005/solution/readme.md create mode 100644 Assignment_006/solution/readme.md create mode 100644 Assignment_008/solution/readme.md create mode 100644 Assignment_009/solution/readme.md create mode 100644 Assignment_010/solution/readme.md create mode 100644 Assignment_011/solution/readme.md create mode 100644 Assignment_013/solution/readme.md create mode 100644 Assignment_014/solution/readme.md create mode 100644 Assignment_015/solution/readme.md create mode 100644 Assignment_016/solution/readme.md create mode 100644 Assignment_017/solution/readme.md create mode 100644 Assignment_018/solution/readme.md create mode 100644 Assignment_019/solution/readme.md create mode 100644 Assignment_020/solution/readme.md create mode 100644 Assignment_021/solution/readme.md create mode 100644 Assignment_022/solution/readme.md create mode 100644 Assignment_023/solution/readme.md create mode 100644 Assignment_024/solution/readme.md create mode 100644 Assignment_025/solution/readme.md create mode 100644 Assignment_026/solution/readme.md create mode 100644 Assignment_027/solution/readme.md create mode 100644 Assignment_028/solution/readme.md create mode 100644 Assignment_029/solution/readme.md create mode 100644 Assignment_030/solution/readme.md create mode 100644 Assignment_031/solution/readme.md create mode 100644 Assignment_032/solution/readme.md create mode 100644 Assignment_033/solution/readme.md create mode 100644 Assignment_034/solution/readme.md create mode 100644 Assignment_035/solution/readme.md create mode 100644 Assignment_036/solution/readme.md create mode 100644 Assignment_037/solution/readme.md create mode 100644 Assignment_038/solution/readme.md create mode 100644 Assignment_039/solution/readme.md create mode 100644 Assignment_040/solution/readme.md create mode 100644 Assignment_041/solution/readme.md create mode 100644 Assignment_042/solution/readme.md create mode 100644 Assignment_043/solution/readme.md create mode 100644 Assignment_044/solution/readme.md create mode 100644 Assignment_045/solution/readme.md create mode 100644 Assignment_046/solution/readme.md create mode 100644 Assignment_047/solution/readme.md create mode 100644 Assignment_048/solution/readme.md create mode 100644 Assignment_049/solution/readme.md create mode 100644 Assignment_050/solution/readme.md create mode 100644 Assignment_051/solution/readme.md create mode 100644 Assignment_052/solution/readme.md create mode 100644 Assignment_053/solution/readme.md create mode 100644 Assignment_054/solution/readme.md create mode 100644 Assignment_055/solution/readme.md create mode 100644 Assignment_056/solution/readme.md create mode 100644 Assignment_057/solution/readme.md create mode 100644 Assignment_058/solution/readme.md create mode 100644 Assignment_059/solution/readme.md create mode 100644 Assignment_060/solution/readme.md create mode 100644 Assignment_061/solution/readme.md create mode 100644 Assignment_062/solution/readme.md create mode 100644 Assignment_063/solution/readme.md create mode 100644 Assignment_064/solution/readme.md create mode 100644 Assignment_065/solution/readme.md create mode 100644 Assignment_066/solution/readme.md create mode 100644 Assignment_067/solution/readme.md create mode 100644 Assignment_068/solution/readme.md create mode 100644 Assignment_069/solution/readme.md create mode 100644 Assignment_070/solution/readme.md create mode 100644 Assignment_071/solution/readme.md create mode 100644 Assignment_072/solution/readme.md create mode 100644 Assignment_073/solution/readme.md create mode 100644 Assignment_074/solution/readme.md create mode 100644 Assignment_075/solution/readme.md create mode 100644 Assignment_076/solution/readme.md create mode 100644 Assignment_077/solution/readme.md create mode 100644 Assignment_078/solution/readme.md create mode 100644 Assignment_079/solution/readme.md create mode 100644 Assignment_080/solution/readme.md create mode 100644 Assignment_081/solution/readme.md create mode 100644 Assignment_082/solution/readme.md create mode 100644 Assignment_083/solution/readme.md create mode 100644 Assignment_084/solution/readme.md create mode 100644 Assignment_085/solution/readme.md create mode 100644 Assignment_086/solution/readme.md create mode 100644 Assignment_087/solution/readme.md create mode 100644 Assignment_088/solution/readme.md create mode 100644 Assignment_089/solution/readme.md create mode 100644 Assignment_090/solution/readme.md create mode 100644 Assignment_091/solution/readme.md create mode 100644 Assignment_092/solution/readme.md create mode 100644 Assignment_093/solution/readme.md create mode 100644 Assignment_094/solution/readme.md create mode 100644 Assignment_095/solution/readme.md create mode 100644 Assignment_096/solution/readme.md create mode 100644 Assignment_097/solution/readme.md create mode 100644 solutions/src/main/java/pr2/auffrischung/grossmacher/Grossmacher.java create mode 100644 solutions/src/main/java/pr2/auffrischung/labeled_break/ArraySucher.java create mode 100644 solutions/src/main/java/pr2/auffrischung/password/PasswortChecker.java create mode 100644 solutions/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java create mode 100644 solutions/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java create mode 100644 solutions/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java create mode 100644 solutions/src/main/java/pr2/collections/iterator/SimpleStack.java create mode 100644 solutions/src/main/java/pr2/collections/iterator/test/SimpleStackTest.java create mode 100644 solutions/src/main/java/pr2/collections/iterieren/CoinGenerator.java create mode 100644 solutions/src/main/java/pr2/collections/iterieren/GameBoard.java create mode 100644 solutions/src/main/java/pr2/collections/iterieren/GameMain.java create mode 100644 solutions/src/main/java/pr2/collections/list/WordCollector.java create mode 100644 solutions/src/main/java/pr2/collections/list_iterator/Liste.java create mode 100644 solutions/src/main/java/pr2/collections/map/WordCount.java create mode 100644 solutions/src/main/java/pr2/collections/map/WordFrequency.java create mode 100644 solutions/src/main/java/pr2/collections/reverser/Reverser.java create mode 100644 solutions/src/main/java/pr2/collections/reverser/test/ReverserTest.java create mode 100644 solutions/src/main/java/pr2/collections/reverser_generic/Reverser.java create mode 100644 solutions/src/main/java/pr2/collections/reverser_generic/test/ReverserTest.java create mode 100644 solutions/src/main/java/pr2/collections/set/WordCollector.java create mode 100644 solutions/src/main/java/pr2/collections/sorter_1/CommandLineSorter.java create mode 100644 solutions/src/main/java/pr2/collections/sorter_2/CommandLineSorter.java create mode 100644 solutions/src/main/java/pr2/collections/sortieren/Sorter.java create mode 100644 solutions/src/main/java/pr2/collections/sortieren/test/SorterTest.java create mode 100644 solutions/src/main/java/pr2/collections/woerterbuch/Woerterbuch.java create mode 100644 solutions/src/main/java/pr2/enums/eigene/Wuerfel.java create mode 100644 solutions/src/main/java/pr2/enums/eigene/WuerfelTyp.java create mode 100644 solutions/src/main/java/pr2/enums/eigene/test/WuerfelTest.java create mode 100644 solutions/src/main/java/pr2/enums/filme/LieblingsFilme.java create mode 100644 solutions/src/main/java/pr2/enums/filme/Main.java create mode 100644 solutions/src/main/java/pr2/enums/schnick/Move.java create mode 100644 solutions/src/main/java/pr2/enums/schnick/SchnickSchnackSchnuck.java create mode 100644 solutions/src/main/java/pr2/enums/singleton/MyEnum.java create mode 100644 solutions/src/main/java/pr2/enums/singleton/SingletonBeweis.java create mode 100644 solutions/src/main/java/pr2/enums/verwenden/GameBoard.java create mode 100644 solutions/src/main/java/pr2/enums/verwenden/GameMain.java create mode 100644 solutions/src/main/java/pr2/enums/verwenden/Professor.java create mode 100644 solutions/src/main/java/pr2/exceptions/ausnahmen_testen/CrypterCaesar.java create mode 100644 solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalKeyException.java create mode 100644 solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalMessageException.java create mode 100644 solutions/src/main/java/pr2/exceptions/ausnahmen_testen/test/CaesarTest.java create mode 100644 solutions/src/main/java/pr2/exceptions/eigene_ausnahme/Fuse.java create mode 100644 solutions/src/main/java/pr2/exceptions/eigene_ausnahme/FuseTrippedException.java create mode 100644 solutions/src/main/java/pr2/exceptions/eigene_ausnahme/IllegalCurrentException.java create mode 100644 solutions/src/main/java/pr2/exceptions/eigene_ausnahme/test/FuseTest.java create mode 100644 solutions/src/main/java/pr2/exceptions/fakultaet/Fakultaet.java create mode 100644 solutions/src/main/java/pr2/exceptions/fakultaet/FakultaetException.java create mode 100644 solutions/src/main/java/pr2/exceptions/fakultaet/test/FakultaetTest.java create mode 100644 solutions/src/main/java/pr2/exceptions/handle_or_declare/Buchstabenzaehler.java create mode 100644 solutions/src/main/java/pr2/exceptions/handle_or_declare/StatistikException.java create mode 100644 solutions/src/main/java/pr2/exceptions/try_catch/Zahlenraten.java create mode 100644 solutions/src/main/java/pr2/generics/einfach/Liste.java create mode 100644 solutions/src/main/java/pr2/generics/einfach/ListeNode.java create mode 100644 solutions/src/main/java/pr2/generics/einfach/test/ListeTest.java create mode 100644 solutions/src/main/java/pr2/generics/number_pair/Main.java create mode 100644 solutions/src/main/java/pr2/generics/number_pair/NumberPair.java create mode 100644 solutions/src/main/java/pr2/generics/number_pair/Pair.java create mode 100644 solutions/src/main/java/pr2/generics/pair/Main.java create mode 100644 solutions/src/main/java/pr2/generics/pair/Pair.java create mode 100644 solutions/src/main/java/pr2/generics/pairlist/Main.java create mode 100644 solutions/src/main/java/pr2/generics/pairlist/Pair.java create mode 100644 solutions/src/main/java/pr2/generics/pairlist/PairList.java create mode 100644 solutions/src/main/java/pr2/generics/printer/CollectionPrinter.java create mode 100644 solutions/src/main/java/pr2/generics/printer/Main.java create mode 100644 solutions/src/main/java/pr2/generics/queue/Queue.java create mode 100644 solutions/src/main/java/pr2/generics/same_pair/Main.java create mode 100644 solutions/src/main/java/pr2/generics/same_pair/Pair.java create mode 100644 solutions/src/main/java/pr2/generics/same_pair/SamePair.java create mode 100644 solutions/src/main/java/pr2/generics/super_extends/Liste.java create mode 100644 solutions/src/main/java/pr2/generics/super_extends/ListeNode.java create mode 100644 solutions/src/main/java/pr2/generics/super_extends/test/ListeTest.java create mode 100644 solutions/src/main/java/pr2/generics/verwenden/GameBoard.java create mode 100644 solutions/src/main/java/pr2/generics/verwenden/GameMain.java create mode 100644 solutions/src/main/java/pr2/generics/wildcards/ListHelper.java create mode 100644 solutions/src/main/java/pr2/generics/wildcards/Main.java create mode 100644 solutions/src/main/java/pr2/generics/wildcards/SimpleList.java create mode 100644 solutions/src/main/java/pr2/interfaces/abstrakt_form/Form.java create mode 100644 solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Dreieck.java create mode 100644 solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Figur.java create mode 100644 solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Geometer.java create mode 100644 solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Kreis.java create mode 100644 solutions/src/main/java/pr2/interfaces/comparable_student/Student.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/api/AnalogController.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/api/DigitalController.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/impl/AnalogControllerImpl.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DigitalControllerImpl.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DualShock4.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/test/AnalogControllerTest.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/test/DigitalControllerTest.java create mode 100644 solutions/src/main/java/pr2/interfaces/interfaces_s/test/DualShock4Test.java create mode 100644 solutions/src/main/java/pr2/interfaces/stack/Stack.java create mode 100644 solutions/src/main/java/pr2/interfaces/stack/StackImpl.java create mode 100644 solutions/src/main/java/pr2/interfaces/stack/test/StackTest.java create mode 100644 solutions/src/main/java/pr2/interfaces/uebersetzer/Translator.java create mode 100644 solutions/src/main/java/pr2/interfaces/uebersetzer/Uebersetzer.java create mode 100644 solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschEnglisch.java create mode 100644 solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschSpanisch.java create mode 100644 solutions/src/main/java/pr2/intro/coding_standard/CodingStandard.java create mode 100644 solutions/src/main/java/pr2/intro/javadoc/Waehrung.java create mode 100644 solutions/src/main/java/pr2/intro/junit_faculty/Fakultaet.java create mode 100644 solutions/src/main/java/pr2/intro/junit_faculty/test/FakultaetTest.java create mode 100644 solutions/src/main/java/pr2/io/buffered_reader/LineNumberPrinter.java create mode 100644 solutions/src/main/java/pr2/io/data_output/Board.java create mode 100644 solutions/src/main/java/pr2/io/data_output/Color.java create mode 100644 solutions/src/main/java/pr2/io/data_output/test/BoardTest.java create mode 100644 solutions/src/main/java/pr2/io/datainputoutput_1/DateReader.java create mode 100644 solutions/src/main/java/pr2/io/datainputoutput_1/DateWriter.java create mode 100644 solutions/src/main/java/pr2/io/datainputoutput_2/DateReader.java create mode 100644 solutions/src/main/java/pr2/io/datainputoutput_2/DateWriter.java create mode 100644 solutions/src/main/java/pr2/io/datei_lesen/ReadData.java create mode 100644 solutions/src/main/java/pr2/io/datei_schreiben/WriteData.java create mode 100644 solutions/src/main/java/pr2/io/datei_schreiben/test/DataTest.java create mode 100644 solutions/src/main/java/pr2/io/file/FilesystemWalker.java create mode 100644 solutions/src/main/java/pr2/io/filetest/FileInfo.java create mode 100644 solutions/src/main/java/pr2/io/filter/UCaseReader.java create mode 100644 solutions/src/main/java/pr2/io/filter/test/UCaseReaderTest.java create mode 100644 solutions/src/main/java/pr2/io/konsole/ConsoleToFile.java create mode 100644 solutions/src/main/java/pr2/io/linecounter/LineCounter.java create mode 100644 solutions/src/main/java/pr2/io/random_access/FileSort.java create mode 100644 solutions/src/main/java/pr2/io/random_access/test/FileSortTest.java create mode 100644 solutions/src/main/java/pr2/io/reader_writer/ReadAndPrintFile.java create mode 100644 solutions/src/main/java/pr2/io/rot13/Rot13.java create mode 100644 solutions/src/main/java/pr2/io/rot13/Rot13Reader.java create mode 100644 solutions/src/main/java/pr2/io/scrambler/Scrambler.java create mode 100644 solutions/src/main/java/pr2/io/serialisierung/Board.java create mode 100644 solutions/src/main/java/pr2/io/serialisierung/Color.java create mode 100644 solutions/src/main/java/pr2/io/serialisierung/test/BoardTest.java create mode 100644 solutions/src/main/java/pr2/lambda/anonymous/Alien.java create mode 100644 solutions/src/main/java/pr2/lambda/anonymous/GameBoard.java create mode 100644 solutions/src/main/java/pr2/lambda/anonymous/GameMain.java create mode 100644 solutions/src/main/java/pr2/lambda/array_sorter/ReverseSort.java create mode 100644 solutions/src/main/java/pr2/lambda/callback/Main.java create mode 100644 solutions/src/main/java/pr2/lambda/callback/NumberSelector.java create mode 100644 solutions/src/main/java/pr2/lambda/callback/Predicate.java create mode 100644 solutions/src/main/java/pr2/lambda/comparator/SortLambda.java create mode 100644 solutions/src/main/java/pr2/lambda/lambdas/Main.java create mode 100644 solutions/src/main/java/pr2/lambda/lambdas/NumberSelector.java create mode 100644 solutions/src/main/java/pr2/lambda/lambdas/Predicate.java create mode 100644 solutions/src/main/java/pr2/lambda/local/Alien.java create mode 100644 solutions/src/main/java/pr2/lambda/local/GameBoard.java create mode 100644 solutions/src/main/java/pr2/lambda/local/GameMain.java create mode 100644 solutions/src/main/java/pr2/lambda/matrixsuche/MatrixSuche.java create mode 100644 solutions/src/main/java/pr2/lambda/matrixsuche/test/MatrixSucheTest.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_1/StringFunction.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_1/StringTransmogrifier.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_1/test/StringTransmogrifierTest.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_2/StringFunction.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_2/StringTransmogrifier.java create mode 100644 solutions/src/main/java/pr2/lambda/mogrifier_2/test/StringTransmogrifierTest.java create mode 100644 solutions/src/main/java/pr2/lambda/nonstatic/Alien.java create mode 100644 solutions/src/main/java/pr2/lambda/nonstatic/GameBoard.java create mode 100644 solutions/src/main/java/pr2/lambda/nonstatic/GameMain.java create mode 100644 solutions/src/main/java/pr2/lambda/static_member/Alien.java create mode 100644 solutions/src/main/java/pr2/lambda/static_member/GameBoard.java create mode 100644 solutions/src/main/java/pr2/lambda/static_member/GameMain.java create mode 100644 solutions/src/main/java/pr2/object/clone_alien/Alien.java create mode 100644 solutions/src/main/java/pr2/object/clone_alien/Raumanzug.java create mode 100644 solutions/src/main/java/pr2/object/clone_alien/test/AlienCloneTest.java create mode 100644 solutions/src/main/java/pr2/object/clone_person/Person.java create mode 100644 solutions/src/main/java/pr2/object/equals_hashcode/Ork.java create mode 100644 solutions/src/main/java/pr2/object/equals_hashcode/Wesen.java create mode 100644 solutions/src/main/java/pr2/object/equals_hashcode/test/WesenTest.java create mode 100644 solutions/src/main/java/pr2/object/equals_person/Person.java create mode 100644 solutions/src/main/java/pr2/object/tostring/Alien.java create mode 100644 solutions/src/main/java/pr2/object/tostring/Ausgabe.java create mode 100644 solutions/src/main/java/pr2/object/tostring/Raumanzug.java create mode 100644 solutions/src/main/java/pr2/object/wrapper_boxsearch/BoxSearcher.java create mode 100644 solutions/src/main/java/pr2/object/wrapper_umwandler/Umwandler.java create mode 100644 solutions/src/main/java/pr2/strukturierung/information_hiding/Rechner.java create mode 100644 solutions/src/main/java/pr2/strukturierung/information_hiding/test/RechnerTest.java create mode 100644 solutions/src/main/java/pr2/vererbung/finals/A.java create mode 100644 solutions/src/main/java/pr2/vererbung/finals/B.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie1/Figur.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie1/Main.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie1/Rechteck.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie2/Figur.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie2/Main.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie2/Rechteck.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Dreieck.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Figur.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Gerade.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Main.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Quadrat.java create mode 100644 solutions/src/main/java/pr2/vererbung/geometrie3/Rechteck.java create mode 100644 solutions/src/main/java/pr2/vererbung/konstruktoren/Nachtelf.java create mode 100644 solutions/src/main/java/pr2/vererbung/konstruktoren/Untoter.java create mode 100644 solutions/src/main/java/pr2/vererbung/konstruktoren/Wesen.java create mode 100644 solutions/src/main/java/pr2/vererbung/konstruktoren/test/WesenTest.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/Affe.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/Futterstelle.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/Giraffe.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/Gorilla.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/ZooSimulation.java create mode 100644 solutions/src/main/java/pr2/vererbung/polymorphie/ZooTier.java create mode 100644 solutions/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java create mode 100644 solutions/src/main/java/pr2/vererbung/statics/Spiel.java create mode 100644 solutions/src/main/java/pr2/vererbung/statics/Wuerfel.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberladen_summe/Summator.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberladen_summe/test/SummatorTest.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java create mode 100644 solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java create mode 100644 solutions/src/main/java/pr2/vererbung/vararg_summe/Summator.java create mode 100644 solutions/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Dreieck.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Figur.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Gerade.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Quadrat.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Rechteck.java create mode 100644 solutions/src/main/java/pr2/vererbung/vererbung_geometrie/test/FigurenTest.java diff --git a/Assignment_001/readme.md b/Assignment_001/readme.md index 839ab73..c0e8f35 100644 --- a/Assignment_001/readme.md +++ b/Assignment_001/readme.md @@ -1,6 +1,6 @@ # String in Großbuchstaben umwandeln - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_001/solution/readme.md b/Assignment_001/solution/readme.md new file mode 100644 index 0000000..1a6e1d0 --- /dev/null +++ b/Assignment_001/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: String in Großbuchstaben umwandeln + + +[pr2.auffrischung.grossmacher](../../solutions/src/main/java/pr2/auffrischung/grossmacher) diff --git a/Assignment_002/readme.md b/Assignment_002/readme.md index a22f5cc..7420abe 100644 --- a/Assignment_002/readme.md +++ b/Assignment_002/readme.md @@ -1,6 +1,6 @@ # Labeled Break - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_002/solution/readme.md b/Assignment_002/solution/readme.md new file mode 100644 index 0000000..6102863 --- /dev/null +++ b/Assignment_002/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Labeled Break + + +[pr2.auffrischung.labeled_break](../../solutions/src/main/java/pr2/auffrischung/labeled_break) diff --git a/Assignment_003/readme.md b/Assignment_003/readme.md index 4c9dc8f..111aa6f 100644 --- a/Assignment_003/readme.md +++ b/Assignment_003/readme.md @@ -1,6 +1,6 @@ # Passwortbewertung - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_003/solution/readme.md b/Assignment_003/solution/readme.md new file mode 100644 index 0000000..6b1732b --- /dev/null +++ b/Assignment_003/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Passwortbewertung + + +[pr2.auffrischung.password](../../solutions/src/main/java/pr2/auffrischung/password) diff --git a/Assignment_004/readme.md b/Assignment_004/readme.md index 4420c01..017d58e 100644 --- a/Assignment_004/readme.md +++ b/Assignment_004/readme.md @@ -1,6 +1,6 @@ # printf mit Formatstring - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_004/solution/readme.md b/Assignment_004/solution/readme.md new file mode 100644 index 0000000..bcda0d2 --- /dev/null +++ b/Assignment_004/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: printf mit Formatstring + + +[pr2.auffrischung.printf](../../solutions/src/main/java/pr2/auffrischung/printf) diff --git a/Assignment_005/readme.md b/Assignment_005/readme.md index 08f0abc..f2f3aff 100644 --- a/Assignment_005/readme.md +++ b/Assignment_005/readme.md @@ -1,6 +1,6 @@ # Maximum in einem Array suchen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_005/solution/readme.md b/Assignment_005/solution/readme.md new file mode 100644 index 0000000..9cd6fa7 --- /dev/null +++ b/Assignment_005/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Maximum in einem Array suchen + + +[pr2.auffrischung.suchemax](../../solutions/src/main/java/pr2/auffrischung/suchemax) diff --git a/Assignment_006/readme.md b/Assignment_006/readme.md index 6af5467..c0d17f4 100644 --- a/Assignment_006/readme.md +++ b/Assignment_006/readme.md @@ -1,6 +1,6 @@ # Taschenrechner - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_006/solution/readme.md b/Assignment_006/solution/readme.md new file mode 100644 index 0000000..7e37500 --- /dev/null +++ b/Assignment_006/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Taschenrechner + + +[pr2.auffrischung.taschenrechner](../../solutions/src/main/java/pr2/auffrischung/taschenrechner) diff --git a/Assignment_008/readme.md b/Assignment_008/readme.md index 372621e..6ebf666 100644 --- a/Assignment_008/readme.md +++ b/Assignment_008/readme.md @@ -1,6 +1,6 @@ # Java-Coding-Standard anwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_008/solution/readme.md b/Assignment_008/solution/readme.md new file mode 100644 index 0000000..8a75113 --- /dev/null +++ b/Assignment_008/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Java-Coding-Standard anwenden + + +[pr2.intro.coding_standard](../../solutions/src/main/java/pr2/intro/coding_standard/) diff --git a/Assignment_009/readme.md b/Assignment_009/readme.md index dbecb82..76708d6 100644 --- a/Assignment_009/readme.md +++ b/Assignment_009/readme.md @@ -1,6 +1,6 @@ # JavaDoc schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_009/solution/readme.md b/Assignment_009/solution/readme.md new file mode 100644 index 0000000..e10eb89 --- /dev/null +++ b/Assignment_009/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: JavaDoc schreiben + + +[pr2.intro.javadoc](../../solutions/src/main/java/pr2/intro/javadoc/) diff --git a/Assignment_010/readme.md b/Assignment_010/readme.md index fea3bdf..95ad1e7 100644 --- a/Assignment_010/readme.md +++ b/Assignment_010/readme.md @@ -1,6 +1,6 @@ # Klasse mit JUnit testen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_010/solution/readme.md b/Assignment_010/solution/readme.md new file mode 100644 index 0000000..ee12743 --- /dev/null +++ b/Assignment_010/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Klasse mit JUnit testen + + +Gehen Sie in das Paket [pr2.intro.junit_faculty](../../solutions/src/main/java/pr2/intro/junit_faculty/). diff --git a/Assignment_011/readme.md b/Assignment_011/readme.md index b7f8a78..d844161 100644 --- a/Assignment_011/readme.md +++ b/Assignment_011/readme.md @@ -1,6 +1,6 @@ # Information-Hiding einer Klasse verbessern - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_011/solution/readme.md b/Assignment_011/solution/readme.md new file mode 100644 index 0000000..7584997 --- /dev/null +++ b/Assignment_011/solution/readme.md @@ -0,0 +1,4 @@ +# Lösung: Information-Hiding einer Klasse verbessern + + +[pr2.strukturierung.information_hiding](../../solutions/src/main/java/pr2/strukturierung/information_hiding/) diff --git a/Assignment_012/readme.md b/Assignment_012/readme.md index 6e5a2a5..0227074 100644 --- a/Assignment_012/readme.md +++ b/Assignment_012/readme.md @@ -1,6 +1,6 @@ # Vorhandene Bibliotheken als JAR einbinden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_013/readme.md b/Assignment_013/readme.md index 79fe752..f21903c 100644 --- a/Assignment_013/readme.md +++ b/Assignment_013/readme.md @@ -1,6 +1,6 @@ # Final anwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_013/solution/readme.md b/Assignment_013/solution/readme.md new file mode 100644 index 0000000..9e13845 --- /dev/null +++ b/Assignment_013/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Final anwenden + +Musterlösung: + +[pr2.vererbung.finals](../../solutions/src/main/java/pr2/vererbung/finals/) diff --git a/Assignment_014/readme.md b/Assignment_014/readme.md index a01a3a3..c84606e 100644 --- a/Assignment_014/readme.md +++ b/Assignment_014/readme.md @@ -1,6 +1,6 @@ # Figur und Rechteck - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_014/solution/readme.md b/Assignment_014/solution/readme.md new file mode 100644 index 0000000..f130d3d --- /dev/null +++ b/Assignment_014/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Figur und Rechteck + +Musterlösung: + +[pr2.vererbung.geometrie1](../../solutions/src/main/java/pr2/vererbung/geometrie1/) diff --git a/Assignment_015/readme.md b/Assignment_015/readme.md index e773d39..0b03d67 100644 --- a/Assignment_015/readme.md +++ b/Assignment_015/readme.md @@ -1,6 +1,6 @@ # Figur erweitern - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_015/solution/readme.md b/Assignment_015/solution/readme.md new file mode 100644 index 0000000..e69cd4f --- /dev/null +++ b/Assignment_015/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Figur erweitern + +Musterlösung: + +[pr2.vererbung.geometrie2](../../solutions/src/main/java/pr2/vererbung/geometrie2/) diff --git a/Assignment_016/readme.md b/Assignment_016/readme.md index c0f799c..3fada28 100644 --- a/Assignment_016/readme.md +++ b/Assignment_016/readme.md @@ -1,6 +1,6 @@ # Figur noch einmal erweitern - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_016/solution/readme.md b/Assignment_016/solution/readme.md new file mode 100644 index 0000000..d5886dd --- /dev/null +++ b/Assignment_016/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Figur noch einmal erweitern + +Musterlösung: + +[pr2.vererbung.geometrie3](../../solutions/src/main/java/pr2/vererbung/geometrie3/) diff --git a/Assignment_017/readme.md b/Assignment_017/readme.md index 1024e29..6687177 100644 --- a/Assignment_017/readme.md +++ b/Assignment_017/readme.md @@ -1,6 +1,6 @@ # Konstruktoren schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_017/solution/readme.md b/Assignment_017/solution/readme.md new file mode 100644 index 0000000..38818c2 --- /dev/null +++ b/Assignment_017/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Konstruktoren schreiben + +Musterlösung: + +[pr2.vererbung.konstruktoren](../../solutions/src/main/java/pr2/vererbung/konstruktoren/) diff --git a/Assignment_018/readme.md b/Assignment_018/readme.md index 08afb8e..bf2bbfc 100644 --- a/Assignment_018/readme.md +++ b/Assignment_018/readme.md @@ -1,6 +1,6 @@ # Polymorphie einsetzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_018/solution/readme.md b/Assignment_018/solution/readme.md new file mode 100644 index 0000000..3dcb152 --- /dev/null +++ b/Assignment_018/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Polymorphie einsetzen + +Musterlösung: + +[pr2.vererbung.polymorphie](../../solutions/src/main/java/pr2/vererbung/polymorphie/) diff --git a/Assignment_019/readme.md b/Assignment_019/readme.md index 2cf5db3..daca5a6 100644 --- a/Assignment_019/readme.md +++ b/Assignment_019/readme.md @@ -1,6 +1,6 @@ # Singleton - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_019/solution/readme.md b/Assignment_019/solution/readme.md new file mode 100644 index 0000000..080acca --- /dev/null +++ b/Assignment_019/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Singleton + +Musterlösung: + +[pr2.vererbung.singleton_einfach](../../solutions/src/main/java/pr2/vererbung/singleton_einfach/) diff --git a/Assignment_020/readme.md b/Assignment_020/readme.md index 7654cf1..04dbfbb 100644 --- a/Assignment_020/readme.md +++ b/Assignment_020/readme.md @@ -1,6 +1,6 @@ # Statische Methoden und Attribute - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_020/solution/readme.md b/Assignment_020/solution/readme.md new file mode 100644 index 0000000..8638c64 --- /dev/null +++ b/Assignment_020/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Statische Methoden und Attribute + +Musterlösung: + +[pr2.vererbung.statics](../../solutions/src/main/java/pr2/vererbung/statics/) diff --git a/Assignment_021/readme.md b/Assignment_021/readme.md index 1353992..e40d58a 100644 --- a/Assignment_021/readme.md +++ b/Assignment_021/readme.md @@ -1,6 +1,6 @@ # Methode überladen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_021/solution/readme.md b/Assignment_021/solution/readme.md new file mode 100644 index 0000000..5106e98 --- /dev/null +++ b/Assignment_021/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Methode überladen + +Musterlösung: + +[pr2.vererbung.ueberladen_summe](../../solutions/src/main/java/pr2/vererbung/ueberladen_summe) diff --git a/Assignment_022/readme.md b/Assignment_022/readme.md index 9ab12f4..3a2b327 100644 --- a/Assignment_022/readme.md +++ b/Assignment_022/readme.md @@ -1,6 +1,6 @@ # Methoden überschreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_022/solution/readme.md b/Assignment_022/solution/readme.md new file mode 100644 index 0000000..02ce09a --- /dev/null +++ b/Assignment_022/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Methoden überschreiben + +Musterlösung: + +[pr2.vererbung.ueberschreiben_lebewesen](../../solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/) diff --git a/Assignment_023/readme.md b/Assignment_023/readme.md index 3d2af88..65eeccd 100644 --- a/Assignment_023/readme.md +++ b/Assignment_023/readme.md @@ -1,6 +1,6 @@ # Varag-Methode schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_023/solution/readme.md b/Assignment_023/solution/readme.md new file mode 100644 index 0000000..6eb5caf --- /dev/null +++ b/Assignment_023/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Varag-Methode schreiben + +Musterlösung: + +[pr2.vererbung.vararg_summe](../../solutions/src/main/java/pr2/vererbung/vararg_summe/) diff --git a/Assignment_024/readme.md b/Assignment_024/readme.md index 745ac9a..77bc4c2 100644 --- a/Assignment_024/readme.md +++ b/Assignment_024/readme.md @@ -1,6 +1,6 @@ # Vererbung von Figuren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_024/solution/readme.md b/Assignment_024/solution/readme.md new file mode 100644 index 0000000..aa8e532 --- /dev/null +++ b/Assignment_024/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Vererbung von Figuren + +Musterlösung: + +[pr2.vererbung.vererbung_geometrie](../../solutions/src/main/java/pr2/vererbung/vererbung_geometrie/) diff --git a/Assignment_025/readme.md b/Assignment_025/readme.md index 1690a2b..26438f1 100644 --- a/Assignment_025/readme.md +++ b/Assignment_025/readme.md @@ -1,6 +1,6 @@ # Abstrakte Klassen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_025/solution/readme.md b/Assignment_025/solution/readme.md new file mode 100644 index 0000000..9132f53 --- /dev/null +++ b/Assignment_025/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Abstrakte Klassen + +Musterlösung: + +[pr2.interfaces.abstrakte_klassen_s](../../solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/) diff --git a/Assignment_026/readme.md b/Assignment_026/readme.md index 2accb71..884e97b 100644 --- a/Assignment_026/readme.md +++ b/Assignment_026/readme.md @@ -1,6 +1,6 @@ # Abstrakte Klasse - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_026/solution/readme.md b/Assignment_026/solution/readme.md new file mode 100644 index 0000000..9d09e79 --- /dev/null +++ b/Assignment_026/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Abstrakte Klasse + +Musterlösung: + +[pr2.interfaces.abstrakt_form](../../solutions/src/main/java/pr2/interfaces/abstrakt_form/) diff --git a/Assignment_027/readme.md b/Assignment_027/readme.md index 0b8c9d2..2ba9fad 100644 --- a/Assignment_027/readme.md +++ b/Assignment_027/readme.md @@ -1,6 +1,6 @@ # Comparable implementieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_027/solution/readme.md b/Assignment_027/solution/readme.md new file mode 100644 index 0000000..10a088c --- /dev/null +++ b/Assignment_027/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Comparable implementieren + +Musterlösung: + +[pr2.interfaces.comparable_student](../../solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/) diff --git a/Assignment_028/readme.md b/Assignment_028/readme.md index d696e9d..31ce6ae 100644 --- a/Assignment_028/readme.md +++ b/Assignment_028/readme.md @@ -1,6 +1,6 @@ # Interface Stack entwerfen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_028/solution/readme.md b/Assignment_028/solution/readme.md new file mode 100644 index 0000000..18e1cff --- /dev/null +++ b/Assignment_028/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Interface Stack entwerfen + +Musterlösung: + +[pr2.interfaces.stack](../../solutions/src/main/java/pr2/interfaces/stack/) diff --git a/Assignment_029/readme.md b/Assignment_029/readme.md index 99b9a04..2502b85 100644 --- a/Assignment_029/readme.md +++ b/Assignment_029/readme.md @@ -1,6 +1,6 @@ # Interface: Uebersetzer - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_029/solution/readme.md b/Assignment_029/solution/readme.md new file mode 100644 index 0000000..bc67dee --- /dev/null +++ b/Assignment_029/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Interface: Uebersetzer + +Musterlösung: + +[pr2.interfaces.uebersetzer](../../solutions/src/main/java/pr2/interfaces/uebersetzer/) diff --git a/Assignment_030/readme.md b/Assignment_030/readme.md index f09f6ff..e100004 100644 --- a/Assignment_030/readme.md +++ b/Assignment_030/readme.md @@ -1,6 +1,6 @@ # Interfaces anwenden und entwerfen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_030/solution/readme.md b/Assignment_030/solution/readme.md new file mode 100644 index 0000000..d93d652 --- /dev/null +++ b/Assignment_030/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Interfaces anwenden und entwerfen + +Musterlösung: + +[pr2.interfaces.interfaces_s](../../solutions/src/main/java/pr2/interfaces/interfaces_s/) diff --git a/Assignment_031/readme.md b/Assignment_031/readme.md index 9ff8980..ef2d45a 100644 --- a/Assignment_031/readme.md +++ b/Assignment_031/readme.md @@ -1,6 +1,6 @@ # Deep-Copy mit `clone()` - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_031/solution/readme.md b/Assignment_031/solution/readme.md new file mode 100644 index 0000000..e4f05e2 --- /dev/null +++ b/Assignment_031/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Deep-Copy mit `clone()` + +Musterlösung: + +[pr2.object.clone_alien](../../solutions/src/main/java/pr2/object/clone_alien/) diff --git a/Assignment_032/readme.md b/Assignment_032/readme.md index 09f72f2..2e17591 100644 --- a/Assignment_032/readme.md +++ b/Assignment_032/readme.md @@ -1,6 +1,6 @@ # Clone - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_032/solution/readme.md b/Assignment_032/solution/readme.md new file mode 100644 index 0000000..9dc85a1 --- /dev/null +++ b/Assignment_032/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Clone + +Musterlösung: + +[pr2.object.clone_person](../../solutions/src/main/java/pr2/object/clone_person) diff --git a/Assignment_033/readme.md b/Assignment_033/readme.md index 36462b4..f625e78 100644 --- a/Assignment_033/readme.md +++ b/Assignment_033/readme.md @@ -1,6 +1,6 @@ # `equals()` und `hashCode()` implementieren und nutzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_033/solution/readme.md b/Assignment_033/solution/readme.md new file mode 100644 index 0000000..ce0c7ab --- /dev/null +++ b/Assignment_033/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `equals()` und `hashCode()` implementieren und nutzen + +Musterlösung: + +[pr2.object.equals_hashcode](../../solutions/src/main/java/pr2/object/equals_hashcode/) diff --git a/Assignment_034/readme.md b/Assignment_034/readme.md index 401e331..43f9c30 100644 --- a/Assignment_034/readme.md +++ b/Assignment_034/readme.md @@ -1,6 +1,6 @@ # equals und hashCode - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_034/solution/readme.md b/Assignment_034/solution/readme.md new file mode 100644 index 0000000..7d94bc2 --- /dev/null +++ b/Assignment_034/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: equals und hashCode + +Musterlösung: + +[pr2.object.equals_person](../../solutions/src/main/java/pr2/object/equals_person) diff --git a/Assignment_035/readme.md b/Assignment_035/readme.md index 75b878f..0111d61 100644 --- a/Assignment_035/readme.md +++ b/Assignment_035/readme.md @@ -1,6 +1,6 @@ # `toString()`-Methode implementieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_035/solution/readme.md b/Assignment_035/solution/readme.md new file mode 100644 index 0000000..2073e97 --- /dev/null +++ b/Assignment_035/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `toString()`-Methode implementieren + +Musterlösung: + +[pr2.object.tostring](../../solutions/src/main/java/pr2/object/tostring/). diff --git a/Assignment_036/readme.md b/Assignment_036/readme.md index d532b34..cb5b83e 100644 --- a/Assignment_036/readme.md +++ b/Assignment_036/readme.md @@ -1,6 +1,6 @@ # Optimierung bei Integer - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_036/solution/readme.md b/Assignment_036/solution/readme.md new file mode 100644 index 0000000..82555df --- /dev/null +++ b/Assignment_036/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Optimierung bei Integer + +Musterlösung: + +[pr2.object.wrapper_boxsearch](../../solutions/src/main/java/pr2/object/wrapper_boxsearch/). diff --git a/Assignment_037/readme.md b/Assignment_037/readme.md index e194821..d19bc5e 100644 --- a/Assignment_037/readme.md +++ b/Assignment_037/readme.md @@ -1,6 +1,6 @@ # Methoden der Wrapper-Klassen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_037/solution/readme.md b/Assignment_037/solution/readme.md new file mode 100644 index 0000000..13bf079 --- /dev/null +++ b/Assignment_037/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Methoden der Wrapper-Klassen + +Musterlösung: + +[pr2.object.wrapper_umwandler](../../solutions/src/main/java/pr2/object/wrapper_umwandler/). diff --git a/Assignment_038/readme.md b/Assignment_038/readme.md index dc5f618..2814b59 100644 --- a/Assignment_038/readme.md +++ b/Assignment_038/readme.md @@ -1,6 +1,6 @@ # Eigene Enumeration schreiben und verwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_038/solution/readme.md b/Assignment_038/solution/readme.md new file mode 100644 index 0000000..b1d9f1c --- /dev/null +++ b/Assignment_038/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Eigene Enumeration schreiben und verwenden + +Musterlösung: + +[pr2.enums.eigene](../../solutions/src/main/java/pr2/enums/eigene_enumeration/) diff --git a/Assignment_039/readme.md b/Assignment_039/readme.md index d4eea00..ae60187 100644 --- a/Assignment_039/readme.md +++ b/Assignment_039/readme.md @@ -1,6 +1,6 @@ # Enumeration schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_039/solution/readme.md b/Assignment_039/solution/readme.md new file mode 100644 index 0000000..9255964 --- /dev/null +++ b/Assignment_039/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Enumeration schreiben + +Musterlösung: + +[pr2.enums.filme](../../solutions/src/main/java/pr2/enums/filme/) diff --git a/Assignment_040/readme.md b/Assignment_040/readme.md index 31aaeb8..4dd2117 100644 --- a/Assignment_040/readme.md +++ b/Assignment_040/readme.md @@ -1,6 +1,6 @@ # Singleton-Eigenschaft von Enumerationen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_040/solution/readme.md b/Assignment_040/solution/readme.md new file mode 100644 index 0000000..e4f7fe2 --- /dev/null +++ b/Assignment_040/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Singleton-Eigenschaft von Enumerationen + +Musterlösung: + +[pr2.enums.singleton](../../solutions/src/main/java/pr2/enums/singleton/) diff --git a/Assignment_041/readme.md b/Assignment_041/readme.md index ab5618e..1cf70b7 100644 --- a/Assignment_041/readme.md +++ b/Assignment_041/readme.md @@ -1,6 +1,6 @@ # Eine vorhandene Enumeration nutzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_041/solution/readme.md b/Assignment_041/solution/readme.md new file mode 100644 index 0000000..ceb9257 --- /dev/null +++ b/Assignment_041/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Eine vorhandene Enumeration nutzen + +Musterlösung: + +[pr2.enums.verwenden](../../solutions/src/main/java/pr2/enums/verwenden/) diff --git a/Assignment_042/readme.md b/Assignment_042/readme.md index 564b2e9..6d0fa55 100644 --- a/Assignment_042/readme.md +++ b/Assignment_042/readme.md @@ -1,6 +1,6 @@ # Assignment: Stein, Papier, Schere, Echse, Spock - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_042/solution/readme.md b/Assignment_042/solution/readme.md new file mode 100644 index 0000000..9c45825 --- /dev/null +++ b/Assignment_042/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Stein, Papier, Schere, Echse, Spock + +Musterlösung: + +[pr2.enums.schnick](../../solutions/src/main/java/pr2/enums/schnick). diff --git a/Assignment_043/readme.md b/Assignment_043/readme.md index 899741d..0fd2f31 100644 --- a/Assignment_043/readme.md +++ b/Assignment_043/readme.md @@ -1,6 +1,6 @@ # Ausnahmen testen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_043/solution/readme.md b/Assignment_043/solution/readme.md new file mode 100644 index 0000000..a03a8f2 --- /dev/null +++ b/Assignment_043/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Ausnahmen testen + +Musterlösung: + +[pr2.exceptions.ausnahmen_testen](../../solutions/src/main/java/pr2/exceptions/ausnahmen_testen/) diff --git a/Assignment_044/readme.md b/Assignment_044/readme.md index 049b139..1b4045f 100644 --- a/Assignment_044/readme.md +++ b/Assignment_044/readme.md @@ -1,6 +1,6 @@ # Eigene Ausnahmen schreiben und an entsprechender Stelle werfen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_044/solution/readme.md b/Assignment_044/solution/readme.md new file mode 100644 index 0000000..7cc2d13 --- /dev/null +++ b/Assignment_044/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Eigene Ausnahmen schreiben und an entsprechender Stelle werfen + +Musterlösung: + +[pr2.exceptions.eigene_ausnahme](../../solutions/src/main/java/pr2/exceptions/eigene_ausnahme/) diff --git a/Assignment_045/readme.md b/Assignment_045/readme.md index d12a438..31751af 100644 --- a/Assignment_045/readme.md +++ b/Assignment_045/readme.md @@ -1,6 +1,6 @@ # Eigene Exception schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_045/solution/readme.md b/Assignment_045/solution/readme.md new file mode 100644 index 0000000..9c7ad33 --- /dev/null +++ b/Assignment_045/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Eigene Exception schreiben + +Musterlösung: + +[pr2.exceptions.fakultaet](../../solutions/src/main/java/pr2/exceptions/fakultaet/) diff --git a/Assignment_046/readme.md b/Assignment_046/readme.md index d913501..43a5a44 100644 --- a/Assignment_046/readme.md +++ b/Assignment_046/readme.md @@ -1,6 +1,6 @@ # Handle-or-Declare-Regel anwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_046/solution/readme.md b/Assignment_046/solution/readme.md new file mode 100644 index 0000000..ca9a0e4 --- /dev/null +++ b/Assignment_046/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Handle-or-Declare-Regel anwenden + +Musterlösung: + +[pr2.exceptions.handle_or_declare](../../solutions/src/main/java/pr2/exceptions/handle_or_declare/) diff --git a/Assignment_047/readme.md b/Assignment_047/readme.md index 04395e1..ddd3f7b 100644 --- a/Assignment_047/readme.md +++ b/Assignment_047/readme.md @@ -1,6 +1,6 @@ # Ausnahmen mit `try` und `catch` behandeln. - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_047/solution/readme.md b/Assignment_047/solution/readme.md new file mode 100644 index 0000000..f28d42d --- /dev/null +++ b/Assignment_047/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Ausnahmen mit `try` und `catch` behandeln. + +Musterlösung: + +[pr2.exceptions.try_catch](../../solutions/src/main/java/pr2/exceptions/try_catch/) diff --git a/Assignment_048/readme.md b/Assignment_048/readme.md index 2ac07e0..aa7964e 100644 --- a/Assignment_048/readme.md +++ b/Assignment_048/readme.md @@ -1,6 +1,6 @@ # `BufferedReader` zum zeilenweisen Lesen einsetzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_048/solution/readme.md b/Assignment_048/solution/readme.md new file mode 100644 index 0000000..1bca802 --- /dev/null +++ b/Assignment_048/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `BufferedReader` zum zeilenweisen Lesen einsetzen + +Musterlösung: + +[pr2.io.buffered_reader](../../solutions/src/main/java/pr2/io/buffered_reader/) diff --git a/Assignment_049/readme.md b/Assignment_049/readme.md index d829315..e2cd1bc 100644 --- a/Assignment_049/readme.md +++ b/Assignment_049/readme.md @@ -1,6 +1,6 @@ # DataOutputStream - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_049/solution/readme.md b/Assignment_049/solution/readme.md new file mode 100644 index 0000000..07be1ad --- /dev/null +++ b/Assignment_049/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: DataOutputStream + +Musterlösung: + +[pr2.io.datainputoutput_1](../../solutions/src/main/java/pr2/io/datainputoutput_1/) diff --git a/Assignment_050/readme.md b/Assignment_050/readme.md index d84aad5..c3462ab 100644 --- a/Assignment_050/readme.md +++ b/Assignment_050/readme.md @@ -1,6 +1,6 @@ # DataOutputStream durch Serialisierung ersetzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_050/solution/readme.md b/Assignment_050/solution/readme.md new file mode 100644 index 0000000..be1a62c --- /dev/null +++ b/Assignment_050/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: DataOutputStream durch Serialisierung ersetzen + +Musterlösung: + +[pr2.io.datainputoutput_2](../../solutions/src/main/java/pr2/io/datainputoutput_2/) diff --git a/Assignment_051/readme.md b/Assignment_051/readme.md index 51e40f6..4494895 100644 --- a/Assignment_051/readme.md +++ b/Assignment_051/readme.md @@ -1,6 +1,6 @@ # Daten mit `DataOutputStream` und `DataInputStream` verarbeiten - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_051/solution/readme.md b/Assignment_051/solution/readme.md new file mode 100644 index 0000000..a168ce3 --- /dev/null +++ b/Assignment_051/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Daten mit `DataOutputStream` und `DataInputStream` verarbeiten + +Musterlösung: + +[pr2.io.data_output](../../solutions/src/main/java/pr2/io/data_output/) diff --git a/Assignment_052/readme.md b/Assignment_052/readme.md index 9aeca1c..fbf66bf 100644 --- a/Assignment_052/readme.md +++ b/Assignment_052/readme.md @@ -1,6 +1,6 @@ # Daten mit einem `InputStream` lesen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_052/solution/readme.md b/Assignment_052/solution/readme.md new file mode 100644 index 0000000..b3a3f87 --- /dev/null +++ b/Assignment_052/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Daten mit einem `InputStream` lesen + +Musterlösung: + +[pr2.io.datei_lesen](../../solutions/src/main/java/pr2/io/datei_lesen/) diff --git a/Assignment_053/readme.md b/Assignment_053/readme.md index dd5e32c..3a203b0 100644 --- a/Assignment_053/readme.md +++ b/Assignment_053/readme.md @@ -1,6 +1,6 @@ # Daten mit einem `OutputStream` schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_053/solution/readme.md b/Assignment_053/solution/readme.md new file mode 100644 index 0000000..0d7f08a --- /dev/null +++ b/Assignment_053/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Daten mit einem `OutputStream` schreiben + +Musterlösung: + +[pr2.io.datei_schreiben](../../solutions/src/main/java/pr2/io/datei_schreiben/) diff --git a/Assignment_054/readme.md b/Assignment_054/readme.md index 2e2bc0e..197a28b 100644 --- a/Assignment_054/readme.md +++ b/Assignment_054/readme.md @@ -1,6 +1,6 @@ # Filesystem-Abstraktion mit `File` - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_054/solution/readme.md b/Assignment_054/solution/readme.md new file mode 100644 index 0000000..0256bc8 --- /dev/null +++ b/Assignment_054/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Filesystem-Abstraktion mit `File` + +Musterlösung: + +[pr2.io.file](../../solutions/src/main/java/pr2/io/file/) diff --git a/Assignment_055/readme.md b/Assignment_055/readme.md index 9d979e5..3647021 100644 --- a/Assignment_055/readme.md +++ b/Assignment_055/readme.md @@ -1,6 +1,6 @@ # Fileattribute lesen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_055/solution/readme.md b/Assignment_055/solution/readme.md new file mode 100644 index 0000000..d3f3fce --- /dev/null +++ b/Assignment_055/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Fileattribute lesen + +Musterlösung: + +[pr2.io.filetest](../../solutions/src/main/java/pr2/io/filetest/) diff --git a/Assignment_056/readme.md b/Assignment_056/readme.md index 3b00e54..dc2236b 100644 --- a/Assignment_056/readme.md +++ b/Assignment_056/readme.md @@ -1,6 +1,6 @@ # `FilterReader` - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_056/solution/readme.md b/Assignment_056/solution/readme.md new file mode 100644 index 0000000..8f59e95 --- /dev/null +++ b/Assignment_056/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `FilterReader` + +Musterlösung: + +[pr2.io.filter](../../solutions/src/main/java/pr2/io/filter/) diff --git a/Assignment_057/readme.md b/Assignment_057/readme.md index 21f4697..ef04af0 100644 --- a/Assignment_057/readme.md +++ b/Assignment_057/readme.md @@ -1,6 +1,6 @@ # Konsolen Input/Output - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_057/solution/readme.md b/Assignment_057/solution/readme.md new file mode 100644 index 0000000..3ed3b1f --- /dev/null +++ b/Assignment_057/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Konsolen Input/Output + +Musterlösung: + +[pr2.io.konsole](../../solutions/src/main/java/pr2/io/konsole/) diff --git a/Assignment_058/readme.md b/Assignment_058/readme.md index d6ef785..9c367d4 100644 --- a/Assignment_058/readme.md +++ b/Assignment_058/readme.md @@ -1,6 +1,6 @@ # Zeilen einer Textdatei zählen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_058/solution/readme.md b/Assignment_058/solution/readme.md new file mode 100644 index 0000000..92e14d5 --- /dev/null +++ b/Assignment_058/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Zeilen einer Textdatei zählen + +Musterlösung: + +[pr2.io.linecounter](../../solutions/src/main/java/pr2/io/linecounter/) diff --git a/Assignment_059/readme.md b/Assignment_059/readme.md index b0cb70e..322a646 100644 --- a/Assignment_059/readme.md +++ b/Assignment_059/readme.md @@ -1,6 +1,6 @@ # `RandomAccessFile` - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_059/solution/readme.md b/Assignment_059/solution/readme.md new file mode 100644 index 0000000..ccacd2b --- /dev/null +++ b/Assignment_059/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `RandomAccessFile` + +Musterlösung: + +[pr2.io.random_access](../../solutions/src/main/java/pr2/io/random_access/) diff --git a/Assignment_060/readme.md b/Assignment_060/readme.md index 5541ca7..5a9bf72 100644 --- a/Assignment_060/readme.md +++ b/Assignment_060/readme.md @@ -1,6 +1,6 @@ # `Reader` verwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_060/solution/readme.md b/Assignment_060/solution/readme.md new file mode 100644 index 0000000..abbde00 --- /dev/null +++ b/Assignment_060/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `Reader` verwenden + +Musterlösung: + +[pr2.io.reader_writer](../../solutions/src/main/java/pr2/io/reader_writer/) diff --git a/Assignment_061/readme.md b/Assignment_061/readme.md index b9c1786..bc07e18 100644 --- a/Assignment_061/readme.md +++ b/Assignment_061/readme.md @@ -1,6 +1,6 @@ # Rot13-Verschlüsselung - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_061/solution/readme.md b/Assignment_061/solution/readme.md new file mode 100644 index 0000000..afe6008 --- /dev/null +++ b/Assignment_061/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Rot13-Verschlüsselung + +Musterlösung: + +[pr2.io.rot13](../../solutions/src/main/java/pr2/io/rot13/) diff --git a/Assignment_062/readme.md b/Assignment_062/readme.md index 745f39a..92bb47f 100644 --- a/Assignment_062/readme.md +++ b/Assignment_062/readme.md @@ -1,6 +1,6 @@ # Datei zerhacken - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_062/solution/readme.md b/Assignment_062/solution/readme.md new file mode 100644 index 0000000..ddb622a --- /dev/null +++ b/Assignment_062/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Datei zerhacken + +Musterlösung: + +[pr2.io.scrambler](../../solutions/src/main/java/pr2/io/scrambler/) diff --git a/Assignment_063/readme.md b/Assignment_063/readme.md index 97c85a3..4f9438f 100644 --- a/Assignment_063/readme.md +++ b/Assignment_063/readme.md @@ -1,6 +1,6 @@ # Serialisierung - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_063/solution/readme.md b/Assignment_063/solution/readme.md new file mode 100644 index 0000000..a523dfa --- /dev/null +++ b/Assignment_063/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Serialisierung + +Musterlösung: + +[pr2.io.serialisierung](../../solutions/src/main/java/pr2/io/serialisierung/) diff --git a/Assignment_064/readme.md b/Assignment_064/readme.md index 1863ef0..e2bf877 100644 --- a/Assignment_064/readme.md +++ b/Assignment_064/readme.md @@ -1,6 +1,6 @@ # Einen generischen Typ schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_064/solution/readme.md b/Assignment_064/solution/readme.md new file mode 100644 index 0000000..1a645c1 --- /dev/null +++ b/Assignment_064/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Einen generischen Typ schreiben + +Musterlösung: + +[pr2.generics.einfach](../../solutions/src/main/java/pr2/generics/einfach/) diff --git a/Assignment_065/readme.md b/Assignment_065/readme.md index 21a41de..2deee4e 100644 --- a/Assignment_065/readme.md +++ b/Assignment_065/readme.md @@ -1,6 +1,6 @@ # Generische Klasse Pair schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_065/solution/readme.md b/Assignment_065/solution/readme.md new file mode 100644 index 0000000..e4303a8 --- /dev/null +++ b/Assignment_065/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generische Klasse Pair schreiben + +Musterlösung: + +[pr2.generics.pair](../../solutions/src/main/java/pr2/generics/pair/) diff --git a/Assignment_066/readme.md b/Assignment_066/readme.md index 29c4869..7937624 100644 --- a/Assignment_066/readme.md +++ b/Assignment_066/readme.md @@ -1,6 +1,6 @@ # Generische Klasse Pair erweitern: NumberPair - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_066/solution/readme.md b/Assignment_066/solution/readme.md new file mode 100644 index 0000000..da73c86 --- /dev/null +++ b/Assignment_066/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generische Klasse Pair erweitern: NumberPair + +Musterlösung: + +[pr2.generics.number_pair](../../solutions/src/main/java/pr2/generics/number_pair/) diff --git a/Assignment_067/readme.md b/Assignment_067/readme.md index ddd4c2c..98caebb 100644 --- a/Assignment_067/readme.md +++ b/Assignment_067/readme.md @@ -1,6 +1,6 @@ # Generische Klasse Pair erweitern: SamePair - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_067/solution/readme.md b/Assignment_067/solution/readme.md new file mode 100644 index 0000000..a8c9b5c --- /dev/null +++ b/Assignment_067/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generische Klasse Pair erweitern: SamePair + +Musterlösung: + +[pr2.generics.same_pair](../../solutions/src/main/java/pr2/generics/same_pair/) diff --git a/Assignment_068/readme.md b/Assignment_068/readme.md index c774b7a..11159a6 100644 --- a/Assignment_068/readme.md +++ b/Assignment_068/readme.md @@ -1,6 +1,6 @@ # PairList - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_068/solution/readme.md b/Assignment_068/solution/readme.md new file mode 100644 index 0000000..7a66bc8 --- /dev/null +++ b/Assignment_068/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: PairList + +Musterlösung: + +[pr2.generics.pairlist](../../solutions/src/main/java/pr2/generics/pairlist/) diff --git a/Assignment_069/readme.md b/Assignment_069/readme.md index c202e79..1a70ac6 100644 --- a/Assignment_069/readme.md +++ b/Assignment_069/readme.md @@ -1,6 +1,6 @@ # Wildcard benutzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_069/solution/readme.md b/Assignment_069/solution/readme.md new file mode 100644 index 0000000..510b1f5 --- /dev/null +++ b/Assignment_069/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Wildcard benutzen + +Musterlösung: + +[pr2.generics.printer](../../solutions/src/main/java/pr2/generics/printer/) diff --git a/Assignment_070/readme.md b/Assignment_070/readme.md index 12c642c..2e9893d 100644 --- a/Assignment_070/readme.md +++ b/Assignment_070/readme.md @@ -1,6 +1,6 @@ # Generische Queue - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_070/solution/readme.md b/Assignment_070/solution/readme.md new file mode 100644 index 0000000..68a84e8 --- /dev/null +++ b/Assignment_070/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generische Queue + +Musterlösung: + +[pr2.generics.queue](../../solutions/src/main/java/pr2/generics/queue/) diff --git a/Assignment_071/readme.md b/Assignment_071/readme.md index 25088c3..584a648 100644 --- a/Assignment_071/readme.md +++ b/Assignment_071/readme.md @@ -1,6 +1,6 @@ # `super` und `extends` einsetzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_071/solution/readme.md b/Assignment_071/solution/readme.md new file mode 100644 index 0000000..6321f6a --- /dev/null +++ b/Assignment_071/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `super` und `extends` einsetzen + +Musterlösung: + +[pr2.generics.super_extends](../../solutions/src/main/java/pr2/generics/super_extends/) diff --git a/Assignment_072/readme.md b/Assignment_072/readme.md index 87ab60f..bf6be09 100644 --- a/Assignment_072/readme.md +++ b/Assignment_072/readme.md @@ -1,6 +1,6 @@ # Generischen Typ verwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_072/solution/readme.md b/Assignment_072/solution/readme.md new file mode 100644 index 0000000..d1c0905 --- /dev/null +++ b/Assignment_072/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generischen Typ verwenden + +Musterlösung: + +[pr2.generics.verwenden](../../solutions/src/main/java/pr2/generics/verwenden/). diff --git a/Assignment_073/readme.md b/Assignment_073/readme.md index 311d5b8..a351cdf 100644 --- a/Assignment_073/readme.md +++ b/Assignment_073/readme.md @@ -1,6 +1,6 @@ # Generische Typen zusammen mit Wildcards einsetzen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_073/solution/readme.md b/Assignment_073/solution/readme.md new file mode 100644 index 0000000..3cd4d43 --- /dev/null +++ b/Assignment_073/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Generische Typen zusammen mit Wildcards einsetzen + +Musterlösung: + +[pr2.generics.wildcards](../../solutions/src/main/java/pr2/generics/wildcards/). diff --git a/Assignment_074/readme.md b/Assignment_074/readme.md index 9351d8f..e037d19 100644 --- a/Assignment_074/readme.md +++ b/Assignment_074/readme.md @@ -1,6 +1,6 @@ # Anonyme Klasse schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_074/solution/readme.md b/Assignment_074/solution/readme.md new file mode 100644 index 0000000..6c6590c --- /dev/null +++ b/Assignment_074/solution/readme.md @@ -0,0 +1,6 @@ +# Lösung: Anonyme Klasse schreiben + +Musterlösung: + +[pr2.lambda.anonymous](../../solutions/src/main/java/pr2/lambda/anonymous/). +va) diff --git a/Assignment_075/readme.md b/Assignment_075/readme.md index fbfbf93..4957e66 100644 --- a/Assignment_075/readme.md +++ b/Assignment_075/readme.md @@ -1,6 +1,6 @@ # Eigene compare-Methode schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_075/solution/readme.md b/Assignment_075/solution/readme.md new file mode 100644 index 0000000..183c8af --- /dev/null +++ b/Assignment_075/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Eigene compare-Methode schreiben + +Musterlösung: + +[pr2.lambda.array_sorter](../../solutions/src/main/java/pr2/lambda/array_sorter/) diff --git a/Assignment_076/readme.md b/Assignment_076/readme.md index 4c370e9..075bcf9 100644 --- a/Assignment_076/readme.md +++ b/Assignment_076/readme.md @@ -1,6 +1,6 @@ # Innere Klasse Beobachter - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_076/solution/readme.md b/Assignment_076/solution/readme.md new file mode 100644 index 0000000..657e078 --- /dev/null +++ b/Assignment_076/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Innere Klasse Beobachter + +Musterlösung: + +pr2.lambda.observer](../../solutions/src/main/java/pr2/lambda/observer/). diff --git a/Assignment_077/readme.md b/Assignment_077/readme.md index 239391b..4a72e78 100644 --- a/Assignment_077/readme.md +++ b/Assignment_077/readme.md @@ -1,6 +1,6 @@ # Callback mit anonymer Klasse realisieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_077/solution/readme.md b/Assignment_077/solution/readme.md new file mode 100644 index 0000000..f3f2c66 --- /dev/null +++ b/Assignment_077/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Callback mit anonymer Klasse realisieren + +Musterlösung: + +[pr2.lambda.callback](../../solutions/src/main/java/pr2/lambda/callback/) diff --git a/Assignment_078/readme.md b/Assignment_078/readme.md index 62f488d..da32b09 100644 --- a/Assignment_078/readme.md +++ b/Assignment_078/readme.md @@ -1,6 +1,6 @@ # Comparator als Lambda - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_078/solution/readme.md b/Assignment_078/solution/readme.md new file mode 100644 index 0000000..43f250d --- /dev/null +++ b/Assignment_078/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Comparator als Lambda + +Musterlösung: + +[pr2.lambda.comparator](../../solutions/src/main/java/pr2/lambda/comparator/) diff --git a/Assignment_079/readme.md b/Assignment_079/readme.md index 3599552..3916f96 100644 --- a/Assignment_079/readme.md +++ b/Assignment_079/readme.md @@ -1,6 +1,6 @@ # Callback mit Lambda realisieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_079/solution/readme.md b/Assignment_079/solution/readme.md new file mode 100644 index 0000000..dec56e3 --- /dev/null +++ b/Assignment_079/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Callback mit Lambda realisieren + +Musterlösung: + +[pr2.lambda.lambdas](../../solutions/src/main/java/pr2/lambda/lambdas/) diff --git a/Assignment_080/readme.md b/Assignment_080/readme.md index 6ac37c7..0ef9cc9 100644 --- a/Assignment_080/readme.md +++ b/Assignment_080/readme.md @@ -1,6 +1,6 @@ # Lokale Klasse schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_080/solution/readme.md b/Assignment_080/solution/readme.md new file mode 100644 index 0000000..c7ab6e7 --- /dev/null +++ b/Assignment_080/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Lokale Klasse schreiben + +Musterlösung: + +[pr2.lambda.local](../../solutions/src/main/java/pr2/lambda/local/) diff --git a/Assignment_081/readme.md b/Assignment_081/readme.md index 4181c4a..1bb5de3 100644 --- a/Assignment_081/readme.md +++ b/Assignment_081/readme.md @@ -1,6 +1,6 @@ # MatrixSuche - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_081/solution/readme.md b/Assignment_081/solution/readme.md new file mode 100644 index 0000000..e3b7c01 --- /dev/null +++ b/Assignment_081/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: MatrixSuche + +Musterlösung: + +[pr2.lambda.matrixsuche](../../solutions/src/main/java/pr2/lambda/matrixsuche/) diff --git a/Assignment_082/readme.md b/Assignment_082/readme.md index 4ed267c..9f77b27 100644 --- a/Assignment_082/readme.md +++ b/Assignment_082/readme.md @@ -1,6 +1,6 @@ # StringTransmogrifier - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_082/solution/readme.md b/Assignment_082/solution/readme.md new file mode 100644 index 0000000..8bf44e0 --- /dev/null +++ b/Assignment_082/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: StringTransmogrifier + +Musterlösung: + +[pr2.lambda.mogrifier_1](../../solutions/src/main/java/pr2/lambda/mogrifier_1/) diff --git a/Assignment_083/readme.md b/Assignment_083/readme.md index df69e87..2b68bbd 100644 --- a/Assignment_083/readme.md +++ b/Assignment_083/readme.md @@ -1,6 +1,6 @@ # StringTransmogrifier erweitern - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_083/solution/readme.md b/Assignment_083/solution/readme.md new file mode 100644 index 0000000..9746d8f --- /dev/null +++ b/Assignment_083/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: StringTransmogrifier erweitern + +Musterlösung: + +[pr2.lambda.mogrifier_2](../../solutions/src/main/java/pr2/lambda/mogrifier_2/) diff --git a/Assignment_084/readme.md b/Assignment_084/readme.md index e4cc852..7cd3271 100644 --- a/Assignment_084/readme.md +++ b/Assignment_084/readme.md @@ -1,6 +1,6 @@ # Nonstatic Member Class - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_084/solution/readme.md b/Assignment_084/solution/readme.md new file mode 100644 index 0000000..c993b1e --- /dev/null +++ b/Assignment_084/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Nonstatic Member Class + +Musterlösung: + +[pr2.lambda.nonstatic](../../solutions/src/main/java/pr2/lambda/nonstatic/) diff --git a/Assignment_085/readme.md b/Assignment_085/readme.md index 976a1a4..f8d84fc 100644 --- a/Assignment_085/readme.md +++ b/Assignment_085/readme.md @@ -1,6 +1,6 @@ # Static Member Class - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_085/solution/readme.md b/Assignment_085/solution/readme.md new file mode 100644 index 0000000..75f6291 --- /dev/null +++ b/Assignment_085/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Static Member Class + +Musterlösung: + +[pr2.lambda.static_member](../../solutions/src/main/java/pr2/lambda/static_member/) diff --git a/Assignment_086/readme.md b/Assignment_086/readme.md index 1d4b2ea..3371860 100644 --- a/Assignment_086/readme.md +++ b/Assignment_086/readme.md @@ -1,6 +1,6 @@ # Iterator schreiben - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_086/solution/readme.md b/Assignment_086/solution/readme.md new file mode 100644 index 0000000..2af19d3 --- /dev/null +++ b/Assignment_086/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Iterator schreiben + +Musterlösung: + +[pr2.collections.iterator](../../solutions/src/main/java/pr2/collections/iterator/) diff --git a/Assignment_087/readme.md b/Assignment_087/readme.md index 0549724..1af5096 100644 --- a/Assignment_087/readme.md +++ b/Assignment_087/readme.md @@ -1,6 +1,6 @@ # Mithilfe eines Iterators über Daten iterieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_087/solution/readme.md b/Assignment_087/solution/readme.md new file mode 100644 index 0000000..a052299 --- /dev/null +++ b/Assignment_087/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Mithilfe eines Iterators über Daten iterieren + +Musterlösung: + +[pr2.collections.iterieren](../../solutions/src/main/java/pr2/collections/iterieren/) diff --git a/Assignment_088/readme.md b/Assignment_088/readme.md index 4e42672..03fc5f4 100644 --- a/Assignment_088/readme.md +++ b/Assignment_088/readme.md @@ -1,6 +1,6 @@ # Iterator programmieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_088/solution/readme.md b/Assignment_088/solution/readme.md new file mode 100644 index 0000000..85889cd --- /dev/null +++ b/Assignment_088/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Iterator programmieren + +Musterlösung: + +[pr2.collections.list_iterator](../../solutions/src/main/java/pr2/collections/list_iterator/) diff --git a/Assignment_089/readme.md b/Assignment_089/readme.md index 240203a..48a6673 100644 --- a/Assignment_089/readme.md +++ b/Assignment_089/readme.md @@ -1,6 +1,6 @@ # `List` und dessen Implementierungen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_089/solution/readme.md b/Assignment_089/solution/readme.md new file mode 100644 index 0000000..6157b4c --- /dev/null +++ b/Assignment_089/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `List` und dessen Implementierungen + +Musterlösung: + +[pr2.collections.list](../../solutions/src/main/java/pr2/collections/list/) diff --git a/Assignment_090/readme.md b/Assignment_090/readme.md index 640f6cd..6432de9 100644 --- a/Assignment_090/readme.md +++ b/Assignment_090/readme.md @@ -1,6 +1,6 @@ # `Map` verwenden - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_090/solution/readme.md b/Assignment_090/solution/readme.md new file mode 100644 index 0000000..cd64cf7 --- /dev/null +++ b/Assignment_090/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `Map` verwenden + +Musterlösung: + +[pr2.collections.map](../../solutions/src/main/java/pr2/collections/map/) diff --git a/Assignment_091/readme.md b/Assignment_091/readme.md index 1451721..45da4e9 100644 --- a/Assignment_091/readme.md +++ b/Assignment_091/readme.md @@ -1,6 +1,6 @@ # ReverserGeneric - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_091/solution/readme.md b/Assignment_091/solution/readme.md new file mode 100644 index 0000000..727c05e --- /dev/null +++ b/Assignment_091/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: ReverserGeneric + +Musterlösung: + +[pr2.collections.reverser_generic](../../solutions/src/main/java/pr2/collections/reverser_generic/) diff --git a/Assignment_092/readme.md b/Assignment_092/readme.md index 5f56bc0..6770802 100644 --- a/Assignment_092/readme.md +++ b/Assignment_092/readme.md @@ -1,6 +1,6 @@ # Strings umgekehrt sortieren: Reverser - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_092/solution/readme.md b/Assignment_092/solution/readme.md new file mode 100644 index 0000000..297fa12 --- /dev/null +++ b/Assignment_092/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Strings umgekehrt sortieren: Reverser + +Musterlösung: + +[pr2.collections.reverser](../../solutions/src/main/java/pr2/collections/reverser/) diff --git a/Assignment_093/readme.md b/Assignment_093/readme.md index 688a1a2..8f28425 100644 --- a/Assignment_093/readme.md +++ b/Assignment_093/readme.md @@ -1,6 +1,6 @@ # `Set` und dessen Implementierungen - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_093/solution/readme.md b/Assignment_093/solution/readme.md new file mode 100644 index 0000000..a7c6da7 --- /dev/null +++ b/Assignment_093/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `Set` und dessen Implementierungen + +Musterlösung: + +[pr2.collections.set](../../solutions/src/main/java/pr2/collections/set/) diff --git a/Assignment_094/readme.md b/Assignment_094/readme.md index 8a56cb3..b094686 100644 --- a/Assignment_094/readme.md +++ b/Assignment_094/readme.md @@ -1,6 +1,6 @@ # CommandLineSorter - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_094/solution/readme.md b/Assignment_094/solution/readme.md new file mode 100644 index 0000000..6c8a0cf --- /dev/null +++ b/Assignment_094/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: CommandLineSorter + +Musterlösung: + +[pr2.collections.sorter_1](../../solutions/src/main/java/pr2/collections/sorter_1/) diff --git a/Assignment_095/readme.md b/Assignment_095/readme.md index 3a41e46..45b249d 100644 --- a/Assignment_095/readme.md +++ b/Assignment_095/readme.md @@ -1,6 +1,6 @@ # CommandLineSorter -- Version 2 - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_095/solution/readme.md b/Assignment_095/solution/readme.md new file mode 100644 index 0000000..be55751 --- /dev/null +++ b/Assignment_095/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: CommandLineSorter -- Version 2 + +Musterlösung: + +[pr2.collections.sorter_2](../../solutions/src/main/java/pr2/collections/sorter_2/) diff --git a/Assignment_096/readme.md b/Assignment_096/readme.md index e1fcc93..9db1174 100644 --- a/Assignment_096/readme.md +++ b/Assignment_096/readme.md @@ -1,6 +1,6 @@ # `Comparator` verwenden und Objekte sortieren - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_096/solution/readme.md b/Assignment_096/solution/readme.md new file mode 100644 index 0000000..f543e35 --- /dev/null +++ b/Assignment_096/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: `Comparator` verwenden und Objekte sortieren + +Musterlösung: + +[pr2.collections.sortieren](../../solutions/src/main/java/pr2/collections/sortieren/) diff --git a/Assignment_097/readme.md b/Assignment_097/readme.md index ae66148..5cc4e93 100644 --- a/Assignment_097/readme.md +++ b/Assignment_097/readme.md @@ -1,6 +1,6 @@ # Wörterbuchprogramm - + [Musterlösung](solution/) ## Lernziel diff --git a/Assignment_097/solution/readme.md b/Assignment_097/solution/readme.md new file mode 100644 index 0000000..8fa5708 --- /dev/null +++ b/Assignment_097/solution/readme.md @@ -0,0 +1,5 @@ +# Lösung: Wörterbuchprogramm + +Musterlösung: + +[pr2.collections.woerterbuch](../../solutions/src/main/java/pr2/collections/woerterbuch/) diff --git a/readme.md b/readme.md index f454677..190a395 100644 --- a/readme.md +++ b/readme.md @@ -8,100 +8,100 @@ Hinweise zur nötigen Softwareausstattung finden Sie [hier](help/softwareausstat | # | Ausgabe | Thema | Musterlösung | |----|---------------------------------------------------------------------------------------------|----------------|----------------| -| 1. | n/a | [String in Großbuchstaben umwandeln](Assignment_001/readme.md) | | -| 2. | n/a | [Labeled Break](Assignment_002/readme.md) | | -| 3. | n/a | [Passwortbewertung](Assignment_003/readme.md) | | -| 4. | n/a | [printf mit Formatstring](Assignment_004/readme.md) | | -| 5. | n/a | [Maximum in einem Array suchen](Assignment_005/readme.md) | | -| 6. | n/a | [Taschenrechner](Assignment_006/readme.md) | | +| 1. | n/a | [String in Großbuchstaben umwandeln](Assignment_001/readme.md) | [✅](Assignment_001/solution/) | +| 2. | n/a | [Labeled Break](Assignment_002/readme.md) | [✅](Assignment_002/solution/) | +| 3. | n/a | [Passwortbewertung](Assignment_003/readme.md) | [✅](Assignment_003/solution/) | +| 4. | n/a | [printf mit Formatstring](Assignment_004/readme.md) | [✅](Assignment_004/solution/) | +| 5. | n/a | [Maximum in einem Array suchen](Assignment_005/readme.md) | [✅](Assignment_005/solution/) | +| 6. | n/a | [Taschenrechner](Assignment_006/readme.md) | [✅](Assignment_006/solution/) | | 7. | n/a | [Quine](Assignment_007/readme.md) | [✅](Assignment_007/solution/) | -| 8. | n/a | [Java-Coding-Standard anwenden](Assignment_008/readme.md) | | -| 9. | n/a | [JavaDoc schreiben](Assignment_009/readme.md) | | -| 10. | n/a | [Klasse mit JUnit testen](Assignment_010/readme.md) | | -| 11. | n/a | [Information-Hiding einer Klasse verbessern](Assignment_011/readme.md) | | -| 12. | n/a | [Vorhandene Bibliotheken als JAR einbinden](Assignment_012/readme.md) | | -| 13. | n/a | [Final anwenden](Assignment_013/readme.md) | | -| 14. | n/a | [Figur und Rechteck](Assignment_014/readme.md) | | -| 15. | n/a | [Figur erweitern](Assignment_015/readme.md) | | -| 16. | n/a | [Figur noch einmal erweitern](Assignment_016/readme.md) | | -| 17. | n/a | [Konstruktoren schreiben](Assignment_017/readme.md) | | -| 18. | n/a | [Polymorphie einsetzen](Assignment_018/readme.md) | | -| 19. | n/a | [Singleton](Assignment_019/readme.md) | | -| 20. | n/a | [Statische Methoden und Attribute](Assignment_020/readme.md) | | -| 21. | n/a | [Methode überladen](Assignment_021/readme.md) | | -| 22. | n/a | [Methoden überschreiben](Assignment_022/readme.md) | | -| 23. | n/a | [Varag-Methode schreiben](Assignment_023/readme.md) | | -| 24. | n/a | [Vererbung von Figuren](Assignment_024/readme.md) | | -| 25. | n/a | [Abstrakte Klassen](Assignment_025/readme.md) | | -| 26. | n/a | [Abstrakte Klasse](Assignment_026/readme.md) | | -| 27. | n/a | [Comparable implementieren](Assignment_027/readme.md) | | -| 28. | n/a | [Interface Stack entwerfen](Assignment_028/readme.md) | | -| 29. | n/a | [Interface: Uebersetzer](Assignment_029/readme.md) | | -| 30. | n/a | [Interfaces anwenden und entwerfen](Assignment_030/readme.md) | | -| 31. | n/a | [Deep-Copy mit `clone()`](Assignment_031/readme.md) | | -| 32. | n/a | [Clone](Assignment_032/readme.md) | | -| 33. | n/a | [`equals()` und `hashCode()` implementieren und nutzen](Assignment_033/readme.md) | | -| 34. | n/a | [equals und hashCode](Assignment_034/readme.md) | | -| 35. | n/a | [`toString()`-Methode implementieren](Assignment_035/readme.md) | | -| 36. | n/a | [Optimierung bei Integer](Assignment_036/readme.md) | | -| 37. | n/a | [Methoden der Wrapper-Klassen](Assignment_037/readme.md) | | -| 38. | n/a | [Eigene Enumeration schreiben und verwenden](Assignment_038/readme.md) | | -| 39. | n/a | [Enumeration schreiben](Assignment_039/readme.md) | | -| 40. | n/a | [Singleton-Eigenschaft von Enumerationen](Assignment_040/readme.md) | | -| 41. | n/a | [Eine vorhandene Enumeration nutzen](Assignment_041/readme.md) | | -| 42. | n/a | [Stein, Papier, Schere, Echse, Spock](Assignment_042/readme.md) | | -| 43. | n/a | [Ausnahmen testen](Assignment_043/readme.md) | | -| 44. | n/a | [Eigene Ausnahmen schreiben und an entsprechender Stelle werfen](Assignment_044/readme.md) | | -| 45. | n/a | [Eigene Exception schreiben](Assignment_045/readme.md) | | -| 46. | n/a | [Handle-or-Declare-Regel anwenden](Assignment_046/readme.md) | | -| 47. | n/a | [Ausnahmen mit `try` und `catch` behandeln.](Assignment_047/readme.md) | | -| 48. | n/a | [`BufferedReader` zum zeilenweisen Lesen einsetzen](Assignment_048/readme.md) | | -| 49. | n/a | [DataOutputStream](Assignment_049/readme.md) | | -| 50. | n/a | [DataOutputStream durch Serialisierung ersetzen](Assignment_050/readme.md) | | -| 51. | n/a | [Daten mit `DataOutputStream` und `DataInputStream` verarbeiten](Assignment_051/readme.md) | | -| 52. | n/a | [Daten mit einem `InputStream` lesen](Assignment_052/readme.md) | | -| 53. | n/a | [Daten mit einem `OutputStream` schreiben](Assignment_053/readme.md) | | -| 54. | n/a | [Filesystem-Abstraktion mit `File`](Assignment_054/readme.md) | | -| 55. | n/a | [Fileattribute lesen](Assignment_055/readme.md) | | -| 56. | n/a | [`FilterReader`](Assignment_056/readme.md) | | -| 57. | n/a | [Konsolen Input/Output](Assignment_057/readme.md) | | -| 58. | n/a | [Zeilen einer Textdatei zählen](Assignment_058/readme.md) | | -| 59. | n/a | [`RandomAccessFile`](Assignment_059/readme.md) | | -| 60. | n/a | [`Reader` verwenden](Assignment_060/readme.md) | | -| 61. | n/a | [Rot13-Verschlüsselung](Assignment_061/readme.md) | | -| 62. | n/a | [Datei zerhacken](Assignment_062/readme.md) | | -| 63. | n/a | [Serialisierung](Assignment_063/readme.md) | | -| 64. | n/a | [Einen generischen Typ schreiben](Assignment_064/readme.md) | | -| 65. | n/a | [Generische Klasse Pair schreiben](Assignment_065/readme.md) | | -| 66. | n/a | [Generische Klasse Pair erweitern: NumberPair](Assignment_066/readme.md) | | -| 67. | n/a | [Generische Klasse Pair erweitern: SamePair](Assignment_067/readme.md) | | -| 68. | n/a | [PairList](Assignment_068/readme.md) | | -| 69. | n/a | [Wildcard benutzen](Assignment_069/readme.md) | | -| 70. | n/a | [Generische Queue](Assignment_070/readme.md) | | -| 71. | n/a | [`super` und `extends` einsetzen](Assignment_071/readme.md) | | -| 72. | n/a | [Generischen Typ verwenden](Assignment_072/readme.md) | | -| 73. | n/a | [Generische Typen zusammen mit Wildcards einsetzen](Assignment_073/readme.md) | | -| 74. | n/a | [Anonyme Klasse schreiben](Assignment_074/readme.md) | | -| 75. | n/a | [Eigene compare-Methode schreiben](Assignment_075/readme.md) | | -| 76. | n/a | [Innere Klasse Beobachter](Assignment_076/readme.md) | | -| 77. | n/a | [Callback mit anonymer Klasse realisieren](Assignment_077/readme.md) | | -| 78. | n/a | [Comparator als Lambda](Assignment_078/readme.md) | | -| 79. | n/a | [Callback mit Lambda realisieren](Assignment_079/readme.md) | | -| 80. | n/a | [Lokale Klasse schreiben](Assignment_080/readme.md) | | -| 81. | n/a | [MatrixSuche](Assignment_081/readme.md) | | -| 82. | n/a | [StringTransmogrifier](Assignment_082/readme.md) | | -| 83. | n/a | [StringTransmogrifier erweitern](Assignment_083/readme.md) | | -| 84. | n/a | [Nonstatic Member Class](Assignment_084/readme.md) | | -| 85. | n/a | [Static Member Class](Assignment_085/readme.md) | | -| 86. | n/a | [Iterator schreiben](Assignment_086/readme.md) | | -| 87. | n/a | [Mithilfe eines Iterators über Daten iterieren](Assignment_087/readme.md) | | -| 88. | n/a | [Iterator programmieren](Assignment_088/readme.md) | | -| 89. | n/a | [`List` und dessen Implementierungen](Assignment_089/readme.md) | | -| 90. | n/a | [`Map` verwenden](Assignment_090/readme.md) | | -| 91. | n/a | [ReverserGeneric](Assignment_091/readme.md) | | -| 92. | n/a | [Strings umgekehrt sortieren: Reverser](Assignment_092/readme.md) | | -| 93. | n/a | [`Set` und dessen Implementierungen](Assignment_093/readme.md) | | -| 94. | n/a | [CommandLineSorter](Assignment_094/readme.md) | | -| 95. | n/a | [CommandLineSorter -- Version 2](Assignment_095/readme.md) | | -| 96. | n/a | [`Comparator` verwenden und Objekte sortieren](Assignment_096/readme.md) | | -| 97. | n/a | [Wörterbuchprogramm](Assignment_097/readme.md) | | +| 8. | n/a | [Java-Coding-Standard anwenden](Assignment_008/readme.md) | [✅](Assignment_008/solution/) | +| 9. | n/a | [JavaDoc schreiben](Assignment_009/readme.md) | [✅](Assignment_009/solution/) | +| 10. | n/a | [Klasse mit JUnit testen](Assignment_010/readme.md) | [✅](Assignment_010/solution/) | +| 11. | n/a | [Information-Hiding einer Klasse verbessern](Assignment_011/readme.md) | [✅](Assignment_011/solution/) | +| 12. | n/a | [Vorhandene Bibliotheken als JAR einbinden](Assignment_012/readme.md) | [✅](Assignment_012/solution/) | +| 13. | n/a | [Final anwenden](Assignment_013/readme.md) | [✅](Assignment_013/solution/) | +| 14. | n/a | [Figur und Rechteck](Assignment_014/readme.md) | [✅](Assignment_014/solution/) | +| 15. | n/a | [Figur erweitern](Assignment_015/readme.md) | [✅](Assignment_015/solution/) | +| 16. | n/a | [Figur noch einmal erweitern](Assignment_016/readme.md) | [✅](Assignment_016/solution/) | +| 17. | n/a | [Konstruktoren schreiben](Assignment_017/readme.md) | [✅](Assignment_017/solution/) | +| 18. | n/a | [Polymorphie einsetzen](Assignment_018/readme.md) | [✅](Assignment_018/solution/) | +| 19. | n/a | [Singleton](Assignment_019/readme.md) | [✅](Assignment_019/solution/) | +| 20. | n/a | [Statische Methoden und Attribute](Assignment_020/readme.md) | [✅](Assignment_020/solution/) | +| 21. | n/a | [Methode überladen](Assignment_021/readme.md) | [✅](Assignment_021/solution/) | +| 22. | n/a | [Methoden überschreiben](Assignment_022/readme.md) | [✅](Assignment_022/solution/) | +| 23. | n/a | [Varag-Methode schreiben](Assignment_023/readme.md) | [✅](Assignment_023/solution/) | +| 24. | n/a | [Vererbung von Figuren](Assignment_024/readme.md) | [✅](Assignment_024/solution/) | +| 25. | n/a | [Abstrakte Klassen](Assignment_025/readme.md) | [✅](Assignment_025/solution/) | +| 26. | n/a | [Abstrakte Klasse](Assignment_026/readme.md) | [✅](Assignment_026/solution/) | +| 27. | n/a | [Comparable implementieren](Assignment_027/readme.md) | [✅](Assignment_027/solution/) | +| 28. | n/a | [Interface Stack entwerfen](Assignment_028/readme.md) | [✅](Assignment_028/solution/) | +| 29. | n/a | [Interface: Uebersetzer](Assignment_029/readme.md) | [✅](Assignment_029/solution/) | +| 30. | n/a | [Interfaces anwenden und entwerfen](Assignment_030/readme.md) | [✅](Assignment_030/solution/) | +| 31. | n/a | [Deep-Copy mit `clone()`](Assignment_031/readme.md) | [✅](Assignment_031/solution/) | +| 32. | n/a | [Clone](Assignment_032/readme.md) | [✅](Assignment_032/solution/) | +| 33. | n/a | [`equals()` und `hashCode()` implementieren und nutzen](Assignment_033/readme.md) | [✅](Assignment_033/solution/) | +| 34. | n/a | [equals und hashCode](Assignment_034/readme.md) | [✅](Assignment_034/solution/) | +| 35. | n/a | [`toString()`-Methode implementieren](Assignment_035/readme.md) | [✅](Assignment_035/solution/) | +| 36. | n/a | [Optimierung bei Integer](Assignment_036/readme.md) | [✅](Assignment_036/solution/) | +| 37. | n/a | [Methoden der Wrapper-Klassen](Assignment_037/readme.md) | [✅](Assignment_037/solution/) | +| 38. | n/a | [Eigene Enumeration schreiben und verwenden](Assignment_038/readme.md) | [✅](Assignment_038/solution/) | +| 39. | n/a | [Enumeration schreiben](Assignment_039/readme.md) | [✅](Assignment_039/solution/) | +| 40. | n/a | [Singleton-Eigenschaft von Enumerationen](Assignment_040/readme.md) | [✅](Assignment_040/solution/) | +| 41. | n/a | [Eine vorhandene Enumeration nutzen](Assignment_041/readme.md) | [✅](Assignment_041/solution/) | +| 42. | n/a | [Stein, Papier, Schere, Echse, Spock](Assignment_042/readme.md) | [✅](Assignment_042/solution/) | +| 43. | n/a | [Ausnahmen testen](Assignment_043/readme.md) | [✅](Assignment_043/solution/) | +| 44. | n/a | [Eigene Ausnahmen schreiben und an entsprechender Stelle werfen](Assignment_044/readme.md) | [✅](Assignment_044/solution/) | +| 45. | n/a | [Eigene Exception schreiben](Assignment_045/readme.md) | [✅](Assignment_045/solution/) | +| 46. | n/a | [Handle-or-Declare-Regel anwenden](Assignment_046/readme.md) | [✅](Assignment_046/solution/) | +| 47. | n/a | [Ausnahmen mit `try` und `catch` behandeln.](Assignment_047/readme.md) | [✅](Assignment_047/solution/) | +| 48. | n/a | [`BufferedReader` zum zeilenweisen Lesen einsetzen](Assignment_048/readme.md) | [✅](Assignment_048/solution/) | +| 49. | n/a | [DataOutputStream](Assignment_049/readme.md) | [✅](Assignment_049/solution/) | +| 50. | n/a | [DataOutputStream durch Serialisierung ersetzen](Assignment_050/readme.md) | [✅](Assignment_050/solution/) | +| 51. | n/a | [Daten mit `DataOutputStream` und `DataInputStream` verarbeiten](Assignment_051/readme.md) | [✅](Assignment_051/solution/) | +| 52. | n/a | [Daten mit einem `InputStream` lesen](Assignment_052/readme.md) | [✅](Assignment_052/solution/) | +| 53. | n/a | [Daten mit einem `OutputStream` schreiben](Assignment_053/readme.md) | [✅](Assignment_053/solution/) | +| 54. | n/a | [Filesystem-Abstraktion mit `File`](Assignment_054/readme.md) | [✅](Assignment_054/solution/) | +| 55. | n/a | [Fileattribute lesen](Assignment_055/readme.md) | [✅](Assignment_055/solution/) | +| 56. | n/a | [`FilterReader`](Assignment_056/readme.md) | [✅](Assignment_056/solution/) | +| 57. | n/a | [Konsolen Input/Output](Assignment_057/readme.md) | [✅](Assignment_057/solution/) | +| 58. | n/a | [Zeilen einer Textdatei zählen](Assignment_058/readme.md) | [✅](Assignment_058/solution/) | +| 59. | n/a | [`RandomAccessFile`](Assignment_059/readme.md) | [✅](Assignment_059/solution/) | +| 60. | n/a | [`Reader` verwenden](Assignment_060/readme.md) | [✅](Assignment_060/solution/) | +| 61. | n/a | [Rot13-Verschlüsselung](Assignment_061/readme.md) | [✅](Assignment_061/solution/) | +| 62. | n/a | [Datei zerhacken](Assignment_062/readme.md) | [✅](Assignment_062/solution/) | +| 63. | n/a | [Serialisierung](Assignment_063/readme.md) | [✅](Assignment_063/solution/) | +| 64. | n/a | [Einen generischen Typ schreiben](Assignment_064/readme.md) | [✅](Assignment_064/solution/) | +| 65. | n/a | [Generische Klasse Pair schreiben](Assignment_065/readme.md) | [✅](Assignment_065/solution/) | +| 66. | n/a | [Generische Klasse Pair erweitern: NumberPair](Assignment_066/readme.md) | [✅](Assignment_066/solution/) | +| 67. | n/a | [Generische Klasse Pair erweitern: SamePair](Assignment_067/readme.md) | [✅](Assignment_067/solution/) | +| 68. | n/a | [PairList](Assignment_068/readme.md) | [✅](Assignment_068/solution/) | +| 69. | n/a | [Wildcard benutzen](Assignment_069/readme.md) | [✅](Assignment_069/solution/) | +| 70. | n/a | [Generische Queue](Assignment_070/readme.md) | [✅](Assignment_070/solution/) | +| 71. | n/a | [`super` und `extends` einsetzen](Assignment_071/readme.md) | [✅](Assignment_071/solution/) | +| 72. | n/a | [Generischen Typ verwenden](Assignment_072/readme.md) | [✅](Assignment_072/solution/) | +| 73. | n/a | [Generische Typen zusammen mit Wildcards einsetzen](Assignment_073/readme.md) | [✅](Assignment_073/solution/) | +| 74. | n/a | [Anonyme Klasse schreiben](Assignment_074/readme.md) | [✅](Assignment_074/solution/) | +| 75. | n/a | [Eigene compare-Methode schreiben](Assignment_075/readme.md) | [✅](Assignment_075/solution/) | +| 76. | n/a | [Innere Klasse Beobachter](Assignment_076/readme.md) | [✅](Assignment_076/solution/) | +| 77. | n/a | [Callback mit anonymer Klasse realisieren](Assignment_077/readme.md) | [✅](Assignment_077/solution/) | +| 78. | n/a | [Comparator als Lambda](Assignment_078/readme.md) | [✅](Assignment_078/solution/) | +| 79. | n/a | [Callback mit Lambda realisieren](Assignment_079/readme.md) | [✅](Assignment_079/solution/) | +| 80. | n/a | [Lokale Klasse schreiben](Assignment_080/readme.md) | [✅](Assignment_080/solution/) | +| 81. | n/a | [MatrixSuche](Assignment_081/readme.md) | [✅](Assignment_081/solution/) | +| 82. | n/a | [StringTransmogrifier](Assignment_082/readme.md) | [✅](Assignment_082/solution/) | +| 83. | n/a | [StringTransmogrifier erweitern](Assignment_083/readme.md) | [✅](Assignment_083/solution/) | +| 84. | n/a | [Nonstatic Member Class](Assignment_084/readme.md) | [✅](Assignment_084/solution/) | +| 85. | n/a | [Static Member Class](Assignment_085/readme.md) | [✅](Assignment_085/solution/) | +| 86. | n/a | [Iterator schreiben](Assignment_086/readme.md) | [✅](Assignment_086/solution/) | +| 87. | n/a | [Mithilfe eines Iterators über Daten iterieren](Assignment_087/readme.md) | [✅](Assignment_087/solution/) | +| 88. | n/a | [Iterator programmieren](Assignment_088/readme.md) | [✅](Assignment_088/solution/) | +| 89. | n/a | [`List` und dessen Implementierungen](Assignment_089/readme.md) | [✅](Assignment_089/solution/) | +| 90. | n/a | [`Map` verwenden](Assignment_090/readme.md) | [✅](Assignment_090/solution/) | +| 91. | n/a | [ReverserGeneric](Assignment_091/readme.md) | [✅](Assignment_091/solution/) | +| 92. | n/a | [Strings umgekehrt sortieren: Reverser](Assignment_092/readme.md) | [✅](Assignment_092/solution/) | +| 93. | n/a | [`Set` und dessen Implementierungen](Assignment_093/readme.md) | [✅](Assignment_093/solution/) | +| 94. | n/a | [CommandLineSorter](Assignment_094/readme.md) | [✅](Assignment_094/solution/) | +| 95. | n/a | [CommandLineSorter -- Version 2](Assignment_095/readme.md) | [✅](Assignment_095/solution/) | +| 96. | n/a | [`Comparator` verwenden und Objekte sortieren](Assignment_096/readme.md) | [✅](Assignment_096/solution/) | +| 97. | n/a | [Wörterbuchprogramm](Assignment_097/readme.md) | [✅](Assignment_097/solution/) | diff --git a/solutions/src/main/java/pr2/auffrischung/grossmacher/Grossmacher.java b/solutions/src/main/java/pr2/auffrischung/grossmacher/Grossmacher.java new file mode 100644 index 0000000..2ed9101 --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/grossmacher/Grossmacher.java @@ -0,0 +1,16 @@ +package pr2.auffrischung.grossmacher; + +public class Grossmacher { + + public static void main(String[] args) { + if (args.length < 1) { + System.err.println("Kein Argument angeben!"); + return; + } + + String ergebnis = args[0].toUpperCase(); + + System.out.printf("Ergebnis: %s%n", ergebnis); + System.out.printf("Zeichen : %d%n", ergebnis.length()); + } +} diff --git a/solutions/src/main/java/pr2/auffrischung/labeled_break/ArraySucher.java b/solutions/src/main/java/pr2/auffrischung/labeled_break/ArraySucher.java new file mode 100644 index 0000000..82e1359 --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/labeled_break/ArraySucher.java @@ -0,0 +1,27 @@ +package pr2.auffrischung.labeled_break; + +public class ArraySucher { + + /** + * Sucht das erste Element, dass nicht 0 ist. + * + * @param array das Array in dem gesucht werden soll + * @return {@code true}, wenn ein Element gefunden wird, + * andernfalls {@code false}. + */ + public boolean suche(int[][] array) { + boolean found = false; + + outer: + for (int[] is : array) { + for (int i : is) { + if (i != 0) { + found = true; + break outer; + } + } + } + + return found; + } +} diff --git a/solutions/src/main/java/pr2/auffrischung/password/PasswortChecker.java b/solutions/src/main/java/pr2/auffrischung/password/PasswortChecker.java new file mode 100644 index 0000000..147b8be --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/password/PasswortChecker.java @@ -0,0 +1,53 @@ +package pr2.auffrischung.password; + +public class PasswortChecker { + + private static final char[] NUMBERS = "1234567890".toCharArray(); + private static final char[] LOWERCASE + = "abcdefghijklmnopqrstuvwxyz".toCharArray(); + private static final char[] UPPERCASE + = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + private static final char[] SPECIAL_CHARS = "!%()\"".toCharArray(); + + private static boolean contains(String string, char[] chars) { + for (char c : chars) { + if (string.indexOf(c) >= 0) { + return true; + } + } + return false; + } + + public static int checkPassword(String password) { + int points = 0; + + if (password.length() >= 8) { + points++; + } + + if (contains(password, NUMBERS) + && (contains(password, LOWERCASE) + || contains(password, UPPERCASE))) { + points++; + } + + if (contains(password, SPECIAL_CHARS)) { + points++; + } + + if (contains(password, LOWERCASE) && contains(password, UPPERCASE)) { + points++; + } + + return points; + } + + public static void main(String[] args) { + System.out.println(checkPassword("mutti")); + System.out.println(checkPassword("Mutti")); + System.out.println(checkPassword("mutti123")); + System.out.println(checkPassword("Mutti123")); + System.out.println(checkPassword("Mutti123!%")); + System.out.println(checkPassword("1234")); + } +} diff --git a/solutions/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java b/solutions/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java new file mode 100644 index 0000000..788b1a6 --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java @@ -0,0 +1,18 @@ +package pr2.auffrischung.printf; + +public class DoubleFormatter { + + public static void printDouble(double d) { + System.out.printf("%.3f%n", d); + } + + public static void main(String[] args) { + printDouble(1.0); + printDouble(10.1); + printDouble(2.01); + printDouble(2.001); + printDouble(2.0001); + printDouble(2.0004); + printDouble(2.0005); + } +} diff --git a/solutions/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java b/solutions/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java new file mode 100644 index 0000000..92f89b7 --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java @@ -0,0 +1,21 @@ +package pr2.auffrischung.suchemax; + +public class GroessteZahl { + + public int sucheMax(int[] zahlen) { + int max = Integer.MIN_VALUE; + + for (int zahl : zahlen) { + max = Math.max(max, zahl); + } + + return max; + } + + public static void main(String[] args) { + GroessteZahl g = new GroessteZahl(); + System.out.println(g.sucheMax(new int[] {1, 5, 8, 2, 0})); + System.out.println(g.sucheMax(new int[] {-1, -5, -8, -2, -20})); + System.out.println(g.sucheMax(new int[] {10000, -10000, 1, 2, 33})); + } +} diff --git a/solutions/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java b/solutions/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java new file mode 100644 index 0000000..ecba9e2 --- /dev/null +++ b/solutions/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java @@ -0,0 +1,25 @@ +package pr2.auffrischung.taschenrechner; + +public class Taschenrechner { + + public double rechne(double o1, char op, double o2) { + return switch (op) { + case '+' -> o1 + o2; + case '-' -> o1 - o2; + case '*' -> o1 * o2; + case '/' -> o1 / o2; + case '^' -> Math.pow(o1, o2); + default -> Double.NaN; + }; +// return 0.0; + } + + public static void main(String[] args) { + Taschenrechner t = new Taschenrechner(); + System.out.println(t.rechne(1, '+', 2)); + System.out.println(t.rechne(1, '-', 2)); + System.out.println(t.rechne(2, '*', 2)); + System.out.println(t.rechne(4, '/', 2)); + System.out.println(t.rechne(2, '^', 32)); + } +} diff --git a/solutions/src/main/java/pr2/collections/iterator/SimpleStack.java b/solutions/src/main/java/pr2/collections/iterator/SimpleStack.java new file mode 100644 index 0000000..e52bfe6 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/iterator/SimpleStack.java @@ -0,0 +1,80 @@ +package pr2.collections.iterator; + +import java.util.Iterator; + +/** + * Eine einfache, naive Stack Implementierung. + * + * @param Typ, der gespeichert werden soll. + */ +public class SimpleStack implements Iterable { + + + // Variablen sind nicht private wegen Zugriff durch den Iterator + T[] stack; + + int pos; + + /** + * Legt einen neuen Stack mit der gegebenen Größe an. + * + * @param size Größe des Stacks. + */ + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + /** + * Fügt dem Stack ein neues Element hinzu. + * + * @param o Element, das hinzugefügt werden soll. + */ + public void push(T o) { + stack[pos++] = o; + } + + /** + * Holt das oberste Element und entfernt es. + * + * @return das oberste Element + */ + public T pop() { + return stack[--pos]; + } + + /** + * Gibt das oberste Element zurück, ohne es zu entfernen. + * + * @return das oberste Element + */ + public T peek() { + return stack[pos - 1]; + } + + + /** + * Erzeugt einen Iterator. + * + * @return Iterator + * @see java.lang.Iterable#iterator() + */ + @Override + public Iterator iterator() { + return new Iterator() { + + int iteratorPos = pos - 1; + + @Override + public boolean hasNext() { + return iteratorPos >= 0; + } + + @Override + public T next() { + return stack[iteratorPos--]; + } + }; + } +} diff --git a/solutions/src/main/java/pr2/collections/iterator/test/SimpleStackTest.java b/solutions/src/main/java/pr2/collections/iterator/test/SimpleStackTest.java new file mode 100644 index 0000000..79ce559 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/iterator/test/SimpleStackTest.java @@ -0,0 +1,86 @@ +package pr2.collections.iterator.test; + +import org.junit.jupiter.api.Test; +import pr2.collections.iterator.SimpleStack; + +import java.util.Iterator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test für den Stack. + */ +public class SimpleStackTest { + + /** + * Testet den Stack an sich. + */ + @Test + void testStack() { + SimpleStack s = new SimpleStack<>(10); + s.push("A"); + s.push("B"); + s.push("C"); + s.push("D"); + assertEquals("D", s.peek()); + assertEquals("D", s.pop()); + assertEquals("C", s.pop()); + assertEquals("B", s.pop()); + s.push("A2"); + s.push("A3"); + assertEquals("A3", s.peek()); + assertEquals("A3", s.pop()); + assertEquals("A2", s.pop()); + assertEquals("A", s.pop()); + } + + + /** + * Testet den Iterator. + */ + @Test + void testIterator() { + SimpleStack s = new SimpleStack<>(10); + s.push("A"); + s.push("B"); + s.push("C"); + s.push("D"); + + String[] result = new String[5]; + int count = 0; + + for (String string : s) { + result[count++] = string; + } + + assertEquals("D", s.peek()); + + assertEquals("D", result[0]); + assertEquals("C", result[1]); + assertEquals("B", result[2]); + assertEquals("A", result[3]); + + s.push("E"); + + Iterator it = s.iterator(); + + count = 0; + while (it.hasNext()) { + result[count++] = it.next(); + } + + assertEquals("E", result[0]); + assertEquals("D", result[1]); + assertEquals("C", result[2]); + assertEquals("B", result[3]); + assertEquals("A", result[4]); + + assertFalse(it.hasNext()); + + it = s.iterator(); + + assertTrue(it.hasNext()); + } +} diff --git a/solutions/src/main/java/pr2/collections/iterieren/CoinGenerator.java b/solutions/src/main/java/pr2/collections/iterieren/CoinGenerator.java new file mode 100644 index 0000000..acb849f --- /dev/null +++ b/solutions/src/main/java/pr2/collections/iterieren/CoinGenerator.java @@ -0,0 +1,115 @@ +package pr2.collections.iterieren; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.SimpleImage; +import de.smits_net.games.framework.sprite.Sprite; +import de.smits_net.games.framework.sprite.Sprite.BoundaryPolicy; + +import java.awt.Point; +import java.util.Iterator; +import java.util.Random; + +/** + * Klasse, die eine beliebige Menge an Geld generiert. + */ +public class CoinGenerator implements Iterable { + + /** + * Zufallszahlen-Generator. + */ + private static final Random RND = new Random(); + + /** + * Das Spielfeld. + */ + private final Board board; + + /** + * Anzahl der Münzen, die erzeugt werden sollen. + */ + int number; // nicht private wegen innerer Klasse + + /** + * Erzeugt eine neue Instanz, die zu dem übergebenen Spielfeld gehört. + * + * @param board Das Spielfeld. + * @param number Anzahl der Münzen. + */ + public CoinGenerator(Board board, int number) { + this.board = board; + this.number = number; + } + + /** + * Erzeugt einen neuen Iterator. + * + * @return Der Iterator. + */ + @Override + public Iterator iterator() { + return new Iterator() { + + int count = 0; + + @Override + public boolean hasNext() { + return count < number; + } + + @Override + public Sprite next() { + count++; + return createCoin(); + } + }; + } + + /** + * Legt eine zufällige Münze an. + * + * @return die Münze als Sprite. + */ + Sprite createCoin() { + final String prefix = "pr2/collections/iterieren"; + + String asset; + + switch (RND.nextInt(8)) { + case 0: + asset = prefix + "/1c.png"; + break; + case 1: + asset = prefix + "/2c.png"; + break; + case 3: + asset = prefix + "/5c.png"; + break; + case 4: + asset = prefix + "/10c.png"; + break; + case 5: + asset = prefix + "/20c.png"; + break; + case 6: + asset = prefix + "/50c.png"; + break; + case 7: + asset = prefix + "/1e.png"; + break; + default: + asset = prefix + "/2e.png"; + break; + } + + int xPos = RND.nextInt(board.getWidth()) - 20; + int yPos = RND.nextInt(board.getHeight()) - 20; + + return new Sprite(board, new Point(xPos, yPos), BoundaryPolicy.NONE, + new SimpleImage(asset)) { + @Override + protected void mousePressed() { + this.setVisible(false); + } + }; + } +} diff --git a/solutions/src/main/java/pr2/collections/iterieren/GameBoard.java b/solutions/src/main/java/pr2/collections/iterieren/GameBoard.java new file mode 100644 index 0000000..275c613 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/iterieren/GameBoard.java @@ -0,0 +1,66 @@ +package pr2.collections.iterieren; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.sprite.Sprite; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.util.Iterator; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Anzahl der Münzen. + */ + private static final int NUM_COINS = 1000; + + /** + * Münzgenerator. + */ + private final CoinGenerator generator; + + /** + * Münzen auf dem Spielfeld. + */ + private final Sprite[] coins = new Sprite[NUM_COINS]; + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(600, 600), Color.BLACK); + + generator = new CoinGenerator(this, NUM_COINS); + + int count = 0; + Iterator it = generator.iterator(); + + while (it.hasNext()) { + coins[count++] = it.next(); + } + } + + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public synchronized void drawGame(Graphics g) { + for (Sprite sprite : coins) { + sprite.draw(g); + } + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + return true; + } +} diff --git a/solutions/src/main/java/pr2/collections/iterieren/GameMain.java b/solutions/src/main/java/pr2/collections/iterieren/GameMain.java new file mode 100644 index 0000000..2f4c578 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/iterieren/GameMain.java @@ -0,0 +1,28 @@ +package pr2.collections.iterieren; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click the coins as fast as you can", new GameBoard()); + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + // Spiel starten + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/collections/list/WordCollector.java b/solutions/src/main/java/pr2/collections/list/WordCollector.java new file mode 100644 index 0000000..84b9e0d --- /dev/null +++ b/solutions/src/main/java/pr2/collections/list/WordCollector.java @@ -0,0 +1,267 @@ +package pr2.collections.list; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** + * Klassen, um die in einem Text vorkommenden Wörter zu sammeln. + */ +public class WordCollector { + + + /** + * Listet alle Worte in der Datei alphabetisch auf. Duplikate werden + * entfernt. Die Wörter werden in Kleinbuchstaben umgewandelt. + * + * @param filename Dateiname + * @return die Liste der vorhandenen Worte + * @throws IOException Fehler beim Dateizugriff. + */ + public static List listWords(String filename) throws IOException { + List allWords = readFileAndSplitIntoWords(filename); + List result = removeDuplicates(allWords); + + return result; + } + + /** + * Listet alle Worte in der Datei auf. + * + * @param filename Dateiname + * @return die Liste der vorhandenen Worte + * @throws IOException Fehler beim Dateizugriff. + */ + private static List readFileAndSplitIntoWords(String filename) + throws IOException { + + // Datei zum Lesen öffnen + BufferedReader reader = new BufferedReader(new FileReader(filename)); + + String line; // aktuelle Zeile + + List wordList = new LinkedList<>(); + + // Über die Zeilen der Datei iterieren + while ((line = reader.readLine()) != null) { + + // Sonderzeichen entfernen und die Zeilen in Worte splitten + line = line.toLowerCase(); + line = line.replaceAll("[\",.:'\\-!?]", ""); + + String[] words = line.toLowerCase().split("[,. ]"); + + // Worte in den Puffer übertragen + wordList.addAll(Arrays.asList(words)); + } + + reader.close(); + + return wordList; + } + + /** + * Sortiert das übergebene Array alphabetisch und entfernt Duplikate. + * + * @param input Eingabe Array + * @return sortiertes und bereinigtes Array + */ + @SuppressWarnings("PMD.AvoidReassigningLoopVariables") + private static List removeDuplicates(List input) { + + // Eingabe-Liste kopieren + List strings = new ArrayList<>(input); + + // Liste sortieren + Collections.sort(strings); + + // Über die Einträge laufen + for (int i = 0; i < strings.size(); i++) { + + String word = strings.get(i); + + // Über die Einträge laufen + for (int k = i + 1; k < strings.size(); k++) { + + String otherWord = strings.get(k); + + if (otherWord.compareTo(word) > 0) { + // Sind schon hinter der möglichen Position + break; + } + else if (otherWord.equals(word)) { + // Duplikat, entfernen + strings.remove(k); + k--; + } + } + } + + return strings; + } + + /** + * Hauptmethode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + + try { + List words = listWords( + "pr2/collections/list/kafka.txt"); + System.out.println(words); + } catch (IOException e) { + System.err.println("Probleme beim Dateizugriff: " + e); + } + } + +// /** +// * Listet alle Worte in der Datei alphabetisch auf. Duplikate werden +// * entfernt. Die Wörter werden in Kleinbuchstaben umgewandelt. +// * +// * @param filename Dateiname +// * @return die Liste der vorhandenen Wort +// * @throws IOException Fehler beim Dateizugriff. +// */ +// public static String[] listWords(String filename) throws IOException { +// String[] allWords = readFileAndSplitIntoWords(filename); +// String[] result = removeDuplicates(allWords); +// +// return result; +// } +// +// /** +// * Listet alle Worte in der Datei auf. +// * +// * @param filename Dateiname +// * @return die Liste der vorhandenen Wort +// * @throws IOException Fehler beim Dateizugriff. +// */ +// private static String[] readFileAndSplitIntoWords(String filename) +// throws IOException { +// +// // Datei zum Lesen öffnen +// BufferedReader reader = new BufferedReader( +// new FileReader(filename)); +// +// String line; // aktuelle Zeile +// String[] wordBuffer = new String[100]; // Puffer für die Worte +// int pos = 0; // Position im Word-Puffer +// +// // Über die Zeilen der Datei iterieren +// while ((line = reader.readLine()) != null) { +// +// // Sonderzeichen entfernen und die Zeilen in Worte splitten +// line = line.toLowerCase(); +// line = line.replaceAll("[\",.:'\\-\\!?]", ""); +// +// String[] words = line.toLowerCase().split("[,. ]"); +// +// // Worte in den Puffer übertragen +// for (String word : words) { +// +// if (pos >= wordBuffer.length) { +// // Puffer ist voll, vergrößern +// String[] newBuffer = +// new String[wordBuffer.length * 2]; +// System.arraycopy(wordBuffer, 0, newBuffer, +// 0, wordBuffer.length); +// wordBuffer = newBuffer; +// } +// +// wordBuffer[pos++] = word; +// } +// } +// +// reader.close(); +// +// // Ergebnis-Array mit der richtigen Größe anlegen +// String[] result = new String[pos]; +// System.arraycopy(wordBuffer, 0, result, 0, pos); +// +// return result; +// } +// +// /** +// * Sortiert das übergebene Array alphabetisch und entfernt Duplikate. +// * +// * @param input Eingabe Array +// * @return sortiertes und bereinigtes Array +// */ +// private static String[] removeDuplicates(String[] input) { +// +// // Eingabe Array clonen, da es verändert wird (Seiteneffekt) +// String[] strings = input.clone(); +// +// // Array sortieren +// Arrays.sort(strings); +// +// // Über die Einträge laufen +// for (int i = 0; i < strings.length; i++) { +// String word = strings[i]; +// +// if (word == null) { +// // Bereits entfernter Eintrag +// continue; +// } +// +// // Über die Einträge laufen +// for (int k = i + 1; k < strings.length; k++) { +// String otherWord = strings[k]; +// +// if (otherWord == null) { +// // Bereits entfernter Eintrag +// continue; +// } +// else if (otherWord.compareTo(word) > 0) { +// // Sind schon hinter der möglichen Position +// break; +// } +// else if (otherWord.equals(word)) { +// // Duplikat, ausnullen +// strings[k] = null; +// } +// } +// } +// +// // Ausgenullte Einträge entfernen +// int pos = 0; +// String[] temp = new String[strings.length]; +// +// for (int i = 0; i < strings.length; i++) { +// if (strings[i] != null) { +// temp[pos++] = strings[i]; +// } +// } +// +// // Ergebnis auf die richtige Länge bringen +// String[] result = new String[pos]; +// System.arraycopy(temp, 0, result, 0, pos); +// +// return result; +// } +// +// /** +// * Hauptmethode. +// * +// * @param args Kommandozeilen-Argumente. +// */ +// public static void main(String[] args) { +// +// try { +// String[] words = listWords( +// "pr2/collections/list/kafka.txt"); +// System.out.println(Arrays.toString(words)); +// } +// catch (IOException e) { +// System.err.println("Probleme beim Dateizugriff: " + e); +// } +// } + +} diff --git a/solutions/src/main/java/pr2/collections/list_iterator/Liste.java b/solutions/src/main/java/pr2/collections/list_iterator/Liste.java new file mode 100644 index 0000000..c76aa82 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/list_iterator/Liste.java @@ -0,0 +1,57 @@ +package pr2.collections.list_iterator; + +import java.util.Iterator; +public class Liste implements Iterable { + + static class Item { + T element; + Item next; + + public Item(T element) { + this.element = element; + } + } + + Item first; + Item last; + + class ListenIterator implements Iterator { + Item position = first; + + @Override + public boolean hasNext() { + return (position != null); + } + + @Override + public T next() { + T element = position.element; + position = position.next; + return element; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + } + + public void add(T element) { + Item item = new Item(element); + + first = (first != null) ? first : item; + + if (last == null) { + last = item; + } else { + last.next = item; + last = item; + } + + } + + @Override + public Iterator iterator() { + return new ListenIterator(); + } +} diff --git a/solutions/src/main/java/pr2/collections/map/WordCount.java b/solutions/src/main/java/pr2/collections/map/WordCount.java new file mode 100644 index 0000000..cd18cba --- /dev/null +++ b/solutions/src/main/java/pr2/collections/map/WordCount.java @@ -0,0 +1,83 @@ +package pr2.collections.map; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Zählen von Worthäufigkeiten. + */ +public class WordCount { + + /** + * Listet alle Worte in der Datei und deren Häufigkeit auf. + * Die zurückgegebene Liste ist bereits nach der Häufigkeit + * sortiert. + * + * @param filename Dateiname + * @return die Liste der vorhandenen Wörter + * @throws IOException Fehler beim Dateizugriff. + */ + private static List countWords(String filename) + throws IOException { + + // Map mit dem Wort als Schlüssel und der Häufigkeit als Key + Map frequencyMap = new HashMap<>(); + + // Datei zum Lesen öffnen + BufferedReader reader = new BufferedReader(new FileReader(filename)); + + String line; // aktuelle Zeile + + // Über die Zeilen der Datei iterieren + while ((line = reader.readLine()) != null) { + + // Sonderzeichen entfernen und die Zeilen in Worte splitten + line = line.toLowerCase(); + line = line.replaceAll("[\",.:;)'\\-!?]", ""); + + String[] words = line.toLowerCase().split("[,. ]"); + + for (String word : words) { + WordFrequency entry = frequencyMap.get(word); + if (entry == null) { + entry = new WordFrequency(word, 0); + frequencyMap.put(word, entry); + } + + entry.incrementFrequency(); + } + } + + reader.close(); + + List liste = new ArrayList<>(frequencyMap.values()); + Collections.sort(liste); + + return liste; + } + + /** + * Hauptmethode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + + try { + List words = countWords( + "pr2/collections/map/kafka.txt"); + + for (WordFrequency word : words) { + System.out.println(word); + } + } catch (IOException e) { + System.err.println("Probleme beim Dateizugriff: " + e); + } + } +} diff --git a/solutions/src/main/java/pr2/collections/map/WordFrequency.java b/solutions/src/main/java/pr2/collections/map/WordFrequency.java new file mode 100644 index 0000000..a4cba2f --- /dev/null +++ b/solutions/src/main/java/pr2/collections/map/WordFrequency.java @@ -0,0 +1,56 @@ +package pr2.collections.map; + +/** + * Klasse für die Verwaltung der Worthäufigkeiten. + */ +public class WordFrequency implements Comparable { + + /** + * Das Wort. + */ + String word; + + /** + * Seine Häufigkeit. + */ + int frequency; + + /** + * Legt ein neues Objekt an. + * + * @param word das gespeicherte Wort + * @param frequency die Häfigkeit + */ + WordFrequency(String word, int frequency) { + this.word = word; + this.frequency = frequency; + } + + + /** + * Vergleich mit anderem Objekt. + * + * @param o das andere Objekt + * @return Ergebnis des Vergleiches + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(WordFrequency o) { + return o.frequency - frequency; + } + + /** + * Erhöht die Häufigkeit des Wortes. + */ + public void incrementFrequency() { + frequency++; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("%s: %d", word, frequency); + } +} diff --git a/solutions/src/main/java/pr2/collections/reverser/Reverser.java b/solutions/src/main/java/pr2/collections/reverser/Reverser.java new file mode 100644 index 0000000..0025563 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/reverser/Reverser.java @@ -0,0 +1,18 @@ +package pr2.collections.reverser; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class Reverser { + public void reverse(List liste) { + + Collections.sort(liste, new Comparator() { + + @Override + public int compare(String o1, String o2) { + return o1.compareTo(o2) * -1; + } + }); + } +} diff --git a/solutions/src/main/java/pr2/collections/reverser/test/ReverserTest.java b/solutions/src/main/java/pr2/collections/reverser/test/ReverserTest.java new file mode 100644 index 0000000..09f0091 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/reverser/test/ReverserTest.java @@ -0,0 +1,23 @@ +package pr2.collections.reverser.test; + +import org.junit.jupiter.api.Test; +import pr2.collections.reverser.Reverser; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReverserTest { + + @Test + void testReverser() { + List testListe = Arrays.asList("Meier", "Zabelmann", "Schulze", + "Xavier", "Albert"); + new Reverser().reverse(testListe); + assertEquals(Arrays.asList( + "Zabelmann", "Xavier", "Schulze", "Meier", "Albert"), + testListe); + } + +} diff --git a/solutions/src/main/java/pr2/collections/reverser_generic/Reverser.java b/solutions/src/main/java/pr2/collections/reverser_generic/Reverser.java new file mode 100644 index 0000000..0958574 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/reverser_generic/Reverser.java @@ -0,0 +1,19 @@ +package pr2.collections.reverser_generic; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class Reverser> { + + public void reverse(List liste) { + + Collections.sort(liste, new Comparator() { + + @Override + public int compare(T o1, T o2) { + return o1.compareTo(o2) * -1; + } + }); + } +} diff --git a/solutions/src/main/java/pr2/collections/reverser_generic/test/ReverserTest.java b/solutions/src/main/java/pr2/collections/reverser_generic/test/ReverserTest.java new file mode 100644 index 0000000..a623dc2 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/reverser_generic/test/ReverserTest.java @@ -0,0 +1,23 @@ +package pr2.collections.reverser_generic.test; + +import org.junit.jupiter.api.Test; +import pr2.collections.reverser_generic.Reverser; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReverserTest { + + @Test + void testReverser() { + List testListe = Arrays.asList("Meier", "Zabelmann", "Schulze", + "Xavier", "Albert"); + new Reverser().reverse(testListe); + assertEquals(Arrays.asList( + "Zabelmann", "Xavier", "Schulze", "Meier", "Albert"), + testListe); + } + +} diff --git a/solutions/src/main/java/pr2/collections/set/WordCollector.java b/solutions/src/main/java/pr2/collections/set/WordCollector.java new file mode 100644 index 0000000..177d6fc --- /dev/null +++ b/solutions/src/main/java/pr2/collections/set/WordCollector.java @@ -0,0 +1,80 @@ +package pr2.collections.set; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Klassen, um die in einem Text vorkommenen Wörter zu sammeln. + */ +public class WordCollector { + + + /** + * Listet alle Worte in der Datei alphabetisch auf. Duplikate werden + * entfernt. Die Wörter werden in Kleinbuchstaben umgewandelt. + * + * @param filename Dateiname + * @return die Liste der vorhandenen Worte + * @throws IOException Fehler beim Dateizugriff. + */ + public static List listWords(String filename) throws IOException { + Set allWords = readFileAndSplitIntoWords(filename); + List wordsSorted = new ArrayList<>(allWords); + Collections.sort(wordsSorted); + return wordsSorted; + } + + /** + * Listet alle Worte in der Datei auf. + * + * @param filename Dateiname + * @return die Liste der vorhandenen Worte + * @throws IOException Fehler beim Dateizugriff. + */ + private static Set readFileAndSplitIntoWords(String filename) + throws IOException { + + // Datei zum Lesen öffnen + BufferedReader reader = new BufferedReader(new FileReader(filename)); + + String line; // aktuelle Zeile + Set wordBuffer = new HashSet<>(); // Puffer für die Worte + + // Über die Zeilen der Datei iterieren + while ((line = reader.readLine()) != null) { + + // Sonderzeichen entfernen und die Zeilen in Worte splitten + line = line.replaceAll("[\",.:'\\-!?]", ""); + + String[] words = line.toLowerCase().split("[,. ]"); + + // Worte in den Puffer übertragen + wordBuffer.addAll(Arrays.asList(words)); + } + + reader.close(); + + return wordBuffer; + } + + /** + * Hauptmethode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + + try { + List words = listWords("pr2/collections/set/kafka.txt"); + System.out.println(words); + } catch (IOException e) { + System.err.println("Probleme beim Dateizugriff: " + e); + } + } diff --git a/solutions/src/main/java/pr2/collections/sorter_1/CommandLineSorter.java b/solutions/src/main/java/pr2/collections/sorter_1/CommandLineSorter.java new file mode 100644 index 0000000..471bf93 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/sorter_1/CommandLineSorter.java @@ -0,0 +1,17 @@ +package pr2.collections.sorter_1; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class CommandLineSorter { + + public static void main(String[] args) { + List argList = Arrays.asList(args); + Collections.sort(argList); + + for (String element : argList) { + System.out.println(element); + } + } +} diff --git a/solutions/src/main/java/pr2/collections/sorter_2/CommandLineSorter.java b/solutions/src/main/java/pr2/collections/sorter_2/CommandLineSorter.java new file mode 100644 index 0000000..6ab92bb --- /dev/null +++ b/solutions/src/main/java/pr2/collections/sorter_2/CommandLineSorter.java @@ -0,0 +1,16 @@ +package pr2.collections.sorter_2; + +import java.util.Arrays; +import java.util.Set; +import java.util.TreeSet; + +public class CommandLineSorter { + + public static void main(String[] args) { + Set argList = new TreeSet(Arrays.asList(args)); + + for (String element : argList) { + System.out.println(element); + } + } +} diff --git a/solutions/src/main/java/pr2/collections/sortieren/Sorter.java b/solutions/src/main/java/pr2/collections/sortieren/Sorter.java new file mode 100644 index 0000000..1613994 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/sortieren/Sorter.java @@ -0,0 +1,83 @@ +package pr2.collections.sortieren; + +import java.util.Arrays; +import java.util.Comparator; + +/** + * Klasse zum Sortieren von Strings. + */ +public class Sorter { + + /** + * Liefert einen Comparator für Strings. + * + * @param order Sortierreihenfolge. + * @return Comparator, entsprechend der gewünschten Sortierreihenfolge. + */ + private static Comparator stringComparator(final SortOrder order) { + /* Comparator. */ + class MyComparator implements Comparator { + + /** + * Vergleicht die Strings. + * + * @param o1 Erster String + * @param o2 Zweiter String + * @return Ergebnis des Vergleichs. + */ + @Override + public int compare(String o1, String o2) { + switch (order) { + case ASCENDING: + return o1.compareTo(o2); + case DESCENDING: + return -o1.compareTo(o2); + case ASCENDING_CASE_INSENSITIVE: + return o1.toUpperCase().compareTo(o2.toUpperCase()); + case DESCENDING_CASE_INSENSITIVE: + return -o1.toUpperCase().compareTo(o2.toUpperCase()); + default: + throw new IllegalArgumentException(); + } + } + } + + return new MyComparator(); + } + + /** + * Sortiert das übergebene Array entsprechend der gewünschten Reihenfolge. + * + * @param array das zu sortierende Array. + * @param order die Sortierreihenfolge. + */ + public static void sort(String[] array, SortOrder order) { + Arrays.sort(array, stringComparator(order)); + } + + /** + * Sortierreihenfolge für die Strings. + */ + public enum SortOrder { + + /** + * Aufsteigend. + */ + ASCENDING, + + /** + * Absteigend. + */ + DESCENDING, + + /** + * Aufsteigend, ohne Beachtung der Groß-/Kleinschreibung. + */ + ASCENDING_CASE_INSENSITIVE, + + /** + * Absteigend, ohne Beachtung der Groß-/Kleinschreibung. + */ + DESCENDING_CASE_INSENSITIVE + } +} diff --git a/solutions/src/main/java/pr2/collections/sortieren/test/SorterTest.java b/solutions/src/main/java/pr2/collections/sortieren/test/SorterTest.java new file mode 100644 index 0000000..a2c30f2 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/sortieren/test/SorterTest.java @@ -0,0 +1,75 @@ +package pr2.collections.sortieren.test; + +import org.junit.jupiter.api.Test; +import pr2.collections.sortieren.Sorter; +import pr2.collections.sortieren.Sorter.SortOrder; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + + +/** + * Test für die Sortierung. + */ +public class SorterTest { + + private static final String[] DATA = + {"aaron", "ALFONS", "bond", "BerND", "henry", "Hugo"}; + + /** + * Testet die Sortierung: aufsteigend. + */ + @Test + void testAscendingSorting() { + + String[] asc = DATA.clone(); + + Sorter.sort(asc, SortOrder.ASCENDING); + + assertArrayEquals( + new String[] {"ALFONS", "BerND", "Hugo", "aaron", "bond", + "henry"}, asc); + } + + /** + * Testet die Sortierung: Absteigend. + */ + @Test + void testDescendingSorting() { + + String[] desc = DATA.clone(); + Sorter.sort(desc, SortOrder.DESCENDING); + + assertArrayEquals( + new String[] {"henry", "bond", "aaron", "Hugo", "BerND", + "ALFONS"}, desc); + } + + /** + * Testet die Sortierung: Aufsteigend, CI. + */ + @Test + void testAscendingCISorting() { + + String[] ascCI = DATA.clone(); + Sorter.sort(ascCI, SortOrder.ASCENDING_CASE_INSENSITIVE); + + assertArrayEquals( + new String[] {"aaron", "ALFONS", "BerND", "bond", "henry", + "Hugo"}, ascCI); + } + + /** + * Testet die Sortierung: Absteigend, CI. + */ + @Test + void testDescendingCISorting() { + + String[] descCI = DATA.clone(); + + Sorter.sort(descCI, SortOrder.DESCENDING_CASE_INSENSITIVE); + + assertArrayEquals( + new String[] {"Hugo", "henry", "bond", "BerND", "ALFONS", + "aaron"}, descCI); + } +} diff --git a/solutions/src/main/java/pr2/collections/woerterbuch/Woerterbuch.java b/solutions/src/main/java/pr2/collections/woerterbuch/Woerterbuch.java new file mode 100644 index 0000000..9a4d618 --- /dev/null +++ b/solutions/src/main/java/pr2/collections/woerterbuch/Woerterbuch.java @@ -0,0 +1,37 @@ +package pr2.collections.woerterbuch; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class Woerterbuch { + + private static final Map WOERTER; + + static { + Map map = new HashMap(); + map.put("gehen", "go"); + map.put("schlafen", "sleep"); + map.put("tanzen", "dance"); + map.put("springen", "jump"); + + WOERTER = Collections.unmodifiableMap(map); + } + + public static void main(String[] args) { + if (args.length == 0) { + System.err.println("Bitte mindestens ein Wort angeben!"); + System.exit(1); + } + + for (String wort : args) { + String uebersetzung = WOERTER.get(wort); + + if (uebersetzung == null) { + uebersetzung = ""; + } + + System.out.printf("%s => %s%n", wort, uebersetzung); + } + } +} diff --git a/solutions/src/main/java/pr2/enums/eigene/Wuerfel.java b/solutions/src/main/java/pr2/enums/eigene/Wuerfel.java new file mode 100644 index 0000000..347679b --- /dev/null +++ b/solutions/src/main/java/pr2/enums/eigene/Wuerfel.java @@ -0,0 +1,9 @@ +package pr2.enums.eigene; + +import java.util.Random; + +/** + * Ein einfacher Würfel. + */ +public class Wuerfel { + diff --git a/solutions/src/main/java/pr2/enums/eigene/WuerfelTyp.java b/solutions/src/main/java/pr2/enums/eigene/WuerfelTyp.java new file mode 100644 index 0000000..c17f4fc --- /dev/null +++ b/solutions/src/main/java/pr2/enums/eigene/WuerfelTyp.java @@ -0,0 +1,71 @@ + +package pr2.enums.eigene; + +/** + * Enumeration für die verschiedenen Würfel-Typen. + */ +public enum WuerfelTyp { + + /** + * 4-seitiger Würfel. + */ + D4(4), + + /** + * 6-seitiger Würfel. + */ + D6(6), + + /** + * 8-seitiger Würfel. + */ + D8(8), + + /** + * 10-seitiger Würfel. + */ + D10(10), + + /** + * 12-seitiger Würfel. + */ + D12(12), + + /** + * 20-seitiger Würfel. + */ + D20(20); + + /** + * Anzahl der Seiten. + */ + private final int seiten; + + /** + * Neue Instanz anlegen. + * + * @param seiten Anzahl der Seiten. + */ + WuerfelTyp(int seiten) { + this.seiten = seiten; + } + + /** + * Anzahl der Seiten. + * + * @return Anzahl der Seiten. + */ + public int getSeiten() { + return seiten; + } + + /** + * Durchschnittliche Augenzahl bei einer großen Anzahl von + * Würfen. + * + * @return Durchschnittliche Augenzahl. + */ + public double average() { + return 0.5 * (seiten + 1); + } +} diff --git a/solutions/src/main/java/pr2/enums/eigene/test/WuerfelTest.java b/solutions/src/main/java/pr2/enums/eigene/test/WuerfelTest.java new file mode 100644 index 0000000..793c803 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/eigene/test/WuerfelTest.java @@ -0,0 +1,24 @@ +package pr2.enums.eigene.test; + +import org.junit.jupiter.api.Test; +import pr2.enums.eigene.Wuerfel; +import pr2.enums.eigene.WuerfelTyp; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test für die Würfel. + */ +public class WuerfelTest { + + /** + * Anzahl der Durchläufe (Gesetz der großen Zahl!). + */ + private static final int RUNS = 10000000; + + @Test + void testFairness() { + for (WuerfelTyp typ : WuerfelTyp.values()) { + internalTestFairness(new Wuerfel(typ), typ.average()); + } + } diff --git a/solutions/src/main/java/pr2/enums/filme/LieblingsFilme.java b/solutions/src/main/java/pr2/enums/filme/LieblingsFilme.java new file mode 100644 index 0000000..7ae95b1 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/filme/LieblingsFilme.java @@ -0,0 +1,50 @@ +package pr2.enums.filme; + +public enum LieblingsFilme { + + /** + * Pulp Fiction. + */ + PULP_FICTION("Pulp Fiction", "Quentin Tarantino", 9), + /** + * Fight Club. + */ + FIGHT_CLUB("Fight Club", "David Fincher", 9), + /** + * Twelve Monkeys. + */ + TWELVE_MONKEYS("Twelve Monkeys", "Terry Giliam", 8), + /** + * Killer Tomatos. + */ + KILLER_TOMATOES("Attack of the Killer Tomatoes", "John de Bello", 4); + + private final String name; + private final int bewertung; + private final String regisseur; + + private LieblingsFilme(String name, String regisseur, + int bewertung) { + this.name = name; + this.regisseur = regisseur; + this.bewertung = bewertung; + } + + public String getName() { + return name; + } + + public int getBewertung() { + return bewertung; + } + + public String getRegisseur() { + return regisseur; + } + + @Override + public String toString() { + return String.format("'%s' von %s; Bewertung %d", name, + regisseur, bewertung); + } +} diff --git a/solutions/src/main/java/pr2/enums/filme/Main.java b/solutions/src/main/java/pr2/enums/filme/Main.java new file mode 100644 index 0000000..8a53673 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/filme/Main.java @@ -0,0 +1,9 @@ +package pr2.enums.filme; + +public class Main { + public static void main(String[] args) { + for (LieblingsFilme f : LieblingsFilme.values()) { + System.out.println(f); + } + } +} diff --git a/solutions/src/main/java/pr2/enums/schnick/Move.java b/solutions/src/main/java/pr2/enums/schnick/Move.java new file mode 100644 index 0000000..eccdd75 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/schnick/Move.java @@ -0,0 +1,46 @@ +package pr2.enums.schnick; + +public enum Move { + /** + * Konstante für Schere. + */ + SCISSORS("Scissors", "s"), + + /** + * Konstante für Papier. + */ + PAPER("Paper", "p"), + + /** + * Konstante für Stein. + */ + ROCK("Rock", "r"); + + private final String name; + private final String key; + + private Move(String name, String key) { + this.name = name; + this.key = key; + } + + @Override + public String toString() { + return name; + } + + /** + * Returns the move corresponding to the key entered. + * + * @param key the key pressed. + * @return the corresponding move. + */ + public static Move find(String key) { + for (Move m : values()) { + if (m.key.equals(key.toLowerCase())) { + return m; + } + } + return null; + } +} diff --git a/solutions/src/main/java/pr2/enums/schnick/SchnickSchnackSchnuck.java b/solutions/src/main/java/pr2/enums/schnick/SchnickSchnackSchnuck.java new file mode 100644 index 0000000..68ada87 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/schnick/SchnickSchnackSchnuck.java @@ -0,0 +1,89 @@ +package pr2.enums.schnick; + +import java.util.Random; +import java.util.Scanner; + +/** + * Schick - Schnack - Schnuck (Rock - Paper - Scissors). + */ +public class SchnickSchnackSchnuck { + + /** + * Vergleicht den Zug zweier Spieler. + * + * @param a Zug von Spieler a. + * @param b Zug von Spieler b. + * @return 1 spieler a hat gewonnen, 0, unentschieden, -1 spieler b + * hat gewonnen. + */ + private static int compare(Move a, Move b) { + + if (a == b) { + return 0; + } + else if (a == Move.SCISSORS && b == Move.PAPER) { + return 1; + } + else if (a == Move.ROCK && b == Move.SCISSORS) { + return 1; + } + else if (a == Move.PAPER && b == Move.ROCK) { + return 1; + } + else { + return compare(b, a) * -1; + } + } + + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + Random rnd = new Random(); + + int pointsPlayer = 0; + int pointsComputer = 0; + + // Play three rounds + for (int i = 0; i < 3; i++) { + + // Read the player's choice + Move movePlayer = null; + + while (movePlayer == null) { + System.out.printf( + "\nRunde %s: Dein Zug ([r]rock, [p]aper, [s]cissors)? ", + i); + String move = scanner.nextLine().trim(); + movePlayer = Move.find(move); + } + + // Random move of the computer + Move moveComputer = + Move.values()[rnd.nextInt(2)]; + + // Compare moves + int winner = compare(movePlayer, moveComputer); + + String winnerText; + + if (winner == 1) { + pointsPlayer++; + winnerText = "Du hast gewonnen"; + } else if (winner == -1) { + pointsComputer++; + winnerText = "Der Computer hat gewonnen"; + } else { + winnerText = "Unentschieden"; + } + System.out.printf("%s: %s, Computer: %s - [%d:%d]\n", + winnerText, + movePlayer, moveComputer, + pointsPlayer, pointsComputer); + } + + System.out.printf( + "\nErgebnis - Du %d Punkt(e), " + + "Computer %d Punkt(e)", + pointsPlayer, pointsComputer); + } +} diff --git a/solutions/src/main/java/pr2/enums/singleton/MyEnum.java b/solutions/src/main/java/pr2/enums/singleton/MyEnum.java new file mode 100644 index 0000000..be83cc0 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/singleton/MyEnum.java @@ -0,0 +1,7 @@ +package pr2.enums.singleton; + +enum MyEnum { + A, + B, + C; +} diff --git a/solutions/src/main/java/pr2/enums/singleton/SingletonBeweis.java b/solutions/src/main/java/pr2/enums/singleton/SingletonBeweis.java new file mode 100644 index 0000000..327cf52 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/singleton/SingletonBeweis.java @@ -0,0 +1,14 @@ +package pr2.enums.singleton; + +public class SingletonBeweis { + + public static void main(String[] args) { + MyEnum e1 = MyEnum.A; + MyEnum e2 = MyEnum.valueOf("A"); + MyEnum e3 = MyEnum.values()[0]; + + System.out.println(e1 == e2); + System.out.println(e2 == e3); + System.out.println(e1 == e3); + } +} diff --git a/solutions/src/main/java/pr2/enums/verwenden/GameBoard.java b/solutions/src/main/java/pr2/enums/verwenden/GameBoard.java new file mode 100644 index 0000000..a0c12b5 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/verwenden/GameBoard.java @@ -0,0 +1,47 @@ +package pr2.enums.verwenden; + +import de.smits_net.games.framework.board.Board; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Sprite, das durch das Bild läuft. + */ + private final Professor sprite; + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(400, 400), Color.BLACK); + + // Sprite initialisieren + sprite = new Professor(this, new Point(300, 200)); + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGame(Graphics g) { + sprite.draw(g, this); + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + sprite.move(); + return sprite.isVisible(); + } +} diff --git a/solutions/src/main/java/pr2/enums/verwenden/GameMain.java b/solutions/src/main/java/pr2/enums/verwenden/GameMain.java new file mode 100644 index 0000000..becc1ae --- /dev/null +++ b/solutions/src/main/java/pr2/enums/verwenden/GameMain.java @@ -0,0 +1,28 @@ +package pr2.enums.verwenden; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Watch me walk...", new GameBoard()); + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + // Spiel starten + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/enums/verwenden/Professor.java b/solutions/src/main/java/pr2/enums/verwenden/Professor.java new file mode 100644 index 0000000..6d648a4 --- /dev/null +++ b/solutions/src/main/java/pr2/enums/verwenden/Professor.java @@ -0,0 +1,32 @@ +package pr2.enums.verwenden; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Point; + +/** + * Ein Sprite. + */ +public class Professor extends AnimatedSprite { + + /** + * Geschwindigkeit der Figur in X-Richtung. + */ + private static final int SPRITE_SPEED = 2; + + /** + * Neues Sprite anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Professor(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.NONE, + new AnimatedImage(50, 9, + "pr2/enums/use/professor_left")); + velocity.setVelocity(Direction.WEST, SPRITE_SPEED); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/CrypterCaesar.java b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/CrypterCaesar.java new file mode 100644 index 0000000..5b9f0ab --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/CrypterCaesar.java @@ -0,0 +1,164 @@ +package pr2.exceptions.ausnahmen_testen; + +/** + * Implementierung einer Caesar-Verschlüsselung. + *

+ * Der Eingabetext ({@literal message}) darf nur aus den Groß-Buchstaben A-Z + * bestehen. Alle anderen Zeichen (einschließlich der Leerzeichen) führen zu + * einer entsprechenden Ausnahme. + *

+ * Dasselbe gilt für die Entschlüsselung. Auch hier dürfen nur gültige + * Zeichen auftauchen. Andernfalls kommt es zu einer Ausnahme. + *

+ * Der Schlüssel darf nur aus einem einzelnen Zeichen bestehen und muss + * ebenfalls aus dem Bereich A-Z stammen. Kleinbuchstaben oder andere + * Zeichen führen zu einer Ausnahme. + *

+ * Zwischen den beiden Methoden muss bei gleichem Schlüssel {@code key} + * folgendes gelten: + *

+ * {@code text.equals(decrypt(key, encrypt(key, text)) == true}. + */ +public class CrypterCaesar { + + /** + * Gültige Zeichen für Schlüssel und Text. + */ + protected static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + /** + * Verschlüsselt den gegebenen Text mit dem angegebenen Schlüssel. + * + * @param key Schlüssel, der verwendet werden soll. + * @param message Nachricht, die Verschlüsselt werden soll. + * @return verschlüsselter Text. + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + public String encrypt(String key, String message) + throws IllegalMessageException, IllegalKeyException { + + checkKeyAndMessage(key, message); + + int shift = key.charAt(0) - 'A' + 1; + return shift(message, shift); + } + + /** + * Entschlüsselt den gegebenen Text mit dem angegebenen Schlüssel. + * + * @param key Schlüssel, der verwendet werden soll. + * @param cypherText Nachricht, die entschlüsselt werden soll. + * @return entschlüsselter Text. + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + public String decrypt(String key, String cypherText) + throws IllegalKeyException, IllegalMessageException { + + checkKeyAndMessage(key, cypherText); + + int shift = key.charAt(0) - 'A' + 1; + return shift(cypherText, -shift); + } + + /** + * Verschiebt die Zeichen im Text um den gegebenen Faktor. + * + * @param text Nachricht + * @param shift Verschiebefaktor + * @return das Ergebnis als String + */ + private String shift(String text, int shift) { + + StringBuilder result = new StringBuilder(); + + for (char c : text.toCharArray()) { + char newChar = (char) (c + shift); + + if (newChar > 'Z') { + // Überlauf nach hinten + newChar = (char) (newChar - ('Z' - 'A' + 1)); + } + else if (newChar < 'A') { + // Überlauf nach vorne + newChar = (char) (newChar + ('Z' - 'A' + 1)); + } + + result.append(newChar); + } + + return result.toString(); + } + + /** + * Prüft Schlüssel und Nachricht. + * + * @param key der Schlüssel + * @param message die Nachricht + * @throws IllegalKeyException Schlüssel nicht OK + * @throws IllegalMessageException Nachricht nicht OK + */ + private void checkKeyAndMessage(String key, String message) + throws IllegalKeyException, IllegalMessageException { + + if (!checkKey(key)) { + throw new IllegalKeyException(key); + } + + if (!checkCharacters(message, ALPHABET)) { + throw new IllegalMessageException(key); + } + } + + /** + * Testet den Schlüssel auf Korrektheit: Er muss mindestens die Länge 1 + * haben und darf nur Zeichen von A-Z enthalten. + * + * @param key zu testender Schlüssel + * @return {@code true} if characters are ok, otherwise {@code false} + */ + protected boolean checkKey(String key) { + + if (key.length() != 1) { + return false; + } + + return checkCharacters(key, ALPHABET); + } + + /** + * Checks the given input to ensure that it only contains the given + * character set and no other characters. + * + * @param inputChars the string to be checked + * @param charSet a string containing all allowed characters + * @return {@code true} if characters are ok, otherwise {@code false} + */ + protected boolean checkCharacters(char[] inputChars, String charSet) { + + for (char c : inputChars) { + if (charSet.indexOf(c) == -1) { + return false; + } + } + + return true; + } + + /** + * Checks the given input to ensure that it only contains the given + * character set and no other characters. + * + * @param input the string to be checked + * @param charSet a string containing all allowed characters + * @return {@code true} if characters are ok, otherwise {@code false} + */ + protected boolean checkCharacters(String input, String charSet) { + return checkCharacters(input.toCharArray(), charSet); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalKeyException.java b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalKeyException.java new file mode 100644 index 0000000..469e5bf --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalKeyException.java @@ -0,0 +1,31 @@ +package pr2.exceptions.ausnahmen_testen; + +/** + * Ungültiger Schlüssel. + */ +public class IllegalKeyException extends Exception { + + /** + * Legt eine neue Ausnahme an. + */ + public IllegalKeyException() { + super(); + } + + /** + * Legt eine neue Ausnahme an. + * + * @param message die Nachricht. + */ + public IllegalKeyException(String message) { + super(message); + } + + /** + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + return getMessage(); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalMessageException.java b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalMessageException.java new file mode 100644 index 0000000..141a2b7 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/IllegalMessageException.java @@ -0,0 +1,31 @@ +package pr2.exceptions.ausnahmen_testen; + +/** + * Ungültige Nachricht. + */ +public class IllegalMessageException extends Exception { + + /** + * Legt eine neue Ausnahme an. + */ + public IllegalMessageException() { + super(); + } + + /** + * Legt eine neue Ausnahme an. + * + * @param message die Nachricht. + */ + public IllegalMessageException(String message) { + super(message); + } + + /** + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + return getMessage(); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/test/CaesarTest.java b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/test/CaesarTest.java new file mode 100644 index 0000000..edc2ebc --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/ausnahmen_testen/test/CaesarTest.java @@ -0,0 +1,149 @@ +package pr2.exceptions.ausnahmen_testen.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pr2.exceptions.ausnahmen_testen.CrypterCaesar; +import pr2.exceptions.ausnahmen_testen.IllegalKeyException; +import pr2.exceptions.ausnahmen_testen.IllegalMessageException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +/** + * Testklasse für die Verschlüsselung. + */ +public class CaesarTest { + + + /** + * Testet auf falschen Schlüssel. + * + * @throws pr2.exceptions.ausnahmen_testen.IllegalMessageException + * Wird geworfen, wenn die Nachricht ungültig ist. + * @throws pr2.exceptions.ausnahmen_testen.IllegalKeyException + * Wird geworfen, wenn der Schlüssel ungültig ist. + */ + @Test + void testWrongKey1() + throws IllegalKeyException, IllegalMessageException { + + String key = "AB"; + CrypterCaesar c = new CrypterCaesar(); + + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.encrypt(key, "HALLO"); + }); + } + + /** + * Testet auf falschen Schlüssel. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testWrongKey2() + throws IllegalKeyException, IllegalMessageException { + String key = "0"; + CrypterCaesar c = new CrypterCaesar(); + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.encrypt(key, "HALLO"); + }); + } + + /** + * Testet auf falsche Nachricht. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testWrongMessage1() + throws IllegalKeyException, IllegalMessageException { + String key = "K"; + CrypterCaesar c = new CrypterCaesar(); + + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.encrypt(key, "üäaD&"); + }); + } + + /** + * Testet auf falsche Nachricht. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testWrongMessage2() + throws IllegalKeyException, IllegalMessageException { + String key = "K"; + CrypterCaesar c = new CrypterCaesar(); + + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.encrypt(key, "üäaD&"); + }); + } + + /** + * Testet auf falsche Nachricht. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testWrongCypher1() + throws IllegalKeyException, IllegalMessageException { + String key = "K"; + CrypterCaesar c = new CrypterCaesar(); + + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.decrypt(key, "üäaD&"); + }); + } + + /** + * Testet auf falsche Nachricht. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testWrongCypher2() + throws IllegalKeyException, IllegalMessageException { + String key = "K"; + CrypterCaesar c = new CrypterCaesar(); + Assertions.assertThrows(IllegalKeyException.class, () -> { + c.decrypt(key, "DIESiSTEINTEST&"); + }); + } + + + /** + * Testet die Verschlüsselung an sich. + * + * @throws IllegalMessageException Wird geworfen, wenn die + * Nachricht ungültig ist. + * @throws IllegalKeyException Wird geworfen, wenn der Schlüssel + * ungültig ist. + */ + @Test + void testCaesar() + throws IllegalKeyException, IllegalMessageException { + String klarText = "KATHARGOMUSSFALLEN"; + CrypterCaesar c = new CrypterCaesar(); + assertEquals(klarText, c.decrypt("C", c.encrypt("C", klarText))); + assertEquals("FDHVDU", c.encrypt("C", "CAESAR")); + assertEquals(klarText, c.encrypt("Z", klarText)); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/Fuse.java b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/Fuse.java new file mode 100644 index 0000000..6f30f3e --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/Fuse.java @@ -0,0 +1,58 @@ +package pr2.exceptions.eigene_ausnahme; + +/** + * Eine Sicherung im Stromkreis. + */ +public class Fuse { + + /** + * 16 Ampere-Sicherung. + */ + public static final int A16 = 16; + + /** + * 25 Ampere-Sicherung. + */ + public static final int A25 = 25; + + /** + * 32 Ampere-Sicherung. + */ + public static final int A32 = 32; + + /** + * Strom, bei dem die Sicherung auslöst. + */ + private final int tripCurrent; + + /** + * Legt eine neue Sicherung an. + * + * @param tripCurrent Strom, bei dem die Sicherung auslösen soll. + * @throws IllegalCurrentException Ausnahme bei einem + * ungültigen Spannungswert. + */ + public Fuse(int tripCurrent) throws IllegalCurrentException { + switch (tripCurrent) { + case A16: + case A25: + case A32: + this.tripCurrent = tripCurrent; + break; + default: + throw new IllegalCurrentException(tripCurrent); + } + } + + /** + * Die Sicherung benutzen. + * + * @param current Strom, der durch die Sicherung fließt. + * @throws FuseTrippedException wird geworfen, wenn der Srom zu groß wird. + */ + public void use(int current) throws FuseTrippedException { + if (current > tripCurrent) { + throw new FuseTrippedException(current, tripCurrent); + } + } +} diff --git a/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/FuseTrippedException.java b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/FuseTrippedException.java new file mode 100644 index 0000000..82308a4 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/FuseTrippedException.java @@ -0,0 +1,37 @@ +package pr2.exceptions.eigene_ausnahme; + +/** + * Ausnahme, wenn die Sicherung auslöst. + */ +public class FuseTrippedException extends Exception { + /** + * Strom, bei dem die Sicherung ausgelöst hat. + */ + private final int current; + + /** + * Auslösestrom der Sicherung. + */ + private final int tripCurrent; + + /** + * Erzeugt eine neue Ausnahme. + * + * @param current Strom, bei dem die Sicherung ausgelöst hat. + * @param tripCurrent Auslösestrom der Sicherung. + */ + public FuseTrippedException(int current, int tripCurrent) { + super(); + this.current = current; + this.tripCurrent = tripCurrent; + } + + /** + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + return String.format("Maximaler Strom: %d, Auslösestrom: %d", + tripCurrent, current); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/IllegalCurrentException.java b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/IllegalCurrentException.java new file mode 100644 index 0000000..980783e --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/IllegalCurrentException.java @@ -0,0 +1,30 @@ +package pr2.exceptions.eigene_ausnahme; + +/** + * Ausnahme, wenn eine Sicherung angelegt werden soll, die es nicht gibt. + */ +public class IllegalCurrentException extends Exception { + + /** + * Übergebener Strom. + */ + private final int givenValue; + + /** + * Erzeugt eine neue Ausnahme. + * + * @param givenValue gegebener Strom. + */ + public IllegalCurrentException(int givenValue) { + super(); + this.givenValue = givenValue; + } + + /** + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + return String.format("Falscher Wert %d", givenValue); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/test/FuseTest.java b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/test/FuseTest.java new file mode 100644 index 0000000..1d37afb --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/eigene_ausnahme/test/FuseTest.java @@ -0,0 +1,158 @@ +package pr2.exceptions.eigene_ausnahme.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pr2.exceptions.eigene_ausnahme.Fuse; +import pr2.exceptions.eigene_ausnahme.FuseTrippedException; +import pr2.exceptions.eigene_ausnahme.IllegalCurrentException; + +/** + * Testet die Sicherung. + */ +public class FuseTest { + + + /** + * Testet die Erzeugung von Sicherungen mit gültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid() throws IllegalCurrentException { + new Fuse(Fuse.A16); + new Fuse(Fuse.A25); + new Fuse(Fuse.A32); + } + + /** + * Testet die Erzeugung von Sicherungen mit ungültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid1() throws IllegalCurrentException { + Assertions.assertThrows(IllegalCurrentException.class, () -> { + new Fuse(15); + }); + } + + /** + * Testet die Erzeugung von Sicherungen mit ungültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid2() throws IllegalCurrentException { + Assertions.assertThrows(IllegalCurrentException.class, () -> { + new Fuse(-1); + }); + } + + /** + * Testet die Erzeugung von Sicherungen mit ungültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid3() throws IllegalCurrentException { + Assertions.assertThrows(IllegalCurrentException.class, () -> { + new Fuse(0); + }); + } + + /** + * Testet die Erzeugung von Sicherungen mit ungültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid4() throws IllegalCurrentException { + Assertions.assertThrows(IllegalCurrentException.class, () -> { + new Fuse(Integer.MAX_VALUE); + }); + } + + /** + * Testet die Erzeugung von Sicherungen mit ungültigen Werten. + * + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testCreationValid5() throws IllegalCurrentException { + Assertions.assertThrows(IllegalCurrentException.class, () -> { + new Fuse(Integer.MIN_VALUE); + }); + } + + /** + * Testet das Auslösen der Sicherung. + * + * @throws FuseTrippedException Sicherung hat ausgelöst. + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testTripping1() + throws FuseTrippedException, IllegalCurrentException { + + Fuse f = new Fuse(Fuse.A16); + f.use(5); + f.use(16); + f.use(0); + + f = new Fuse(Fuse.A25); + f.use(5); + f.use(16); + f.use(25); + + + Assertions.assertThrows(FuseTrippedException.class, () -> { + Fuse f2 = new Fuse(Fuse.A32); + f2.use(0); + f2.use(16); + f2.use(25); + }); + } + + /** + * Testet das Auslösen der Sicherung. + * + * @throws FuseTrippedException Sicherung hat ausgelöst. + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testTripping2() + throws FuseTrippedException, IllegalCurrentException { + Assertions.assertThrows(FuseTrippedException.class, () -> { + new Fuse(Fuse.A16).use(17); + }); + } + + /** + * Testet das Auslösen der Sicherung. + * + * @throws FuseTrippedException Sicherung hat ausgelöst. + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testTripping3() + throws FuseTrippedException, IllegalCurrentException { + + Assertions.assertThrows(FuseTrippedException.class, () -> { + new Fuse(Fuse.A16).use(Integer.MAX_VALUE); + }); + } + + /** + * Testet das Auslösen der Sicherung. + * + * @throws FuseTrippedException Sicherung hat ausgelöst. + * @throws IllegalCurrentException ungültiger Wert für die Sicherung. + */ + @Test + void testTripping4() + throws FuseTrippedException, IllegalCurrentException { + Assertions.assertThrows(FuseTrippedException.class, () -> { + new Fuse(Fuse.A32).use(40); + }); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/fakultaet/Fakultaet.java b/solutions/src/main/java/pr2/exceptions/fakultaet/Fakultaet.java new file mode 100644 index 0000000..736edf6 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/fakultaet/Fakultaet.java @@ -0,0 +1,18 @@ +package pr2.exceptions.fakultaet; + +public class Fakultaet { + + public int fact(int n) throws FakultaetException { + if (n < 0) { + throw new FakultaetException("Keine negativen Zahlen!", n); + } + else if (n > 20) { + throw new FakultaetException("Keine Zahlen > 20", n); + } + return factIntern(n); + } + + private int factIntern(int n) { + return (n == 0) ? 1 : factIntern(n - 1) * n; + } +} diff --git a/solutions/src/main/java/pr2/exceptions/fakultaet/FakultaetException.java b/solutions/src/main/java/pr2/exceptions/fakultaet/FakultaetException.java new file mode 100644 index 0000000..5c47848 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/fakultaet/FakultaetException.java @@ -0,0 +1,15 @@ +package pr2.exceptions.fakultaet; + +public class FakultaetException extends Exception { + + private final int zahl; + + public FakultaetException(String message, int zahl) { + super(message); + this.zahl = zahl; + } + + public int getZahl() { + return zahl; + } +} diff --git a/solutions/src/main/java/pr2/exceptions/fakultaet/test/FakultaetTest.java b/solutions/src/main/java/pr2/exceptions/fakultaet/test/FakultaetTest.java new file mode 100644 index 0000000..3892e52 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/fakultaet/test/FakultaetTest.java @@ -0,0 +1,38 @@ +package pr2.exceptions.fakultaet.test; + +import org.junit.jupiter.api.Test; +import pr2.exceptions.fakultaet.Fakultaet; +import pr2.exceptions.fakultaet.FakultaetException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class FakultaetTest { + + @Test + void testFact() throws FakultaetException { + Fakultaet f = new Fakultaet(); + assertEquals(1, f.fact(0)); + assertEquals(1, f.fact(1)); + assertEquals(2, f.fact(2)); + assertEquals(6, f.fact(3)); + assertEquals(24, f.fact(4)); + assertEquals(3628800, f.fact(10)); + } + + @Test + void testFactEx1() { + assertThrows(FakultaetException.class, () -> { + Fakultaet f = new Fakultaet(); + f.fact(-1); + }); + } + + @Test + void testFactEx2() { + assertThrows(FakultaetException.class, () -> { + Fakultaet f = new Fakultaet(); + f.fact(21); + }); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/handle_or_declare/Buchstabenzaehler.java b/solutions/src/main/java/pr2/exceptions/handle_or_declare/Buchstabenzaehler.java new file mode 100644 index 0000000..895b5ab --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/handle_or_declare/Buchstabenzaehler.java @@ -0,0 +1,124 @@ +package pr2.exceptions.handle_or_declare; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Scanner; + +/** + * Anwendung, die die Buchstabenhäufigkeit in einem Text analysiert. + */ +public class Buchstabenzaehler { + + /** + * Einstiegspunkt in das Programm. + * + * @param args Kommandozeilenargumente + */ + public static void main(String[] args) { + Buchstabenzaehler bs = new Buchstabenzaehler(); + + try { + bs.run(); + } + catch (StatistikException e) { + System.out.println("Statistik-Fehler: " + e); + } + } + + /** + * Startet den Buchstabenzaehler. + * + * @throws StatistikException Fehler bei der Berechnung der + * Buchstabenhäufigkeit. + */ + private void run() throws StatistikException { + System.out.print("Bitte geben Sie den Dateinamen an: "); + Scanner scanner = new Scanner(System.in); + String dateiname = scanner.nextLine(); + + int[] statistik; + + + + + try { + statistik = parseFile("assets/" + dateiname); + printStatistik(statistik); + } + catch (FileNotFoundException e) { + System.out.println("Datei gibt es nicht"); + } + catch (IOException e) { + System.out.println("Fehler beim Zugriff auf die Datei: " + e); + } + finally { + scanner.close(); + } + } + + /** + * Druckt die Statistik aus. + * + * @param statistik Statistik + * @throws StatistikException Fehler bei den Eingabedaten + */ + private void printStatistik(int[] statistik) throws StatistikException { + + int summe = 0; + + for (int haeufigkeit : statistik) { + summe += haeufigkeit; + } + + double prozentSumme = 0.0; + + for (char c = 'a'; c <= 'z'; c++) { + int anzahl = statistik[c - 'a']; + double prozent = (double) anzahl / (double) summe * 100; + System.out.printf("%s: %.2f%% %n", "" + c, prozent); + + prozentSumme += prozent; + } + + if ((prozentSumme < 99.0) || (prozentSumme > 101.0)) { + throw new StatistikException(Double.toString(prozentSumme)); + } + + System.out.printf("Summe: %.2f%% %n", prozentSumme); + } + + /** + * Analysiert die übergebene Datei und berechnet die Buchstabenhäufigkeit. + * + * @param filename Dateiname + * @return die Buchstabenhäufigkeit. Das 'a' ist das erste Element, + * das 'b' das zweite etc. + * @throws IOException generelles IO-Problem + * @throws FileNotFoundException Datei gibt es nicht + */ + private int[] parseFile(String filename) + throws IOException, FileNotFoundException { + + int[] statistik = new int['z' - 'a' + 1]; + + BufferedReader br = new BufferedReader(new FileReader(filename)); + + String line; + + while ((line = br.readLine()) != null) { + char[] chars = line.toLowerCase().toCharArray(); + + for (char c : chars) { + if (('a' <= c) && (c <= 'z')) { + statistik[c - 'a']++; + } + } + } + + br.close(); + + return statistik; + } +} diff --git a/solutions/src/main/java/pr2/exceptions/handle_or_declare/StatistikException.java b/solutions/src/main/java/pr2/exceptions/handle_or_declare/StatistikException.java new file mode 100644 index 0000000..0d83170 --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/handle_or_declare/StatistikException.java @@ -0,0 +1,31 @@ +package pr2.exceptions.handle_or_declare; + +/** + * Ausnahme, wenn die Statisitk nicht konsistent ist. + */ +public class StatistikException extends Exception { + + /** + * Legt eine neue Ausnahme an. + */ + public StatistikException() { + super(); + } + + /** + * Legt eine neue Ausnahme an. + * + * @param message die Nachricht. + */ + public StatistikException(String message) { + super(message); + } + + /** + * @see java.lang.Throwable#toString() + */ + @Override + public String toString() { + return getMessage(); + } +} diff --git a/solutions/src/main/java/pr2/exceptions/try_catch/Zahlenraten.java b/solutions/src/main/java/pr2/exceptions/try_catch/Zahlenraten.java new file mode 100644 index 0000000..712287d --- /dev/null +++ b/solutions/src/main/java/pr2/exceptions/try_catch/Zahlenraten.java @@ -0,0 +1,67 @@ +package pr2.exceptions.try_catch; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Random; + +/** + * Ein simples Zahlenraten-Spiel. + */ +public final class Zahlenraten { + + /** + * Liest einen String von der Tastatur. + * + * @return der gelesene String + * @throws IOException Probleme mit der Console + */ + private static String readNumber() throws IOException { + BufferedReader br = + new BufferedReader(new InputStreamReader(System.in)); + return br.readLine(); + } + + /** + * Hauptmethode. + * + * @param args Kommandozeilenargumente + */ + public static void main(String[] args) { + + // zu ratende Zahl bestimmen + int zahl = new Random().nextInt(100) + 1; + + int versuche = 0; + + while (true) { + System.out.print("Bitte geben Sie eine Zahl ein: "); + + int geraten; + + try { + geraten = Integer.parseInt(readNumber()); + } + catch (IOException e) { + System.out.println("Problem mit der Konsole."); + break; + } + catch (NumberFormatException e) { + System.out.println("Bitte nur Zahlen eingeben!"); + continue; + } + + versuche++; + + if (geraten < zahl) { + System.out.println("Zu niedrig"); + } + else if (geraten > zahl) { + System.out.println("Zu hoch."); + } + else { + System.out.printf("Richtig in %d Versuchen", versuche); + break; + } + } + } diff --git a/solutions/src/main/java/pr2/generics/einfach/Liste.java b/solutions/src/main/java/pr2/generics/einfach/Liste.java new file mode 100644 index 0000000..25518e5 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/einfach/Liste.java @@ -0,0 +1,90 @@ +package pr2.generics.einfach; + +/** + * Eine einfache, verkettete Liste. + * + * @param Typ der gespeicherten Objekte + */ + +public class Liste { + + /** + * Referenz auf den ersten Knoten. + */ + private ListeNode first; + + /** + * Referenz auf den aktuellen Knoten. + */ + private ListeNode current; + + /** + * Fügt ein neues Element an das Ende der Liste an. + * + * @param data das Element + */ + public void add(T data) { + + ListeNode nextNode = new ListeNode<>(data); + + if (current == null) { + // Liste komplett leer + first = nextNode; + current = nextNode; + } + else { + current.next = nextNode; + current = nextNode; + } + } + + /** + * Liest das Element an der gegebenen Position. + * + * @param index Index, beginnend bei 0. + * @return Das Element oder {@code null}, wenn es nicht gefunden wurde. + */ + public T get(int index) { + int count = 0; + ListeNode node = first; + + while ((node != null) && (count < index)) { + node = node.next; + count++; + } + + if ((count == index) && (node != null)) { + return node.data; + } + else { + // index does not exist + return null; + } + } + + /** + * Löscht die Liste und entfernt alle Elemente. + */ + public void clear() { + first = null; + current = null; + } + + /** + * Liefert die Anzahl der Elemente der Liste zurück. + * + * @return die Anzahl der Elemente. + */ + public int size() { + int count = 0; + ListeNode node = first; + + while (node != null) { + node = node.next; + count++; + } + + return count; + } +} + diff --git a/solutions/src/main/java/pr2/generics/einfach/ListeNode.java b/solutions/src/main/java/pr2/generics/einfach/ListeNode.java new file mode 100644 index 0000000..b981cd2 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/einfach/ListeNode.java @@ -0,0 +1,30 @@ +package pr2.generics.einfach; + + +/** + * Interne Repräsentation der Knoten in der Liste. + * + * @param Typ der gespeicherten Daten. + */ +class ListeNode { + + /** + * Daten. + */ + T data; + + /** + * Referenz auf den nächsten Knoten. + */ + ListeNode next; + + /** + * Legt einen neuen Knoten an. + * + * @param data daten, die gespeichert werden + */ + ListeNode(T data) { + this.data = data; + } +} + diff --git a/solutions/src/main/java/pr2/generics/einfach/test/ListeTest.java b/solutions/src/main/java/pr2/generics/einfach/test/ListeTest.java new file mode 100644 index 0000000..b9d09bc --- /dev/null +++ b/solutions/src/main/java/pr2/generics/einfach/test/ListeTest.java @@ -0,0 +1,61 @@ +package pr2.generics.einfach.test; + +import org.junit.jupiter.api.Test; +import pr2.generics.einfach.Liste; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * Test für die Liste. + */ +public class ListeTest { + + /** + * Testet das Hinzufügen und Löschen der Liste. + */ + @Test + void testAddAndClear() { + Liste l = new Liste<>(); + + assertEquals(0, l.size()); + + l.add("Hallo"); + assertEquals(1, l.size()); + l.add("Hugo"); + assertEquals(2, l.size()); + l.add("Peter"); + l.add("Alfons"); + assertEquals(4, l.size()); + + l.clear(); + assertEquals(0, l.size()); + } + + /** + * Testet das Lesen von Elementen. + */ + @Test + void testGet() { + Liste l = new Liste<>(); + l.add("Hallo"); + l.add("Hugo"); + l.add("Peter"); + l.add("Alfons"); + + assertNull(l.get(-1)); + assertNull(l.get(4)); + assertEquals("Hallo", l.get(0)); + assertEquals("Hugo", l.get(1)); + assertEquals("Peter", l.get(2)); + assertEquals("Alfons", l.get(3)); + + assertEquals(4, l.size()); + l.clear(); + assertEquals(0, l.size()); + assertNull(l.get(0)); + assertNull(l.get(1)); + assertNull(l.get(2)); + assertNull(l.get(3)); + } +} diff --git a/solutions/src/main/java/pr2/generics/number_pair/Main.java b/solutions/src/main/java/pr2/generics/number_pair/Main.java new file mode 100644 index 0000000..fcbc53b --- /dev/null +++ b/solutions/src/main/java/pr2/generics/number_pair/Main.java @@ -0,0 +1,15 @@ +package pr2.generics.number_pair; + +public class Main { + public static void main(String[] args) { + var note = new Pair("Peter", 1); + var name = new Pair("Peter", "Meier"); + var tel = new NumberPair(621, 292122); + + System.out.printf("%s:%d\n", note.getFirst(), note.getSecond()); + + System.out.printf("%s %s\n", name.getFirst(), name.getSecond()); + + System.out.printf("%d %d\n", tel.getFirst(), tel.getSecond()); + } +} diff --git a/solutions/src/main/java/pr2/generics/number_pair/NumberPair.java b/solutions/src/main/java/pr2/generics/number_pair/NumberPair.java new file mode 100644 index 0000000..beaa2cf --- /dev/null +++ b/solutions/src/main/java/pr2/generics/number_pair/NumberPair.java @@ -0,0 +1,8 @@ +package pr2.generics.number_pair; + +public class NumberPair extends Pair { + + public NumberPair(T first, T second) { + super(first, second); + } +} diff --git a/solutions/src/main/java/pr2/generics/number_pair/Pair.java b/solutions/src/main/java/pr2/generics/number_pair/Pair.java new file mode 100644 index 0000000..63d841e --- /dev/null +++ b/solutions/src/main/java/pr2/generics/number_pair/Pair.java @@ -0,0 +1,20 @@ +package pr2.generics.number_pair; + +public class Pair { + + private final T first; + private final V second; + + public Pair(T first, V second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return first; + } + + public V getSecond() { + return second; + } +} diff --git a/solutions/src/main/java/pr2/generics/pair/Main.java b/solutions/src/main/java/pr2/generics/pair/Main.java new file mode 100644 index 0000000..2df11ce --- /dev/null +++ b/solutions/src/main/java/pr2/generics/pair/Main.java @@ -0,0 +1,17 @@ +package pr2.generics.pair; + +public class Main { + public static void main(String[] args) { + var note1 = new Pair("Peter", 1); + var note2 = new Pair("Frank", 3); + var note3 = new Pair("Sabine", 1); + + var name = new Pair("Peter", "Meier"); + + System.out.printf("%s:%d\n", note1.getFirst(), note1.getSecond()); + System.out.printf("%s:%d\n", note2.getFirst(), note1.getSecond()); + System.out.printf("%s:%d\n", note3.getFirst(), note1.getSecond()); + + System.out.printf("%s %s\n", name.getFirst(), name.getSecond()); + } +} diff --git a/solutions/src/main/java/pr2/generics/pair/Pair.java b/solutions/src/main/java/pr2/generics/pair/Pair.java new file mode 100644 index 0000000..8680eb1 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/pair/Pair.java @@ -0,0 +1,20 @@ +package pr2.generics.pair; + +public class Pair { + + private final T first; + private final V second; + + public Pair(T first, V second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return first; + } + + public V getSecond() { + return second; + } +} diff --git a/solutions/src/main/java/pr2/generics/pairlist/Main.java b/solutions/src/main/java/pr2/generics/pairlist/Main.java new file mode 100644 index 0000000..d414e41 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/pairlist/Main.java @@ -0,0 +1,19 @@ +package pr2.generics.pairlist; + +public class Main { + public static void main(String[] args) { + var note1 = new Pair("Peter", 1); + var note2 = new Pair("Frank", 3); + var note3 = new Pair("Sabine", 1); + + var pl = new PairList(3); + pl.add(note1); + pl.add(note2); + pl.add(note3); + + for (int i = 0; i < 3; i++) { + System.out.printf("%s:%d\n", pl.get(i).getFirst(), + pl.get(i).getSecond()); + } + } +} diff --git a/solutions/src/main/java/pr2/generics/pairlist/Pair.java b/solutions/src/main/java/pr2/generics/pairlist/Pair.java new file mode 100644 index 0000000..3fbbfdf --- /dev/null +++ b/solutions/src/main/java/pr2/generics/pairlist/Pair.java @@ -0,0 +1,20 @@ +package pr2.generics.pairlist; + +public class Pair { + + private final T first; + private final V second; + + public Pair(T first, V second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return first; + } + + public V getSecond() { + return second; + } +} diff --git a/solutions/src/main/java/pr2/generics/pairlist/PairList.java b/solutions/src/main/java/pr2/generics/pairlist/PairList.java new file mode 100644 index 0000000..0d6a7e1 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/pairlist/PairList.java @@ -0,0 +1,20 @@ +package pr2.generics.pairlist; + +public class PairList { + + protected final Pair[] elements; + protected int pos; + + @SuppressWarnings("unchecked") + public PairList(int size) { + elements = (Pair[]) new Pair[size]; + } + + public void add(Pair element) { + elements[pos++] = element; + } + + public Pair get(int index) { + return elements[index]; + } +} diff --git a/solutions/src/main/java/pr2/generics/printer/CollectionPrinter.java b/solutions/src/main/java/pr2/generics/printer/CollectionPrinter.java new file mode 100644 index 0000000..4c895ab --- /dev/null +++ b/solutions/src/main/java/pr2/generics/printer/CollectionPrinter.java @@ -0,0 +1,11 @@ +package pr2.generics.printer; + +import java.util.Collection; + +public class CollectionPrinter { + public static void print(Collection collection) { + for (Object object : collection) { + System.out.println(object); + } + } +} diff --git a/solutions/src/main/java/pr2/generics/printer/Main.java b/solutions/src/main/java/pr2/generics/printer/Main.java new file mode 100644 index 0000000..4f162d5 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/printer/Main.java @@ -0,0 +1,23 @@ +package pr2.generics.printer; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Main { + public static void main(String[] args) { + List ls = new ArrayList<>(); + List li = new ArrayList<>(); + ls.add("PR2"); + ls.add("ist"); + ls.add("cool"); + Set s = new HashSet<>(ls); + li.add(23); + li.add(42); + + CollectionPrinter.print(ls); + CollectionPrinter.print(li); + CollectionPrinter.print(s); + } +} diff --git a/solutions/src/main/java/pr2/generics/queue/Queue.java b/solutions/src/main/java/pr2/generics/queue/Queue.java new file mode 100644 index 0000000..258604a --- /dev/null +++ b/solutions/src/main/java/pr2/generics/queue/Queue.java @@ -0,0 +1,39 @@ +package pr2.generics.queue; + +public class Queue { + + private final T[] elements; + private int pos = 0; + private int pos2 = 0; + + @SuppressWarnings("unchecked") + public Queue(int size) { + elements = (T[]) new Object[size]; + } + + public void offer(T element) { + elements[pos] = element; + pos++; + pos = pos % elements.length; + } + + public T poll() { + T element = elements[pos2]; + elements[pos2] = null; + pos2++; + pos2 = pos2 % elements.length; + return element; + } + + public void addAll(Queue other) { + for (T t : other.elements) { + offer(t); + } + } + + public void copyInto(Queue other) { + for (T t : elements) { + other.offer(t); + } + } +} diff --git a/solutions/src/main/java/pr2/generics/same_pair/Main.java b/solutions/src/main/java/pr2/generics/same_pair/Main.java new file mode 100644 index 0000000..0b85368 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/same_pair/Main.java @@ -0,0 +1,15 @@ +package pr2.generics.same_pair; + +public class Main { + public static void main(String[] args) { + var note = new Pair("Peter", 1); + var name = new SamePair("Peter", "Meier"); + var tel = new SamePair(621, 2009992); + + System.out.printf("%s:%d\n", note.getFirst(), note.getSecond()); + + System.out.printf("%s %s\n", name.getFirst(), name.getSecond()); + + System.out.printf("%d %d\n", tel.getFirst(), tel.getSecond()); + } +} diff --git a/solutions/src/main/java/pr2/generics/same_pair/Pair.java b/solutions/src/main/java/pr2/generics/same_pair/Pair.java new file mode 100644 index 0000000..587225a --- /dev/null +++ b/solutions/src/main/java/pr2/generics/same_pair/Pair.java @@ -0,0 +1,20 @@ +package pr2.generics.same_pair; + +public class Pair { + + private final T first; + private final V second; + + public Pair(T first, V second) { + this.first = first; + this.second = second; + } + + public T getFirst() { + return first; + } + + public V getSecond() { + return second; + } +} diff --git a/solutions/src/main/java/pr2/generics/same_pair/SamePair.java b/solutions/src/main/java/pr2/generics/same_pair/SamePair.java new file mode 100644 index 0000000..32323a8 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/same_pair/SamePair.java @@ -0,0 +1,8 @@ +package pr2.generics.same_pair; + +public class SamePair extends Pair { + + public SamePair(T first, T second) { + super(first, second); + } +} diff --git a/solutions/src/main/java/pr2/generics/super_extends/Liste.java b/solutions/src/main/java/pr2/generics/super_extends/Liste.java new file mode 100644 index 0000000..26cce2b --- /dev/null +++ b/solutions/src/main/java/pr2/generics/super_extends/Liste.java @@ -0,0 +1,108 @@ +package pr2.generics.super_extends; + +/** + * Eine einfache, verkettete Liste. + * + * @param Typ der gespeicherten Objekte + */ +public class Liste { + + /** + * Referenz auf den ersten Knoten. + */ + private ListeNode first; + + /** + * Referenz auf den aktuellen Knoten. + */ + private ListeNode current; + + /** + * Fügt ein neues Element an das Ende der Liste an. + * + * @param data das Element + */ + public void add(T data) { + + ListeNode nextNode = new ListeNode<>(data); + + if (current == null) { + // Liste komplett leer + first = nextNode; + current = nextNode; + } + else { + current.next = nextNode; + current = nextNode; + } + } + + /** + * Liest das Element an der gegebenen Position. + * + * @param index Index, beginnend bei 0. + * @return Das Element oder {@code null}, wenn es nicht gefunden wurde. + */ + public T get(int index) { + int count = 0; + ListeNode node = first; + + while ((node != null) && (count < index)) { + node = node.next; + count++; + } + + if ((count == index) && (node != null)) { + return node.data; + } + else { + // index does not exist + return null; + } + } + + /** + * Löscht die Liste und entfernt alle Elemente. + */ + public void clear() { + first = null; + current = null; + } + + /** + * Liefert die Anzahl der Elemente der Liste zurück. + * + * @return die Anzahl der Elemente. + */ + public int size() { + int count = 0; + ListeNode node = first; + + while (node != null) { + node = node.next; + count++; + } + + return count; + } + + /** + * Kopiert alle Elemente dieser Liste in die andere Liste. + * + * @param other die andere Liste. + */ + public void copyInto(Liste other) { + for (int i = 0; i < size(); i++) { + other.add(get(i)); + } + } + + /** + * Füllt diese Liste mit dem Inhalt der übergebenen Liste. + * + * @param other die andere Liste. + */ + public void fillFrom(Liste other) { + other.copyInto(this); + } +} diff --git a/solutions/src/main/java/pr2/generics/super_extends/ListeNode.java b/solutions/src/main/java/pr2/generics/super_extends/ListeNode.java new file mode 100644 index 0000000..487f1b1 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/super_extends/ListeNode.java @@ -0,0 +1,28 @@ +package pr2.generics.super_extends; + +/** + * Interne Repräsentation der Knoten in der Liste. + * + * @param Typ der gespeicherten Daten. + */ +class ListeNode { + + /** + * Daten. + */ + T data; + + /** + * Referenz auf den nächsten Knoten. + */ + ListeNode next; + + /** + * Legt einen neuen Knoten an. + * + * @param data daten, die gespeichert werden + */ + ListeNode(T data) { + this.data = data; + } +} \ No newline at end of file diff --git a/solutions/src/main/java/pr2/generics/super_extends/test/ListeTest.java b/solutions/src/main/java/pr2/generics/super_extends/test/ListeTest.java new file mode 100644 index 0000000..84997ad --- /dev/null +++ b/solutions/src/main/java/pr2/generics/super_extends/test/ListeTest.java @@ -0,0 +1,42 @@ +package pr2.generics.super_extends.test; + +import org.junit.jupiter.api.Test; +import pr2.generics.super_extends.Liste; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +/** + * Test für die Liste. + */ +public class ListeTest { + + /** + * Testet das Umkopieren. + */ + @Test + void testAddAndClear() { + Liste ls = new Liste<>(); + Liste lo = new Liste<>(); + + ls.add("Hallo"); + ls.add("Hugo"); + ls.add("Peter"); + ls.add("Alfons"); + + ls.copyInto(lo); + + assertEquals("Hallo", lo.get(0)); + assertEquals("Hugo", lo.get(1)); + assertEquals("Peter", lo.get(2)); + assertEquals("Alfons", lo.get(3)); + + lo = new Liste<>(); + lo.fillFrom(ls); + + assertEquals("Hallo", lo.get(0)); + assertEquals("Hugo", lo.get(1)); + assertEquals("Peter", lo.get(2)); + assertEquals("Alfons", lo.get(3)); + } +} diff --git a/solutions/src/main/java/pr2/generics/verwenden/GameBoard.java b/solutions/src/main/java/pr2/generics/verwenden/GameBoard.java new file mode 100644 index 0000000..9de262e --- /dev/null +++ b/solutions/src/main/java/pr2/generics/verwenden/GameBoard.java @@ -0,0 +1,169 @@ +package pr2.generics.verwenden; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.SimpleImage; +import de.smits_net.games.framework.sprite.Sprite; +import de.smits_net.games.framework.sprite.Sprite.BoundaryPolicy; +import de.smits_net.games.framework.sprite.Velocity; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseEvent; +import java.util.Random; +import java.util.Stack; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Zufallszahlengenerator. + */ + private final Random rnd = new Random(); + + /** + * Münzstapel. + */ + private final Stack stapel = new Stack<>(); + + /** + * A moving coin. + */ + private Sprite moving; + + /** + * Punkte. + */ + private int points = 0; + + /** + * Zeitpunkt des ersten Klicks. + */ + private long startzeit; + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(400, 400), Color.BLACK); + + // Münzen anlegen + for (int i = 0; i < 20; i++) { + stapel.push(createCoin()); + } + } + + /** + * Legt eine zufällige Münze an. + * + * @return die Münze als Sprite. + */ + private Sprite createCoin() { + final String prefix = "pr2/generics/verwendung"; + + String asset; + + switch (rnd.nextInt(8)) { + case 0: + asset = prefix + "/1c.png"; + break; + case 1: + asset = prefix + "/2c.png"; + break; + case 3: + asset = prefix + "/5c.png"; + break; + case 4: + asset = prefix + "/10c.png"; + break; + case 5: + asset = prefix + "/20c.png"; + break; + case 6: + asset = prefix + "/50c.png"; + break; + case 7: + asset = prefix + "/1e.png"; + break; + default: + asset = prefix + "/2e.png"; + break; + } + + int offset = rnd.nextInt(10); + + return new Sprite(this, new Point(100 + offset, 100 + offset), + BoundaryPolicy.NONE, new SimpleImage(asset)); + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public synchronized void drawGame(Graphics g) { + for (Sprite sprite : stapel) { + sprite.draw(g, this); + } + + if (moving != null) { + moving.draw(g, this); + } + + writeText(g, 0, 20, "Punkte: " + points); + } + + /** + * Spielfeld zeichnen. + * + * @param g Grafik-Kontext. + */ + @Override + protected void drawGameOver(Graphics g) { + centerText(g, String.format("%d Punkte in %.2f Sekunden", points, + (System.currentTimeMillis() - startzeit) / 1000.0)); + } + + /** + * Mausklick abfangen. + * + * @param e Maus-Event. + */ + @Override + public synchronized void mouseClicked(MouseEvent e) { + + if (startzeit == 0) { + startzeit = System.currentTimeMillis(); + } + + if (stapel.isEmpty()) { + return; + } + + // Oberstes Sprite vom Stapel ansehen und s zuweisen + Sprite s = stapel.peek(); + + if (s.intersects(new Point(e.getX(), e.getY()))) { + points++; + s = stapel.pop(); + moving = s; + moving.setVelocity(new Velocity(0, 20)); + } + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + + if (moving != null) { + moving.move(); + } + + return !stapel.isEmpty(); + } +} diff --git a/solutions/src/main/java/pr2/generics/verwenden/GameMain.java b/solutions/src/main/java/pr2/generics/verwenden/GameMain.java new file mode 100644 index 0000000..c305e4a --- /dev/null +++ b/solutions/src/main/java/pr2/generics/verwenden/GameMain.java @@ -0,0 +1,28 @@ +package pr2.generics.verwenden; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click the coins as fast as you can", new GameBoard()); + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + // Spiel starten + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/generics/wildcards/ListHelper.java b/solutions/src/main/java/pr2/generics/wildcards/ListHelper.java new file mode 100644 index 0000000..a6aba36 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/wildcards/ListHelper.java @@ -0,0 +1,35 @@ +package pr2.generics.wildcards; + +/** + * Klasse zum Ausdrucken von Listen. + */ +public class ListHelper { + + /** + * Druckt alle Elemente der Liste auf der Konsole aus. + * + * @param liste Die zu druckende Liste. + */ + public static void printList(SimpleList liste) { + for (Object o : liste) { + System.out.println(o); + } + } + + /** + * Summiert die Elemente der Liste. Deswegen können nur Listen übergeben + * werden, deren Elemente mindestens vom Typ {@code Number} sind. + * + * @param liste Die Liste, deren Elemente summiert werden sollen. + * @return das Ergebnis + */ + public static double sumList(SimpleList liste) { + double sum = 0; + + for (Number number : liste) { + sum += number.doubleValue(); + } + + return sum; + } +} diff --git a/solutions/src/main/java/pr2/generics/wildcards/Main.java b/solutions/src/main/java/pr2/generics/wildcards/Main.java new file mode 100644 index 0000000..6401bdc --- /dev/null +++ b/solutions/src/main/java/pr2/generics/wildcards/Main.java @@ -0,0 +1,39 @@ +package pr2.generics.wildcards; + +/** + * Ausgabe der Liste. + */ +public final class Main { + + /** + * Constructor. + */ + private Main() { + // keine Instanzen + } + + /** + * Haupt-Methode. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + SimpleList l1 = new SimpleList<>(); + l1.add("Dies"); + l1.add("ist"); + l1.add("ein"); + l1.add("Test"); + l1.add("."); + + SimpleList l2 = new SimpleList<>(); + l2.add(1); + l2.add(2); + l2.add(3); + l2.add(4); + l2.add(5); + + ListHelper.printList(l1); + ListHelper.printList(l2); + System.out.println(ListHelper.sumList(l2)); + } +} diff --git a/solutions/src/main/java/pr2/generics/wildcards/SimpleList.java b/solutions/src/main/java/pr2/generics/wildcards/SimpleList.java new file mode 100644 index 0000000..b4241c1 --- /dev/null +++ b/solutions/src/main/java/pr2/generics/wildcards/SimpleList.java @@ -0,0 +1,12 @@ +package pr2.generics.wildcards; + +import java.util.ArrayList; + +/** + * Listenklasse. + * + * @param Typ, der in der Liste gespeichert wird. + */ +public class SimpleList extends ArrayList { + // nothing here +} diff --git a/solutions/src/main/java/pr2/interfaces/abstrakt_form/Form.java b/solutions/src/main/java/pr2/interfaces/abstrakt_form/Form.java new file mode 100644 index 0000000..6ea1f70 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/abstrakt_form/Form.java @@ -0,0 +1,16 @@ +package pr2.interfaces.abstrakt_form; + +public abstract class Form { + + protected double x; + protected double y; + + public Form(double x, double y) { + this.x = x; + this.y = y; + } + + public abstract double flaeche(); + + public abstract double umfang(); +} diff --git a/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Dreieck.java b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Dreieck.java new file mode 100644 index 0000000..a11edee --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Dreieck.java @@ -0,0 +1,39 @@ +package pr2.interfaces.abstrakte_klassen_s; + +/** + * Ein Dreieck. + */ +public class Dreieck extends Figur { + + /** + * Grundseite des Dreiecks. + */ + private final double grundseite; + + /** + * Höhe. + */ + private final double hoehe; + + /** + * Erzeugt ein neues Dreieck. + * + * @param grundseite Grundseite. + * @param hoehe Höhe. + */ + public Dreieck(double grundseite, double hoehe) { + super(); + this.grundseite = grundseite; + this.hoehe = hoehe; + } + + /** + * Berechnet die Fläche. + * + * @return die Fläche. + */ + @Override + public double flaeche() { + return grundseite * hoehe / 2; + } +} diff --git a/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Figur.java b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Figur.java new file mode 100644 index 0000000..a0351f2 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Figur.java @@ -0,0 +1,14 @@ +package pr2.interfaces.abstrakte_klassen_s; + +/** + * Basisklasse für geometrische Figuren. + */ +public abstract class Figur { + + /** + * Berechnet die Fläche einer geometrischen Figur. + * + * @return die Fläche. + */ + public abstract double flaeche(); +} diff --git a/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Geometer.java b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Geometer.java new file mode 100644 index 0000000..3257c45 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Geometer.java @@ -0,0 +1,43 @@ +package pr2.interfaces.abstrakte_klassen_s; + +/** + * Flächenberechnung. + */ +public final class Geometer { + + /** + * Keine Instanzen. + */ + private Geometer() { + // keine Objekte + } + + /** + * Hauptmethode. + * + * @param args Kommandozeilenargumente + */ + public static void main(String[] args) { + Figur[] figuren = new Figur[10]; + + figuren[0] = new Kreis(10.0); + figuren[1] = new Kreis(5.5); + figuren[2] = new Kreis(3.3); + figuren[3] = new Kreis(7.2); + figuren[4] = new Kreis(8.0); + + figuren[5] = new Dreieck(4.0, 3.0); + figuren[6] = new Dreieck(2.0, 1.0); + figuren[7] = new Dreieck(5.0, 8.0); + figuren[8] = new Dreieck(2.0, 7.2); + figuren[9] = new Dreieck(14.0, 3.8); + + double gesamtflaeche = 0.0; + + for (Figur figur : figuren) { + gesamtflaeche += figur.flaeche(); + } + + System.out.printf("Die Fläche beträgt: %.2f", gesamtflaeche); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Kreis.java b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Kreis.java new file mode 100644 index 0000000..86bf749 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/abstrakte_klassen_s/Kreis.java @@ -0,0 +1,32 @@ +package pr2.interfaces.abstrakte_klassen_s; + +/** + * Ein Kreis. + */ +public class Kreis extends Figur { + + /** + * Radius des Kreises. + */ + private final double radius; + + /** + * Legt einen neuen Kreis an. + * + * @param radius Radius des Kreises. + */ + public Kreis(double radius) { + super(); + this.radius = radius; + } + + /** + * Berechnet die Fläche. + * + * @return die Fläche. + */ + @Override + public double flaeche() { + return Math.PI * radius * radius; + } +} diff --git a/solutions/src/main/java/pr2/interfaces/comparable_student/Student.java b/solutions/src/main/java/pr2/interfaces/comparable_student/Student.java new file mode 100644 index 0000000..13ab812 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/comparable_student/Student.java @@ -0,0 +1,42 @@ +package pr2.interfaces.comparable_student; + +public class Student implements Comparable { + + private final String vorname; + private final String nachname; + private final int matrikelNr; + + public Student(String vorname, String nachname, + int matrikelNr) { + this.vorname = vorname; + this.nachname = nachname; + this.matrikelNr = matrikelNr; + } + + @Override + public String toString() { + return String.format("%s, %s (%d)", nachname, vorname, + matrikelNr); + } + + @Override + public int compareTo(Object o) { + + Student other = (Student) o; + + if (nachname.compareTo(other.nachname) == 0) { + if (vorname.compareTo(other.vorname) == 0) { + if (matrikelNr == other.matrikelNr) { + return 0; + } + else { + return (matrikelNr > other.matrikelNr) ? 1 : -1; + } + } + + return vorname.compareTo(other.vorname); + } + + return nachname.compareTo(other.nachname); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/api/AnalogController.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/api/AnalogController.java new file mode 100644 index 0000000..5cea5ce --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/api/AnalogController.java @@ -0,0 +1,44 @@ +package pr2.interfaces.interfaces_s.api; + +import java.awt.Point; + +/** + * Analoger Joystick. + */ +public interface AnalogController { + + /** + * Nach oben bewegen. + * + * @param percentage Auslenkung in Prozent (zwischen 0 - 1) + */ + void up(double percentage); + + /** + * Nach unten bewegen. + * + * @param percentage Auslenkung in Prozent (zwischen 0 - 1) + */ + void down(double percentage); + + /** + * Nach links bewegen. + * + * @param percentage Auslenkung in Prozent (zwischen 0 - 1) + */ + void left(double percentage); + + /** + * Nach rechts bewegen. + * + * @param percentage Auslenkung in Prozent (zwischen 0 - 1) + */ + void right(double percentage); + + /** + * Liefert die aktuelle Position zurück. + * + * @return Die Position. + */ + Point getPosition(); +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/api/DigitalController.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/api/DigitalController.java new file mode 100644 index 0000000..ec760b9 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/api/DigitalController.java @@ -0,0 +1,36 @@ +package pr2.interfaces.interfaces_s.api; + +import java.awt.Point; + +/** + * Ein digitaler Joystick. + */ +public interface DigitalController { + + /** + * Nach oben bewegen. + */ + void up(); + + /** + * Nach unten bewegen. + */ + void down(); + + /** + * Nach links bewegen. + */ + void left(); + + /** + * Nach rechts bewegen. + */ + void right(); + + /** + * Liefert die aktuelle Position zurück. + * + * @return Die Position. + */ + Point getPosition(); +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/AnalogControllerImpl.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/AnalogControllerImpl.java new file mode 100644 index 0000000..e6241ee --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/AnalogControllerImpl.java @@ -0,0 +1,61 @@ +package pr2.interfaces.interfaces_s.impl; + +import pr2.interfaces.interfaces_s.api.AnalogController; + +import java.awt.Point; + +/** + * Analoger Joystick. + */ +public class AnalogControllerImpl implements AnalogController { + + /** + * Auslenkung in X-Richtung. + */ + private double deltaX; + + /** + * Auslenkung in Y-Richtung. + */ + private double deltaY; + + /** + * @see AnalogController#up(double) + */ + @Override + public void up(double percentage) { + deltaY -= percentage; + } + + /** + * @see AnalogController#down(double) + */ + @Override + public void down(double percentage) { + deltaY += percentage; + } + + /** + * @see AnalogController#left(double) + */ + @Override + public void left(double percentage) { + deltaX -= percentage; + } + + /** + * @see AnalogController#right(double) + */ + @Override + public void right(double percentage) { + deltaX += percentage; + } + + /** + * @see AnalogController#getPosition() + */ + @Override + public Point getPosition() { + return new Point((int) deltaX, (int) deltaY); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DigitalControllerImpl.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DigitalControllerImpl.java new file mode 100644 index 0000000..2007280 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DigitalControllerImpl.java @@ -0,0 +1,55 @@ +package pr2.interfaces.interfaces_s.impl; + +import pr2.interfaces.interfaces_s.api.DigitalController; + +import java.awt.Point; + +/** + * Implementierung eines digitalen Joysticks. + */ +public class DigitalControllerImpl implements DigitalController { + + private final Point position = new Point(0, 0); + + /** + * @see DigitalController#up() + */ + @Override + public void up() { + position.translate(0, -1); + } + + /** + * @see DigitalController#down() + */ + @Override + public void down() { + position.translate(0, 1); + } + + /** + * @see DigitalController#left() + */ + @Override + public void left() { + position.translate(-1, 0); + } + + /** + * @see DigitalController#right() + */ + @Override + public void right() { + position.translate(1, 0); + } + + /** + * @see DigitalController#getPosition() + */ + @Override + public Point getPosition() { + return (Point) position.clone(); + } + + +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DualShock4.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DualShock4.java new file mode 100644 index 0000000..35c2b8f --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/impl/DualShock4.java @@ -0,0 +1,95 @@ +package pr2.interfaces.interfaces_s.impl; + +import pr2.interfaces.interfaces_s.api.AnalogController; +import pr2.interfaces.interfaces_s.api.DigitalController; + +import java.awt.Point; + +/** + * Linke Seite eines DualShock 4 Controllers. + */ +public class DualShock4 implements AnalogController, DigitalController { + + private final AnalogController analogStick = new AnalogControllerImpl(); + + private final DigitalController digitalStick = new DigitalControllerImpl(); + + /** + * @see DigitalController#up() + */ + @Override + public void up() { + digitalStick.up(); + } + + /** + * @see DigitalController#down() + */ + @Override + public void down() { + digitalStick.down(); + } + + /** + * @see DigitalController#left() + */ + @Override + public void left() { + digitalStick.left(); + } + + /** + * @see DigitalController#right() + */ + @Override + public void right() { + digitalStick.right(); + } + + /** + * @see AnalogController#up(double) + */ + @Override + public void up(double percentage) { + analogStick.up(percentage); + } + + /** + * @see AnalogController#down(double) + */ + @Override + public void down(double percentage) { + analogStick.down(percentage); + } + + /** + * @see AnalogController#left(double) + */ + @Override + public void left(double percentage) { + analogStick.left(percentage); + } + + /** + * @see AnalogController#right(double) + */ + @Override + public void right(double percentage) { + analogStick.right(percentage); + } + + /** + * @see AnalogController#getPosition() + */ + @Override + public Point getPosition() { + Point resultAnalog = analogStick.getPosition(); + Point resultDigital = digitalStick.getPosition(); + Point result = new Point(); + + result.x = resultAnalog.x + resultDigital.x; + result.y = resultAnalog.y + resultDigital.y; + + return result; + } +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/test/AnalogControllerTest.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/AnalogControllerTest.java new file mode 100644 index 0000000..65dd577 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/AnalogControllerTest.java @@ -0,0 +1,52 @@ +package pr2.interfaces.interfaces_s.test; + +import org.junit.jupiter.api.Test; +import pr2.interfaces.interfaces_s.api.AnalogController; +import pr2.interfaces.interfaces_s.impl.AnalogControllerImpl; + +/** + * Tests für einen analogen Joystick. + */ +public class AnalogControllerTest extends DigitalControllerTest { + + /** + * Testet einen anlogen Controller. + */ + @Test + void testAnalogController() { + AnalogController c = new AnalogControllerImpl(); + testAnalogController(c); + } + + /** + * Interne Hilfsmethode zum Test von analogen Controllern. + * + * @param c Controller, der getestet wird + */ + protected void testAnalogController(AnalogController c) { + c.up(0.5); + assertPointEquals(0, 0, c.getPosition()); + c.up(0.5); + assertPointEquals(0, -1, c.getPosition()); + + c.left(0.3); + c.left(0.3); + c.left(0.3); + assertPointEquals(0, -1, c.getPosition()); + c.left(0.2); + assertPointEquals(-1, -1, c.getPosition()); + + c.down(1.0); + assertPointEquals(-1, 0, c.getPosition()); + + c.down(1.0); + assertPointEquals(-1, 1, c.getPosition()); + c.down(1.0); + assertPointEquals(-1, 2, c.getPosition()); + + c.right(0.5); + c.right(0.5); + c.right(0.5); + assertPointEquals(0, 2, c.getPosition()); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DigitalControllerTest.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DigitalControllerTest.java new file mode 100644 index 0000000..a533be7 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DigitalControllerTest.java @@ -0,0 +1,62 @@ +package pr2.interfaces.interfaces_s.test; + +import org.junit.jupiter.api.Test; +import pr2.interfaces.interfaces_s.impl.DigitalControllerImpl; +import pr2.interfaces.interfaces_s.api.DigitalController; + +import java.awt.Point; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests für einen digitalen Joystick. + */ +public class DigitalControllerTest { + + /** + * Testet einen digitalen Controller. + */ + @Test + void testDigitalController() { + DigitalController c = new DigitalControllerImpl(); + testDigitalController(c); + } + + /** + * Interne Hilfsmethode zum Test von digitalen Controllern. + * + * @param c Controller, der getestet wird + */ + protected void testDigitalController(DigitalController c) { + assertPointEquals(0, 0, c.getPosition()); + c.up(); + c.up(); + assertPointEquals(0, -2, c.getPosition()); + c.down(); + c.down(); + assertPointEquals(0, 0, c.getPosition()); + c.left(); + c.right(); + assertPointEquals(0, 0, c.getPosition()); + c.left(); + assertPointEquals(-1, 0, c.getPosition()); + c.left(); + assertPointEquals(-2, 0, c.getPosition()); + c.right(); + c.right(); + c.right(); + assertPointEquals(1, 0, c.getPosition()); + } + + /** + * Interne Hilfsmethode, um einen Punkt mit seinem x und + * y-Wert zu vergleichen. + * + * @param x x-Wert + * @param y y-Wert + * @param point Punkt, der verglichen werden soll + */ + protected void assertPointEquals(int x, int y, Point point) { + assertEquals(new Point(x, y), point); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DualShock4Test.java b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DualShock4Test.java new file mode 100644 index 0000000..9f4bb3d --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/interfaces_s/test/DualShock4Test.java @@ -0,0 +1,45 @@ +package pr2.interfaces.interfaces_s.test; + +import org.junit.jupiter.api.Test; +import pr2.interfaces.interfaces_s.api.AnalogController; +import pr2.interfaces.interfaces_s.api.DigitalController; +import pr2.interfaces.interfaces_s.impl.DualShock4; + + +/** + * Tests für einen analogen Joystick. + */ +public class DualShock4Test extends AnalogControllerTest { + + /** + * Testet einen DualShock4 Kombi-Controller. + */ + @Test + void testDualShock4() { + DigitalController cd = new DualShock4(); + testDigitalController(cd); + + AnalogController ca = new DualShock4(); + testAnalogController(ca); + + DualShock4 ds = new DualShock4(); + + ds.up(); + ds.up(1.0); + assertPointEquals(0, -2, ds.getPosition()); + + ds.left(); + ds.right(); + ds.right(0.5); + ds.right(0.5); + assertPointEquals(1, -2, ds.getPosition()); + + ds.down(); + ds.down(); + ds.down(0.3); + ds.down(0.3); + ds.down(0.3); + ds.down(0.1); + assertPointEquals(1, 1, ds.getPosition()); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/stack/Stack.java b/solutions/src/main/java/pr2/interfaces/stack/Stack.java new file mode 100644 index 0000000..f9e4e14 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/stack/Stack.java @@ -0,0 +1,12 @@ +package pr2.interfaces.stack; + +public interface Stack { + + void push(Object o); + + Object pop(); + + Object peek(); + + int size(); +} diff --git a/solutions/src/main/java/pr2/interfaces/stack/StackImpl.java b/solutions/src/main/java/pr2/interfaces/stack/StackImpl.java new file mode 100644 index 0000000..2d286e0 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/stack/StackImpl.java @@ -0,0 +1,43 @@ +package pr2.interfaces.stack; + +public class StackImpl implements Stack { + + private final Object[] puffer; + private int pos = -1; + + public StackImpl(int size) { + puffer = new Object[size]; + } + + @Override + public void push(Object o) { + if (pos < puffer.length - 1) { + puffer[++pos] = o; + } + } + + @Override + public Object pop() { + if (pos >= 0) { + return puffer[pos--]; + } + else { + return null; + } + } + + @Override + public Object peek() { + if (pos >= 0) { + return puffer[pos]; + } + else { + return null; + } + } + + @Override + public int size() { + return pos + 1; + } +} diff --git a/solutions/src/main/java/pr2/interfaces/stack/test/StackTest.java b/solutions/src/main/java/pr2/interfaces/stack/test/StackTest.java new file mode 100644 index 0000000..4c1534f --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/stack/test/StackTest.java @@ -0,0 +1,35 @@ +package pr2.interfaces.stack.test; + +import org.junit.jupiter.api.Test; +import pr2.interfaces.stack.Stack; +import pr2.interfaces.stack.StackImpl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class StackTest { + + @Test + void testStack() { + Stack s = new StackImpl(100); + + assertEquals(0, s.size()); + + s.push("Hallo"); + s.push("wie"); + s.push("gehts"); + + assertEquals(3, s.size()); + + assertEquals("gehts", s.peek()); + assertEquals("gehts", s.pop()); + + assertEquals("wie", s.peek()); + assertEquals("wie", s.pop()); + + assertEquals("Hallo", s.peek()); + assertEquals("Hallo", s.pop()); + + assertNull(s.pop()); + } +} diff --git a/solutions/src/main/java/pr2/interfaces/uebersetzer/Translator.java b/solutions/src/main/java/pr2/interfaces/uebersetzer/Translator.java new file mode 100644 index 0000000..e65a2b2 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/uebersetzer/Translator.java @@ -0,0 +1,38 @@ + +package pr2.interfaces.uebersetzer; + +public class Translator { + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("Bitte geben Sie genau zwei " + + "Optionen an: Zielsprache und zu " + + "übersetzendes Wort"); + System.exit(1); + } + + String sprache = args[0].toLowerCase(); + String wort = args[1]; + + Uebersetzer uebersetzer = null; + + if ("englisch".equals(sprache)) { + uebersetzer = new UebersetzerDeutschEnglisch(); + } else if ("spanisch".equals(sprache)) { + uebersetzer = new UebersetzerDeutschSpanisch(); + } else { + System.out.println("Unbekannte Sprache"); + System.exit(1); + } + + String uebersetzung = uebersetzer.uebersetze(wort); + + if (uebersetzung != null) { + System.out.printf("Übersetzung: deutsch -> " + + "%s%n%s -> %s%n", + sprache, wort, uebersetzung); + } else { + System.out.printf("Unbekanntes Wort: %s%n", wort); + } + } +} diff --git a/solutions/src/main/java/pr2/interfaces/uebersetzer/Uebersetzer.java b/solutions/src/main/java/pr2/interfaces/uebersetzer/Uebersetzer.java new file mode 100644 index 0000000..50cf30d --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/uebersetzer/Uebersetzer.java @@ -0,0 +1,13 @@ +package pr2.interfaces.uebersetzer; + +public interface Uebersetzer { + + /** + * Übersetzt den gegebeben String. + * + * @param string String, der übersetzt werden soll + * @return die Übersetzung oder {@literal null}, wenn das Wort nicht + * bekannt ist + */ + public String uebersetze(String string); +} diff --git a/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschEnglisch.java b/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschEnglisch.java new file mode 100644 index 0000000..e5d1ebc --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschEnglisch.java @@ -0,0 +1,17 @@ + +package pr2.interfaces.uebersetzer; + +public class UebersetzerDeutschEnglisch implements Uebersetzer { + + @Override + public String uebersetze(String string) { + + return switch (string) { + case "gehen" -> "go"; + case "schalfen" -> "sleep"; + case "tanzen" -> "dance"; + default -> null; + }; + + } +} diff --git a/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschSpanisch.java b/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschSpanisch.java new file mode 100644 index 0000000..9877392 --- /dev/null +++ b/solutions/src/main/java/pr2/interfaces/uebersetzer/UebersetzerDeutschSpanisch.java @@ -0,0 +1,17 @@ + +package pr2.interfaces.uebersetzer; + +public class UebersetzerDeutschSpanisch implements Uebersetzer { + + @Override + public String uebersetze(String string) { + + return switch (string) { + case "gehen" -> "ir"; + case "schalfen" -> "dormir"; + case "tanzen" -> "bailar"; + default -> null; + }; + + } +} diff --git a/solutions/src/main/java/pr2/intro/coding_standard/CodingStandard.java b/solutions/src/main/java/pr2/intro/coding_standard/CodingStandard.java new file mode 100644 index 0000000..64a8726 --- /dev/null +++ b/solutions/src/main/java/pr2/intro/coding_standard/CodingStandard.java @@ -0,0 +1,54 @@ +package pr2.intro.coding_standard; + +/** + * Klasse dient als Beispiel für den Coding-Standard und die Namenskonventionen + * bei Java-Programmen. + */ + +@SuppressWarnings("PMD.UnusedPrivateField") +public class CodingStandard { + + /** + * Konstante, die dem Rest der Welt etwas mitteilen soll. + */ + public static final int KONSTANTE_MIT_TOLLEM_WERT = 3; + + private int erstesFeld; + + private double zweitesFeld; + + /** + * Legt eine neue Instanz an. + * + * @param erstesFeld das erste Feld. + * @param zweitesFeld das zweite Feld. + */ + public CodingStandard(int erstesFeld, double zweitesFeld) { + this.erstesFeld = erstesFeld; + this.zweitesFeld = zweitesFeld; + } + + /** + * Methode, die etwas tut. + * + * @param parameter Eingabewert für die Methode. + * @return gibt einen Wert abhängig von {@code parameter} zurück. + */ + public int methodeDieWasTut(int parameter) { + + int result; + + if (parameter > KONSTANTE_MIT_TOLLEM_WERT) { + result = 12; + } + else { + result = 13; + } + + erstesFeld = result; + zweitesFeld = 2 * result; + + return result; + } +} + diff --git a/solutions/src/main/java/pr2/intro/javadoc/Waehrung.java b/solutions/src/main/java/pr2/intro/javadoc/Waehrung.java new file mode 100644 index 0000000..5cb7837 --- /dev/null +++ b/solutions/src/main/java/pr2/intro/javadoc/Waehrung.java @@ -0,0 +1,142 @@ +package pr2.intro.javadoc; + + +/** + * Objekte dieser Klasse repräsentieren eine Währung. Eine Währung zeichnet + * sich durch einen Namen (z.B. EURO), ein Kürzel (z.B. € oder EUR) und einen + * Umrechnungskurs zum US-Dollar aus. + *

+ * Objekte dieser Klasse sind imutable, d.h. sie können nach der Erzeugung + * nicht mehr verändert werden. + */ + +public class Waehrung { + + /** + * Teiler für die Nachkommastellen. + */ + private static final long TEILER = 10000; + + /** + * Umrechnungskurs zum Dollar. + */ + private final int kurs; + + /** + * Langer Name der Währung. + */ + private final String name; + + /** + * Abkürzung zur Währung. + */ + private final String kuerzel; + + /** + * Erzeugt ein neues Objekt. + * + * @param name Name der Währung (z.B. EURO). + * @param kuerzel Kürzel der Währung (z.B. €). + * @param kurs Wechselkurs zum Dollar. + */ + public Waehrung(String name, String kuerzel, double kurs) { + this.kurs = (int) (kurs * TEILER); + this.name = name; + this.kuerzel = kuerzel; + } + + /** + * Rechnet einen Berag in der aktuellen Währung in eine andere Währung + * um. + * + * @param betrag Betrag der umgerechnet werden soll. + * @param toWaehrung die Zielwährung. + * @return Ergebnis der Umrechnung. + */ + public long umrechnen(long betrag, Waehrung toWaehrung) { + return betrag * kurs / toWaehrung.kurs; + } + + /** + * Gibt den Kurs der aktuellen Währung zurück. + * + * @return Wechselkurs bezogen auf den US-Dollar. + */ + public int getKurs() { + return kurs; + } + + /** + * Gibt den Namen der Währung zurück. + * + * @return Name der Währung (z.B. Euro oder Dollar). + */ + public String getName() { + return name; + } + + /** + * Gibt das Kürzel für die Währung zurück. + * + * @return Das Kürzel der Währung (z.B. EUR oder $) + */ + public String getKuerzel() { + return kuerzel; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("%s [%s] 1 %s = %.4f %s", name, kuerzel, "$", + kurs / (double) TEILER, kuerzel); + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + kurs; + result = prime * result + ((kuerzel == null) ? 0 : kuerzel.hashCode()); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Waehrung other = (Waehrung) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } + else if (!name.equals(other.name)) { + return false; + } + if (kurs != other.kurs) { + return false; + } + if (kuerzel == null) { + return other.kuerzel == null; + } + else { + return kuerzel.equals(other.kuerzel); + } + } +} diff --git a/solutions/src/main/java/pr2/intro/junit_faculty/Fakultaet.java b/solutions/src/main/java/pr2/intro/junit_faculty/Fakultaet.java new file mode 100644 index 0000000..2f31efa --- /dev/null +++ b/solutions/src/main/java/pr2/intro/junit_faculty/Fakultaet.java @@ -0,0 +1,14 @@ +package pr2.intro.junit_faculty; + +public class Fakultaet { + + /** + * Berechnet die Fakultät fuer die gegebene Zahl. + * + * @param n Zahl für die die Fakultät berechnet werden soll. + * @return die Fakultät + */ + public int fact(int n) { + return (n == 0) ? 1 : n * fact(n - 1); + } +} diff --git a/solutions/src/main/java/pr2/intro/junit_faculty/test/FakultaetTest.java b/solutions/src/main/java/pr2/intro/junit_faculty/test/FakultaetTest.java new file mode 100644 index 0000000..33f1df1 --- /dev/null +++ b/solutions/src/main/java/pr2/intro/junit_faculty/test/FakultaetTest.java @@ -0,0 +1,20 @@ +package pr2.intro.junit_faculty.test; + +import org.junit.jupiter.api.Test; +import pr2.intro.junit_faculty.Fakultaet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FakultaetTest { + + @Test + void testFact() { + Fakultaet f = new Fakultaet(); + assertEquals(1, f.fact(0)); + assertEquals(1, f.fact(1)); + assertEquals(2, f.fact(2)); + assertEquals(6, f.fact(3)); + assertEquals(24, f.fact(4)); + assertEquals(3628800, f.fact(10)); + } +} diff --git a/solutions/src/main/java/pr2/io/buffered_reader/LineNumberPrinter.java b/solutions/src/main/java/pr2/io/buffered_reader/LineNumberPrinter.java new file mode 100644 index 0000000..3b255c2 --- /dev/null +++ b/solutions/src/main/java/pr2/io/buffered_reader/LineNumberPrinter.java @@ -0,0 +1,36 @@ +package pr2.io.buffered_reader; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +/** + * Liest eine Textdatei und gibt den Inhalt auf der Konsole aus. + */ +public class LineNumberPrinter { + + /** + * Hauptmethode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + + String filename = args.length == 1 + ? args[0] + : "pr2/io/reader_writer/kafka.txt"; + + try (BufferedReader r = new BufferedReader(new FileReader(filename))) { + + String line; + int count = 1; + + while ((line = r.readLine()) != null) { + System.out.printf("%3d: %s%n", count, line); + count++; + } + } catch (IOException e) { + System.err.println("IO-Problem: " + e); + } + } +} diff --git a/solutions/src/main/java/pr2/io/data_output/Board.java b/solutions/src/main/java/pr2/io/data_output/Board.java new file mode 100644 index 0000000..8433336 --- /dev/null +++ b/solutions/src/main/java/pr2/io/data_output/Board.java @@ -0,0 +1,172 @@ +package pr2.io.data_output; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Ein Dame-Brett. + */ +public class Board { + + /** + * Belegung des Brettes. + */ + private final Color[][] brett = new Color[8][8]; + + /** + * Liest den Inhalt aus einer Datei. + * + * @param file Dateipfad. + * @return das gelesene Spielfeld + * @throws IOException IO-Probleme + */ + public static Board loadFromFile(String file) throws IOException { + + Board result = new Board(); + + DataInputStream dis = new DataInputStream(new FileInputStream(file)); + + String header = dis.readUTF(); + + if (!"Schachbrett".equals(header)) { + dis.close(); + throw new IOException("Falsches Dateiformat"); + } + + while (true) { + int x = dis.readByte(); + int y = dis.readByte(); + int ordinal = dis.readInt(); + + if (x == 0xff || y == 0xff || ordinal == -1) { + break; + } + + result.brett[x][y] = Color.values()[ordinal]; + } + + dis.close(); + + + return result; + } + + + /** + * Schreibt den Inhalt des Spielfeldes in eine Datei. + * + * @param file Dateiname. + * @throws IOException IO-Probleme + */ + public void writeToFile(String file) throws IOException { + + DataOutputStream dos = new DataOutputStream( + new FileOutputStream(file)); + + dos.writeUTF("Schachbrett"); + + for (int x = 0; x < brett.length; x++) { + for (int y = 0; y < brett[x].length; y++) { + + Color f = brett[x][y]; + + if (f != null) { + dos.writeByte(x); + dos.writeByte(y); + dos.writeInt(f.ordinal()); + } + } + } + + dos.writeByte(0xff); + dos.writeByte(0xff); + dos.writeInt(-1); + + dos.close(); + } + + /** + * Setzt einen Stein an die gegebene Koordinate. + * + * @param koordinate Koordinate in "a1"-Notation. + * @param farbe Fabe des Spielsteins. + */ + public void set(String koordinate, Color farbe) { + Tupel t = parseCoordinates(koordinate); + brett[t.x][t.y] = farbe; + } + + /** + * Wandelt textuelle Koordinaten in x- und y-Wert um. + * + * @param koordinate Koordinate als String. + * @return Koordinate als Integer-Tupel. + */ + private Tupel parseCoordinates(String koordinate) { + char buchstabe = koordinate.toLowerCase().charAt(0); + char zahl = koordinate.charAt(1); + + Tupel t = new Tupel(); + + t.y = buchstabe - 'a'; + t.x = zahl - '1'; + + if (t.x < 0 || t.x > 7) { + throw new IllegalArgumentException(); + } + + if (t.y < 0 || t.y > 7) { + throw new IllegalArgumentException(); + } + + return t; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder result = new StringBuilder(); + + char y = '1'; + + result.append(" "); + for (char c : "abcdefgh".toCharArray()) { + result.append(' ').append(c); + } + + result.append("\n"); + + for (Color[] figurs : brett) { + + result.append(y++); + + for (Color figur : figurs) { + if (figur != null) { + result.append(' ').append(figur); + } + else { + result.append(" "); + } + } + + result.append("\n"); + } + + return result.toString(); + } + + /** + * Tupel von zwei int-Werten. + */ + private static class Tupel { + int x; + + int y; + } +} diff --git a/solutions/src/main/java/pr2/io/data_output/Color.java b/solutions/src/main/java/pr2/io/data_output/Color.java new file mode 100644 index 0000000..63eb878 --- /dev/null +++ b/solutions/src/main/java/pr2/io/data_output/Color.java @@ -0,0 +1,30 @@ +package pr2.io.data_output; + +/** + * Farben der Spielsteine. + */ +public enum Color { + + /** + * Schwarzer Stein. + */ + BLACK, + + /** + * Weißer Stein. + */ + WHITE; + + /** + * @see java.lang.Enum#toString() + */ + @Override + public String toString() { + if (this == Color.BLACK) { + return "B"; + } + else { + return "W"; + } + } +} diff --git a/solutions/src/main/java/pr2/io/data_output/test/BoardTest.java b/solutions/src/main/java/pr2/io/data_output/test/BoardTest.java new file mode 100644 index 0000000..8643548 --- /dev/null +++ b/solutions/src/main/java/pr2/io/data_output/test/BoardTest.java @@ -0,0 +1,63 @@ +package pr2.io.data_output.test; + +import org.junit.jupiter.api.Test; +import pr2.io.data_output.Board; +import pr2.io.data_output.Color; + +import java.io.FileOutputStream; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +/** + * Test. + */ +public class BoardTest { + + private static final byte[] RESULT = new byte[] { + 0x00, 0x0B, 0x53, 0x63, 0x68, 0x61, 0x63, 0x68, 0x62, 0x72, 0x65, + 0x74, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x00, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, + 0x00, 0x00, 0x00, 0x01, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00, 0x02, + 0x04, 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x00, 0x00, 0x00, 0x01, + 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x05, 0x00, 0x00, 0x00, + 0x00, (byte) 0xff, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF + }; + + /** + * Test für die Implementierung des Spielbrettes. + * + * @throws IOException Datei-Probleme. + */ + @Test + void testDame() throws IOException { + Board s = new Board(); + s.set("a1", Color.BLACK); + s.set("b1", Color.WHITE); + s.set("h1", Color.BLACK); + s.set("h2", Color.BLACK); + s.set("a8", Color.BLACK); + s.set("f8", Color.BLACK); + s.set("f5", Color.WHITE); + s.set("e3", Color.WHITE); + s.set("g2", Color.WHITE); + + String stringRepresentation = s.toString(); + + s.writeToFile("/tmp/daten.dat"); + + s = Board.loadFromFile("/tmp/daten.dat"); + + assertEquals(stringRepresentation, s.toString()); + + FileOutputStream fos = new FileOutputStream("/tmp/testdata.dat"); + fos.write(RESULT); + fos.close(); + + s = Board.loadFromFile("/tmp/testdata.dat"); + + assertEquals(stringRepresentation, s.toString()); + } +} diff --git a/solutions/src/main/java/pr2/io/datainputoutput_1/DateReader.java b/solutions/src/main/java/pr2/io/datainputoutput_1/DateReader.java new file mode 100644 index 0000000..ca6b8b7 --- /dev/null +++ b/solutions/src/main/java/pr2/io/datainputoutput_1/DateReader.java @@ -0,0 +1,43 @@ +package pr2.io.datainputoutput_1; + +import java.io.DataInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateReader { + public static void main(String[] args) { + + DataInputStream dis = null; + + try { + DateFormat df = new SimpleDateFormat(DateWriter.FORMAT); + dis = new DataInputStream( + new FileInputStream(DateWriter.DATEINAME)); + + long time = dis.readLong(); + System.out.printf("Das Datum war: %s%n%n", + df.format(new Date(time))); + + } catch (FileNotFoundException e) { + System.err.printf("Datei %s kann nicht gelesen werden: %s%n%n", + DateWriter.DATEINAME, e.getMessage()); + System.exit(1); + } catch (IOException e) { + System.err.printf("Fehler beim Lesen der Datei %s:%s%n%n", + DateWriter.DATEINAME, e.getMessage()); + System.exit(1); + } finally { + if (dis != null) { + try { + dis.close(); + } catch (IOException e) { + // ignore + } + } + } + } +} diff --git a/solutions/src/main/java/pr2/io/datainputoutput_1/DateWriter.java b/solutions/src/main/java/pr2/io/datainputoutput_1/DateWriter.java new file mode 100644 index 0000000..bc13dcb --- /dev/null +++ b/solutions/src/main/java/pr2/io/datainputoutput_1/DateWriter.java @@ -0,0 +1,63 @@ +package pr2.io.datainputoutput_1; + +import java.io.DataOutputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateWriter { + + /** + * Dateiname mit den Testdaten. + */ + public static final String DATEINAME = "/tmp/test.data"; + + /** + * Datumsformat. + */ + public static final String FORMAT = "yyyy-MM-dd"; + + public static void main(String[] args) { + + if (args.length != 1) { + System.err.printf("Bitte eine Datum im Format %s angeben%n%n", + FORMAT); + System.exit(1); + } + + DataOutputStream dos = null; + + try { + DateFormat df = new SimpleDateFormat(FORMAT); + Date date = df.parse(args[0]); + dos = new DataOutputStream( + new FileOutputStream(DATEINAME)); + + dos.writeLong(date.getTime()); + } catch (ParseException e) { + System.err.printf("%s ist kein gültiges Datumsformat: %s%n%n", + args[0], FORMAT); + System.exit(1); + } catch (FileNotFoundException e) { + System.err.printf("Datei %s kann nicht geschrieben werden: %s%n%n", + DATEINAME, e.getMessage()); + System.exit(1); + } catch (IOException e) { + System.err.printf("Fehler beim Schreiben der Datei %s:%s%n%n", + DATEINAME, e.getMessage()); + System.exit(1); + } finally { + if (dos != null) { + try { + dos.close(); + } catch (IOException e) { + // ignore + } + } + } + } +} diff --git a/solutions/src/main/java/pr2/io/datainputoutput_2/DateReader.java b/solutions/src/main/java/pr2/io/datainputoutput_2/DateReader.java new file mode 100644 index 0000000..a8ed6d6 --- /dev/null +++ b/solutions/src/main/java/pr2/io/datainputoutput_2/DateReader.java @@ -0,0 +1,45 @@ +package pr2.io.datainputoutput_2; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateReader { + + public static void main(String[] args) { + + ObjectInputStream ois = null; + + try { + DateFormat df = new SimpleDateFormat(DateWriter.FORMAT); + ois = new ObjectInputStream( + new FileInputStream(DateWriter.DATEINAME)); + + Date date = (Date) ois.readObject(); + System.out.printf("Das Datum war: %s%n%n", df.format(date)); + + } catch (FileNotFoundException e) { + System.err.printf("Datei %s kann nicht gelesen werden: %s%n%n", + DateWriter.DATEINAME, e.getMessage()); + System.exit(1); + } catch (IOException e) { + System.err.printf("Fehler beim Lesen der Datei %s:%s%n%n", + DateWriter.DATEINAME, e.getMessage()); + System.exit(1); + } catch (ClassNotFoundException e) { + // Date ist Teil der Library, ist immer vorhanden + } finally { + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + // ignore + } + } + } + } +} diff --git a/solutions/src/main/java/pr2/io/datainputoutput_2/DateWriter.java b/solutions/src/main/java/pr2/io/datainputoutput_2/DateWriter.java new file mode 100644 index 0000000..a966e19 --- /dev/null +++ b/solutions/src/main/java/pr2/io/datainputoutput_2/DateWriter.java @@ -0,0 +1,63 @@ +package pr2.io.datainputoutput_2; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateWriter { + + /** + * Dateiname mit den Testdaten. + */ + public static final String DATEINAME = "/tmp/test.ser"; + + /** + * Datumsformat. + */ + public static final String FORMAT = "yyyy-MM-dd"; + + public static void main(String[] args) { + + if (args.length != 1) { + System.err.printf("Bitte eine Datum im Format %s angeben%n%n", + FORMAT); + System.exit(1); + } + + ObjectOutputStream dos = null; + + try { + DateFormat df = new SimpleDateFormat(FORMAT); + Date date = df.parse(args[0]); + dos = new ObjectOutputStream( + new FileOutputStream(DATEINAME)); + + dos.writeObject(date); + } catch (ParseException e) { + System.err.printf("%s ist kein gültiges Datumsformat: %s%n%n", + args[0], FORMAT); + System.exit(1); + } catch (FileNotFoundException e) { + System.err.printf("Datei %s kann nicht geschrieben werden: %s%n%n", + DATEINAME, e.getMessage()); + System.exit(1); + } catch (IOException e) { + System.err.printf("Fehler beim Schreiben der Datei %s:%s%n%n", + DATEINAME, e.getMessage()); + System.exit(1); + } finally { + if (dos != null) { + try { + dos.close(); + } catch (IOException e) { + // ignore + } + } + } + } +} diff --git a/solutions/src/main/java/pr2/io/datei_lesen/ReadData.java b/solutions/src/main/java/pr2/io/datei_lesen/ReadData.java new file mode 100644 index 0000000..b75a8dd --- /dev/null +++ b/solutions/src/main/java/pr2/io/datei_lesen/ReadData.java @@ -0,0 +1,33 @@ +package pr2.io.datei_lesen; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * Daten aus einer Datei lesen. + */ +public class ReadData { + + /** + * Datei. + */ + private static final String FILE = "pr2/io/datei_lesen/daten.dat"; + + /** + * Hauptmethode. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + + try (InputStream is = new FileInputStream(FILE)) { + int data; + while ((data = is.read()) != -1) { + System.out.print("0x" + Integer.toHexString(data) + " "); + } + } catch (IOException e) { + System.err.println("Problem mit Datei: " + e); + } + } +} diff --git a/solutions/src/main/java/pr2/io/datei_schreiben/WriteData.java b/solutions/src/main/java/pr2/io/datei_schreiben/WriteData.java new file mode 100644 index 0000000..dbac0c5 --- /dev/null +++ b/solutions/src/main/java/pr2/io/datei_schreiben/WriteData.java @@ -0,0 +1,33 @@ +package pr2.io.datei_schreiben; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * Daten aus einer Datei lesen. + */ +public class WriteData { + + /** + * Datei. + */ + public static final String FILE = "pr2/io/datei_schreiben/daten.dat"; + + /** + * Hauptmethode. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + + try (OutputStream os = new FileOutputStream(FILE)) { + + os.write(new byte[] {(byte) 0xca, (byte) 0xff, (byte) 0xfe, + (byte) 0xba, (byte) 0xbe, (byte) 0x00, (byte) 0xde, + (byte) 0xad, (byte) 0xbe, (byte) 0xef}); + } catch (IOException e) { + System.err.println("Problem mit Datei: " + e); + } + } +} diff --git a/solutions/src/main/java/pr2/io/datei_schreiben/test/DataTest.java b/solutions/src/main/java/pr2/io/datei_schreiben/test/DataTest.java new file mode 100644 index 0000000..c4fc9fd --- /dev/null +++ b/solutions/src/main/java/pr2/io/datei_schreiben/test/DataTest.java @@ -0,0 +1,39 @@ +package pr2.io.datei_schreiben.test; + +import org.junit.jupiter.api.Test; +import pr2.io.datei_schreiben.WriteData; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static pr2.io.datei_schreiben.WriteData.FILE; + +/** + * Test für die geschriebenen Daten. + */ +public class DataTest { + + /** + * Geschriebene Daten testen. + * + * @throws IOException Datei-Probleme. + */ + @Test + void testData() throws IOException { + + WriteData.main(new String[0]); + + InputStream is = new FileInputStream(FILE); + + byte[] buffer = new byte[10]; + is.read(buffer); + assertArrayEquals( + new byte[] {(byte) 0xca, (byte) 0xff, (byte) 0xfe, (byte) 0xba, + (byte) 0xbe, (byte) 0x00, (byte) 0xde, (byte) 0xad, + (byte) 0xbe, (byte) 0xef}, buffer); + + is.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/file/FilesystemWalker.java b/solutions/src/main/java/pr2/io/file/FilesystemWalker.java new file mode 100644 index 0000000..d34770d --- /dev/null +++ b/solutions/src/main/java/pr2/io/file/FilesystemWalker.java @@ -0,0 +1,94 @@ +package pr2.io.file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Stream; + +/** + * Klasse, die das Dateisystem durchläuft und Informationen ausgibt. + */ +public class FilesystemWalker { + + /** + * Haupt-Methode. + * + * @param args Kommandozeilen-Argumente. + * @throws IOException IO-Probleme + */ + public static void main(String[] args) throws IOException { + + String path = args.length > 0 ? args[0] : "."; + + Path root = Path.of(path).normalize().toAbsolutePath(); + + System.out.println("Untersuche: " + root); + System.out.println(); + + long total = walk(root); + + System.out.println(); + System.out.println("Gesamtgröße: " + niceSize(total)); + } + + /** + * Rekursive Methode, um den Baum zu durchlaufen. + * + * @param startDir Verzeichnis, bei dem gestartet werden + * soll. + * @return die Größe des Verzeichnisses. + */ + private static long walk(Path startDir) { + + long size = 0; + + if (!Files.isDirectory(startDir)) { + throw new IllegalArgumentException( + startDir + " ist kein Verzeichnis."); + } + + try (Stream fileStream = Files.list(startDir)) { + List files = fileStream.toList(); + + for (Path file : files) { + + if (Files.isDirectory(file) + && Files.isReadable(file) + && Files.isExecutable(file) + && !Files.isSymbolicLink(file) + && Files.exists(file)) { + long dirSize = walk(file); + size += dirSize; + System.out.printf("%s - %s%n", niceSize(dirSize), file); + } + else if (Files.exists(file) && !Files.isSymbolicLink(file)) { + size += Files.size(file); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + return size; + } + + /** + * Wandelt die Größe in eine schönere Darstellung um. + * + * @param size Die Größe. + * @return Schönere Darstellung. + */ + private static String niceSize(long size) { + + if (size > 1000_000L) { + return String.format("%.1f MByte", size / 1024.0 / 1024.0); + } + else if (size > 1000L) { + return String.format("%.1f kByte", size / 1024.0); + } + else { + return String.format("%d Byte", size); + } + } +} diff --git a/solutions/src/main/java/pr2/io/filetest/FileInfo.java b/solutions/src/main/java/pr2/io/filetest/FileInfo.java new file mode 100644 index 0000000..6d3d875 --- /dev/null +++ b/solutions/src/main/java/pr2/io/filetest/FileInfo.java @@ -0,0 +1,40 @@ +package pr2.io.filetest; + +import java.nio.file.Files; +import java.io.IOException; +import java.nio.file.Path; + +public class FileInfo { + + public static void main(String[] args) throws IOException { + + if (args.length != 1) { + System.err.println( + "Bitte eine Datei oder ein Verzeichnis angeben"); + System.exit(1); + } + + Path path = Path.of(args[0]).normalize().toAbsolutePath(); + + if (Files.exists(path)) { + System.out.printf("%s existiert%n", path); + System.out.printf("%s ist %d Bytes groß%n", path, Files.size(path)); + } + + if (Files.isDirectory(path)) { + System.out.printf("%s ist ein Verzeichnis%n", path); + } + + if (Files.isRegularFile(path)) { + System.out.printf("%s ist ein normale Datei%n", path); + } + + if (Files.isReadable(path)) { + System.out.printf("%s darf gelesen werden%n", path); + } + + if (Files.isWritable(path)) { + System.out.printf("%s darf geschrieben werden%n", path); + } + } +} diff --git a/solutions/src/main/java/pr2/io/filter/UCaseReader.java b/solutions/src/main/java/pr2/io/filter/UCaseReader.java new file mode 100644 index 0000000..672e75e --- /dev/null +++ b/solutions/src/main/java/pr2/io/filter/UCaseReader.java @@ -0,0 +1,36 @@ +package pr2.io.filter; + +import java.io.FilterReader; +import java.io.IOException; +import java.io.Reader; + +/** + * Filter-Reader, der den Inhalt in Großbuchstaben umwandelt. + */ +public class UCaseReader extends FilterReader { + + + /** + * Legt einen neuen Reader an. + * + * @param in Reader, von dem die Daten gelesen werden. + */ + public UCaseReader(Reader in) { + super(in); + } + + /** + * @see FilterReader#read(char[], int, int). + */ + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + + int charsGelesen = super.read(cbuf, off, len); + + for (int i = 0; i < cbuf.length; i++) { + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + + return charsGelesen; + } +} diff --git a/solutions/src/main/java/pr2/io/filter/test/UCaseReaderTest.java b/solutions/src/main/java/pr2/io/filter/test/UCaseReaderTest.java new file mode 100644 index 0000000..9546c4a --- /dev/null +++ b/solutions/src/main/java/pr2/io/filter/test/UCaseReaderTest.java @@ -0,0 +1,34 @@ +package pr2.io.filter.test; + +import org.junit.jupiter.api.Test; +import pr2.io.filter.UCaseReader; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test für die Rot13 "Verschlüsselung". + */ +public class UCaseReaderTest { + + /** + * Testmethode. + * + * @throws IOException IO-Probleme + */ + @Test + void testReader() throws IOException { + + BufferedReader br = new BufferedReader( + new UCaseReader(new StringReader( + "Dies ist" + " ein Test für den UCASE-Reader."))); + + String line = br.readLine(); + assertEquals("DIES IST EIN TEST FÜR DEN UCASE-READER.", line); + + br.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/konsole/ConsoleToFile.java b/solutions/src/main/java/pr2/io/konsole/ConsoleToFile.java new file mode 100644 index 0000000..20e6935 --- /dev/null +++ b/solutions/src/main/java/pr2/io/konsole/ConsoleToFile.java @@ -0,0 +1,46 @@ +package pr2.io.konsole; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; + +/** + * Liest einen Text von der Console und + * schreibt ihn dann in eine Datei. Allerdings + * erfolgt das Schreiben über eine Ausgabeumleitung + * und nicht über Dateioperationen. + */ +public class ConsoleToFile { + + /** + * Logfile für Ausgaben. + */ + private static final String LOGFILE = "console.log"; + + /** + * Hauptmethode. + * + * @param args Kommandozeile-Argumente. + */ + public static void main(String[] args) { + + try (BufferedReader br = new BufferedReader( + new InputStreamReader(System.in)); + + PrintStream ps = new PrintStream(LOGFILE)) { + + System.setOut(ps); + + String line; + int count = 1; + + while ((line = br.readLine()) != null) { + System.out.println(line); + System.err.println(count++); + } + } catch (IOException ex) { + System.err.println("Dateiproblem: " + ex); + } + } +} diff --git a/solutions/src/main/java/pr2/io/linecounter/LineCounter.java b/solutions/src/main/java/pr2/io/linecounter/LineCounter.java new file mode 100644 index 0000000..3c51768 --- /dev/null +++ b/solutions/src/main/java/pr2/io/linecounter/LineCounter.java @@ -0,0 +1,29 @@ +package pr2.io.linecounter; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class LineCounter { + + public static void main(String[] args) throws IOException { + + int lines = 0; + + if (args.length != 1) { + System.err.println("Bitte eine Datei angeben"); + System.exit(1); + } + + BufferedReader br = new BufferedReader(new FileReader(args[0])); + + + while (br.readLine() != null) { + lines++; + } + + br.close(); + + System.out.printf("Datei: %s hat %d Zeilen%n", args[0], lines); + } +} diff --git a/solutions/src/main/java/pr2/io/random_access/FileSort.java b/solutions/src/main/java/pr2/io/random_access/FileSort.java new file mode 100644 index 0000000..f35241c --- /dev/null +++ b/solutions/src/main/java/pr2/io/random_access/FileSort.java @@ -0,0 +1,58 @@ +package pr2.io.random_access; + +import java.io.IOException; +import java.io.RandomAccessFile; + +/** + * Sortierung von Dateien (in place). + */ +public class FileSort { + + /** + * Sortiert die Datei von Byte-Werten per Bubble-Sort. + * + * @param filename Dateiname + * @throws IOException IO-Probleme + */ + public static void sortFile(String filename) throws IOException { + + RandomAccessFile rf = new RandomAccessFile(filename, "rw"); + + byte b1; // erstes Byte + byte b2; // zweites Byte + + long pos = 0; + boolean swap = false; + + while (true) { + // an die Position springen + rf.seek(pos); + + // zwei Bytes lesen + b1 = rf.readByte(); + b2 = rf.readByte(); + + if (b1 > b2) { + // erstes ist größer als zweites, austauschen + rf.seek(pos); + rf.write(b2); + rf.write(b1); + swap = true; + } + + pos++; + + if (pos >= rf.length() - 1) { + if (swap) { + pos = 0; + swap = false; + } + else { + break; + } + } + } + + rf.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/random_access/test/FileSortTest.java b/solutions/src/main/java/pr2/io/random_access/test/FileSortTest.java new file mode 100644 index 0000000..9ffdc05 --- /dev/null +++ b/solutions/src/main/java/pr2/io/random_access/test/FileSortTest.java @@ -0,0 +1,63 @@ +package pr2.io.random_access.test; + +import org.junit.jupiter.api.Test; +import pr2.io.random_access.FileSort; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Testet die Sortierung per Random-Access-File. + */ +public class FileSortTest { + + /** + * Kopiert die Datei f1 als Datei f2. + * + * @param f1 Quell-Datei + * @param f2 Ziel-Datei + * @throws IOException IO-Probleme + */ + private static void copyFile(String f1, String f2) throws IOException { + + FileInputStream fis = new FileInputStream(f1); + FileOutputStream fos = new FileOutputStream(f2); + + int b; + + while ((b = fis.read()) != -1) { + fos.write(b); + } + + fis.close(); + fos.close(); + } + + /** + * Testet die File-Sortierung. + * + * @throws IOException IO-Problem + */ + @Test + void testBubbleSort() throws IOException { + copyFile("assets/data.dat", "output/data.dat"); + FileSort.sortFile("output/data.dat"); + + InputStream is = new FileInputStream("output/data.dat"); + + int data; + byte lastByte = 0; + + while ((data = is.read()) >= 0) { + byte b = (byte) data; + assertTrue(b >= lastByte); + lastByte = b; + } + + is.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/reader_writer/ReadAndPrintFile.java b/solutions/src/main/java/pr2/io/reader_writer/ReadAndPrintFile.java new file mode 100644 index 0000000..88818a3 --- /dev/null +++ b/solutions/src/main/java/pr2/io/reader_writer/ReadAndPrintFile.java @@ -0,0 +1,34 @@ +package pr2.io.reader_writer; + +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; + +/** + * Liest eine Textdatei und gibt den Inhalt auf der Konsole aus. + */ +public class ReadAndPrintFile { + + /** + * Hauptmethode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + + String filename = args.length == 1 + ? args[0] + : "pr2/io/reader_writer/kafka.txt"; + + try (Reader r = new FileReader(filename)) { + char[] buffer = new char[1024]; + int length; + + while ((length = r.read(buffer)) >= 0) { + System.out.print(new String(buffer, 0, length)); + } + } catch (IOException e) { + System.err.println("IO-Problem: " + e); + } + } +} diff --git a/solutions/src/main/java/pr2/io/rot13/Rot13.java b/solutions/src/main/java/pr2/io/rot13/Rot13.java new file mode 100644 index 0000000..a0a3787 --- /dev/null +++ b/solutions/src/main/java/pr2/io/rot13/Rot13.java @@ -0,0 +1,25 @@ +package pr2.io.rot13; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class Rot13 { + + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("Bitte eine Datei angeben"); + System.exit(1); + } + + BufferedReader br = new BufferedReader( + new Rot13Reader(new FileReader(args[0]))); + + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + } + + br.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/rot13/Rot13Reader.java b/solutions/src/main/java/pr2/io/rot13/Rot13Reader.java new file mode 100644 index 0000000..04cb929 --- /dev/null +++ b/solutions/src/main/java/pr2/io/rot13/Rot13Reader.java @@ -0,0 +1,24 @@ +package pr2.io.rot13; + +import java.io.FilterReader; +import java.io.IOException; +import java.io.Reader; + +public class Rot13Reader extends FilterReader { + + public Rot13Reader(Reader in) { + super(in); + } + + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + + int charsGelesen = super.read(cbuf, off, len); + + for (int i = 0; i < cbuf.length; i++) { + cbuf[i] = cbuf[i] != '\n' ? (char) (cbuf[i] + 13) : '\n'; + } + + return charsGelesen; + } +} diff --git a/solutions/src/main/java/pr2/io/scrambler/Scrambler.java b/solutions/src/main/java/pr2/io/scrambler/Scrambler.java new file mode 100644 index 0000000..f045627 --- /dev/null +++ b/solutions/src/main/java/pr2/io/scrambler/Scrambler.java @@ -0,0 +1,38 @@ +package pr2.io.scrambler; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class Scrambler { + + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("Bitte eine Datei angeben"); + System.exit(1); + } + + FileInputStream fis; + + try { + fis = new FileInputStream(args[0]); + } catch (FileNotFoundException e) { + System.err.printf("Datei nicht gefunden: %s%n", args[0]); + System.exit(1); + return; // never reached + } + + int input; + int count = 0; + + while ((input = fis.read()) != -1) { + byte b = (byte) input; + + if (count++ % 2 == 0) { + System.out.write(b); + } + } + + fis.close(); + } +} diff --git a/solutions/src/main/java/pr2/io/serialisierung/Board.java b/solutions/src/main/java/pr2/io/serialisierung/Board.java new file mode 100644 index 0000000..f40fa6b --- /dev/null +++ b/solutions/src/main/java/pr2/io/serialisierung/Board.java @@ -0,0 +1,139 @@ +package pr2.io.serialisierung; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * Ein Dame-Brett. + */ +public class Board implements Serializable { + + /** + * Belegung des Brettes. + */ + private final Color[][] brett = new Color[8][8]; + + /** + * Liest den Inhalt aus einer Datei. + * + * @param file Dateipfad. + * @return das gelesene Spielfeld + * @throws IOException IO-Probleme + */ + public static Board loadFromFile(String file) throws IOException { + + ObjectInputStream ois = + new ObjectInputStream(new FileInputStream(file)); + + Board result; + + try { + result = (Board) ois.readObject(); + ois.close(); + } catch (ClassNotFoundException e) { + ois.close(); + throw new IOException("Serialization: Class not found", e); + } + + return result; + } + + /** + * Schreibt den Inhalt des Spielfeldes in eine Datei. + * + * @param file Dateiname. + * @throws IOException IO-Probleme + */ + public void writeToFile(String file) throws IOException { + ObjectOutputStream oos = + new ObjectOutputStream(new FileOutputStream(file)); + oos.writeObject(this); + oos.close(); + } + + /** + * Setzt einen Stein an die gegebene Koordinate. + * + * @param koordinate Koordinate in "a1"-Notation. + * @param farbe Fabe des Spielsteins. + */ + public void set(String koordinate, Color farbe) { + Tupel t = parseCoordinates(koordinate); + brett[t.x][t.y] = farbe; + } + + /** + * Wandelt textuelle Koordinaten in x- und y-Wert um. + * + * @param koordinate Koordinate als String. + * @return Koordinate als Integer-Tupel. + */ + private Tupel parseCoordinates(String koordinate) { + char buchstabe = koordinate.toLowerCase().charAt(0); + char zahl = koordinate.charAt(1); + + Tupel t = new Tupel(); + + t.y = buchstabe - 'a'; + t.x = zahl - '1'; + + if (t.x < 0 || t.x > 7) { + throw new IllegalArgumentException(); + } + + if (t.y < 0 || t.y > 7) { + throw new IllegalArgumentException(); + } + + return t; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder result = new StringBuilder(); + + char y = '1'; + + result.append(" "); + for (char c : "abcdefgh".toCharArray()) { + result.append(' ').append(c); + } + + result.append("\n"); + + for (Color[] figurs : brett) { + + result.append(y++); + + for (Color figur : figurs) { + if (figur != null) { + result.append(' ').append(figur); + } + else { + result.append(" "); + } + } + + result.append("\n"); + } + + return result.toString(); + } + + /** + * Tupel von zwei int-Werten. + */ + private static class Tupel { + int x; + + int y; + } +} diff --git a/solutions/src/main/java/pr2/io/serialisierung/Color.java b/solutions/src/main/java/pr2/io/serialisierung/Color.java new file mode 100644 index 0000000..7f8f76b --- /dev/null +++ b/solutions/src/main/java/pr2/io/serialisierung/Color.java @@ -0,0 +1,30 @@ +package pr2.io.serialisierung; + +/** + * Farben der Spielsteine. + */ +public enum Color { + + /** + * Schwarzer Stein. + */ + BLACK, + + /** + * Weißer Stein. + */ + WHITE; + + /** + * @see java.lang.Enum#toString() + */ + @Override + public String toString() { + if (this == Color.BLACK) { + return "B"; + } + else { + return "W"; + } + } +} diff --git a/solutions/src/main/java/pr2/io/serialisierung/test/BoardTest.java b/solutions/src/main/java/pr2/io/serialisierung/test/BoardTest.java new file mode 100644 index 0000000..c2ab936 --- /dev/null +++ b/solutions/src/main/java/pr2/io/serialisierung/test/BoardTest.java @@ -0,0 +1,46 @@ +package pr2.io.serialisierung.test; + +import org.junit.jupiter.api.Test; +import pr2.io.serialisierung.Board; +import pr2.io.serialisierung.Color; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test. + */ +public class BoardTest { + + /** + * Test für die Implementierung des Spielbrettes. + * + * @throws IOException Datei-Probleme. + */ + @Test + void testDame() throws IOException { + Board s = new Board(); + s.set("a1", Color.BLACK); + s.set("b1", Color.WHITE); + s.set("h1", Color.BLACK); + s.set("h2", Color.BLACK); + s.set("a8", Color.BLACK); + s.set("f8", Color.BLACK); + s.set("f5", Color.WHITE); + s.set("e3", Color.WHITE); + s.set("g2", Color.WHITE); + + String stringRepresentation = s.toString(); + + s.writeToFile("/tmp/daten.dat"); + + s = Board.loadFromFile("/tmp/daten.dat"); + + assertEquals(stringRepresentation, s.toString()); + + s = Board.loadFromFile("pr2/io/serialisierung/testdata.dat"); + + assertEquals(stringRepresentation, s.toString()); + } +} diff --git a/solutions/src/main/java/pr2/lambda/anonymous/Alien.java b/solutions/src/main/java/pr2/lambda/anonymous/Alien.java new file mode 100644 index 0000000..15ab6ce --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/anonymous/Alien.java @@ -0,0 +1,46 @@ +package pr2.lambda.anonymous; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Point; + +/** + * Ein Alien. + */ +public class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Aliens X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Alien(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("pr2/lambda/anonymous/", + "ship01", "ship02", "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("pr2/lambda/anonymous/explosion_1" + ".png", + 43))); + setInvisibleAfterFrames(70); + } +} diff --git a/solutions/src/main/java/pr2/lambda/anonymous/GameBoard.java b/solutions/src/main/java/pr2/lambda/anonymous/GameBoard.java new file mode 100644 index 0000000..b436c76 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/anonymous/GameBoard.java @@ -0,0 +1,73 @@ +package pr2.lambda.anonymous; + +import de.smits_net.games.framework.board.Board; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.Random; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Alien, das durch das Bild läuft. + */ + Alien alien; // nicht private wegen des Zugriffs aus der inneren Klasse + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(800, 300), Color.BLACK); + + // Alien initialisieren + alien = new Alien(this, + new Point(800, 50 + new Random().nextInt(100))); + + // Event-Handler für Clicks. + addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + if (alien.intersects(e.getPoint())) { + alien.explode(); + } + else { + alien.setPosition(e.getPoint()); + } + } + }); + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGame(Graphics g) { + // Alien zeichnen + alien.draw(g, this); + } + + /** + * Game-Over-Text anzeigen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGameOver(Graphics g) { + centerText(g, "Das Spiel ist aus!"); + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + alien.move(); + return alien.isVisible(); + } +} diff --git a/solutions/src/main/java/pr2/lambda/anonymous/GameMain.java b/solutions/src/main/java/pr2/lambda/anonymous/GameMain.java new file mode 100644 index 0000000..33056e6 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/anonymous/GameMain.java @@ -0,0 +1,28 @@ +package pr2.lambda.anonymous; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click Alien - (04) Anonymous Class", new GameBoard()); + + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/lambda/array_sorter/ReverseSort.java b/solutions/src/main/java/pr2/lambda/array_sorter/ReverseSort.java new file mode 100644 index 0000000..50ac85d --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/array_sorter/ReverseSort.java @@ -0,0 +1,28 @@ +package pr2.lambda.array_sorter; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Date; + +public class ReverseSort { + + public static void main(String[] args) { + Date[] dates = { + new Date(9200000000L), + new Date(9300000000L), + new Date(92100000000L), + new Date(9600000000L), + new Date(93300000000L) + }; + + Arrays.sort(dates, new Comparator() { + + @Override + public int compare(Date o1, Date o2) { + return o1.compareTo(o2) * -1; + } + }); + + System.out.println(Arrays.asList(dates)); + } +} diff --git a/solutions/src/main/java/pr2/lambda/callback/Main.java b/solutions/src/main/java/pr2/lambda/callback/Main.java new file mode 100644 index 0000000..b493c8f --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/callback/Main.java @@ -0,0 +1,46 @@ +package pr2.lambda.callback; + +import java.util.Arrays; + +/** + * Hauptklasse. + */ +public class Main { + + private static final int[] ZAHLEN = + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20}; + + /** + * Hauptmethode. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + + NumberSelector s = new NumberSelector(); + + int[] gerade; + + gerade = s.filter(new Predicate() { + @Override + public boolean accept(Integer object) { + return object % 2 == 0; + } + }, ZAHLEN); + + System.out.println(Arrays.toString(gerade)); + + int[] ungerade; + + ungerade = s.filter(new Predicate() { + @Override + public boolean accept(Integer object) { + return object % 2 != 0; + } + }, ZAHLEN); + + System.out.println(Arrays.toString(ungerade)); + + } +} diff --git a/solutions/src/main/java/pr2/lambda/callback/NumberSelector.java b/solutions/src/main/java/pr2/lambda/callback/NumberSelector.java new file mode 100644 index 0000000..bbce522 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/callback/NumberSelector.java @@ -0,0 +1,30 @@ +package pr2.lambda.callback; + +/** + * Auswählen von Zahlen mithilfe eines Callbacks. + */ +public class NumberSelector { + + /** + * Filtert die übergebenen Zahlen anhand des Prädikates. + * + * @param predicate Prädikat. + * @param numbers die zu filternden Zahlen. + * @return Das Ergebnis + */ + public int[] filter(Predicate predicate, int[] numbers) { + + int[] temp = new int[numbers.length]; + int count = 0; + + for (int i : numbers) { + if (predicate.accept(i)) { + temp[count++] = i; + } + } + + int[] result = new int[count]; + System.arraycopy(temp, 0, result, 0, count); + return result; + } +} diff --git a/solutions/src/main/java/pr2/lambda/callback/Predicate.java b/solutions/src/main/java/pr2/lambda/callback/Predicate.java new file mode 100644 index 0000000..bf3f416 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/callback/Predicate.java @@ -0,0 +1,20 @@ +package pr2.lambda.callback; + +/** + * Interface, um Entscheidung über ein Objekt zu + * treffen. + * + * @param Typ des Objekts + */ +@FunctionalInterface +public interface Predicate { + + /** + * Entscheidet, ob ein Objekt akzeptiert wird oder nicht. + * + * @param object das zu untersuchende Objekt + * @return {@code true} wenn das Objekt akzeptiert wird, + * andernfalls {@code false}. + */ + boolean accept(T object); +} diff --git a/solutions/src/main/java/pr2/lambda/comparator/SortLambda.java b/solutions/src/main/java/pr2/lambda/comparator/SortLambda.java new file mode 100644 index 0000000..9a605d8 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/comparator/SortLambda.java @@ -0,0 +1,12 @@ +package pr2.lambda.comparator; + +import java.util.Arrays; + +public class SortLambda { + + public static void main(String[] args) { + Integer[] zahlen = {23, 42, 17, 9, 1, 5, 7, 88, 35}; + Arrays.sort(zahlen, (a, b) -> b - a); + System.out.println(Arrays.toString(zahlen)); + } +} diff --git a/solutions/src/main/java/pr2/lambda/lambdas/Main.java b/solutions/src/main/java/pr2/lambda/lambdas/Main.java new file mode 100644 index 0000000..bbca68e --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/lambdas/Main.java @@ -0,0 +1,31 @@ +package pr2.lambda.lambdas; + +import java.util.Arrays; + +/** + * Hauptklasse. + */ +public class Main { + + private static final int[] ZAHLEN = + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20}; + + /** + * Hauptmethode. + * + * @param args Kommandozeileargumente. + */ + public static void main(String[] args) { + + NumberSelector s = new NumberSelector(); + + int[] gerade = s.filter((i) -> i % 2 == 0, ZAHLEN); + + System.out.println(Arrays.toString(gerade)); + + int[] ungerade = s.filter((i) -> i % 2 != 0, ZAHLEN); + + System.out.println(Arrays.toString(ungerade)); + } +} diff --git a/solutions/src/main/java/pr2/lambda/lambdas/NumberSelector.java b/solutions/src/main/java/pr2/lambda/lambdas/NumberSelector.java new file mode 100644 index 0000000..16f974b --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/lambdas/NumberSelector.java @@ -0,0 +1,30 @@ +package pr2.lambda.lambdas; + +/** + * Auswählen von Zahlen mithilfe eines Callbacks. + */ +public class NumberSelector { + + /** + * Filtert die übergebenen Zahlen anhand des Prädikates. + * + * @param predicate Prädikat. + * @param numbers die zu filternden Zahlen. + * @return Das Ergebnis + */ + public int[] filter(Predicate predicate, int[] numbers) { + + int[] temp = new int[numbers.length]; + int count = 0; + + for (int i : numbers) { + if (predicate.accept(i)) { + temp[count++] = i; + } + } + + int[] result = new int[count]; + System.arraycopy(temp, 0, result, 0, count); + return result; + } +} diff --git a/solutions/src/main/java/pr2/lambda/lambdas/Predicate.java b/solutions/src/main/java/pr2/lambda/lambdas/Predicate.java new file mode 100644 index 0000000..d8aa4ee --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/lambdas/Predicate.java @@ -0,0 +1,20 @@ +package pr2.lambda.lambdas; + +/** + * Interface, um Entscheidung über ein Objekt zu + * treffen. + * + * @param Typ des Objekts + */ +@FunctionalInterface +public interface Predicate { + + /** + * Entscheidet, ob ein Objekt akzeptiert wird oder nicht. + * + * @param object das zu untersuchende Objekt + * @return {@code true} wenn das Objekt akzeptiert wird, + * andernfalls {@code false}. + */ + boolean accept(T object); +} diff --git a/solutions/src/main/java/pr2/lambda/local/Alien.java b/solutions/src/main/java/pr2/lambda/local/Alien.java new file mode 100644 index 0000000..0a83b03 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/local/Alien.java @@ -0,0 +1,46 @@ +package pr2.lambda.local; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Point; + +/** + * Ein Alien. + */ +public class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Aliens X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Alien(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("pr2/lambda/local/", + "ship01", "ship02", "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("pr2/lambda/local/explosion_1" + ".png", + 43))); + setInvisibleAfterFrames(70); + } +} diff --git a/solutions/src/main/java/pr2/lambda/local/GameBoard.java b/solutions/src/main/java/pr2/lambda/local/GameBoard.java new file mode 100644 index 0000000..52cb76d --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/local/GameBoard.java @@ -0,0 +1,72 @@ +package pr2.lambda.local; + +import de.smits_net.games.framework.board.Board; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.Random; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Alien, das durch das Bild läuft. + */ + Alien alien; // nicht private wegen Zugriff aus innerer Klasse + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(800, 300), Color.BLACK); + + // Alien initialisieren + alien = new Alien(this, + new Point(800, 50 + new Random().nextInt(100))); + + /* Event-Handler für Clicks. */ + class AlienExploder extends MouseAdapter { + @Override + public void mousePressed(MouseEvent e) { + if (alien.intersects(e.getPoint())) { + alien.explode(); + } + } + } + + addMouseListener(new AlienExploder()); + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGame(Graphics g) { + // Alien zeichnen + alien.draw(g, this); + } + + /** + * Game-Over-Text anzeigen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGameOver(Graphics g) { + centerText(g, "Das Spiel ist aus!"); + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + alien.move(); + return alien.isVisible(); + } +} diff --git a/solutions/src/main/java/pr2/lambda/local/GameMain.java b/solutions/src/main/java/pr2/lambda/local/GameMain.java new file mode 100644 index 0000000..ec4aab1 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/local/GameMain.java @@ -0,0 +1,28 @@ +package pr2.lambda.local; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click Alien - (03) Local Class", new GameBoard()); + + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/lambda/matrixsuche/MatrixSuche.java b/solutions/src/main/java/pr2/lambda/matrixsuche/MatrixSuche.java new file mode 100644 index 0000000..21b2d88 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/matrixsuche/MatrixSuche.java @@ -0,0 +1,39 @@ +package pr2.lambda.matrixsuche; + +public class MatrixSuche { + + public static class Position { + private final int xPos; + private final int yPos; + + public Position(int xPos, int yPos) { + this.xPos = xPos; + this.yPos = yPos; + } + + public int getxPos() { + return xPos; + } + + public int getyPos() { + return yPos; + } + + @Override + public String toString() { + return String.format("(%d, %d)", xPos, yPos); + } + } + + public static Position findEntry(int[][] matrix, int wert) { + for (int i = 0; i < matrix.length; i++) { + for (int k = 0; k < matrix[i].length; k++) { + if (matrix[i][k] == wert) { + return new Position(i, k); + } + } + } + + return null; + } +} diff --git a/solutions/src/main/java/pr2/lambda/matrixsuche/test/MatrixSucheTest.java b/solutions/src/main/java/pr2/lambda/matrixsuche/test/MatrixSucheTest.java new file mode 100644 index 0000000..f481817 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/matrixsuche/test/MatrixSucheTest.java @@ -0,0 +1,32 @@ +package pr2.lambda.matrixsuche.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pr2.lambda.matrixsuche.MatrixSuche; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class MatrixSucheTest { + + @Test + void testSuche() { + + int[][] matrix = { + {3, 5, 6, 7, 8}, + {10, 12, 14, 16, 18}, + {23, 25, 26, 27, 28} + }; + + Assertions.assertEquals("(0, 1)", + MatrixSuche.findEntry(matrix, 5).toString()); + assertEquals("(2, 0)", + MatrixSuche.findEntry(matrix, 23).toString()); + assertEquals("(1, 2)", + MatrixSuche.findEntry(matrix, 14).toString()); + assertEquals("(0, 4)", + MatrixSuche.findEntry(matrix, 8).toString()); + assertNull( + MatrixSuche.findEntry(matrix, 99)); + } +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_1/StringFunction.java b/solutions/src/main/java/pr2/lambda/mogrifier_1/StringFunction.java new file mode 100644 index 0000000..9d98357 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_1/StringFunction.java @@ -0,0 +1,6 @@ +package pr2.lambda.mogrifier_1; + +@FunctionalInterface +public interface StringFunction { + String apply(String s); +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_1/StringTransmogrifier.java b/solutions/src/main/java/pr2/lambda/mogrifier_1/StringTransmogrifier.java new file mode 100644 index 0000000..21d976a --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_1/StringTransmogrifier.java @@ -0,0 +1,14 @@ +package pr2.lambda.mogrifier_1; + +public class StringTransmogrifier { + + public static String[] transmogrify(String[] elements, StringFunction t) { + String[] result = new String[elements.length]; + + for (int i = 0; i < elements.length; i++) { + result[i] = t.apply(elements[i]); + } + + return result; + } +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_1/test/StringTransmogrifierTest.java b/solutions/src/main/java/pr2/lambda/mogrifier_1/test/StringTransmogrifierTest.java new file mode 100644 index 0000000..8b3bb4d --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_1/test/StringTransmogrifierTest.java @@ -0,0 +1,33 @@ +package pr2.lambda.mogrifier_1.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pr2.lambda.mogrifier_1.StringTransmogrifier; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class StringTransmogrifierTest { + + @Test + void testMogrification() { + String[] elements = {"Hello", "World"}; + + Assertions.assertArrayEquals(new String[] { "hello", "world"}, + StringTransmogrifier.transmogrify(elements, + String::toLowerCase)); + + assertArrayEquals(new String[] { "HELLO", "WORLD"}, + StringTransmogrifier.transmogrify(elements, + String::toUpperCase)); + + assertArrayEquals(new String[] { "Ifmmp", "Xpsme"}, + StringTransmogrifier.transmogrify(elements, + s -> { + char[] c = s.toCharArray(); + for (int i = 0; i < c.length; i++) { + c[i] = (char) (c[i] + 1); + } + return new String(c); + })); + } +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_2/StringFunction.java b/solutions/src/main/java/pr2/lambda/mogrifier_2/StringFunction.java new file mode 100644 index 0000000..070ee01 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_2/StringFunction.java @@ -0,0 +1,18 @@ +package pr2.lambda.mogrifier_2; + +@FunctionalInterface +public interface StringFunction { + static StringFunction caesar(final int shift) { + return (s) -> shiftCharacters(s, shift); + } + + static String shiftCharacters(String s, int shift) { + char[] c = s.toCharArray(); + for (int i = 0; i < c.length; i++) { + c[i] = (char) (c[i] + shift); + } + return new String(c); + } + + String apply(String s); +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_2/StringTransmogrifier.java b/solutions/src/main/java/pr2/lambda/mogrifier_2/StringTransmogrifier.java new file mode 100644 index 0000000..79b64a4 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_2/StringTransmogrifier.java @@ -0,0 +1,14 @@ +package pr2.lambda.mogrifier_2; + +public class StringTransmogrifier { + + public static String[] transmogrify(String[] elements, StringFunction t) { + String[] result = new String[elements.length]; + + for (int i = 0; i < elements.length; i++) { + result[i] = t.apply(elements[i]); + } + + return result; + } +} diff --git a/solutions/src/main/java/pr2/lambda/mogrifier_2/test/StringTransmogrifierTest.java b/solutions/src/main/java/pr2/lambda/mogrifier_2/test/StringTransmogrifierTest.java new file mode 100644 index 0000000..b842940 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/mogrifier_2/test/StringTransmogrifierTest.java @@ -0,0 +1,43 @@ +package pr2.lambda.mogrifier_2.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pr2.lambda.mogrifier_2.StringFunction; +import pr2.lambda.mogrifier_2.StringTransmogrifier; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +public class StringTransmogrifierTest { + + static final String[] ELEMENTS = {"Hello", "World"}; + + @Test + void testMogrification() { + + + Assertions.assertArrayEquals(new String[] {"hello", "world"}, + StringTransmogrifier.transmogrify(ELEMENTS, + String::toLowerCase)); + + assertArrayEquals(new String[] {"HELLO", "WORLD"}, + StringTransmogrifier.transmogrify(ELEMENTS, + String::toUpperCase)); + + assertArrayEquals(new String[] {"Ifmmp", "Xpsme"}, + StringTransmogrifier.transmogrify(ELEMENTS, + s -> { + char[] c = s.toCharArray(); + for (int i = 0; i < c.length; i++) { + c[i] = (char) (c[i] + 1); + } + return new String(c); + })); + } + + @Test + void testCaesar() { + assertArrayEquals(new String[] {"Khoor", "Zruog"}, + StringTransmogrifier.transmogrify(ELEMENTS, + StringFunction.caesar(3))); + } +} diff --git a/solutions/src/main/java/pr2/lambda/nonstatic/Alien.java b/solutions/src/main/java/pr2/lambda/nonstatic/Alien.java new file mode 100644 index 0000000..ca7ba90 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/nonstatic/Alien.java @@ -0,0 +1,54 @@ +package pr2.lambda.nonstatic; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Point; + +/** + * Ein Alien. + */ +public class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Aliens X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Alien(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("pr2/lambda/static_member", + "ship01", "ship02", "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("pr2/lambda/static_member/explosion_1.png", + 43))); + setInvisibleAfterFrames(70); + } + + /** + * Klick auf das Alien lässt es explodieren. + */ + @Override + public void mousePressed() { + explode(); + } +} diff --git a/solutions/src/main/java/pr2/lambda/nonstatic/GameBoard.java b/solutions/src/main/java/pr2/lambda/nonstatic/GameBoard.java new file mode 100644 index 0000000..a8ce053 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/nonstatic/GameBoard.java @@ -0,0 +1,108 @@ +package pr2.lambda.nonstatic; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.util.Random; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Alien, das durch das Bild läuft. + */ + private final Alien alien; + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(800, 300), Color.BLACK); + + // Alien initialisieren + alien = new Alien(new Point(800, 50 + new Random().nextInt(100))); + + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGame(Graphics g) { + // Alien zeichnen + alien.draw(g, this); + } + + /** + * Game-Over-Text anzeigen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGameOver(Graphics g) { + centerText(g, "Das Spiel ist aus!"); + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + alien.move(); + return alien.isVisible(); + } + + + /** + * Ein Alien. + */ + public class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Alien X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param startPoint Start-Position + */ + public Alien(Point startPoint) { + super(GameBoard.this, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("assets", "ship01", "ship02", + "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + + GameBoard.this.addMouseListener(this); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("assets" + "/explosion_1.png", 43))); + setInvisibleAfterFrames(70); + } + + /** + * Klick auf das Alien lässt es explodieren. + */ + @Override + public void mousePressed() { + explode(); + } + } +} diff --git a/solutions/src/main/java/pr2/lambda/nonstatic/GameMain.java b/solutions/src/main/java/pr2/lambda/nonstatic/GameMain.java new file mode 100644 index 0000000..d4a3d5a --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/nonstatic/GameMain.java @@ -0,0 +1,27 @@ +package pr2.lambda.nonstatic; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click Alien - (02) Nonstatic Member", new GameBoard()); + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/lambda/static_member/Alien.java b/solutions/src/main/java/pr2/lambda/static_member/Alien.java new file mode 100644 index 0000000..21722ee --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/static_member/Alien.java @@ -0,0 +1,54 @@ +package pr2.lambda.static_member; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Point; + +/** + * Ein Alien. + */ +public class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Aliens X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Alien(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("pr2/lambda/static_member", + "ship01", "ship02", "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("pr2/lambda/static_member/explosion_1.png", + 43))); + setInvisibleAfterFrames(70); + } + + /** + * Klick auf das Alien lässt es explodieren. + */ + @Override + public void mousePressed() { + explode(); + } +} diff --git a/solutions/src/main/java/pr2/lambda/static_member/GameBoard.java b/solutions/src/main/java/pr2/lambda/static_member/GameBoard.java new file mode 100644 index 0000000..c1985ff --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/static_member/GameBoard.java @@ -0,0 +1,111 @@ +package pr2.lambda.static_member; + +import de.smits_net.games.framework.board.Board; +import de.smits_net.games.framework.image.AnimatedImage; +import de.smits_net.games.framework.image.ImagePack; +import de.smits_net.games.framework.image.StripedImage; +import de.smits_net.games.framework.sprite.AnimatedSprite; +import de.smits_net.games.framework.sprite.Direction; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Point; +import java.util.Random; + +/** + * Spielfeld. + */ +public class GameBoard extends Board { + + /** + * Alien, das durch das Bild läuft. + */ + private final Alien alien; + + /** + * Erzeugt ein neues Board. + */ + public GameBoard() { + // neues Spielfeld anlegen + super(10, new Dimension(800, 300), Color.BLACK); + + // Alien initialisieren + alien = new Alien(this, + new Point(800, 50 + new Random().nextInt(100))); + + // Alien soll auf Maus-Klicks reagieren + addMouseListener(alien); + } + + /** + * Spielfeld neu zeichnen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGame(Graphics g) { + // Alien zeichnen + alien.draw(g, this); + } + + /** + * Game-Over-Text anzeigen. Wird vom Framework aufgerufen. + */ + @Override + public void drawGameOver(Graphics g) { + centerText(g, "Das Spiel ist aus!"); + } + + /** + * Spielsituation updaten. Wird vom Framework aufgerufen. + */ + @Override + public boolean updateGame() { + alien.move(); + return alien.isVisible(); + } + + + /** + * Ein Alien. + */ + public static class Alien extends AnimatedSprite { + + /** + * Geschwindigkeit des Aliens X-Richtung. + */ + private static final int ALIEN_SPEED = 2; + + /** + * Neues Alien anlegen. + * + * @param board das Spielfeld + * @param startPoint Start-Position + */ + public Alien(Board board, Point startPoint) { + super(board, startPoint, BoundaryPolicy.JUMP_BACK, + new AnimatedImage(50, + new ImagePack("pr2/lambda/static_member", + "ship01", "ship02", "ship03"))); + velocity.setVelocity(Direction.WEST, ALIEN_SPEED); + } + + /** + * Alien explodieren lassen. + */ + public void explode() { + setActive(false); + setImages(new AnimatedImage(20, + new StripedImage("pr2/lambda/static_member" + + "/explosion_1.png", 43))); + setInvisibleAfterFrames(70); + } + + /** + * Klick auf das Alien lässt es explodieren. + */ + @Override + public void mousePressed() { + explode(); + } + } +} diff --git a/solutions/src/main/java/pr2/lambda/static_member/GameMain.java b/solutions/src/main/java/pr2/lambda/static_member/GameMain.java new file mode 100644 index 0000000..1011145 --- /dev/null +++ b/solutions/src/main/java/pr2/lambda/static_member/GameMain.java @@ -0,0 +1,27 @@ +package pr2.lambda.static_member; + +import de.smits_net.games.framework.board.MainWindow; + +import java.awt.EventQueue; + +/** + * Hauptklasse des Spiels. + */ +public class GameMain extends MainWindow { + + /** + * Neues Spiel anlegen. + */ + public GameMain() { + super("Click Alien - (01) Static Member", new GameBoard()); + } + + /** + * Startpunkt. + * + * @param args command line arguments. + */ + public static void main(String[] args) { + EventQueue.invokeLater(GameMain::new); + } +} diff --git a/solutions/src/main/java/pr2/object/clone_alien/Alien.java b/solutions/src/main/java/pr2/object/clone_alien/Alien.java new file mode 100644 index 0000000..b993a4a --- /dev/null +++ b/solutions/src/main/java/pr2/object/clone_alien/Alien.java @@ -0,0 +1,100 @@ +package pr2.object.clone_alien; + +/** + * Ein Alien. + */ +public class Alien implements Cloneable { + + /** + * Name des Aliens. + */ + private final String name; + + /** + * Raumanzug des Aliens. + */ + private Raumanzug raumanzug; + + /** + * Erzeugt ein neues Alien. + * + * @param name Name des Aliens. + * @param raumanzug Anzug. + */ + public Alien(String name, Raumanzug raumanzug) { + this.name = name; + this.raumanzug = raumanzug; + } + + /** + * Gibt den Namen des Aliens zurück. + * + * @return Name des Aliens. + */ + public String getName() { + return name; + } + + /** + * Gibt den Anzug zurück. + * + * @return der Anzug. + */ + public Raumanzug getAnzug() { + return raumanzug; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((raumanzug + == null) ? 0 : raumanzug.hashCode()); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Alien other = (Alien) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } + else if (!name.equals(other.name)) { + return false; + } + if (raumanzug == null) { + return other.raumanzug == null; + } + else { + return raumanzug.equals(other.raumanzug); + } + } + + /** + * @see java.lang.Object#clone() + */ + @Override + public Object clone() throws CloneNotSupportedException { + Alien o = (Alien) super.clone(); + o.raumanzug = (Raumanzug) raumanzug.clone(); + return o; + } +} diff --git a/solutions/src/main/java/pr2/object/clone_alien/Raumanzug.java b/solutions/src/main/java/pr2/object/clone_alien/Raumanzug.java new file mode 100644 index 0000000..ae4171e --- /dev/null +++ b/solutions/src/main/java/pr2/object/clone_alien/Raumanzug.java @@ -0,0 +1,75 @@ +package pr2.object.clone_alien; + +/** + * Ein Raumanzug. + */ +public class Raumanzug implements Cloneable { + + /** + * Sauerstoffvorrat, der noch im Raumanzug ist. + */ + private double sauerstoffVorrat; + + /** + * Ertzeugt einen neuen Raumanzug. + */ + public Raumanzug() { + sauerstoffVorrat = Math.random(); + } + + /** + * Sauerstoffvorrat im Anzug. + * + * @return Vorrat in % (0.0-1.0) + */ + public double getSauerstoffVorrat() { + return sauerstoffVorrat; + } + + /** + * Tankt den Anzug auf. + */ + public void auftanken() { + sauerstoffVorrat = 1.0; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(sauerstoffVorrat); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Raumanzug other = (Raumanzug) obj; + return Double.doubleToLongBits(sauerstoffVorrat) + == Double.doubleToLongBits(other.sauerstoffVorrat); + } + + /** + * @see java.lang.Object#clone() + */ + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} diff --git a/solutions/src/main/java/pr2/object/clone_alien/test/AlienCloneTest.java b/solutions/src/main/java/pr2/object/clone_alien/test/AlienCloneTest.java new file mode 100644 index 0000000..4248732 --- /dev/null +++ b/solutions/src/main/java/pr2/object/clone_alien/test/AlienCloneTest.java @@ -0,0 +1,37 @@ +package pr2.object.clone_alien.test; + +import org.junit.jupiter.api.Test; +import pr2.object.clone_alien.Alien; +import pr2.object.clone_alien.Raumanzug; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +/** + * Test für die Clone-Methoden. + */ +public class AlienCloneTest { + + /** + * Test-Methode. + * + * @throws CloneNotSupportedException wird geworfen, wenn clone_alien + * nicht korrekt implementiert wurde. + */ + @Test + void testClone() throws CloneNotSupportedException { + Raumanzug r1 = new Raumanzug(); + Alien a1 = new Alien("Predator", r1); + + Alien a2 = (Alien) a1.clone(); + Raumanzug r2 = a2.getAnzug(); + + assertNotSame(a1, a2); + assertNotSame(r1, r2); + + assertEquals(a1, a2); + assertEquals(r1, r2); + assertEquals(r1.getSauerstoffVorrat(), r2.getSauerstoffVorrat(), + 0.0001); + } +} diff --git a/solutions/src/main/java/pr2/object/clone_person/Person.java b/solutions/src/main/java/pr2/object/clone_person/Person.java new file mode 100644 index 0000000..8f69899 --- /dev/null +++ b/solutions/src/main/java/pr2/object/clone_person/Person.java @@ -0,0 +1,34 @@ +package pr2.object.clone_person; + +import java.util.Date; + +public class Person implements Cloneable { + + private final String vorname = ""; + private final String nachname = ""; + private Date geburtsdatum = new Date(); + + @Override + public Object clone() { + Person clone; + try { + clone = (Person) super.clone(); + clone.geburtsdatum = (Date) geburtsdatum.clone(); + return clone; + } + catch (CloneNotSupportedException e) { + // cannot happen + return null; + } + } + + + @Override + public String toString() { + return "Person{" + + "vorname='" + vorname + '\'' + + ", nachname='" + nachname + '\'' + + ", geburtsdatum=" + geburtsdatum + + '}'; + } +} diff --git a/solutions/src/main/java/pr2/object/equals_hashcode/Ork.java b/solutions/src/main/java/pr2/object/equals_hashcode/Ork.java new file mode 100644 index 0000000..abae1a9 --- /dev/null +++ b/solutions/src/main/java/pr2/object/equals_hashcode/Ork.java @@ -0,0 +1,57 @@ +package pr2.object.equals_hashcode; + +/** + * Ork. + */ +public class Ork extends Wesen { + + /** + * Stärke der Orks. + */ + private static final int ORK_STAERKE = 17; + + /** + * Zeigt an, ob der Ork gut oder böse ist. + */ + private final boolean boese; + + /** + * Legt einen neuen Ork an. + * + * @param name Name des Orks. + * @param boese zeigt an, ob der Ork gut oder böse ist + */ + public Ork(String name, boolean boese) { + super(name, ORK_STAERKE); + this.boese = boese; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + (boese ? 1231 : 1237); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Ork other = (Ork) obj; + return boese == other.boese; + } +} diff --git a/solutions/src/main/java/pr2/object/equals_hashcode/Wesen.java b/solutions/src/main/java/pr2/object/equals_hashcode/Wesen.java new file mode 100644 index 0000000..424641c --- /dev/null +++ b/solutions/src/main/java/pr2/object/equals_hashcode/Wesen.java @@ -0,0 +1,66 @@ +package pr2.object.equals_hashcode; + +/** + * Ein Wesen in der Spielwelt. + */ +public class Wesen { + + /** + * Name des Wesens. + */ + private final String name; + + /** + * Stärke des Wesens. + */ + private final int staerke; + + /** + * Legt ein neues Wesen an. + * + * @param name Name des Wesens. + * @param staerke Stärke des Wesens. + */ + public Wesen(String name, int staerke) { + this.name = name; + this.staerke = staerke; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + staerke; + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Wesen other = (Wesen) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } + else if (!name.equals(other.name)) { + return false; + } + return staerke == other.staerke; + } +} diff --git a/solutions/src/main/java/pr2/object/equals_hashcode/test/WesenTest.java b/solutions/src/main/java/pr2/object/equals_hashcode/test/WesenTest.java new file mode 100644 index 0000000..bdbe397 --- /dev/null +++ b/solutions/src/main/java/pr2/object/equals_hashcode/test/WesenTest.java @@ -0,0 +1,87 @@ +package pr2.object.equals_hashcode.test; + +import org.junit.jupiter.api.Test; +import pr2.object.equals_hashcode.Ork; +import pr2.object.equals_hashcode.Wesen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +/** + * Testklasse für die Wesen. + */ +public class WesenTest { + + /** + * Testet equals und hashCode von Wesen. + */ + @Test + void testWesen() { + Wesen w1 = new Wesen("Azok", 30); + Wesen w2 = new Wesen("Azok", 30); + Wesen w3 = new Wesen("Azok", 31); + Wesen w4 = new Wesen("Gorrok", 30); + Wesen w5 = new Wesen("Gorrok", 31); + + assertNotSame(w1, w2); + assertEquals(w1, w2); + + assertNotSame(w1, w3); + assertNotEquals(w1, w3); + + assertNotSame(w1, w4); + assertNotEquals(w1, w4); + + assertNotSame(w1, w5); + assertNotEquals(w1, w5); + + assertEquals(w1.hashCode(), w2.hashCode()); + } + + /** + * Testet equals und hashCode von Ork. + */ + @Test + void testOrk() { + Ork o1 = new Ork("Snagas", true); + Ork o2 = new Ork("Snagas", true); + Ork o3 = new Ork("Snagas", false); + Ork o4 = new Ork("Berg-Ork", false); + Ork o5 = new Ork("Berg-Ork", true); + Ork o6 = new Ork("Berg-Ork", false); + Wesen w = new Wesen("Snagas", 17); + + assertNotSame(o1, o2); + assertEquals(o1, o2); + + assertNotSame(o1, o3); + assertNotEquals(o1, o3); + + assertNotSame(o1, o4); + assertNotEquals(o1, o4); + + assertNotSame(o1, o5); + assertNotEquals(o1, o5); + + assertEquals(o1.hashCode(), o2.hashCode()); + assertEquals(o4.hashCode(), o6.hashCode()); + + assertNotEquals(w, o1); + assertNotEquals(w, o2); + assertNotEquals(w, o3); + assertNotEquals(null, o1); + assertNotEquals("Hugo", o1); + assertEquals(o1, o1); + + // Diese Tests muss nicht zwangsweise funktionieren, da + // ungleiche Objekte einen identischen Hashcode haben können. + // Wir gehen hier aber davon aus, dass eine Hashkollision + // sehr selten ist und machen daher aus didaktischen Gründen + // diesen Test. + assertNotEquals(o1.hashCode(), o3.hashCode()); + assertNotEquals(o1.hashCode(), o4.hashCode()); + assertNotEquals(o1.hashCode(), o5.hashCode()); + assertNotEquals(o4.hashCode(), o5.hashCode()); + } +} diff --git a/solutions/src/main/java/pr2/object/equals_person/Person.java b/solutions/src/main/java/pr2/object/equals_person/Person.java new file mode 100644 index 0000000..b45948d --- /dev/null +++ b/solutions/src/main/java/pr2/object/equals_person/Person.java @@ -0,0 +1,54 @@ +package pr2.object.equals_person; + +public class Person { + + private String vorname; + private String nachname; + private int alter; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + alter; + result = prime * result + + ((nachname == null) ? 0 : nachname.hashCode()); + result = prime * result + + ((vorname == null) ? 0 : vorname.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (this == obj) { + return true; + } + if (getClass() != obj.getClass()) { + return false; + } + + Person other = (Person) obj; + + if (alter != other.alter) { + return false; + } + + if (nachname == null) { + if (other.nachname != null) { + return false; + } + } + else if (!nachname.equals(other.nachname)) { + return false; + } + if (vorname == null) { + return other.vorname == null; + } + else { + return vorname.equals(other.vorname); + } + } +} diff --git a/solutions/src/main/java/pr2/object/tostring/Alien.java b/solutions/src/main/java/pr2/object/tostring/Alien.java new file mode 100644 index 0000000..0a18c04 --- /dev/null +++ b/solutions/src/main/java/pr2/object/tostring/Alien.java @@ -0,0 +1,56 @@ +package pr2.object.tostring; + +/** + * Ein Alien. + */ +public class Alien implements Cloneable { + + /** + * Name des Aliens. + */ + private final String name; + + /** + * Raumanzug des Aliens. + */ + private final Raumanzug raumanzug; + + /** + * Erzeugt ein neues Alien. + * + * @param name Name des Aliens. + * @param raumanzug Anzug. + */ + public Alien(String name, Raumanzug raumanzug) { + this.name = name; + this.raumanzug = raumanzug; + } + + /** + * Gibt den Namen des Aliens zurück. + * + * @return Name des Aliens. + */ + public String getName() { + return name; + } + + /** + * Gibt den Anzug zurück. + * + * @return der Anzug. + */ + public Raumanzug getAnzug() { + return raumanzug; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format( + "Ich bin ein Alien und heiße '%s'. Ich trage " + "einen %s.", + name, raumanzug); + } +} diff --git a/solutions/src/main/java/pr2/object/tostring/Ausgabe.java b/solutions/src/main/java/pr2/object/tostring/Ausgabe.java new file mode 100644 index 0000000..637f3c6 --- /dev/null +++ b/solutions/src/main/java/pr2/object/tostring/Ausgabe.java @@ -0,0 +1,24 @@ +package pr2.object.tostring; + +/** + * Ausgabe. + */ +public final class Ausgabe { + + /** + * Konstruktor. + */ + private Ausgabe() { + // keine Objekte. + } + + /** + * Einstiegspunkt in das Programm. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + Alien alien = new Alien("E.T.", new Raumanzug("Spaceman 2000")); + System.out.println(alien); + } +} diff --git a/solutions/src/main/java/pr2/object/tostring/Raumanzug.java b/solutions/src/main/java/pr2/object/tostring/Raumanzug.java new file mode 100644 index 0000000..795e95e --- /dev/null +++ b/solutions/src/main/java/pr2/object/tostring/Raumanzug.java @@ -0,0 +1,52 @@ +package pr2.object.tostring; + +/** + * Ein Raumanzug. + */ +public class Raumanzug implements Cloneable { + + /** + * Hersteller des Raumanzuges. + */ + private final String hersteller; + + /** + * Sauerstoffvorrat, der noch im Raumanzug ist. + */ + private double sauerstoffVorrat; + + /** + * Ertzeugt einen neuen Raumanzug. + * + * @param hersteller Hersteller des Anzuges. + */ + public Raumanzug(String hersteller) { + this.hersteller = hersteller; + sauerstoffVorrat = Math.random(); + } + + /** + * Sauerstoffvorrat im Anzug. + * + * @return Vorrat in % (0.0-1.0) + */ + public double getSauerstoffVorrat() { + return sauerstoffVorrat; + } + + /** + * Tankt den Anzug auf. + */ + public void auftanken() { + sauerstoffVorrat = 1.0; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("Raumanzug von %s (Füllstand %d%%)", hersteller, + (int) (sauerstoffVorrat * 100)); + } +} diff --git a/solutions/src/main/java/pr2/object/wrapper_boxsearch/BoxSearcher.java b/solutions/src/main/java/pr2/object/wrapper_boxsearch/BoxSearcher.java new file mode 100644 index 0000000..7370cda --- /dev/null +++ b/solutions/src/main/java/pr2/object/wrapper_boxsearch/BoxSearcher.java @@ -0,0 +1,18 @@ +package pr2.object.wrapper_boxsearch; + +public class BoxSearcher { + + public static void main(String[] args) { + Integer[] array = new Integer[200]; + + for (int i = 0; i < array.length; i++) { + array[i] = i; + } + + for (Integer element : array) { + boolean identisch = (element == Integer.valueOf( + element.intValue())); + System.out.printf("%d: %s%n", element, identisch); + } + } +} diff --git a/solutions/src/main/java/pr2/object/wrapper_umwandler/Umwandler.java b/solutions/src/main/java/pr2/object/wrapper_umwandler/Umwandler.java new file mode 100644 index 0000000..53090d4 --- /dev/null +++ b/solutions/src/main/java/pr2/object/wrapper_umwandler/Umwandler.java @@ -0,0 +1,47 @@ +package pr2.object.wrapper_umwandler; + +/** + * Enthält Methoden, um Integer-Zahlen umzuwandeln. + */ +public final class Umwandler { + + /** + * Konstruktor. + */ + private Umwandler() { + // keine Instanzen + } + + /** + * Wandelt eine Zahl in meherer Formate um. + * + * @param i die Zahl. + * @return String mit mehreren Formaten. + */ + public static String toString(int i) { + return "Dezimal: " + i + "\n" + "Hex : " + Integer.toHexString(i) + + "\n" + "Oktal : " + Integer.toOctalString(i) + "\n" + + "Binär : " + Integer.toBinaryString(i); + } + + /** + * Wandelt einen String (zur Basis 10) in eine Zahl um. + * + * @param s String, der umgewandelt werden soll + * @return Umgewandelte Zahl + */ + public static int fromString(String s) { + return Integer.parseInt(s); + } + + /** + * Einstiegspunkt in das Programm. + * + * @param args Kommandozeilenargumente. + */ + public static void main(String[] args) { + String s = "17871"; + int i = fromString(s); + System.out.println(toString(i)); + } +} diff --git a/solutions/src/main/java/pr2/strukturierung/information_hiding/Rechner.java b/solutions/src/main/java/pr2/strukturierung/information_hiding/Rechner.java new file mode 100644 index 0000000..b85b71d --- /dev/null +++ b/solutions/src/main/java/pr2/strukturierung/information_hiding/Rechner.java @@ -0,0 +1,6 @@ +package pr2.strukturierung.information_hiding; + +/** + * Ein einfacher Taschenrechner. + */ +public class Rechner { diff --git a/solutions/src/main/java/pr2/strukturierung/information_hiding/test/RechnerTest.java b/solutions/src/main/java/pr2/strukturierung/information_hiding/test/RechnerTest.java new file mode 100644 index 0000000..264712f --- /dev/null +++ b/solutions/src/main/java/pr2/strukturierung/information_hiding/test/RechnerTest.java @@ -0,0 +1,50 @@ +package pr2.strukturierung.information_hiding.test; + +import org.junit.jupiter.api.Test; +import pr2.strukturierung.information_hiding.Rechner; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit-Test für den Rechner. + */ +public class RechnerTest { + + /** + * Testet alle Grundoperationen des Rechners. + */ + @Test + void testRechner() { + Rechner r = new Rechner(); + + // 2 + 2 = 4 + assertEquals(4.0, r.addiere(2.0, 2.0), 0.0001); + + // 3 - 1 = 2 + assertEquals(2.0, r.subtrahiere(3, 1), 0.0001); + + // 3 * 3 = 9 + assertEquals(9.0, r.multipliziere(3, 3), 0.0001); + + // 2 + 2 * 2 = 6 + r.setSpeicher(r.multipliziere(2, 2)); + assertEquals(6.0, r.addiere(2, r.getSpeicher()), 0.0001); +// // 2 + 2 = 4 +// r.addiere(2.0, 2.0); +// assertEquals(4.0, r.wert, 0.0001); +// +// // 3 - 1 = 2 +// r.subtrahiere(3, 1); +// assertEquals(2.0, r.wert, 0.0001); +// +// // 3 * 3 = 9 +// r.multipliziere(3, 3); +// assertEquals(9.0, r.wert, 0.0001); +// +// // 2 + 2 * 2 = 6 +// r.multipliziere(2, 2); +// r.speichern(); +// r.addiere(2, r.speicher); +// assertEquals(6.0, r.wert, 0.0001); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/finals/A.java b/solutions/src/main/java/pr2/vererbung/finals/A.java new file mode 100644 index 0000000..c698cd3 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/finals/A.java @@ -0,0 +1,32 @@ +package pr2.vererbung.finals; + +/** + * A-Klasse. + */ +public class A { + + /** + * Konstante für die Vorzeichenumkehr. + */ + public static final int KONSTANTE = 100; + + /** + * Addiert zwei Zahlen. Wenn das Ergebnis größer ist als + * der Wert von KONSTANTE, dann wird das + * Vorzeichen umgekehrt. + * + * @param a erster Wert + * @param b zweiter Wert + * @return Ergebnis + */ + public final int add(final int a, final int b) { + final int result = a + b; + + if (result > KONSTANTE) { + return result * -1; + } + else { + return result; + } + } +} diff --git a/solutions/src/main/java/pr2/vererbung/finals/B.java b/solutions/src/main/java/pr2/vererbung/finals/B.java new file mode 100644 index 0000000..4abf03b --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/finals/B.java @@ -0,0 +1,22 @@ +package pr2.vererbung.finals; + +/** + * B-Klasse. + */ +public final class B extends A { + + /** + * Subtrahiert zwei Zahlen. Wenn das Ergebnis kleiner ist als + * der Wert von KONSTANTE, dann wird das + * Vorzeichen umgekehrt. + * + * @param a erster Wert + * @param b zweiter Wert + * @return Ergebnis + */ + public static int sub(final int a, final int b) { + final int result = a + b; + + return (result < KONSTANTE) ? (result * -1) : (result); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie1/Figur.java b/solutions/src/main/java/pr2/vererbung/geometrie1/Figur.java new file mode 100644 index 0000000..193626a --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie1/Figur.java @@ -0,0 +1,14 @@ +package pr2.vererbung.geometrie1; + +public class Figur { + + protected int flaeche; + + protected Figur() { + /* nichts zu tun */ + } + + public int getFlaeche() { + return flaeche; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie1/Main.java b/solutions/src/main/java/pr2/vererbung/geometrie1/Main.java new file mode 100644 index 0000000..803774b --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie1/Main.java @@ -0,0 +1,12 @@ +package pr2.vererbung.geometrie1; + +public class Main { + + public static void main(String[] args) { + Figur f1 = new Rechteck(10, 10); + Figur f2 = new Rechteck(5, 10); + + System.out.println(f1.getFlaeche()); + System.out.println(f2.getFlaeche()); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie1/Rechteck.java b/solutions/src/main/java/pr2/vererbung/geometrie1/Rechteck.java new file mode 100644 index 0000000..b86cbaf --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie1/Rechteck.java @@ -0,0 +1,8 @@ +package pr2.vererbung.geometrie1; + +public class Rechteck extends Figur { + + public Rechteck(int breite, int hoehe) { + flaeche = breite * hoehe; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie2/Figur.java b/solutions/src/main/java/pr2/vererbung/geometrie2/Figur.java new file mode 100644 index 0000000..59536d1 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie2/Figur.java @@ -0,0 +1,18 @@ +package pr2.vererbung.geometrie2; + +public class Figur { + + private final int flaeche; + + +// protected Figur() { +// /* nichts zu tun */ +// } + protected Figur(int flaeche) { + this.flaeche = flaeche; + } + + public int getFlaeche() { + return flaeche; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie2/Main.java b/solutions/src/main/java/pr2/vererbung/geometrie2/Main.java new file mode 100644 index 0000000..bf3ee32 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie2/Main.java @@ -0,0 +1,12 @@ +package pr2.vererbung.geometrie2; + +public class Main { + + public static void main(String[] args) { + Figur f1 = new Rechteck(10, 10); + Figur f2 = new Rechteck(5, 10); + + System.out.println(f1.getFlaeche()); + System.out.println(f2.getFlaeche()); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie2/Rechteck.java b/solutions/src/main/java/pr2/vererbung/geometrie2/Rechteck.java new file mode 100644 index 0000000..62c1f25 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie2/Rechteck.java @@ -0,0 +1,13 @@ +package pr2.vererbung.geometrie2; + + +public class Rechteck extends Figur { + + public Rechteck(int breite, int hoehe) { + super(breite * hoehe); + } + +// public Rechteck(int breite, int hoehe) { +// flaeche = breite * hoehe; +// } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Dreieck.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Dreieck.java new file mode 100644 index 0000000..9a1a5bf --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Dreieck.java @@ -0,0 +1,8 @@ +package pr2.vererbung.geometrie3; + +public class Dreieck extends Figur { + + public Dreieck(int grundseite, int hoehe) { + super(grundseite * hoehe / 2); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Figur.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Figur.java new file mode 100644 index 0000000..983f4ea --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Figur.java @@ -0,0 +1,19 @@ +package pr2.vererbung.geometrie3; + +public class Figur { + + private int flaeche; + + protected Figur() { + /* leer */ + } + + protected Figur(int flaeche) { + this.flaeche = flaeche; + } + + public int getFlaeche() { + return flaeche; + } + +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Gerade.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Gerade.java new file mode 100644 index 0000000..58f2461 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Gerade.java @@ -0,0 +1,17 @@ +package pr2.vererbung.geometrie3; + +public class Gerade extends Figur { + + private final int laenge; + + public Gerade(int laenge) { + this.laenge = laenge; + } + + @Override + public String toString() { + return "Gerade{" + + "laenge=" + laenge + + '}'; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Main.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Main.java new file mode 100644 index 0000000..ef073b9 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Main.java @@ -0,0 +1,12 @@ +package pr2.vererbung.geometrie3; + +public class Main { + + public static void main(String[] args) { + Figur f1 = new Gerade(10); + Figur f2 = new Gerade(5); + + System.out.println(f1.getFlaeche()); + System.out.println(f2.getFlaeche()); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Quadrat.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Quadrat.java new file mode 100644 index 0000000..bc20fa0 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Quadrat.java @@ -0,0 +1,8 @@ +package pr2.vererbung.geometrie3; + +public class Quadrat extends Rechteck { + + public Quadrat(int seitenlaenge) { + super(seitenlaenge, seitenlaenge); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/geometrie3/Rechteck.java b/solutions/src/main/java/pr2/vererbung/geometrie3/Rechteck.java new file mode 100644 index 0000000..a02354c --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/geometrie3/Rechteck.java @@ -0,0 +1,8 @@ +package pr2.vererbung.geometrie3; + +public class Rechteck extends Figur { + + public Rechteck(int breite, int hoehe) { + super(breite * hoehe); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/konstruktoren/Nachtelf.java b/solutions/src/main/java/pr2/vererbung/konstruktoren/Nachtelf.java new file mode 100644 index 0000000..852640b --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/konstruktoren/Nachtelf.java @@ -0,0 +1,47 @@ +package pr2.vererbung.konstruktoren; + +/** + * Nachtelf aus der Allianz. + */ +public class Nachtelf extends Wesen { + + /** + * Standardmäßige Stärke der Fähigkeit Naturwiderstand. + */ + public static final int STANDARD_NATURWIDERSTAND = 5; + + /** + * Fähigkeit zum Widerstand gegen Naturmagie. + */ + private int naturwiderstand; + + /** + * Legt einen neuen Nachtelf an. + * + * @param name Name der Figur. + * @param naturwiderstand Stärke der Fähigkeit Naturwiderstand. + */ + public Nachtelf(String name, int naturwiderstand) { + super(name); + this.naturwiderstand = naturwiderstand; + } + + /** + * Legt einen neuen Nachtelf an. Die Stärke des Naturwiderstandes entspricht + * dem Standardwert. + * + * @param name Name der Figur. + */ + public Nachtelf(String name) { + this(name, STANDARD_NATURWIDERSTAND); + } + + /** + * Stärke des Naturwiderstandes. + * + * @return the naturwiderstand + */ + public int getNaturwiderstand() { + return naturwiderstand; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/konstruktoren/Untoter.java b/solutions/src/main/java/pr2/vererbung/konstruktoren/Untoter.java new file mode 100644 index 0000000..7505875 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/konstruktoren/Untoter.java @@ -0,0 +1,47 @@ +package pr2.vererbung.konstruktoren; + +/** + * Untoter aus der Horde. + */ +public class Untoter extends Wesen { + + /** + * Standardmäßige Stärke der Fähigkeit zur Unterwasseratmung. + */ + public static final int STANDARD_UNTERWASSERATMUNG = 10; + + /** + * Fähigkeit zur Atmung unter Wasser. + */ + private int unterwasseratmung; + + /** + * Legt einen neuen Untoten an. + * + * @param name Name der Figur. + * @param unterwasseratmung Stärke der Fähigkeit zur Unterwasseratmung. + */ + public Untoter(String name, int unterwasseratmung) { + super(name); + this.unterwasseratmung = unterwasseratmung; + } + + /** + * Legt einen neuen Untoten an. Die Stärke der Unterwasseratmung entspricht + * dem Standardwert. + * + * @param name Name der Figur. + */ + public Untoter(String name) { + this(name, STANDARD_UNTERWASSERATMUNG); + } + + /** + * Fähigkeit zur Unterwasseratmnung. + * + * @return the Stärke der Fähigkeit. + */ + public int getUnterwasseratmung() { + return unterwasseratmung; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/konstruktoren/Wesen.java b/solutions/src/main/java/pr2/vererbung/konstruktoren/Wesen.java new file mode 100644 index 0000000..0287b62 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/konstruktoren/Wesen.java @@ -0,0 +1,30 @@ +package pr2.vererbung.konstruktoren; + +/** + * Basisklasse für Spielfiguren. + */ +public class Wesen { + + /** + * Name der Spielfigur. + */ + private final String name; + + /** + * Legt eine neue Spielfigur an. + * + * @param name Name der Figur. + */ + public Wesen(String name) { + this.name = name; + } + + /** + * Gibt den Namen der Spielfigur zurück. + * + * @return Name der Spielfigur. + */ + public String getName() { + return name; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/konstruktoren/test/WesenTest.java b/solutions/src/main/java/pr2/vererbung/konstruktoren/test/WesenTest.java new file mode 100644 index 0000000..3587cf9 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/konstruktoren/test/WesenTest.java @@ -0,0 +1,47 @@ +package pr2.vererbung.konstruktoren.test; + +import org.junit.jupiter.api.Test; +import pr2.vererbung.konstruktoren.Nachtelf; +import pr2.vererbung.konstruktoren.Untoter; +import pr2.vererbung.konstruktoren.Wesen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test für die Aufgabe. + */ +public class WesenTest { + + /** + * Testet den Nachtelf. + */ + @Test + void testNachtelf() { + assertTrue(new Nachtelf("") instanceof Wesen, + "Nachtelf erbt nicht " + "von Wesen"); + + assertEquals(Nachtelf.STANDARD_NATURWIDERSTAND, + new Nachtelf("").getNaturwiderstand()); + + assertEquals(120, new Nachtelf("", 120).getNaturwiderstand()); + + assertEquals("Hugo", new Nachtelf("Hugo").getName()); + } + + /** + * Testet den Untoten. + */ + @Test + void testUntoten() { + assertTrue(new Untoter("") instanceof Wesen, + "Untoter erbt nicht von " + "Wesen"); + + assertEquals(Untoter.STANDARD_UNTERWASSERATMUNG, + new Untoter("").getUnterwasseratmung()); + + assertEquals(120, new Untoter("", 120).getUnterwasseratmung()); + + assertEquals("Hugo", new Untoter("Hugo").getName()); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/Affe.java b/solutions/src/main/java/pr2/vererbung/polymorphie/Affe.java new file mode 100644 index 0000000..612e4ee --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/Affe.java @@ -0,0 +1,16 @@ +package pr2.vererbung.polymorphie; + +/** + * Ein Affe im Zoo. + */ +public class Affe extends ZooTier { + + /** + * Legt einen neuen Affen an. + * + * @param name Name des Affen. + */ + public Affe(String name) { + super(name); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/Futterstelle.java b/solutions/src/main/java/pr2/vererbung/polymorphie/Futterstelle.java new file mode 100644 index 0000000..a378303 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/Futterstelle.java @@ -0,0 +1,16 @@ +package pr2.vererbung.polymorphie; + +/** + * Fütterung der Tiere. + */ +public class Futterstelle { + + /** + * Füttert das Tier. + * + * @param tier Tier, das gefüttert werden soll. + */ + public void gibFutter(ZooTier tier) { + tier.fuettern(); + } + diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/Giraffe.java b/solutions/src/main/java/pr2/vererbung/polymorphie/Giraffe.java new file mode 100644 index 0000000..1c4cf69 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/Giraffe.java @@ -0,0 +1,16 @@ +package pr2.vererbung.polymorphie; + +/** + * Eine Giraffe im Zoo. + */ +public class Giraffe extends ZooTier { + + /** + * Legt einen neue Giraffe an. + * + * @param name Name der Giraffe. + */ + public Giraffe(String name) { + super(name); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/Gorilla.java b/solutions/src/main/java/pr2/vererbung/polymorphie/Gorilla.java new file mode 100644 index 0000000..fd6f23d --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/Gorilla.java @@ -0,0 +1,16 @@ +package pr2.vererbung.polymorphie; + +/** + * Ein Gorilla. + */ +public class Gorilla extends Affe { + + /** + * Legt einen neuen Gorilla an. + * + * @param name Name des Gorilla. + */ + public Gorilla(String name) { + super(name); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/ZooSimulation.java b/solutions/src/main/java/pr2/vererbung/polymorphie/ZooSimulation.java new file mode 100644 index 0000000..11db908 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/ZooSimulation.java @@ -0,0 +1,39 @@ +package pr2.vererbung.polymorphie; + +/** + * Simulationsklasse. + */ +public final class ZooSimulation { + + /** + * Konstruktor. + */ + private ZooSimulation() { + // keine Objekte benötigt + } + + /** + * Main-Methode. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + Futterstelle futterstelle = new Futterstelle(); + + ZooTier[] tiere = {new Affe("Charlie"), new Gorilla("Buck"), + new Giraffe("Debbie")}; + + for (ZooTier tier : tiere) { + System.out.println(tier); + } + + System.out.println("Fütterung..."); + + for (ZooTier tier : tiere) { + futterstelle.gibFutter(tier); + } + + for (ZooTier tier : tiere) { + System.out.println(tier); + } + } diff --git a/solutions/src/main/java/pr2/vererbung/polymorphie/ZooTier.java b/solutions/src/main/java/pr2/vererbung/polymorphie/ZooTier.java new file mode 100644 index 0000000..ff9edf9 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/polymorphie/ZooTier.java @@ -0,0 +1,41 @@ +package pr2.vererbung.polymorphie; + +/** + * Ein Tier im Zoo. + */ +public class ZooTier { + + /** + * Name des Tiers. + */ + private final String name; + + /** + * Zeigt an, ob das Tier hungrig ist. + */ + private boolean hungrig = true; + + /** + * Legt ein neues Zootier an. + * + * @param name Name des Tiers + */ + public ZooTier(String name) { + this.name = name; + } + + /** + * Gibt dem Tier futter. + */ + public void fuettern() { + hungrig = false; + } + + /** + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return name + ": Ich bin " + (hungrig ? "hungrig" : "satt") + "!"; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java b/solutions/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java new file mode 100644 index 0000000..f471b44 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java @@ -0,0 +1,12 @@ +package pr2.vererbung.singleton_einfach; + +public final class Singleton { + + private static final Singleton INSTANCE = new Singleton(); + + private Singleton() { /* leer */ } + + public static Singleton getInstance() { + return INSTANCE; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/statics/Spiel.java b/solutions/src/main/java/pr2/vererbung/statics/Spiel.java new file mode 100644 index 0000000..afa174a --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/statics/Spiel.java @@ -0,0 +1,33 @@ +package pr2.vererbung.statics; + +/** + * Spiel mit den Würfeln. + */ +public final class Spiel { + + /** + * Keine Objekte. + */ + private Spiel() { + // leer + } + + /** + * Programm-Einstieg. + * + * @param args Kommandozeilen-Argumente. + */ + public static void main(String[] args) { + Wuerfel wuerfel1 = new Wuerfel(); + Wuerfel wuerfel2 = new Wuerfel(); + + // 1000 Mal würfeln + for (int i = 0; i < 1000; i++) { + wuerfel1.wuerfele(); + wuerfel2.wuerfele(); + } + + // Statistik ausgeben + System.out.println("Statistik für alle Würfel"); + System.out.println(Wuerfel.statistik()); + System.out.println(); diff --git a/solutions/src/main/java/pr2/vererbung/statics/Wuerfel.java b/solutions/src/main/java/pr2/vererbung/statics/Wuerfel.java new file mode 100644 index 0000000..98856c1 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/statics/Wuerfel.java @@ -0,0 +1,52 @@ +package pr2.vererbung.statics; + +import java.util.Random; + +/** + * Ein einfacher Würfel. + */ +public class Wuerfel { + + /** Häufigkeit der Werte. */ + private static final int[] HAEUFIGKEIT = new int[6]; + + /** Anzahl der Würfe mit dem Würfel. */ + private static int wuerfe = 0; + + /** Zufallsgenerator. */ + private final Random random = new Random(); + + /** + * Bestimmt den nächsten Wurf. + * + * @return der Wurf. + */ + public int wuerfele() { + int wert = random.nextInt(6); + HAEUFIGKEIT[wert]++; + wuerfe++; + return wert + 1; + } + + /** + * Gibt die Häufigkeit der Würfe zurück. + * + * @return die Statistik mit der Häufigkeit. + */ + public static String statistik() { + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < HAEUFIGKEIT.length; i++) { + result.append(i + 1) + .append(": ") + .append(String.format("%.2f %%", + ((double) HAEUFIGKEIT[i] / wuerfe) * 100)) + .append("\n"); + } + + result.append("Summe: ") + .append(wuerfe) + .append("\n"); + + return result.toString(); + } diff --git a/solutions/src/main/java/pr2/vererbung/ueberladen_summe/Summator.java b/solutions/src/main/java/pr2/vererbung/ueberladen_summe/Summator.java new file mode 100644 index 0000000..7f0bfa1 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberladen_summe/Summator.java @@ -0,0 +1,20 @@ +package pr2.vererbung.ueberladen_summe; + +public class Summator { + + public int sum(int a, int b) { + return a + b; + } + + public int sum(int a, int b, int c) { + return a + b + c; + } + + public int sum(int a, int b, int c, int d) { + return a + b + c + d; + } + + public int sum(int a, int b, int c, int d, int e) { + return a + b + c + d + e; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/ueberladen_summe/test/SummatorTest.java b/solutions/src/main/java/pr2/vererbung/ueberladen_summe/test/SummatorTest.java new file mode 100644 index 0000000..d20882e --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberladen_summe/test/SummatorTest.java @@ -0,0 +1,22 @@ +package pr2.vererbung.ueberladen_summe.test; + +import org.junit.jupiter.api.Test; +import pr2.vererbung.ueberladen_summe.Summator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SummatorTest { + + @Test + void testSum() { + Summator s = new Summator(); + assertEquals(3, s.sum(1, 2)); + assertEquals(1, s.sum(-1, 2)); + assertEquals(6, s.sum(1, 2, 3)); + assertEquals(0, s.sum(1, 2, -3)); + assertEquals(10, s.sum(1, 2, 3, 4)); + assertEquals(2, s.sum(1, 2, 3, -4)); + assertEquals(15, s.sum(1, 2, 3, 4, 5)); + assertEquals(5, s.sum(1, 2, 3, 4, -5)); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java new file mode 100644 index 0000000..86dcc48 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java @@ -0,0 +1,9 @@ +package pr2.vererbung.ueberschreiben_lebewesen; + +public class Amoebe extends Lebewesen { + + @Override + public void bewegen() { + System.out.println("Schleimen"); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java new file mode 100644 index 0000000..89aeb8d --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java @@ -0,0 +1,13 @@ +package pr2.vererbung.ueberschreiben_lebewesen; + +public class Dackel extends Lebewesen { + + @Override + public void bewegen() { + System.out.println("Auf kurzen Beinen vorwärts wackeln"); + } + + public void bellen() { + System.out.println("Wau wau wau"); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java new file mode 100644 index 0000000..85ebef2 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java @@ -0,0 +1,8 @@ +package pr2.vererbung.ueberschreiben_lebewesen; + +public class Lebewesen { + + public void bewegen() { + /* nichts machen */ + } +} diff --git a/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java new file mode 100644 index 0000000..e535a17 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java @@ -0,0 +1,16 @@ +package pr2.vererbung.ueberschreiben_lebewesen; + +public class Main { + + public static void main(String[] args) { + Lebewesen l1 = new Dackel(); + Lebewesen l2 = new Amoebe(); + + l1.bewegen(); + l2.bewegen(); + + if (l1 instanceof Dackel) { + ((Dackel) l1).bellen(); + } + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vararg_summe/Summator.java b/solutions/src/main/java/pr2/vererbung/vararg_summe/Summator.java new file mode 100644 index 0000000..6457e2a --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vararg_summe/Summator.java @@ -0,0 +1,22 @@ +package pr2.vererbung.vararg_summe; + +public class Summator { + + public int sum(int a, int b) { + return a + b; + } + + public int sum(int a, int b, int c) { + return a + b + c; + } + + public int sum(int a, int b, int c, int... as) { + int sum = a + b + c; + + for (int i : as) { + sum += i; + } + + return sum; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java b/solutions/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java new file mode 100644 index 0000000..93aba84 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java @@ -0,0 +1,23 @@ +package pr2.vererbung.vararg_summe.test; + +import org.junit.jupiter.api.Test; +import pr2.vererbung.vararg_summe.Summator; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SummatorTest { + + @Test + void testSum() { + Summator s = new Summator(); + assertEquals(3, s.sum(1, 2)); + assertEquals(1, s.sum(-1, 2)); + assertEquals(6, s.sum(1, 2, 3)); + assertEquals(0, s.sum(1, 2, -3)); + assertEquals(10, s.sum(1, 2, 3, 4)); + assertEquals(2, s.sum(1, 2, 3, -4)); + assertEquals(15, s.sum(1, 2, 3, 4, 5)); + assertEquals(5, s.sum(1, 2, 3, 4, -5)); + assertEquals(36, s.sum(1, 2, 3, 4, 5, 6, 7, 8)); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Dreieck.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Dreieck.java new file mode 100644 index 0000000..70162b9 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Dreieck.java @@ -0,0 +1,17 @@ +package pr2.vererbung.vererbung_geometrie; + +/** + * Ein Dreieck. + */ +public class Dreieck extends Figur { + + /** + * Erzeugt ein neues Dreieck mit den gegebenen Daten. + * + * @param grundseite Grundseite des Dreiecks. + * @param hoehe Höhe des Dreiecks. + */ + public Dreieck(double grundseite, double hoehe) { + super(grundseite * hoehe / 2); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Figur.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Figur.java new file mode 100644 index 0000000..ebe5383 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Figur.java @@ -0,0 +1,37 @@ +package pr2.vererbung.vererbung_geometrie; + +/** + * Basisklasse für geometrische Formen. + */ +public class Figur { + + /** + * Fläche der Figur. + */ + private double flaeche; + + /** + * Legt eine neue Figur mit der Fläche 0 an. + */ + protected Figur() { + /* leer */ + } + + /** + * Legt eine neue Figur mit der gegebenen Fläche an. + * + * @param flaeche Fläche der Figur. + */ + protected Figur(double flaeche) { + this.flaeche = flaeche; + } + + /** + * Liefert die Fläche der Figur. + * + * @return die Fläche. + */ + public double getFlaeche() { + return flaeche; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Gerade.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Gerade.java new file mode 100644 index 0000000..55dbcf3 --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Gerade.java @@ -0,0 +1,31 @@ +package pr2.vererbung.vererbung_geometrie; + +/** + * Eine Gerade. + */ +public class Gerade extends Figur { + + /** + * Länge der Geraden. + */ + private double laenge; + + /** + * Legt eine neue Gerade mit der gegebenen Länge an. + * + * @param laenge Länge der Geraden. + */ + public Gerade(double laenge) { + super(); + this.laenge = laenge; + } + + /** + * Liefert die Länge der Geraden. + * + * @return die Länge. + */ + public double getLaenge() { + return laenge; + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Quadrat.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Quadrat.java new file mode 100644 index 0000000..5e0626f --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Quadrat.java @@ -0,0 +1,16 @@ +package pr2.vererbung.vererbung_geometrie; + +/** + * Qudrat. + */ +public class Quadrat extends Rechteck { //*** replace(public class Quadrat {) + + /** + * Legt ein neues Quadrat an. + * + * @param seitenlaenge Länge der Seiten des Quadrates. + */ + public Quadrat(double seitenlaenge) { + super(seitenlaenge, seitenlaenge); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Rechteck.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Rechteck.java new file mode 100644 index 0000000..e1dd8dc --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/Rechteck.java @@ -0,0 +1,17 @@ +package pr2.vererbung.vererbung_geometrie; + +/** + * Rechteck. + */ +public class Rechteck extends Figur { + + /** + * Legt ein neues Rechteck an. + * + * @param breite Breite. + * @param hoehe Höhe. + */ + public Rechteck(double breite, double hoehe) { + super(breite * hoehe); + } +} diff --git a/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/test/FigurenTest.java b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/test/FigurenTest.java new file mode 100644 index 0000000..446402d --- /dev/null +++ b/solutions/src/main/java/pr2/vererbung/vererbung_geometrie/test/FigurenTest.java @@ -0,0 +1,67 @@ +package pr2.vererbung.vererbung_geometrie.test; + +import org.junit.jupiter.api.Test; +import pr2.vererbung.vererbung_geometrie.Dreieck; +import pr2.vererbung.vererbung_geometrie.Gerade; +import pr2.vererbung.vererbung_geometrie.Quadrat; +import pr2.vererbung.vererbung_geometrie.Rechteck; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests für die Figuren. + */ +public class FigurenTest { + + private static final double PRECISION = 0.0001; + + /** + * Rechteck. + */ + @Test + void testReckteck() { + Rechteck r1 = new Rechteck(4.0, 5.0); + Rechteck r2 = new Rechteck(0.0, 3.0); + assertEquals(20.0, r1.getFlaeche(), PRECISION); + assertEquals(0.0, r2.getFlaeche(), PRECISION); + } + + /** + * Dreieck. + */ + @Test + void testDreieck() { + Dreieck d1 = new Dreieck(6.0, 3.5); + assertEquals(10.5, d1.getFlaeche(), PRECISION); + } + + /** + * Gerade. + */ + @Test + void testGerade() { + Gerade g1 = new Gerade(8.0); + assertEquals(8.0, g1.getLaenge(), PRECISION); + assertEquals(0.0, g1.getFlaeche(), PRECISION); + + g1 = new Gerade(0.0); + assertEquals(0.0, g1.getLaenge(), PRECISION); + assertEquals(0.0, g1.getFlaeche(), PRECISION); + } + + /** + * Quadrat. + */ + @Test + void testQuadrat() { + Quadrat q1 = new Quadrat(3.0); + assertEquals(9.0, q1.getFlaeche(), PRECISION); + + Rechteck r1 = new Rechteck(5.0, 5.0); + Quadrat q2 = new Quadrat(5.0); + assertEquals(r1.getFlaeche(), q2.getFlaeche(), PRECISION); + + q1 = new Quadrat(0.0); + assertEquals(0.0, q1.getFlaeche(), PRECISION); + } +}