From 75e72c3adf9f8fbdfbf559b995a525fc40473ab8 Mon Sep 17 00:00:00 2001 From: Thomas Smits Date: Sat, 18 Feb 2023 15:27:55 +0100 Subject: [PATCH] Updated examples --- src/main/java/pr2/ads/adt/BinaryTree.java | 14 ++ src/main/java/pr2/ads/adt/Queue.java | 8 + src/main/java/pr2/ads/adt/QueueException.java | 4 + src/main/java/pr2/ads/adt/Stack.java | 8 + src/main/java/pr2/ads/adt/StackException.java | 4 + src/main/java/pr2/ads/adt/TreeNode.java | 22 ++ .../java/pr2/ads/searching/BinarySearch.java | 23 ++ .../java/pr2/ads/searching/LinearSearch.java | 14 ++ src/main/java/pr2/ads/searching/Searcher.java | 8 + .../java/pr2/ads/searching/TestSearch.java | 27 +++ .../pr2/ads/shuffle/FisherYatesShuffle.java | 18 ++ .../java/pr2/ads/shuffle/NaiveShuffle.java | 28 +++ src/main/java/pr2/ads/shuffle/Shuffler.java | 12 ++ .../java/pr2/ads/shuffle/TestShuffle.java | 20 ++ src/main/java/pr2/ads/sorting/BubbleSort.java | 24 +++ .../java/pr2/ads/sorting/InsertionSort.java | 28 +++ src/main/java/pr2/ads/sorting/MergeSort.java | 46 ++++ src/main/java/pr2/ads/sorting/QuickSort.java | 47 ++++ .../java/pr2/ads/sorting/SelectionSort.java | 26 +++ src/main/java/pr2/ads/sorting/Sorter.java | 12 ++ .../java/pr2/ads/sorting/TestSorting.java | 75 +++++++ .../ads/textsearch/BoyerMooreTextSearch.java | 49 +++++ .../ads/textsearch/BruteForceTextSearch.java | 22 ++ .../pr2/ads/textsearch/KMPTextSearch.java | 46 ++++ .../pr2/ads/textsearch/TestTextSearch.java | 21 ++ .../java/pr2/ads/textsearch/TextSearch.java | 8 + .../java/pr2/auffrischung/CodingStandard.java | 39 ++++ src/main/java/pr2/auffrischung/Ding.java | 29 +++ src/main/java/pr2/auffrischung/IsPrim.java | 22 ++ .../pr2/auffrischung/Kontrollstrukturen.java | 201 ++++++++++++++++++ .../java/pr2/auffrischung/QuizString.java | 18 ++ src/main/java/pr2/auffrischung/Scope.java | 35 +++ src/main/java/pr2/auffrischung/Zuweisung.java | 34 +++ .../pr2/auffrischung/quiz/IfWithoutBlock.java | 14 ++ .../java/pr2/auffrischung/quiz/Puzzles.java | 113 ++++++++++ .../java/pr2/auffrischung/quiz/Reference.java | 17 ++ src/main/java/pr2/auffrischung/quiz/Swap.java | 22 ++ src/main/java/pr2/ausnahmen/Java7.java | 118 ++++++++++ .../pr2/ausnahmen/callstack/CallStack1.java | 40 ++++ .../pr2/ausnahmen/callstack/CallStack2.java | 29 +++ .../pr2/ausnahmen/callstack/CallStack3.java | 29 +++ .../ausnahmen/callstack/HandleOrDeclare.java | 53 +++++ .../java/pr2/ausnahmen/eigene/Client.java | 26 +++ .../java/pr2/ausnahmen/eigene/Server.java | 21 ++ .../pr2/ausnahmen/eigene/ServerException.java | 24 +++ .../pr2/ausnahmen/eigene/SimpleExample2.java | 27 +++ .../pr2/ausnahmen/eigene/SimpleExamples.java | 25 +++ .../finall/TryCatchFinallyExample.java | 46 ++++ .../finall/TryCatchFinallyExample2.java | 51 +++++ .../java/pr2/ausnahmen/junit/BrokenTest.java | 25 +++ .../pr2/ausnahmen/junit/TestMitExpected.java | 18 ++ .../pr2/ausnahmen/junit/TestMitTryCatch.java | 25 +++ src/main/java/pr2/ausnahmen/quiz/Finally.java | 21 ++ .../pr2/ausnahmen/trycatch/BlossNicht.java | 42 ++++ .../ausnahmen/trycatch/ExceptionExample.java | 16 ++ .../trycatch/FinallyMitSeiteneffekt.java | 23 ++ .../pr2/ausnahmen/trycatch/GanzBoese.java | 22 ++ .../java/pr2/ausnahmen/trycatch/Netzwerk.java | 36 ++++ .../ausnahmen/trycatch/TryCatchExample.java | 40 ++++ .../trycatch/TryCatchExampleZwei.java | 52 +++++ .../pr2/ausnahmen/trycatch/polymorphie/A.java | 12 ++ .../pr2/ausnahmen/trycatch/polymorphie/B.java | 31 +++ src/main/java/pr2/collections/StackDemo.java | 13 ++ .../interfaces/collection/CollectionDemo.java | 54 +++++ .../collection/CollectionsDemo.java | 11 + .../collections/interfaces/list/ListDemo.java | 25 +++ .../interfaces/list/ListDemo2.java | 41 ++++ .../collections/interfaces/set/SetDemo.java | 25 +++ .../pr2/collections/iterable/IterableFor.java | 21 ++ .../collections/iterable/IterableWhile.java | 27 +++ .../iterable/ListIteratorDemo.java | 34 +++ .../eigenes/IterableInnereKlasse.java | 42 ++++ .../iterable/eigenes/IterableMitKlasse.java | 50 +++++ .../iterable/eigenes/IterableSelbst.java | 41 ++++ .../eigenes/IterableSelbstKorrekt.java | 45 ++++ .../pr2/collections/maps/MapBeispiel.java | 41 ++++ .../pr2/collections/maps/MapBeispiel2.java | 26 +++ .../pr2/collections/maps/MapBeispiel3.java | 23 ++ .../pr2/collections/maps/Mitarbeiter.java | 68 ++++++ .../pr2/collections/maps/Personalnummer.java | 45 ++++ .../collections/sorting/ListSortierung.java | 36 ++++ .../collections/sorting/SortedSetDemo.java | 27 +++ .../sorting/comparable/Student.java | 31 +++ .../sorting/comparable/StudentDemo.java | 39 ++++ .../sorting/comparator/Student.java | 19 ++ .../sorting/comparator/StudentComparator.java | 21 ++ .../sorting/comparator/StudentDemo.java | 42 ++++ .../ConcurrentModificationException.java | 34 +++ src/main/java/pr2/ds/avl/AVLTree.java | 118 ++++++++++ src/main/java/pr2/ds/bt/BinaryTree.java | 66 ++++++ src/main/java/pr2/ds/bt/SimpleTree.java | 94 ++++++++ src/main/java/pr2/ds/buffer/RingBuffer.java | 55 +++++ src/main/java/pr2/ds/graph/Graph.java | 54 +++++ .../java/pr2/ds/hash/BetterHashTable.java | 53 +++++ src/main/java/pr2/ds/hash/HashTable.java | 51 +++++ .../java/pr2/ds/hash/SimpleHashTable.java | 45 ++++ src/main/java/pr2/ds/liste/ArrayIterator.java | 24 +++ .../java/pr2/ds/liste/ArrayIteratorTest.java | 14 ++ .../java/pr2/ds/liste/DoubleLinkedList.java | 112 ++++++++++ src/main/java/pr2/ds/liste/IntList.java | 9 + src/main/java/pr2/ds/liste/LinkedList.java | 120 +++++++++++ src/main/java/pr2/ds/liste/Pr2LinkedList.java | 168 +++++++++++++++ .../java/pr2/ds/liste/Pr2LinkedListTest.java | 124 +++++++++++ src/main/java/pr2/ds/liste/Pr2List.java | 61 ++++++ src/main/java/pr2/ds/liste/Pr2Node.java | 23 ++ src/main/java/pr2/ds/liste/TestLists.java | 31 +++ src/main/java/pr2/ds/queue/Queue.java | 42 ++++ src/main/java/pr2/ds/search/BoyerMoore.java | 52 +++++ src/main/java/pr2/ds/search/KMP.java | 38 ++++ src/main/java/pr2/ds/stack/Stack.java | 42 ++++ .../abstraktemethoden/Operation.java | 33 +++ .../abstraktemethoden/Rechner.java | 21 ++ .../enumerationen/classic/DateJuggler.java | 16 ++ .../pr2/enumerationen/classic/Verwender.java | 15 ++ .../pr2/enumerationen/classic/Wochentag.java | 14 ++ .../enumerationen/enummethoden/Planet.java | 15 ++ .../enumerationen/enummethoden/Verwender.java | 17 ++ .../enumerationen/enumset/EnumSetDemo.java | 23 ++ .../pr2/enumerationen/enumset/Planet.java | 16 ++ .../enumset/Textauszeichnung.java | 10 + .../enumset/Textverarbeitung.java | 20 ++ .../pr2/enumerationen/enumset/Todesstern.java | 26 +++ .../enumset/alt/Auszeichnungen.java | 12 ++ .../enumset/alt/Textprogramm.java | 32 +++ .../enumset/neu/Auszeichnungen.java | 8 + .../enumset/neu/Textprogramm.java | 35 +++ .../enumerationen/mitenums/DateJuggler.java | 16 ++ .../pr2/enumerationen/mitenums/Verwender.java | 15 ++ .../pr2/enumerationen/mitenums/Wochentag.java | 8 + .../enumerationen/mitkonstruktor/Ausgabe.java | 14 ++ .../enumerationen/mitkonstruktor/Planet.java | 28 +++ .../mitkonstruktor/SpaceTravel.java | 48 +++++ .../pr2/enumerationen/quiz/Geschlecht.java | 8 + .../pr2/enumerationen/quiz/Verwender.java | 17 ++ .../pr2/enumerationen/quiz/Wochentage.java | 20 ++ .../java/pr2/enumerationen/simple/Planet.java | 10 + src/main/java/pr2/generics/Pair.java | 23 ++ src/main/java/pr2/generics/PairUser.java | 11 + .../pr2/generics/boundparameter/Form.java | 9 + .../generics/boundparameter/FormenStack.java | 39 ++++ .../pr2/generics/boundparameter/Kreis.java | 18 ++ .../pr2/generics/boundparameter/Rechteck.java | 20 ++ .../generics/boundparameter/Verwender.java | 28 +++ .../pr2/generics/boundwildcard/Berechner.java | 18 ++ .../java/pr2/generics/boundwildcard/Form.java | 9 + .../pr2/generics/boundwildcard/Kreis.java | 18 ++ .../pr2/generics/boundwildcard/Rechteck.java | 20 ++ .../generics/boundwildcard/SimpleStack.java | 36 ++++ .../pr2/generics/boundwildcard/SuperDemo.java | 21 ++ .../pr2/generics/boundwildcard/Verwender.java | 30 +++ .../pr2/generics/covariance/Covariance.java | 30 +++ .../java/pr2/generics/covariance/Refied.java | 17 ++ .../pr2/generics/erasure/ShowErasure.java | 19 ++ .../pr2/generics/erasure/SimpleStack.java | 24 +++ src/main/java/pr2/generics/inheritance/A.java | 8 + src/main/java/pr2/generics/inheritance/B.java | 8 + .../pr2/generics/inheritance/FormStack.java | 20 ++ .../pr2/generics/inheritance/Generic.java | 11 + .../pr2/generics/inheritance/MyStack.java | 19 ++ .../pr2/generics/inheritance/Verwender.java | 15 ++ .../pr2/generics/methoden/SimpleStack.java | 28 +++ .../java/pr2/generics/methoden/Unifier.java | 28 +++ .../java/pr2/generics/methoden/Verwender.java | 18 ++ .../generics/methoden/Zusammenfasser1.java | 24 +++ .../generics/methoden/Zusammenfasser2.java | 23 ++ .../pr2/generics/mitgenerics/SimpleStack.java | 24 +++ .../pr2/generics/mitgenerics/Verwender.java | 19 ++ .../generics/ohnegenerics/SimpleStack.java | 23 ++ .../ohnegenerics/SimpleStackInteger.java | 21 ++ .../ohnegenerics/SimpleStackString.java | 21 ++ .../pr2/generics/ohnegenerics/Verwender.java | 19 ++ src/main/java/pr2/generics/wildcard/A.java | 8 + src/main/java/pr2/generics/wildcard/B.java | 8 + src/main/java/pr2/generics/wildcard/C.java | 8 + .../java/pr2/generics/wildcard/Generic.java | 15 ++ .../pr2/generics/wildcard/GenericUser.java | 38 ++++ .../pr2/generics/wildcard/GenericUser2.java | 33 +++ .../pr2/generics/wildcard/SimpleStack.java | 28 +++ .../pr2/generics/wildcard/StackPrinter.java | 14 ++ .../java/pr2/generics/wildcard/Verwender.java | 21 ++ src/main/java/pr2/io/console/Echo.java | 22 ++ .../java/pr2/io/console/Einzelzeichen.java | 16 ++ src/main/java/pr2/io/console/Umleitung.java | 18 ++ src/main/java/pr2/io/file/CreateDir.java | 25 +++ src/main/java/pr2/io/file/DirLister.java | 27 +++ src/main/java/pr2/io/nio/FileReadExample.java | 22 ++ .../java/pr2/io/nio/FileWriteExample.java | 29 +++ .../java/pr2/io/nio/KopierprogrammNIO.java | 43 ++++ .../io/nio/KopierprogrammNIODirectBuffer.java | 40 ++++ .../java/pr2/io/nio/MemoryMappedFile.java | 18 ++ src/main/java/pr2/io/nio/SelectorExample.java | 93 ++++++++ .../java/pr2/io/randomaccess/Beispiel.java | 62 ++++++ .../java/pr2/io/reader/ArrayReaderDemo.java | 27 +++ .../java/pr2/io/reader/ArrayWriterDemo.java | 25 +++ .../io/reader/InputStreamReaderExample.java | 41 ++++ .../java/pr2/io/reader/LineNumberReader.java | 28 +++ src/main/java/pr2/io/reader/ReadChar.java | 42 ++++ .../java/pr2/io/reader/ReadCharArrays.java | 27 +++ .../java/pr2/io/reader/UpperCaseReader.java | 26 +++ .../pr2/io/reader/UpperCaseReaderTest.java | 27 +++ src/main/java/pr2/io/reader/WriteChar.java | 25 +++ .../java/pr2/io/reader/WriteCharArrays.java | 29 +++ .../java/pr2/io/serialization/Manager.java | 21 ++ .../pr2/io/serialization/Mitarbeiter.java | 37 ++++ .../pr2/io/serialization/SimpleExample.java | 36 ++++ .../io/serialization/readobject/Liste.java | 77 +++++++ .../readobject/ReadObjectDemo.java | 33 +++ .../io/serialization/readresolve/Demo.java | 30 +++ .../readresolve/TrueSingleton.java | 24 +++ .../java/pr2/io/serialization/trans/A.java | 28 +++ .../io/serialization/trans/TransientDemo.java | 32 +++ .../java/pr2/io/streams/AppendStream.java | 33 +++ .../java/pr2/io/streams/BufferedFileCopy.java | 36 ++++ src/main/java/pr2/io/streams/DataOutput.java | 47 ++++ src/main/java/pr2/io/streams/FileCopy.java | 36 ++++ .../java/pr2/io/streams/ReadByteArrays.java | 27 +++ src/main/java/pr2/io/streams/ReadBytes.java | 42 ++++ src/main/java/pr2/io/streams/SimpleHash.java | 38 ++++ .../java/pr2/io/streams/WriteByteArrays.java | 27 +++ src/main/java/pr2/io/streams/WriteBytes.java | 24 +++ src/main/java/pr2/junit/KomplexTest.java | 63 ++++++ src/main/java/pr2/junit/KomplexeZahl.java | 87 ++++++++ src/main/java/pr2/junit/KomplexeZahlTest.java | 38 ++++ .../java/pr2/nestedclasses/MagicButton.java | 53 +++++ .../anonym/v1/AnonymBeispiel.java | 17 ++ .../pr2/nestedclasses/anonym/v1/Escape.java | 19 ++ .../pr2/nestedclasses/anonym/v1/Outer.java | 9 + .../nestedclasses/anonym/v2/Anonymous.java | 44 ++++ .../pr2/nestedclasses/anonym/v2/Outer.java | 40 ++++ .../pr2/nestedclasses/anonym/v2/Outer2.java | 33 +++ .../anonym/v3/WasIstDennDas.java | 14 ++ .../pr2/nestedclasses/basics/anonym/A.java | 9 + .../pr2/nestedclasses/basics/anzahl/A.java | 8 + .../pr2/nestedclasses/basics/arten/A.java | 8 + .../pr2/nestedclasses/basics/local/A.java | 8 + .../java/pr2/nestedclasses/basics/priv/A.java | 11 + .../java/pr2/nestedclasses/basics/priv/C.java | 14 ++ .../nestedclasses/basics/sichtbarkeit/A.java | 9 + .../pr2/nestedclasses/basics/tiefe/A.java | 10 + .../pr2/nestedclasses/basics/vererbung/A.java | 12 ++ .../basics/vererbunginnen/A.java | 6 + .../nestedclasses/callback/Berechnung.java | 12 ++ .../pr2/nestedclasses/callback/Funktion.java | 8 + .../callback/TestBerechnung.java | 22 ++ .../pr2/nestedclasses/decompiled/A$B.java | 24 +++ .../java/pr2/nestedclasses/decompiled/A.java | 23 ++ .../java/pr2/nestedclasses/lambda/Aktion.java | 32 +++ .../pr2/nestedclasses/lambda/Berechnung.java | 13 ++ .../pr2/nestedclasses/lambda/Capture.java | 16 ++ .../lambda/ComparatorFactory.java | 20 ++ .../pr2/nestedclasses/lambda/Funktion.java | 5 + .../pr2/nestedclasses/lambda/Rechner.java | 35 +++ .../nestedclasses/lambda/RechnerLambda.java | 14 ++ .../pr2/nestedclasses/lambda/Scoping.java | 17 ++ .../pr2/nestedclasses/lambda/Scoping2.java | 20 ++ .../lambda/function/Rechner.java | 20 ++ .../lambda/function/RechnerKurz.java | 17 ++ .../lambda/methodref/Funktion.java | 9 + .../lambda/methodref/Funktionssammlung.java | 12 ++ .../nestedclasses/lambda/methodref/Main.java | 8 + .../nestedclasses/lambda/typing/Caller.java | 6 + .../nestedclasses/lambda/typing/Runner.java | 6 + .../lambda/typing/TargetTyping.java | 22 ++ .../java/pr2/nestedclasses/local/Escape.java | 20 ++ .../nestedclasses/local/LocalBeispiel.java | 17 ++ .../nestedclasses/local/LocalMitVariable.java | 19 ++ .../local/LocalMitVariableUndInstanz.java | 21 ++ .../java/pr2/nestedclasses/local/Outer.java | 9 + .../nonstatic/computer/Computer.java | 17 ++ .../nonstatic/computer/Computer2.java | 16 ++ .../nonstatic/computer/Computer3.java | 16 ++ .../nonstatic/computer/Computer4.java | 34 +++ .../nonstatic/computer/Outer.java | 6 + .../pr2/nestedclasses/nonstatic/v1/Outer.java | 24 +++ .../nestedclasses/nonstatic/v1/UseOuter.java | 15 ++ .../pr2/nestedclasses/nonstatic/v2/Outer.java | 25 +++ .../nestedclasses/nonstatic/v2/UseOuter.java | 12 ++ .../pr2/nestedclasses/nonstatic/v3/A.java | 27 +++ .../java/pr2/nestedclasses/quiz/Outer.java | 6 + .../java/pr2/nestedclasses/quiz/Twister.java | 25 +++ .../pr2/nestedclasses/statisch/v1/Outer.java | 6 + .../statisch/v1/StaticMember.java | 25 +++ .../pr2/nestedclasses/statisch/v2/Outer.java | 18 ++ .../statisch/v2/PrivaterZugriff.java | 25 +++ .../nestedclasses/statisch/v2/UseOuter.java | 13 ++ .../nestedclasses/statisch/v2/Zugriff.java | 29 +++ .../pr2/nestedclasses/vererbung/Outer.java | 37 ++++ .../pr2/oo1/konstruktor/hierarchie/A.java | 14 ++ .../pr2/oo1/konstruktor/hierarchie/B.java | 17 ++ .../java/pr2/oo1/konstruktor/probleme/A.java | 17 ++ .../java/pr2/oo1/konstruktor/probleme/B.java | 21 ++ .../java/pr2/oo1/object/clone/CloneDemo.java | 16 ++ .../pr2/oo1/object/clone/Mitarbeiter.java | 26 +++ src/main/java/pr2/oo1/object/clone2/Main.java | 29 +++ .../java/pr2/oo1/object/clone2/Schaf.java | 48 +++++ .../pr2/oo1/object/equals/EqualsDemo.java | 21 ++ .../java/pr2/oo1/object/equals/MeinDatum.java | 42 ++++ .../pr2/oo1/object/equals/Mitarbeiter.java | 62 ++++++ .../pr2/oo1/object/intern/InternDemo.java | 22 ++ .../pr2/oo1/quiz/binding/BindingQuiz.java | 31 +++ .../java/pr2/oo1/quiz/boxing/BoxingQuiz.java | 16 ++ .../oo1/quiz/constructor/ConstructorQuiz.java | 19 ++ .../pr2/oo1/quiz/constructor/NameHolder.java | 23 ++ src/main/java/pr2/oo1/quiz/equals/A.java | 32 +++ src/main/java/pr2/oo1/quiz/equals2/A.java | 32 +++ .../pr2/oo1/quiz/polymorphie/Polymorphie.java | 29 +++ src/main/java/pr2/oo1/quiz/verdecken/A.java | 14 ++ src/main/java/pr2/oo1/quiz/verdecken/B.java | 19 ++ src/main/java/pr2/oo1/quiz/vererbung/C.java | 19 ++ src/main/java/pr2/oo1/ueberschreiben/A.java | 19 ++ .../oo1/ueberschreiben/rueckgabetyp/A.java | 11 + .../oo1/ueberschreiben/rueckgabetyp/B.java | 11 + src/main/java/pr2/oo1/verdecken/A.java | 20 ++ src/main/java/pr2/oo1/verdecken/B.java | 13 ++ src/main/java/pr2/oo1/verdecken/C.java | 8 + src/main/java/pr2/oo1/verdecken/D.java | 8 + src/main/java/pr2/oo1/verdecken/E.java | 12 ++ .../oo1/verdecken/ellipse/EllipseDemo.java | 23 ++ .../oo1/verdecken/ellipse/EllipseDetails.java | 17 ++ .../verdecken/ellipse/EllipseDetailsEcht.java | 17 ++ .../ueberladen/UeberladenUndPolymorphie.java | 29 +++ .../pr2/oo1/vererbung/privat/Kampfechse.java | 9 + .../pr2/oo1/vererbung/privat/Rennechse.java | 23 ++ .../pr2/oo1/vererbung/privat/Terrarium.java | 21 ++ .../oo1/vererbung/v1/Geschaeftsfuehrer.java | 8 + .../vererbung/v1/GeschaeftsfuehrerBean.java | 36 ++++ .../java/pr2/oo1/vererbung/v1/Manager.java | 8 + .../pr2/oo1/vererbung/v1/Mitarbeiter.java | 17 ++ .../pr2/oo1/vererbung/v1/MitarbeiterBean.java | 34 +++ .../oo1/vererbung/v2/Geschaeftsfuehrer.java | 8 + .../java/pr2/oo1/vererbung/v2/Manager.java | 18 ++ .../pr2/oo1/vererbung/v2/Mitarbeiter.java | 17 ++ .../oo1/vererbung/v3/Geschaeftsfuehrer.java | 15 ++ .../java/pr2/oo1/vererbung/v3/Manager.java | 14 ++ .../pr2/oo1/vererbung/v3/Mitarbeiter.java | 17 ++ .../oo1/vererbung/v4/Geschaeftsfuehrer.java | 13 ++ .../java/pr2/oo1/vererbung/v4/Manager.java | 13 ++ .../pr2/oo1/vererbung/v4/Methodenaufruf.java | 144 +++++++++++++ .../pr2/oo1/vererbung/v4/Mitarbeiter.java | 17 ++ .../java/pr2/oo1/vererbung/v5/Klasse.java | 8 + .../java/pr2/oo1/vererbung/v5/Klasse2.java | 14 ++ .../java/pr2/oo1/vererbung/v5/Manager.java | 20 ++ .../pr2/oo1/vererbung/v5/Mitarbeiter.java | 35 +++ .../wasprotectedwirklichheisst/pack1/A.java | 12 ++ .../wasprotectedwirklichheisst/pack2/B.java | 17 ++ src/main/java/pr2/oo2/abstrakt/v1/Ente.java | 10 + src/main/java/pr2/oo2/abstrakt/v1/Hund.java | 9 + src/main/java/pr2/oo2/abstrakt/v1/Tier.java | 11 + .../java/pr2/oo2/abstrakt/v1/Verwender.java | 15 ++ src/main/java/pr2/oo2/abstrakt/v10/A.java | 13 ++ src/main/java/pr2/oo2/abstrakt/v10/B.java | 15 ++ src/main/java/pr2/oo2/abstrakt/v10/C.java | 9 + src/main/java/pr2/oo2/abstrakt/v10/D.java | 4 + src/main/java/pr2/oo2/abstrakt/v10/E.java | 9 + src/main/java/pr2/oo2/abstrakt/v10/F.java | 4 + src/main/java/pr2/oo2/abstrakt/v10/G.java | 13 ++ .../java/pr2/oo2/abstrakt/v10/Verwender.java | 22 ++ src/main/java/pr2/oo2/abstrakt/v11/A.java | 13 ++ src/main/java/pr2/oo2/abstrakt/v11/B.java | 16 ++ src/main/java/pr2/oo2/abstrakt/v11/C.java | 15 ++ src/main/java/pr2/oo2/abstrakt/v11/D.java | 10 + src/main/java/pr2/oo2/abstrakt/v2/Ente.java | 11 + src/main/java/pr2/oo2/abstrakt/v2/Hund.java | 11 + src/main/java/pr2/oo2/abstrakt/v2/Tier.java | 6 + .../java/pr2/oo2/abstrakt/v2/Verwender.java | 23 ++ src/main/java/pr2/oo2/abstrakt/v3/Ente.java | 10 + src/main/java/pr2/oo2/abstrakt/v3/Hund.java | 13 ++ src/main/java/pr2/oo2/abstrakt/v3/Tier.java | 10 + .../java/pr2/oo2/abstrakt/v3/Verwender.java | 32 +++ src/main/java/pr2/oo2/abstrakt/v4/Ente.java | 13 ++ src/main/java/pr2/oo2/abstrakt/v4/Hund.java | 13 ++ src/main/java/pr2/oo2/abstrakt/v4/Tier.java | 11 + .../java/pr2/oo2/abstrakt/v4/Verwender.java | 32 +++ .../pr2/oo2/abstrakt/v5/Angestellter.java | 13 ++ .../java/pr2/oo2/abstrakt/v5/Arbeiter.java | 15 ++ .../pr2/oo2/abstrakt/v5/Lohnbuchhaltung.java | 31 +++ .../java/pr2/oo2/abstrakt/v5/Mitarbeiter.java | 7 + .../java/pr2/oo2/abstrakt/v5/Vorstand.java | 9 + .../pr2/oo2/abstrakt/v6/Angestellter.java | 13 ++ .../java/pr2/oo2/abstrakt/v6/Arbeiter.java | 15 ++ .../pr2/oo2/abstrakt/v6/Lohnbuchhaltung.java | 15 ++ .../java/pr2/oo2/abstrakt/v6/Mitarbeiter.java | 12 ++ .../java/pr2/oo2/abstrakt/v6/Vorstand.java | 9 + .../pr2/oo2/abstrakt/v7/Angestellter.java | 13 ++ .../java/pr2/oo2/abstrakt/v7/Arbeiter.java | 15 ++ .../pr2/oo2/abstrakt/v7/Lohnbuchhaltung.java | 15 ++ .../java/pr2/oo2/abstrakt/v7/Mitarbeiter.java | 9 + .../java/pr2/oo2/abstrakt/v7/Vorstand.java | 8 + .../pr2/oo2/abstrakt/v8/Angestellter.java | 18 ++ src/main/java/pr2/oo2/abstrakt/v8/Main.java | 14 ++ .../java/pr2/oo2/abstrakt/v8/Mitarbeiter.java | 14 ++ .../java/pr2/oo2/abstrakt/v8/Vorstand.java | 17 ++ .../java/pr2/oo2/abstrakt/v9/Mitarbeiter.java | 23 ++ .../java/pr2/oo2/finale/blankfinal/Kunde.java | 13 ++ .../oo2/finale/v1/DeadCodeElemination.java | 18 ++ .../java/pr2/oo2/finale/v1/Konstanten.java | 14 ++ .../oo2/finale/v1/NoDeadCodeElemination.java | 18 ++ src/main/java/pr2/oo2/finale/v2/A.java | 9 + src/main/java/pr2/oo2/finale/v2/B.java | 14 ++ .../pr2/oo2/finale/v2/FinaleVariablen.java | 37 ++++ .../java/pr2/oo2/finale/v2/Konstanten.java | 8 + .../oo2/finale/v2/Konstantenverwender.java | 15 ++ src/main/java/pr2/oo2/finale/v3/A.java | 8 + src/main/java/pr2/oo2/finale/v3/B.java | 8 + .../pr2/oo2/interfaces/callback/Addition.java | 11 + .../oo2/interfaces/callback/Berechnung.java | 20 ++ .../pr2/oo2/interfaces/callback/Funktion.java | 8 + .../oo2/interfaces/callback/Subtraktion.java | 11 + .../pr2/oo2/interfaces/java8/ExtFlieger.java | 17 ++ .../pr2/oo2/interfaces/java8/Flieger.java | 12 ++ .../pr2/oo2/interfaces/java8/Flugzeug.java | 11 + .../java/pr2/oo2/interfaces/java8/IA.java | 5 + .../java/pr2/oo2/interfaces/java8/IB.java | 5 + .../java/pr2/oo2/interfaces/java8/Impl.java | 5 + .../java/pr2/oo2/interfaces/java8/Main.java | 11 + .../pr2/oo2/interfaces/java8/Rechner.java | 10 + .../pr2/oo2/interfaces/java8/diamond/A.java | 9 + .../pr2/oo2/interfaces/java8/diamond/B.java | 10 + .../pr2/oo2/interfaces/java8/diamond/C.java | 6 + .../pr2/oo2/interfaces/java8/diamond/I1.java | 9 + .../pr2/oo2/interfaces/java8/diamond/I2.java | 10 + .../oo2/interfaces/java8/diamond/Main.java | 10 + .../oo2/interfaces/mehrfachvererbung/A.java | 13 ++ .../oo2/interfaces/mehrfachvererbung/I1.java | 8 + .../oo2/interfaces/mehrfachvererbung/I2.java | 8 + .../java/pr2/oo2/interfaces/v1/Flieger.java | 13 ++ .../java/pr2/oo2/interfaces/v1/Flughafen.java | 13 ++ .../java/pr2/oo2/interfaces/v1/Flugzeug.java | 19 ++ .../java/pr2/oo2/interfaces/v2/Flieger.java | 15 ++ .../java/pr2/oo2/interfaces/v2/Flieger2.java | 15 ++ .../java/pr2/oo2/interfaces/v2/Flugzeug.java | 19 ++ .../java/pr2/oo2/interfaces/v3/Airbus.java | 8 + src/main/java/pr2/oo2/interfaces/v3/Ente.java | 22 ++ .../java/pr2/oo2/interfaces/v3/Flieger.java | 10 + .../java/pr2/oo2/interfaces/v3/Flugzeug.java | 19 ++ .../java/pr2/oo2/interfaces/v3/Maschine.java | 8 + .../java/pr2/oo2/interfaces/v3/Schwimmer.java | 9 + .../java/pr2/oo2/interfaces/v3/Superman.java | 19 ++ src/main/java/pr2/oo2/interfaces/v3/Tier.java | 9 + .../java/pr2/oo2/interfaces/v3/Vogel.java | 8 + .../pr2/oo2/interfaces/v3/Wasserflugzeug.java | 10 + .../pr2/oo2/interfaces/v4/Konstanten.java | 8 + .../java/pr2/oo2/interfaces/v4/Mathe.java | 11 + .../java/pr2/oo2/interfaces/v4/Mathe2.java | 13 ++ .../java/pr2/oo2/interfaces/v5/Sorter.java | 19 ++ .../pr2/oo2/interfaces/v5/Telefonnummer.java | 40 ++++ src/main/java/pr2/oo2/interfaces/v6/A.java | 8 + src/main/java/pr2/oo2/interfaces/v6/B.java | 8 + src/main/java/pr2/oo2/interfaces/v6/I1.java | 8 + src/main/java/pr2/oo2/interfaces/v6/I2.java | 8 + src/main/java/pr2/oo2/interfaces/v6/Main.java | 18 ++ src/main/java/pr2/oo2/quiz/Ente.java | 20 ++ src/main/java/pr2/oo2/quiz/Initializer.java | 21 ++ src/main/java/pr2/oo2/quiz/QuizStatic.java | 16 ++ .../pr2/oo2/statisch/apiimpl/Calculator.java | 9 + .../statisch/apiimpl/CalculatorFactory.java | 13 ++ .../oo2/statisch/apiimpl/CalculatorImpl.java | 15 ++ .../CalculatorImplQuantumSuperDuper.java | 22 ++ .../pr2/oo2/statisch/apiimpl/Verwender.java | 14 ++ src/main/java/pr2/oo2/statisch/cycle/A.java | 16 ++ src/main/java/pr2/oo2/statisch/cycle/B.java | 16 ++ src/main/java/pr2/oo2/statisch/cycle/C.java | 10 + .../pr2/oo2/statisch/factory/Erzeugter.java | 13 ++ .../pr2/oo2/statisch/factory/Factory.java | 11 + .../pr2/oo2/statisch/initializer/Crash.java | 13 ++ .../oo2/statisch/initializer/CrashUser.java | 9 + .../pr2/oo2/statisch/singleton/Broken.java | 25 +++ .../pr2/oo2/statisch/singleton/Firma.java | 17 ++ .../pr2/oo2/statisch/singleton/Firma2.java | 21 ++ src/main/java/pr2/oo2/statisch/v1/Ticket.java | 18 ++ .../java/pr2/oo2/statisch/v2/TestTicket.java | 15 ++ src/main/java/pr2/oo2/statisch/v2/Ticket.java | 22 ++ .../java/pr2/oo2/statisch/v3/TestTicket.java | 14 ++ src/main/java/pr2/oo2/statisch/v3/Ticket.java | 30 +++ src/main/java/pr2/oo2/statisch/v4/A.java | 11 + src/main/java/pr2/oo2/statisch/v4/B.java | 11 + src/main/java/pr2/oo2/statisch/v4/Test.java | 17 ++ .../java/pr2/oo2/wrapper/AutoboxingDemo.java | 21 ++ .../java/pr2/oo2/wrapper/BoxingFailure.java | 21 ++ .../java/pr2/oo2/wrapper/WrapperDemo.java | 16 ++ .../java/pr2/records/newstyle/Rectangle.java | 26 +++ .../java/pr2/records/plain/Rectangle.java | 55 +++++ src/main/java/pr2/reflection/Voldemort.java | 40 ++++ src/main/java/pr2/reflection/api/Arrays.java | 26 +++ src/main/java/pr2/reflection/api/Fields.java | 46 ++++ .../java/pr2/reflection/api/Instanzen.java | 42 ++++ .../java/pr2/reflection/api/Methoden.java | 51 +++++ .../reflection/apiimpltrennung/Verwender.java | 17 ++ .../apiimpltrennung/api/Calculator.java | 9 + .../api/CalculatorFactory.java | 32 +++ .../apiimpltrennung/impl/CalculatorImpl.java | 17 ++ .../java/pr2/reflection/classloader/A.java | 11 + .../java/pr2/reflection/classloader/Test.java | 14 ++ .../pr2/reflection/classobj/Angestellter.java | 18 ++ .../java/pr2/reflection/classobj/Basics.java | 29 +++ .../pr2/reflection/classobj/ClazzDemo.java | 39 ++++ .../classobj/Geschaeftsfuehrer.java | 13 ++ .../java/pr2/reflection/classobj/Manager.java | 15 ++ .../pr2/reflection/classobj/Mitarbeiter.java | 17 ++ .../pr2/reflection/classobj/Vorstand.java | 17 ++ .../java/pr2/reflection/ugly/Erdogan.java | 28 +++ src/main/java/pr2/reflection/ugly/Killer.java | 25 +++ .../advanced/CollectorAggregation.java | 27 +++ .../pr2/streams/advanced/CollectorType.java | 17 ++ .../java/pr2/streams/advanced/Grouping.java | 21 ++ .../java/pr2/streams/advanced/Joining.java | 18 ++ .../java/pr2/streams/advanced/Partition.java | 24 +++ .../pr2/streams/basics/CollectSimple.java | 21 ++ src/main/java/pr2/streams/basics/Filter.java | 17 ++ src/main/java/pr2/streams/basics/FlatMap.java | 44 ++++ .../pr2/streams/basics/IntStreamExample.java | 25 +++ src/main/java/pr2/streams/basics/Lazy.java | 37 ++++ .../java/pr2/streams/basics/MapExample.java | 62 ++++++ src/main/java/pr2/streams/basics/MaxMin.java | 44 ++++ src/main/java/pr2/streams/basics/Reduce.java | 25 +++ .../java/pr2/streams/basics/StreamOf.java | 12 ++ .../java/pr2/streams/basics/kombi/Item.java | 21 ++ .../java/pr2/streams/basics/kombi/Order.java | 20 ++ .../pr2/streams/basics/kombi/Simulation.java | 147 +++++++++++++ .../streams/motivation/ExternalIteration.java | 40 ++++ src/main/java/pr2/strukturierung/C.java | 23 ++ src/main/java/pr2/strukturierung/Datum.java | 38 ++++ .../strukturierung/PackagePrivateClass.java | 16 ++ .../java/pr2/strukturierung/PublicClass.java | 8 + src/main/java/pr2/strukturierung/Scope.java | 27 +++ .../java/pr2/strukturierung/StaticImport.java | 13 ++ .../java/pr2/strukturierung/Variablen.java | 47 ++++ src/main/java/pr2/strukturierung/X.java | 8 + .../firma/db/DbMitarbeiter.java | 8 + .../firma/domain/Angestellter.java | 8 + .../strukturierung/firma/domain/Arbeiter.java | 8 + .../firma/domain/Mitarbeiter.java | 8 + .../firma/gui/EingabeMitarbeiter.java | 8 + .../pr2/strukturierung/sichtbarkeit/A.java | 15 ++ .../pr2/strukturierung/sichtbarkeit/B.java | 17 ++ src/main/java/pr2/swing/AdapterDemo.java | 38 ++++ src/main/java/pr2/swing/BorderLayoutDemo.java | 24 +++ src/main/java/pr2/swing/CardLayoutDemo.java | 27 +++ .../java/pr2/swing/ComplexLayoutDemo.java | 45 ++++ src/main/java/pr2/swing/EventHandlerDemo.java | 49 +++++ src/main/java/pr2/swing/FlowLayoutDemo.java | 25 +++ src/main/java/pr2/swing/GridLayoutDemo.java | 26 +++ src/main/java/pr2/swing/HelloWorld.java | 8 + src/main/java/pr2/swing/JFrameDemo.java | 24 +++ src/main/java/pr2/swing/MenuDemo.java | 34 +++ src/main/java/pr2/swing/OptionPaneDemo.java | 41 ++++ src/main/java/pr2/swing/XYLayout.java | 26 +++ .../concurrent/atomic/AtomicExample.java | 28 +++ .../concurrent/callable/CallableExample.java | 36 ++++ .../threads/concurrent/collections/Cache.java | 27 +++ .../concurrent/executors/FutureDemo.java | 35 +++ .../concurrent/executors/TaskDemo.java | 39 ++++ .../concurrent/locks/ConnectionPool.java | 38 ++++ .../pr2/threads/concurrent/locks/Join.java | 50 +++++ .../concurrent/producerconsumer/Consumer.java | 30 +++ .../concurrent/producerconsumer/Producer.java | 29 +++ .../concurrent/producerconsumer/Starter.java | 29 +++ .../threads/concurrent/timer/Countdown.java | 23 ++ .../concurrent/timer/CountdownUser.java | 22 ++ .../threads/concurrent/timer/Timebomb.java | 15 ++ .../concurrent/timer/TimebombUser.java | 22 ++ .../pr2/threads/grundlagen/DaemonDemo.java | 19 ++ .../threads/grundlagen/HeapAndStackDemo.java | 42 ++++ .../pr2/threads/grundlagen/Interruptible.java | 29 +++ .../threads/grundlagen/Interruptible2.java | 34 +++ .../threads/grundlagen/Interruptible3.java | 29 +++ .../java/pr2/threads/grundlagen/Join.java | 37 ++++ .../grundlagen/SimpleHeapAndStack.java | 31 +++ .../pr2/threads/grundlagen/Stoppable.java | 30 +++ .../pr2/threads/grundlagen/ThreadDemo.java | 27 +++ .../threads/grundlagen/nested/DaemonDemo.java | 14 ++ .../grundlagen/nested/HeapAndStackDemo.java | 38 ++++ .../threads/grundlagen/nested/ThreadDemo.java | 24 +++ .../grundlagen/runnable/BrokenRestart.java | 20 ++ .../grundlagen/runnable/ThreadDemo.java | 31 +++ .../threads/grundlagen/threadlocal/Daten.java | 8 + .../threadlocal/ThreadLocalDemo.java | 24 +++ .../producerconumer/Consumer.java | 39 ++++ .../producerconumer/Producer.java | 38 ++++ .../koordination/producerconumer/Starter.java | 27 +++ .../producerconumer_broken/Consumer.java | 46 ++++ .../producerconumer_broken/Producer.java | 46 ++++ .../producerconumer_broken/Starter.java | 28 +++ .../threads/schrecken/deadlock/Deadlock.java | 39 ++++ .../threads/schrecken/hazards/Livelock.java | 27 +++ .../hazards/SerialNumberGenerator.java | 20 ++ .../threads/schrecken/hazards/Singleton.java | 18 ++ .../schrecken/synchro/BrokenSimpleStack.java | 27 +++ .../schrecken/synchro/BrokenStack.java | 30 +++ .../schrecken/synchro/FixedLiveLock.java | 36 ++++ .../threads/schrecken/synchro/LiveLock2.java | 32 +++ .../threads/schrecken/synchro/Livelock.java | 32 +++ .../schrecken/synchro/MemoryBarrier.java | 11 + .../synchro/explizit/SimpleStack.java | 28 +++ .../synchro/methods/SimpleStack.java | 23 ++ .../schrecken/synchro/none/SimpleStack.java | 23 ++ .../schrecken/synchro/thi/SimpleStack.java | 27 +++ .../threads/schrecken/vola/LivelockFixed.java | 27 +++ .../schrecken/vola/PossibleReordering.java | 20 ++ .../threads/schrecken/vola/Reordering.java | 28 +++ 600 files changed, 14869 insertions(+) create mode 100644 src/main/java/pr2/ads/adt/BinaryTree.java create mode 100644 src/main/java/pr2/ads/adt/Queue.java create mode 100644 src/main/java/pr2/ads/adt/QueueException.java create mode 100644 src/main/java/pr2/ads/adt/Stack.java create mode 100644 src/main/java/pr2/ads/adt/StackException.java create mode 100644 src/main/java/pr2/ads/adt/TreeNode.java create mode 100644 src/main/java/pr2/ads/searching/BinarySearch.java create mode 100644 src/main/java/pr2/ads/searching/LinearSearch.java create mode 100644 src/main/java/pr2/ads/searching/Searcher.java create mode 100644 src/main/java/pr2/ads/searching/TestSearch.java create mode 100644 src/main/java/pr2/ads/shuffle/FisherYatesShuffle.java create mode 100644 src/main/java/pr2/ads/shuffle/NaiveShuffle.java create mode 100644 src/main/java/pr2/ads/shuffle/Shuffler.java create mode 100644 src/main/java/pr2/ads/shuffle/TestShuffle.java create mode 100644 src/main/java/pr2/ads/sorting/BubbleSort.java create mode 100644 src/main/java/pr2/ads/sorting/InsertionSort.java create mode 100644 src/main/java/pr2/ads/sorting/MergeSort.java create mode 100644 src/main/java/pr2/ads/sorting/QuickSort.java create mode 100644 src/main/java/pr2/ads/sorting/SelectionSort.java create mode 100644 src/main/java/pr2/ads/sorting/Sorter.java create mode 100644 src/main/java/pr2/ads/sorting/TestSorting.java create mode 100644 src/main/java/pr2/ads/textsearch/BoyerMooreTextSearch.java create mode 100644 src/main/java/pr2/ads/textsearch/BruteForceTextSearch.java create mode 100644 src/main/java/pr2/ads/textsearch/KMPTextSearch.java create mode 100644 src/main/java/pr2/ads/textsearch/TestTextSearch.java create mode 100644 src/main/java/pr2/ads/textsearch/TextSearch.java create mode 100644 src/main/java/pr2/auffrischung/CodingStandard.java create mode 100644 src/main/java/pr2/auffrischung/Ding.java create mode 100644 src/main/java/pr2/auffrischung/IsPrim.java create mode 100644 src/main/java/pr2/auffrischung/Kontrollstrukturen.java create mode 100644 src/main/java/pr2/auffrischung/QuizString.java create mode 100644 src/main/java/pr2/auffrischung/Scope.java create mode 100644 src/main/java/pr2/auffrischung/Zuweisung.java create mode 100644 src/main/java/pr2/auffrischung/quiz/IfWithoutBlock.java create mode 100644 src/main/java/pr2/auffrischung/quiz/Puzzles.java create mode 100644 src/main/java/pr2/auffrischung/quiz/Reference.java create mode 100644 src/main/java/pr2/auffrischung/quiz/Swap.java create mode 100644 src/main/java/pr2/ausnahmen/Java7.java create mode 100644 src/main/java/pr2/ausnahmen/callstack/CallStack1.java create mode 100644 src/main/java/pr2/ausnahmen/callstack/CallStack2.java create mode 100644 src/main/java/pr2/ausnahmen/callstack/CallStack3.java create mode 100644 src/main/java/pr2/ausnahmen/callstack/HandleOrDeclare.java create mode 100644 src/main/java/pr2/ausnahmen/eigene/Client.java create mode 100644 src/main/java/pr2/ausnahmen/eigene/Server.java create mode 100644 src/main/java/pr2/ausnahmen/eigene/ServerException.java create mode 100644 src/main/java/pr2/ausnahmen/eigene/SimpleExample2.java create mode 100644 src/main/java/pr2/ausnahmen/eigene/SimpleExamples.java create mode 100644 src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample.java create mode 100644 src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample2.java create mode 100644 src/main/java/pr2/ausnahmen/junit/BrokenTest.java create mode 100644 src/main/java/pr2/ausnahmen/junit/TestMitExpected.java create mode 100644 src/main/java/pr2/ausnahmen/junit/TestMitTryCatch.java create mode 100644 src/main/java/pr2/ausnahmen/quiz/Finally.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/BlossNicht.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/ExceptionExample.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/FinallyMitSeiteneffekt.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/GanzBoese.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/Netzwerk.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/TryCatchExample.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/TryCatchExampleZwei.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/polymorphie/A.java create mode 100644 src/main/java/pr2/ausnahmen/trycatch/polymorphie/B.java create mode 100644 src/main/java/pr2/collections/StackDemo.java create mode 100644 src/main/java/pr2/collections/interfaces/collection/CollectionDemo.java create mode 100644 src/main/java/pr2/collections/interfaces/collection/CollectionsDemo.java create mode 100644 src/main/java/pr2/collections/interfaces/list/ListDemo.java create mode 100644 src/main/java/pr2/collections/interfaces/list/ListDemo2.java create mode 100644 src/main/java/pr2/collections/interfaces/set/SetDemo.java create mode 100644 src/main/java/pr2/collections/iterable/IterableFor.java create mode 100644 src/main/java/pr2/collections/iterable/IterableWhile.java create mode 100644 src/main/java/pr2/collections/iterable/ListIteratorDemo.java create mode 100644 src/main/java/pr2/collections/iterable/eigenes/IterableInnereKlasse.java create mode 100644 src/main/java/pr2/collections/iterable/eigenes/IterableMitKlasse.java create mode 100644 src/main/java/pr2/collections/iterable/eigenes/IterableSelbst.java create mode 100644 src/main/java/pr2/collections/iterable/eigenes/IterableSelbstKorrekt.java create mode 100644 src/main/java/pr2/collections/maps/MapBeispiel.java create mode 100644 src/main/java/pr2/collections/maps/MapBeispiel2.java create mode 100644 src/main/java/pr2/collections/maps/MapBeispiel3.java create mode 100644 src/main/java/pr2/collections/maps/Mitarbeiter.java create mode 100644 src/main/java/pr2/collections/maps/Personalnummer.java create mode 100644 src/main/java/pr2/collections/sorting/ListSortierung.java create mode 100644 src/main/java/pr2/collections/sorting/SortedSetDemo.java create mode 100644 src/main/java/pr2/collections/sorting/comparable/Student.java create mode 100644 src/main/java/pr2/collections/sorting/comparable/StudentDemo.java create mode 100644 src/main/java/pr2/collections/sorting/comparator/Student.java create mode 100644 src/main/java/pr2/collections/sorting/comparator/StudentComparator.java create mode 100644 src/main/java/pr2/collections/sorting/comparator/StudentDemo.java create mode 100644 src/main/java/pr2/collections/threads/ConcurrentModificationException.java create mode 100644 src/main/java/pr2/ds/avl/AVLTree.java create mode 100644 src/main/java/pr2/ds/bt/BinaryTree.java create mode 100644 src/main/java/pr2/ds/bt/SimpleTree.java create mode 100644 src/main/java/pr2/ds/buffer/RingBuffer.java create mode 100644 src/main/java/pr2/ds/graph/Graph.java create mode 100644 src/main/java/pr2/ds/hash/BetterHashTable.java create mode 100644 src/main/java/pr2/ds/hash/HashTable.java create mode 100644 src/main/java/pr2/ds/hash/SimpleHashTable.java create mode 100644 src/main/java/pr2/ds/liste/ArrayIterator.java create mode 100644 src/main/java/pr2/ds/liste/ArrayIteratorTest.java create mode 100644 src/main/java/pr2/ds/liste/DoubleLinkedList.java create mode 100644 src/main/java/pr2/ds/liste/IntList.java create mode 100644 src/main/java/pr2/ds/liste/LinkedList.java create mode 100644 src/main/java/pr2/ds/liste/Pr2LinkedList.java create mode 100644 src/main/java/pr2/ds/liste/Pr2LinkedListTest.java create mode 100644 src/main/java/pr2/ds/liste/Pr2List.java create mode 100644 src/main/java/pr2/ds/liste/Pr2Node.java create mode 100644 src/main/java/pr2/ds/liste/TestLists.java create mode 100644 src/main/java/pr2/ds/queue/Queue.java create mode 100644 src/main/java/pr2/ds/search/BoyerMoore.java create mode 100644 src/main/java/pr2/ds/search/KMP.java create mode 100644 src/main/java/pr2/ds/stack/Stack.java create mode 100644 src/main/java/pr2/enumerationen/abstraktemethoden/Operation.java create mode 100644 src/main/java/pr2/enumerationen/abstraktemethoden/Rechner.java create mode 100644 src/main/java/pr2/enumerationen/classic/DateJuggler.java create mode 100644 src/main/java/pr2/enumerationen/classic/Verwender.java create mode 100644 src/main/java/pr2/enumerationen/classic/Wochentag.java create mode 100644 src/main/java/pr2/enumerationen/enummethoden/Planet.java create mode 100644 src/main/java/pr2/enumerationen/enummethoden/Verwender.java create mode 100644 src/main/java/pr2/enumerationen/enumset/EnumSetDemo.java create mode 100644 src/main/java/pr2/enumerationen/enumset/Planet.java create mode 100644 src/main/java/pr2/enumerationen/enumset/Textauszeichnung.java create mode 100644 src/main/java/pr2/enumerationen/enumset/Textverarbeitung.java create mode 100644 src/main/java/pr2/enumerationen/enumset/Todesstern.java create mode 100644 src/main/java/pr2/enumerationen/enumset/alt/Auszeichnungen.java create mode 100644 src/main/java/pr2/enumerationen/enumset/alt/Textprogramm.java create mode 100644 src/main/java/pr2/enumerationen/enumset/neu/Auszeichnungen.java create mode 100644 src/main/java/pr2/enumerationen/enumset/neu/Textprogramm.java create mode 100644 src/main/java/pr2/enumerationen/mitenums/DateJuggler.java create mode 100644 src/main/java/pr2/enumerationen/mitenums/Verwender.java create mode 100644 src/main/java/pr2/enumerationen/mitenums/Wochentag.java create mode 100644 src/main/java/pr2/enumerationen/mitkonstruktor/Ausgabe.java create mode 100644 src/main/java/pr2/enumerationen/mitkonstruktor/Planet.java create mode 100644 src/main/java/pr2/enumerationen/mitkonstruktor/SpaceTravel.java create mode 100644 src/main/java/pr2/enumerationen/quiz/Geschlecht.java create mode 100644 src/main/java/pr2/enumerationen/quiz/Verwender.java create mode 100644 src/main/java/pr2/enumerationen/quiz/Wochentage.java create mode 100644 src/main/java/pr2/enumerationen/simple/Planet.java create mode 100644 src/main/java/pr2/generics/Pair.java create mode 100644 src/main/java/pr2/generics/PairUser.java create mode 100644 src/main/java/pr2/generics/boundparameter/Form.java create mode 100644 src/main/java/pr2/generics/boundparameter/FormenStack.java create mode 100644 src/main/java/pr2/generics/boundparameter/Kreis.java create mode 100644 src/main/java/pr2/generics/boundparameter/Rechteck.java create mode 100644 src/main/java/pr2/generics/boundparameter/Verwender.java create mode 100644 src/main/java/pr2/generics/boundwildcard/Berechner.java create mode 100644 src/main/java/pr2/generics/boundwildcard/Form.java create mode 100644 src/main/java/pr2/generics/boundwildcard/Kreis.java create mode 100644 src/main/java/pr2/generics/boundwildcard/Rechteck.java create mode 100644 src/main/java/pr2/generics/boundwildcard/SimpleStack.java create mode 100644 src/main/java/pr2/generics/boundwildcard/SuperDemo.java create mode 100644 src/main/java/pr2/generics/boundwildcard/Verwender.java create mode 100644 src/main/java/pr2/generics/covariance/Covariance.java create mode 100644 src/main/java/pr2/generics/covariance/Refied.java create mode 100644 src/main/java/pr2/generics/erasure/ShowErasure.java create mode 100644 src/main/java/pr2/generics/erasure/SimpleStack.java create mode 100644 src/main/java/pr2/generics/inheritance/A.java create mode 100644 src/main/java/pr2/generics/inheritance/B.java create mode 100644 src/main/java/pr2/generics/inheritance/FormStack.java create mode 100644 src/main/java/pr2/generics/inheritance/Generic.java create mode 100644 src/main/java/pr2/generics/inheritance/MyStack.java create mode 100644 src/main/java/pr2/generics/inheritance/Verwender.java create mode 100644 src/main/java/pr2/generics/methoden/SimpleStack.java create mode 100644 src/main/java/pr2/generics/methoden/Unifier.java create mode 100644 src/main/java/pr2/generics/methoden/Verwender.java create mode 100644 src/main/java/pr2/generics/methoden/Zusammenfasser1.java create mode 100644 src/main/java/pr2/generics/methoden/Zusammenfasser2.java create mode 100644 src/main/java/pr2/generics/mitgenerics/SimpleStack.java create mode 100644 src/main/java/pr2/generics/mitgenerics/Verwender.java create mode 100644 src/main/java/pr2/generics/ohnegenerics/SimpleStack.java create mode 100644 src/main/java/pr2/generics/ohnegenerics/SimpleStackInteger.java create mode 100644 src/main/java/pr2/generics/ohnegenerics/SimpleStackString.java create mode 100644 src/main/java/pr2/generics/ohnegenerics/Verwender.java create mode 100644 src/main/java/pr2/generics/wildcard/A.java create mode 100644 src/main/java/pr2/generics/wildcard/B.java create mode 100644 src/main/java/pr2/generics/wildcard/C.java create mode 100644 src/main/java/pr2/generics/wildcard/Generic.java create mode 100644 src/main/java/pr2/generics/wildcard/GenericUser.java create mode 100644 src/main/java/pr2/generics/wildcard/GenericUser2.java create mode 100644 src/main/java/pr2/generics/wildcard/SimpleStack.java create mode 100644 src/main/java/pr2/generics/wildcard/StackPrinter.java create mode 100644 src/main/java/pr2/generics/wildcard/Verwender.java create mode 100644 src/main/java/pr2/io/console/Echo.java create mode 100644 src/main/java/pr2/io/console/Einzelzeichen.java create mode 100644 src/main/java/pr2/io/console/Umleitung.java create mode 100644 src/main/java/pr2/io/file/CreateDir.java create mode 100644 src/main/java/pr2/io/file/DirLister.java create mode 100644 src/main/java/pr2/io/nio/FileReadExample.java create mode 100644 src/main/java/pr2/io/nio/FileWriteExample.java create mode 100644 src/main/java/pr2/io/nio/KopierprogrammNIO.java create mode 100644 src/main/java/pr2/io/nio/KopierprogrammNIODirectBuffer.java create mode 100644 src/main/java/pr2/io/nio/MemoryMappedFile.java create mode 100644 src/main/java/pr2/io/nio/SelectorExample.java create mode 100644 src/main/java/pr2/io/randomaccess/Beispiel.java create mode 100644 src/main/java/pr2/io/reader/ArrayReaderDemo.java create mode 100644 src/main/java/pr2/io/reader/ArrayWriterDemo.java create mode 100644 src/main/java/pr2/io/reader/InputStreamReaderExample.java create mode 100644 src/main/java/pr2/io/reader/LineNumberReader.java create mode 100644 src/main/java/pr2/io/reader/ReadChar.java create mode 100644 src/main/java/pr2/io/reader/ReadCharArrays.java create mode 100644 src/main/java/pr2/io/reader/UpperCaseReader.java create mode 100644 src/main/java/pr2/io/reader/UpperCaseReaderTest.java create mode 100644 src/main/java/pr2/io/reader/WriteChar.java create mode 100644 src/main/java/pr2/io/reader/WriteCharArrays.java create mode 100644 src/main/java/pr2/io/serialization/Manager.java create mode 100644 src/main/java/pr2/io/serialization/Mitarbeiter.java create mode 100644 src/main/java/pr2/io/serialization/SimpleExample.java create mode 100644 src/main/java/pr2/io/serialization/readobject/Liste.java create mode 100644 src/main/java/pr2/io/serialization/readobject/ReadObjectDemo.java create mode 100644 src/main/java/pr2/io/serialization/readresolve/Demo.java create mode 100644 src/main/java/pr2/io/serialization/readresolve/TrueSingleton.java create mode 100644 src/main/java/pr2/io/serialization/trans/A.java create mode 100644 src/main/java/pr2/io/serialization/trans/TransientDemo.java create mode 100644 src/main/java/pr2/io/streams/AppendStream.java create mode 100644 src/main/java/pr2/io/streams/BufferedFileCopy.java create mode 100644 src/main/java/pr2/io/streams/DataOutput.java create mode 100644 src/main/java/pr2/io/streams/FileCopy.java create mode 100644 src/main/java/pr2/io/streams/ReadByteArrays.java create mode 100644 src/main/java/pr2/io/streams/ReadBytes.java create mode 100644 src/main/java/pr2/io/streams/SimpleHash.java create mode 100644 src/main/java/pr2/io/streams/WriteByteArrays.java create mode 100644 src/main/java/pr2/io/streams/WriteBytes.java create mode 100644 src/main/java/pr2/junit/KomplexTest.java create mode 100644 src/main/java/pr2/junit/KomplexeZahl.java create mode 100644 src/main/java/pr2/junit/KomplexeZahlTest.java create mode 100644 src/main/java/pr2/nestedclasses/MagicButton.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v1/AnonymBeispiel.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v1/Escape.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v1/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v2/Anonymous.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v2/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v2/Outer2.java create mode 100644 src/main/java/pr2/nestedclasses/anonym/v3/WasIstDennDas.java create mode 100644 src/main/java/pr2/nestedclasses/basics/anonym/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/anzahl/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/arten/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/local/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/priv/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/priv/C.java create mode 100644 src/main/java/pr2/nestedclasses/basics/sichtbarkeit/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/tiefe/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/vererbung/A.java create mode 100644 src/main/java/pr2/nestedclasses/basics/vererbunginnen/A.java create mode 100644 src/main/java/pr2/nestedclasses/callback/Berechnung.java create mode 100644 src/main/java/pr2/nestedclasses/callback/Funktion.java create mode 100644 src/main/java/pr2/nestedclasses/callback/TestBerechnung.java create mode 100644 src/main/java/pr2/nestedclasses/decompiled/A$B.java create mode 100644 src/main/java/pr2/nestedclasses/decompiled/A.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Aktion.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Berechnung.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Capture.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/ComparatorFactory.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Funktion.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Rechner.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/RechnerLambda.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Scoping.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/Scoping2.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/function/Rechner.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/function/RechnerKurz.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/methodref/Funktion.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/methodref/Funktionssammlung.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/methodref/Main.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/typing/Caller.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/typing/Runner.java create mode 100644 src/main/java/pr2/nestedclasses/lambda/typing/TargetTyping.java create mode 100644 src/main/java/pr2/nestedclasses/local/Escape.java create mode 100644 src/main/java/pr2/nestedclasses/local/LocalBeispiel.java create mode 100644 src/main/java/pr2/nestedclasses/local/LocalMitVariable.java create mode 100644 src/main/java/pr2/nestedclasses/local/LocalMitVariableUndInstanz.java create mode 100644 src/main/java/pr2/nestedclasses/local/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/computer/Computer.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/computer/Computer2.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/computer/Computer3.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/computer/Computer4.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/computer/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/v1/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/v1/UseOuter.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/v2/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/v2/UseOuter.java create mode 100644 src/main/java/pr2/nestedclasses/nonstatic/v3/A.java create mode 100644 src/main/java/pr2/nestedclasses/quiz/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/quiz/Twister.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v1/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v1/StaticMember.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v2/Outer.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v2/PrivaterZugriff.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v2/UseOuter.java create mode 100644 src/main/java/pr2/nestedclasses/statisch/v2/Zugriff.java create mode 100644 src/main/java/pr2/nestedclasses/vererbung/Outer.java create mode 100644 src/main/java/pr2/oo1/konstruktor/hierarchie/A.java create mode 100644 src/main/java/pr2/oo1/konstruktor/hierarchie/B.java create mode 100644 src/main/java/pr2/oo1/konstruktor/probleme/A.java create mode 100644 src/main/java/pr2/oo1/konstruktor/probleme/B.java create mode 100644 src/main/java/pr2/oo1/object/clone/CloneDemo.java create mode 100644 src/main/java/pr2/oo1/object/clone/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/object/clone2/Main.java create mode 100644 src/main/java/pr2/oo1/object/clone2/Schaf.java create mode 100644 src/main/java/pr2/oo1/object/equals/EqualsDemo.java create mode 100644 src/main/java/pr2/oo1/object/equals/MeinDatum.java create mode 100644 src/main/java/pr2/oo1/object/equals/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/object/intern/InternDemo.java create mode 100644 src/main/java/pr2/oo1/quiz/binding/BindingQuiz.java create mode 100644 src/main/java/pr2/oo1/quiz/boxing/BoxingQuiz.java create mode 100644 src/main/java/pr2/oo1/quiz/constructor/ConstructorQuiz.java create mode 100644 src/main/java/pr2/oo1/quiz/constructor/NameHolder.java create mode 100644 src/main/java/pr2/oo1/quiz/equals/A.java create mode 100644 src/main/java/pr2/oo1/quiz/equals2/A.java create mode 100644 src/main/java/pr2/oo1/quiz/polymorphie/Polymorphie.java create mode 100644 src/main/java/pr2/oo1/quiz/verdecken/A.java create mode 100644 src/main/java/pr2/oo1/quiz/verdecken/B.java create mode 100644 src/main/java/pr2/oo1/quiz/vererbung/C.java create mode 100644 src/main/java/pr2/oo1/ueberschreiben/A.java create mode 100644 src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/A.java create mode 100644 src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/B.java create mode 100644 src/main/java/pr2/oo1/verdecken/A.java create mode 100644 src/main/java/pr2/oo1/verdecken/B.java create mode 100644 src/main/java/pr2/oo1/verdecken/C.java create mode 100644 src/main/java/pr2/oo1/verdecken/D.java create mode 100644 src/main/java/pr2/oo1/verdecken/E.java create mode 100644 src/main/java/pr2/oo1/verdecken/ellipse/EllipseDemo.java create mode 100644 src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetails.java create mode 100644 src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetailsEcht.java create mode 100644 src/main/java/pr2/oo1/verdecken/ueberladen/UeberladenUndPolymorphie.java create mode 100644 src/main/java/pr2/oo1/vererbung/privat/Kampfechse.java create mode 100644 src/main/java/pr2/oo1/vererbung/privat/Rennechse.java create mode 100644 src/main/java/pr2/oo1/vererbung/privat/Terrarium.java create mode 100644 src/main/java/pr2/oo1/vererbung/v1/Geschaeftsfuehrer.java create mode 100644 src/main/java/pr2/oo1/vererbung/v1/GeschaeftsfuehrerBean.java create mode 100644 src/main/java/pr2/oo1/vererbung/v1/Manager.java create mode 100644 src/main/java/pr2/oo1/vererbung/v1/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/vererbung/v1/MitarbeiterBean.java create mode 100644 src/main/java/pr2/oo1/vererbung/v2/Geschaeftsfuehrer.java create mode 100644 src/main/java/pr2/oo1/vererbung/v2/Manager.java create mode 100644 src/main/java/pr2/oo1/vererbung/v2/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/vererbung/v3/Geschaeftsfuehrer.java create mode 100644 src/main/java/pr2/oo1/vererbung/v3/Manager.java create mode 100644 src/main/java/pr2/oo1/vererbung/v3/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/vererbung/v4/Geschaeftsfuehrer.java create mode 100644 src/main/java/pr2/oo1/vererbung/v4/Manager.java create mode 100644 src/main/java/pr2/oo1/vererbung/v4/Methodenaufruf.java create mode 100644 src/main/java/pr2/oo1/vererbung/v4/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/vererbung/v5/Klasse.java create mode 100644 src/main/java/pr2/oo1/vererbung/v5/Klasse2.java create mode 100644 src/main/java/pr2/oo1/vererbung/v5/Manager.java create mode 100644 src/main/java/pr2/oo1/vererbung/v5/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack1/A.java create mode 100644 src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack2/B.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v1/Ente.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v1/Hund.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v1/Tier.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v1/Verwender.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/A.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/B.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/C.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/D.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/E.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/F.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/G.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v10/Verwender.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v11/A.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v11/B.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v11/C.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v11/D.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v2/Ente.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v2/Hund.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v2/Tier.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v2/Verwender.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v3/Ente.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v3/Hund.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v3/Tier.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v3/Verwender.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v4/Ente.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v4/Hund.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v4/Tier.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v4/Verwender.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v5/Angestellter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v5/Arbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v5/Lohnbuchhaltung.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v5/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v5/Vorstand.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v6/Angestellter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v6/Arbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v6/Lohnbuchhaltung.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v6/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v6/Vorstand.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v7/Angestellter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v7/Arbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v7/Lohnbuchhaltung.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v7/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v7/Vorstand.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v8/Angestellter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v8/Main.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v8/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v8/Vorstand.java create mode 100644 src/main/java/pr2/oo2/abstrakt/v9/Mitarbeiter.java create mode 100644 src/main/java/pr2/oo2/finale/blankfinal/Kunde.java create mode 100644 src/main/java/pr2/oo2/finale/v1/DeadCodeElemination.java create mode 100644 src/main/java/pr2/oo2/finale/v1/Konstanten.java create mode 100644 src/main/java/pr2/oo2/finale/v1/NoDeadCodeElemination.java create mode 100644 src/main/java/pr2/oo2/finale/v2/A.java create mode 100644 src/main/java/pr2/oo2/finale/v2/B.java create mode 100644 src/main/java/pr2/oo2/finale/v2/FinaleVariablen.java create mode 100644 src/main/java/pr2/oo2/finale/v2/Konstanten.java create mode 100644 src/main/java/pr2/oo2/finale/v2/Konstantenverwender.java create mode 100644 src/main/java/pr2/oo2/finale/v3/A.java create mode 100644 src/main/java/pr2/oo2/finale/v3/B.java create mode 100644 src/main/java/pr2/oo2/interfaces/callback/Addition.java create mode 100644 src/main/java/pr2/oo2/interfaces/callback/Berechnung.java create mode 100644 src/main/java/pr2/oo2/interfaces/callback/Funktion.java create mode 100644 src/main/java/pr2/oo2/interfaces/callback/Subtraktion.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/ExtFlieger.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/Flieger.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/Flugzeug.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/IA.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/IB.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/Impl.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/Main.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/Rechner.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/A.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/B.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/C.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/I1.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/I2.java create mode 100644 src/main/java/pr2/oo2/interfaces/java8/diamond/Main.java create mode 100644 src/main/java/pr2/oo2/interfaces/mehrfachvererbung/A.java create mode 100644 src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I1.java create mode 100644 src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I2.java create mode 100644 src/main/java/pr2/oo2/interfaces/v1/Flieger.java create mode 100644 src/main/java/pr2/oo2/interfaces/v1/Flughafen.java create mode 100644 src/main/java/pr2/oo2/interfaces/v1/Flugzeug.java create mode 100644 src/main/java/pr2/oo2/interfaces/v2/Flieger.java create mode 100644 src/main/java/pr2/oo2/interfaces/v2/Flieger2.java create mode 100644 src/main/java/pr2/oo2/interfaces/v2/Flugzeug.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Airbus.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Ente.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Flieger.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Flugzeug.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Maschine.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Schwimmer.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Superman.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Tier.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Vogel.java create mode 100644 src/main/java/pr2/oo2/interfaces/v3/Wasserflugzeug.java create mode 100644 src/main/java/pr2/oo2/interfaces/v4/Konstanten.java create mode 100644 src/main/java/pr2/oo2/interfaces/v4/Mathe.java create mode 100644 src/main/java/pr2/oo2/interfaces/v4/Mathe2.java create mode 100644 src/main/java/pr2/oo2/interfaces/v5/Sorter.java create mode 100644 src/main/java/pr2/oo2/interfaces/v5/Telefonnummer.java create mode 100644 src/main/java/pr2/oo2/interfaces/v6/A.java create mode 100644 src/main/java/pr2/oo2/interfaces/v6/B.java create mode 100644 src/main/java/pr2/oo2/interfaces/v6/I1.java create mode 100644 src/main/java/pr2/oo2/interfaces/v6/I2.java create mode 100644 src/main/java/pr2/oo2/interfaces/v6/Main.java create mode 100644 src/main/java/pr2/oo2/quiz/Ente.java create mode 100644 src/main/java/pr2/oo2/quiz/Initializer.java create mode 100644 src/main/java/pr2/oo2/quiz/QuizStatic.java create mode 100644 src/main/java/pr2/oo2/statisch/apiimpl/Calculator.java create mode 100644 src/main/java/pr2/oo2/statisch/apiimpl/CalculatorFactory.java create mode 100644 src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImpl.java create mode 100644 src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImplQuantumSuperDuper.java create mode 100644 src/main/java/pr2/oo2/statisch/apiimpl/Verwender.java create mode 100644 src/main/java/pr2/oo2/statisch/cycle/A.java create mode 100644 src/main/java/pr2/oo2/statisch/cycle/B.java create mode 100644 src/main/java/pr2/oo2/statisch/cycle/C.java create mode 100644 src/main/java/pr2/oo2/statisch/factory/Erzeugter.java create mode 100644 src/main/java/pr2/oo2/statisch/factory/Factory.java create mode 100644 src/main/java/pr2/oo2/statisch/initializer/Crash.java create mode 100644 src/main/java/pr2/oo2/statisch/initializer/CrashUser.java create mode 100644 src/main/java/pr2/oo2/statisch/singleton/Broken.java create mode 100644 src/main/java/pr2/oo2/statisch/singleton/Firma.java create mode 100644 src/main/java/pr2/oo2/statisch/singleton/Firma2.java create mode 100644 src/main/java/pr2/oo2/statisch/v1/Ticket.java create mode 100644 src/main/java/pr2/oo2/statisch/v2/TestTicket.java create mode 100644 src/main/java/pr2/oo2/statisch/v2/Ticket.java create mode 100644 src/main/java/pr2/oo2/statisch/v3/TestTicket.java create mode 100644 src/main/java/pr2/oo2/statisch/v3/Ticket.java create mode 100644 src/main/java/pr2/oo2/statisch/v4/A.java create mode 100644 src/main/java/pr2/oo2/statisch/v4/B.java create mode 100644 src/main/java/pr2/oo2/statisch/v4/Test.java create mode 100644 src/main/java/pr2/oo2/wrapper/AutoboxingDemo.java create mode 100644 src/main/java/pr2/oo2/wrapper/BoxingFailure.java create mode 100644 src/main/java/pr2/oo2/wrapper/WrapperDemo.java create mode 100644 src/main/java/pr2/records/newstyle/Rectangle.java create mode 100644 src/main/java/pr2/records/plain/Rectangle.java create mode 100644 src/main/java/pr2/reflection/Voldemort.java create mode 100644 src/main/java/pr2/reflection/api/Arrays.java create mode 100644 src/main/java/pr2/reflection/api/Fields.java create mode 100644 src/main/java/pr2/reflection/api/Instanzen.java create mode 100644 src/main/java/pr2/reflection/api/Methoden.java create mode 100644 src/main/java/pr2/reflection/apiimpltrennung/Verwender.java create mode 100644 src/main/java/pr2/reflection/apiimpltrennung/api/Calculator.java create mode 100644 src/main/java/pr2/reflection/apiimpltrennung/api/CalculatorFactory.java create mode 100644 src/main/java/pr2/reflection/apiimpltrennung/impl/CalculatorImpl.java create mode 100644 src/main/java/pr2/reflection/classloader/A.java create mode 100644 src/main/java/pr2/reflection/classloader/Test.java create mode 100644 src/main/java/pr2/reflection/classobj/Angestellter.java create mode 100644 src/main/java/pr2/reflection/classobj/Basics.java create mode 100644 src/main/java/pr2/reflection/classobj/ClazzDemo.java create mode 100644 src/main/java/pr2/reflection/classobj/Geschaeftsfuehrer.java create mode 100644 src/main/java/pr2/reflection/classobj/Manager.java create mode 100644 src/main/java/pr2/reflection/classobj/Mitarbeiter.java create mode 100644 src/main/java/pr2/reflection/classobj/Vorstand.java create mode 100644 src/main/java/pr2/reflection/ugly/Erdogan.java create mode 100644 src/main/java/pr2/reflection/ugly/Killer.java create mode 100644 src/main/java/pr2/streams/advanced/CollectorAggregation.java create mode 100644 src/main/java/pr2/streams/advanced/CollectorType.java create mode 100644 src/main/java/pr2/streams/advanced/Grouping.java create mode 100644 src/main/java/pr2/streams/advanced/Joining.java create mode 100644 src/main/java/pr2/streams/advanced/Partition.java create mode 100644 src/main/java/pr2/streams/basics/CollectSimple.java create mode 100644 src/main/java/pr2/streams/basics/Filter.java create mode 100644 src/main/java/pr2/streams/basics/FlatMap.java create mode 100644 src/main/java/pr2/streams/basics/IntStreamExample.java create mode 100644 src/main/java/pr2/streams/basics/Lazy.java create mode 100644 src/main/java/pr2/streams/basics/MapExample.java create mode 100644 src/main/java/pr2/streams/basics/MaxMin.java create mode 100644 src/main/java/pr2/streams/basics/Reduce.java create mode 100644 src/main/java/pr2/streams/basics/StreamOf.java create mode 100644 src/main/java/pr2/streams/basics/kombi/Item.java create mode 100644 src/main/java/pr2/streams/basics/kombi/Order.java create mode 100644 src/main/java/pr2/streams/basics/kombi/Simulation.java create mode 100644 src/main/java/pr2/streams/motivation/ExternalIteration.java create mode 100644 src/main/java/pr2/strukturierung/C.java create mode 100644 src/main/java/pr2/strukturierung/Datum.java create mode 100644 src/main/java/pr2/strukturierung/PackagePrivateClass.java create mode 100644 src/main/java/pr2/strukturierung/PublicClass.java create mode 100644 src/main/java/pr2/strukturierung/Scope.java create mode 100644 src/main/java/pr2/strukturierung/StaticImport.java create mode 100644 src/main/java/pr2/strukturierung/Variablen.java create mode 100644 src/main/java/pr2/strukturierung/X.java create mode 100644 src/main/java/pr2/strukturierung/firma/db/DbMitarbeiter.java create mode 100644 src/main/java/pr2/strukturierung/firma/domain/Angestellter.java create mode 100644 src/main/java/pr2/strukturierung/firma/domain/Arbeiter.java create mode 100644 src/main/java/pr2/strukturierung/firma/domain/Mitarbeiter.java create mode 100644 src/main/java/pr2/strukturierung/firma/gui/EingabeMitarbeiter.java create mode 100644 src/main/java/pr2/strukturierung/sichtbarkeit/A.java create mode 100644 src/main/java/pr2/strukturierung/sichtbarkeit/B.java create mode 100644 src/main/java/pr2/swing/AdapterDemo.java create mode 100644 src/main/java/pr2/swing/BorderLayoutDemo.java create mode 100644 src/main/java/pr2/swing/CardLayoutDemo.java create mode 100644 src/main/java/pr2/swing/ComplexLayoutDemo.java create mode 100644 src/main/java/pr2/swing/EventHandlerDemo.java create mode 100644 src/main/java/pr2/swing/FlowLayoutDemo.java create mode 100644 src/main/java/pr2/swing/GridLayoutDemo.java create mode 100644 src/main/java/pr2/swing/HelloWorld.java create mode 100644 src/main/java/pr2/swing/JFrameDemo.java create mode 100644 src/main/java/pr2/swing/MenuDemo.java create mode 100644 src/main/java/pr2/swing/OptionPaneDemo.java create mode 100644 src/main/java/pr2/swing/XYLayout.java create mode 100644 src/main/java/pr2/threads/concurrent/atomic/AtomicExample.java create mode 100644 src/main/java/pr2/threads/concurrent/callable/CallableExample.java create mode 100644 src/main/java/pr2/threads/concurrent/collections/Cache.java create mode 100644 src/main/java/pr2/threads/concurrent/executors/FutureDemo.java create mode 100644 src/main/java/pr2/threads/concurrent/executors/TaskDemo.java create mode 100644 src/main/java/pr2/threads/concurrent/locks/ConnectionPool.java create mode 100644 src/main/java/pr2/threads/concurrent/locks/Join.java create mode 100644 src/main/java/pr2/threads/concurrent/producerconsumer/Consumer.java create mode 100644 src/main/java/pr2/threads/concurrent/producerconsumer/Producer.java create mode 100644 src/main/java/pr2/threads/concurrent/producerconsumer/Starter.java create mode 100644 src/main/java/pr2/threads/concurrent/timer/Countdown.java create mode 100644 src/main/java/pr2/threads/concurrent/timer/CountdownUser.java create mode 100644 src/main/java/pr2/threads/concurrent/timer/Timebomb.java create mode 100644 src/main/java/pr2/threads/concurrent/timer/TimebombUser.java create mode 100644 src/main/java/pr2/threads/grundlagen/DaemonDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/HeapAndStackDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/Interruptible.java create mode 100644 src/main/java/pr2/threads/grundlagen/Interruptible2.java create mode 100644 src/main/java/pr2/threads/grundlagen/Interruptible3.java create mode 100644 src/main/java/pr2/threads/grundlagen/Join.java create mode 100644 src/main/java/pr2/threads/grundlagen/SimpleHeapAndStack.java create mode 100644 src/main/java/pr2/threads/grundlagen/Stoppable.java create mode 100644 src/main/java/pr2/threads/grundlagen/ThreadDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/nested/DaemonDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/nested/HeapAndStackDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/nested/ThreadDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/runnable/BrokenRestart.java create mode 100644 src/main/java/pr2/threads/grundlagen/runnable/ThreadDemo.java create mode 100644 src/main/java/pr2/threads/grundlagen/threadlocal/Daten.java create mode 100644 src/main/java/pr2/threads/grundlagen/threadlocal/ThreadLocalDemo.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer/Consumer.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer/Producer.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer/Starter.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer_broken/Consumer.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer_broken/Producer.java create mode 100644 src/main/java/pr2/threads/koordination/producerconumer_broken/Starter.java create mode 100644 src/main/java/pr2/threads/schrecken/deadlock/Deadlock.java create mode 100644 src/main/java/pr2/threads/schrecken/hazards/Livelock.java create mode 100644 src/main/java/pr2/threads/schrecken/hazards/SerialNumberGenerator.java create mode 100644 src/main/java/pr2/threads/schrecken/hazards/Singleton.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/BrokenSimpleStack.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/BrokenStack.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/FixedLiveLock.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/LiveLock2.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/Livelock.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/MemoryBarrier.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/explizit/SimpleStack.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/methods/SimpleStack.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/none/SimpleStack.java create mode 100644 src/main/java/pr2/threads/schrecken/synchro/thi/SimpleStack.java create mode 100644 src/main/java/pr2/threads/schrecken/vola/LivelockFixed.java create mode 100644 src/main/java/pr2/threads/schrecken/vola/PossibleReordering.java create mode 100644 src/main/java/pr2/threads/schrecken/vola/Reordering.java diff --git a/src/main/java/pr2/ads/adt/BinaryTree.java b/src/main/java/pr2/ads/adt/BinaryTree.java new file mode 100644 index 0000000..92c1a03 --- /dev/null +++ b/src/main/java/pr2/ads/adt/BinaryTree.java @@ -0,0 +1,14 @@ +package pr2.ads.adt; + +public class BinaryTree { + private TreeNode head; + private TreeNode nullNode; + + public BinaryTree() { + head = new TreeNode(null); + nullNode = new TreeNode(null); + head.setRight(nullNode); + nullNode.setLeft(nullNode); + nullNode.setRight(nullNode); + } +} diff --git a/src/main/java/pr2/ads/adt/Queue.java b/src/main/java/pr2/ads/adt/Queue.java new file mode 100644 index 0000000..bac42cf --- /dev/null +++ b/src/main/java/pr2/ads/adt/Queue.java @@ -0,0 +1,8 @@ +package pr2.ads.adt; + +public interface Queue { + public void enter(Object obj) throws QueueException; + public Object leave() throws QueueException; + public Object front() throws QueueException; + public boolean isEmpty(); +} diff --git a/src/main/java/pr2/ads/adt/QueueException.java b/src/main/java/pr2/ads/adt/QueueException.java new file mode 100644 index 0000000..1f71b03 --- /dev/null +++ b/src/main/java/pr2/ads/adt/QueueException.java @@ -0,0 +1,4 @@ +package pr2.ads.adt; + +public class QueueException extends RuntimeException { +} diff --git a/src/main/java/pr2/ads/adt/Stack.java b/src/main/java/pr2/ads/adt/Stack.java new file mode 100644 index 0000000..5aff585 --- /dev/null +++ b/src/main/java/pr2/ads/adt/Stack.java @@ -0,0 +1,8 @@ +package pr2.ads.adt; + +public interface Stack { + void push(Object obj) throws StackException; + Object pop() throws StackException; + Object top() throws StackException; + boolean isEmpty(); +} diff --git a/src/main/java/pr2/ads/adt/StackException.java b/src/main/java/pr2/ads/adt/StackException.java new file mode 100644 index 0000000..cd8aac0 --- /dev/null +++ b/src/main/java/pr2/ads/adt/StackException.java @@ -0,0 +1,4 @@ +package pr2.ads.adt; + +public class StackException extends RuntimeException { +} diff --git a/src/main/java/pr2/ads/adt/TreeNode.java b/src/main/java/pr2/ads/adt/TreeNode.java new file mode 100644 index 0000000..5ab3529 --- /dev/null +++ b/src/main/java/pr2/ads/adt/TreeNode.java @@ -0,0 +1,22 @@ +package pr2.ads.adt; + +public class TreeNode { + TreeNode left = null; + TreeNode right = null; + + Object key; + + public TreeNode(Object o) { key = o; } + + public Object getKey() { return key; } + + public TreeNode getLeft() { return left; } + + public TreeNode getRight() { return right; } + + public void setLeft(TreeNode n) { left = n; } + + public void setRight(TreeNode n) { right = n; } + + public String toString() { return key.toString (); } +} diff --git a/src/main/java/pr2/ads/searching/BinarySearch.java b/src/main/java/pr2/ads/searching/BinarySearch.java new file mode 100644 index 0000000..08e087e --- /dev/null +++ b/src/main/java/pr2/ads/searching/BinarySearch.java @@ -0,0 +1,23 @@ +package pr2.ads.searching; + +public class BinarySearch implements Searcher { + @Override + public int search(int e, int[] data) { + int u = 0; + int o = data.length - 1; + while (u <= o) { + int m = (u + o) / 2; + if (data[m] == e) { + return m; + } + else if (data[m] > e) { + o = m - 1; + } + else { + u = m + 1; + } + } + + return NOT_FOUND; + } +} diff --git a/src/main/java/pr2/ads/searching/LinearSearch.java b/src/main/java/pr2/ads/searching/LinearSearch.java new file mode 100644 index 0000000..5861284 --- /dev/null +++ b/src/main/java/pr2/ads/searching/LinearSearch.java @@ -0,0 +1,14 @@ +package pr2.ads.searching; + +public class LinearSearch implements Searcher { + @Override + public int search(int e, int[] data) { + for (int i = 0; i < data.length; i++) { + if (e == data[i]) { + return i; + } + } + + return NOT_FOUND; + } +} diff --git a/src/main/java/pr2/ads/searching/Searcher.java b/src/main/java/pr2/ads/searching/Searcher.java new file mode 100644 index 0000000..fe89094 --- /dev/null +++ b/src/main/java/pr2/ads/searching/Searcher.java @@ -0,0 +1,8 @@ +package pr2.ads.searching; + +public interface Searcher { + + int NOT_FOUND = -1; + + int search(int e, int[] data); +} diff --git a/src/main/java/pr2/ads/searching/TestSearch.java b/src/main/java/pr2/ads/searching/TestSearch.java new file mode 100644 index 0000000..43e6089 --- /dev/null +++ b/src/main/java/pr2/ads/searching/TestSearch.java @@ -0,0 +1,27 @@ +package pr2.ads.searching; + +import java.util.Random; + +public class TestSearch { + + public static void main(String[] args) { + final int size = 1000; + var data = new int[size]; + var r = new Random(); + + for (int i = 0; i < data.length; i++) { + data[i] = i; + } + + Searcher s; + int index; + + s = new LinearSearch(); + index = s.search(r.nextInt(size), data); + System.out.println(index); + + s = new BinarySearch(); + index = s.search(r.nextInt(size), data); + System.out.println(index); + } +} diff --git a/src/main/java/pr2/ads/shuffle/FisherYatesShuffle.java b/src/main/java/pr2/ads/shuffle/FisherYatesShuffle.java new file mode 100644 index 0000000..f785166 --- /dev/null +++ b/src/main/java/pr2/ads/shuffle/FisherYatesShuffle.java @@ -0,0 +1,18 @@ +package pr2.ads.shuffle; + +import java.util.Random; + +public class FisherYatesShuffle implements Shuffler { + + @Override + public void shuffle(int[] data) { + Random rand = new Random(); + + for (int i = data.length - 1; i > 0; i--) { + int randomIndexToSwap = rand.nextInt(data.length); + int temp = data[randomIndexToSwap]; + data[randomIndexToSwap] = data[i]; + data[i] = temp; + } + } +} diff --git a/src/main/java/pr2/ads/shuffle/NaiveShuffle.java b/src/main/java/pr2/ads/shuffle/NaiveShuffle.java new file mode 100644 index 0000000..591b6d4 --- /dev/null +++ b/src/main/java/pr2/ads/shuffle/NaiveShuffle.java @@ -0,0 +1,28 @@ +package pr2.ads.shuffle; + +import java.util.LinkedList; +import java.util.Random; + +public class NaiveShuffle implements Shuffler { + @Override + public void shuffle(int[] data) { + Random rand = new Random(); + + int n = data.length; + + var result = new int[n]; + var index = new LinkedList(); + + for (int i = 0; i < n; i++) { + index.add(i); + } + + for (int i = 0; i < n; i++) { + int z = rand.nextInt(n - i); + result[i] = data[index.get(z)]; + index.remove(z); + } + + System.arraycopy(result, 0, data, 0, n); + } +} diff --git a/src/main/java/pr2/ads/shuffle/Shuffler.java b/src/main/java/pr2/ads/shuffle/Shuffler.java new file mode 100644 index 0000000..0b2c5ea --- /dev/null +++ b/src/main/java/pr2/ads/shuffle/Shuffler.java @@ -0,0 +1,12 @@ +package pr2.ads.shuffle; + +public interface Shuffler { + + void shuffle(int[] data); + + default void fill(int[] data) { + for (int i = 0; i < data.length; i++) { + data[i] = i; + } + } +} diff --git a/src/main/java/pr2/ads/shuffle/TestShuffle.java b/src/main/java/pr2/ads/shuffle/TestShuffle.java new file mode 100644 index 0000000..b7f0dc8 --- /dev/null +++ b/src/main/java/pr2/ads/shuffle/TestShuffle.java @@ -0,0 +1,20 @@ +package pr2.ads.shuffle; + +import java.util.Arrays; + +public class TestShuffle { + + public static void main(String[] args) { + Shuffler naive = new NaiveShuffle(); + Shuffler fisher = new FisherYatesShuffle(); + + int[] data = new int[10]; + naive.fill(data); + naive.shuffle(data); + System.out.println(Arrays.toString(data)); + + fisher.fill(data); + fisher.shuffle(data); + System.out.println(Arrays.toString(data)); + } +} diff --git a/src/main/java/pr2/ads/sorting/BubbleSort.java b/src/main/java/pr2/ads/sorting/BubbleSort.java new file mode 100644 index 0000000..b186e33 --- /dev/null +++ b/src/main/java/pr2/ads/sorting/BubbleSort.java @@ -0,0 +1,24 @@ +package pr2.ads.sorting; + +public class BubbleSort implements Sorter { + + @Override + public void sort(int[] data) { + boolean swapped; + do { + swapped = false; + for (int i = 0; i < data.length - 1; i++) { + if (data[i] > data[i + 1]) { + // Elemente vertauschen + swap(data, i, i + 1); + swapped = true; + } + } + } while (swapped); + } + + @Override + public String toString() { + return "BubbleSort"; + } +} diff --git a/src/main/java/pr2/ads/sorting/InsertionSort.java b/src/main/java/pr2/ads/sorting/InsertionSort.java new file mode 100644 index 0000000..15bda5d --- /dev/null +++ b/src/main/java/pr2/ads/sorting/InsertionSort.java @@ -0,0 +1,28 @@ +package pr2.ads.sorting; + +public class InsertionSort implements Sorter { + + @Override + public void sort(int[] data) { + for (int i = 1; i < data.length; i++) { + int m = data[i]; + int j = i; + while (j > 0) { + if (data[j - 1] >= m) { + /* Verschiebe data[j - 1] eine Position nach rechts */ + data[j] = data[j - 1]; + j--; + } + else { + break; + } + } + data[j] = m; + } + } + + @Override + public String toString() { + return "InsertionSort"; + } +} diff --git a/src/main/java/pr2/ads/sorting/MergeSort.java b/src/main/java/pr2/ads/sorting/MergeSort.java new file mode 100644 index 0000000..03831d8 --- /dev/null +++ b/src/main/java/pr2/ads/sorting/MergeSort.java @@ -0,0 +1,46 @@ +package pr2.ads.sorting; + +public class MergeSort implements Sorter { + + void msort(int[] array, int le, int ri, int[] helper) { + int i, j, k; + if (ri > le) { + // zu sortierendes Feld teilen + int mid = (ri + le) / 2; + + // Teilfelder sortieren + msort(array, le, mid, helper); + msort(array, mid + 1, ri, helper); + + // Hilfsfeld aufbauen + for (k = le; k <= mid; k++) { + helper[k] = array[k]; + } + + for (k = mid; k < ri; k++) { + helper[ri + mid - k] = array[k + 1]; + } + + // Ergebnisse mischen über Hilfsfeld + i = le; j = ri; + for (k = le; k <= ri; k++) { + if (helper[i] < helper[j]) { + array[k] = helper[i++]; + } else { + array[k] = helper[j--]; + } + } + } + } + + @Override + public void sort(int[] data) { + int[] helper = new int[data.length]; + msort(data, 0, data.length - 1, helper); + } + + @Override + public String toString() { + return "MergeSort"; + } +} diff --git a/src/main/java/pr2/ads/sorting/QuickSort.java b/src/main/java/pr2/ads/sorting/QuickSort.java new file mode 100644 index 0000000..1aef27b --- /dev/null +++ b/src/main/java/pr2/ads/sorting/QuickSort.java @@ -0,0 +1,47 @@ +package pr2.ads.sorting; + +public class QuickSort implements Sorter { + + int partition(int[] array, int u, int o, int p) { + int pn = u; + int pv = array[p]; + // Pivot-Element an das Ende verschieben + swap(array, p, o); + for (int i = u; i < o; i++) { + if (array[i] <= pv) { + swap(array, pn++, i); + } + } + + // Pivot-Element an die richtige Position kopieren + swap(array, o, pn); + + // neue Pivot-Position zurückgeben + return pn; + } + + + // Hilfsmethode zum rekursiven Sortieren + void qsort(int[] array, int u, int o) { + + // Pivot-Element bestimmen + int p = (u + o) / 2; + if (o > u) { + // Feld zerlegen + int pn = partition(array, u, o, p); + // und Partitionen sortieren + qsort(array, u, pn - 1); + qsort(array, pn + 1, o); + } + } + + @Override + public void sort(int[] data) { + qsort(data, 0, data.length - 1); + } + + @Override + public String toString() { + return "QuickSort"; + } +} diff --git a/src/main/java/pr2/ads/sorting/SelectionSort.java b/src/main/java/pr2/ads/sorting/SelectionSort.java new file mode 100644 index 0000000..bc88421 --- /dev/null +++ b/src/main/java/pr2/ads/sorting/SelectionSort.java @@ -0,0 +1,26 @@ +package pr2.ads.sorting; + +public class SelectionSort implements Sorter { + + @Override + public void sort(int[] data) { + int marker = data.length - 1; + while (marker >= 0) { + // bestimme größtes Element + int max = 0; + for (int i = 1; i <= marker; i++) { + if (data[i] > data[max]) { + max = i; + } + } + // tausche array[marker] mit diesem Element + swap(data, marker, max); + marker--; + } + } + + @Override + public String toString() { + return "SelectionSort"; + } +} diff --git a/src/main/java/pr2/ads/sorting/Sorter.java b/src/main/java/pr2/ads/sorting/Sorter.java new file mode 100644 index 0000000..0721117 --- /dev/null +++ b/src/main/java/pr2/ads/sorting/Sorter.java @@ -0,0 +1,12 @@ +package pr2.ads.sorting; + +public interface Sorter { + + void sort(int[] data); + + default void swap(int[] data, int i1, int i2) { + int tmp = data[i1]; + data[i1] = data[i2]; + data[i2] = tmp; + } +} diff --git a/src/main/java/pr2/ads/sorting/TestSorting.java b/src/main/java/pr2/ads/sorting/TestSorting.java new file mode 100644 index 0000000..6b96310 --- /dev/null +++ b/src/main/java/pr2/ads/sorting/TestSorting.java @@ -0,0 +1,75 @@ +package pr2.ads.sorting; + +import pr2.nestedclasses.lambda.function.RechnerKurz; + +import java.util.*; + +public class TestSorting { + + private static void shuffle(int[] data) { + Random rand = new Random(); + + for (int i = 0; i < data.length; i++) { + int randomIndexToSwap = rand.nextInt(data.length); + int temp = data[randomIndexToSwap]; + data[randomIndexToSwap] = data[i]; + data[i] = temp; + } + } + + static long testAlgorithm(Class clazz, int size, int runs) { + + Sorter s; + + var data = new int[size]; + for (int i = 0; i < data.length; i++) { + data[i] = i; + } + + long start = System.nanoTime(); + + try { + s = (Sorter) clazz.getDeclaredConstructor().newInstance(); + + for (int i = 0; i < runs; i++) { + shuffle(data); + s.sort(data); + } + } + catch (Exception e) { + // do nothing + return 0; + } + + + long runtime = System.nanoTime() - start; + + return runtime; + + } + + public static void measure(int size_steps, int max_size, int runs) { + System.out.println("Runs\tBS\tIS\tMS\tQS\tSS"); + for (int size = 0; size <= max_size; size += size_steps) { + long bs = testAlgorithm(BubbleSort.class, size, runs); + long is = testAlgorithm(InsertionSort.class, size, runs); + long ms = testAlgorithm(MergeSort.class, size, runs); + long qs = testAlgorithm(QuickSort.class, size, runs); + long ss = testAlgorithm(SelectionSort.class, size, runs); + System.out.printf("%d\t%d\t%d\t%d\t%d\t%d\n", size, bs, is, ms, qs, ss); + } + } + public static void main(String[] args) { + + measure(10, 1000, 100); + + final int size = 1000; + final int runs = 1000; + + testAlgorithm(BubbleSort.class, size, runs); + testAlgorithm(InsertionSort.class, size, runs); + testAlgorithm(MergeSort.class, size, runs); + testAlgorithm(QuickSort.class, size, runs); + testAlgorithm(SelectionSort.class, size, runs); + } +} diff --git a/src/main/java/pr2/ads/textsearch/BoyerMooreTextSearch.java b/src/main/java/pr2/ads/textsearch/BoyerMooreTextSearch.java new file mode 100644 index 0000000..fa8cb7b --- /dev/null +++ b/src/main/java/pr2/ads/textsearch/BoyerMooreTextSearch.java @@ -0,0 +1,49 @@ +package pr2.ads.textsearch; + +import java.util.HashMap; +import java.util.Map; + +public class BoyerMooreTextSearch implements TextSearch { + + public int search(String haystack, String needle) { + char[] text = haystack.toCharArray(); + char[] pattern = needle.toCharArray(); + + int n = text.length; + int m = pattern.length; + // Test for empty string + if (m == 0) return 0; + // Initialization, create Map of last position of each character = O(n) + Map last = new HashMap<>(); + for (char c : text) { + // set all chars, by default, to -1 + last.put(c, -1); + } + for (int i = 0; i < m; i++) { + // update last seen positions + last.put(pattern[i], i); + } + //Start with the end of the pattern aligned at index m-1 in the text. + //index into the text + int i = m - 1; + // index into the pattern + int k = m - 1; + while (i < n) { + if (text[i] == pattern[k]) { + // match! return i if complete match; otherwise, keep checking + if (k == 0) { + return i; + } + i--; k--; + } + else { // jump step + restart at end of pattern + //iterate over text + i += m - Math.min(k, 1 + last.get(text[i])); + //move to end of pattern + k = m - 1; + } + } + // not found + return NOT_FOUND; + } +} diff --git a/src/main/java/pr2/ads/textsearch/BruteForceTextSearch.java b/src/main/java/pr2/ads/textsearch/BruteForceTextSearch.java new file mode 100644 index 0000000..3f37ca9 --- /dev/null +++ b/src/main/java/pr2/ads/textsearch/BruteForceTextSearch.java @@ -0,0 +1,22 @@ +package pr2.ads.textsearch; + +public class BruteForceTextSearch implements TextSearch { + + public int search(String haystack, String needle) { + int n = haystack.length(); + int m = needle.length(); + + for (int i = 0; i <= n - m; i++) { + int j; + for (j = 0; j < m; j++) { + if (haystack.charAt(i + j) != needle.charAt(j)) { + break; + } + } + if (j == m) { + return i; + } + } + return NOT_FOUND; + } +} diff --git a/src/main/java/pr2/ads/textsearch/KMPTextSearch.java b/src/main/java/pr2/ads/textsearch/KMPTextSearch.java new file mode 100644 index 0000000..d47ba77 --- /dev/null +++ b/src/main/java/pr2/ads/textsearch/KMPTextSearch.java @@ -0,0 +1,46 @@ +package pr2.ads.textsearch; + +public class KMPTextSearch implements TextSearch { + + public int search(String haystack, String needle) { + int[] lps = computeLPS(needle); + int i = 0, j = 0; + while (i < haystack.length()) { + if (needle.charAt(j) == haystack.charAt(i)) { + i++; + j++; + } + if (j == needle.length()) { + return i - j; + } else if (i < haystack.length() && needle.charAt(j) != haystack.charAt(i)) { + if (j != 0) { + j = lps[j - 1]; + } else { + i = i + 1; + } + } + } + return -1; + } + + public int[] computeLPS(String pattern) { + int[] lps = new int[pattern.length()]; + int j = 0; + for (int i = 1; i < pattern.length();) { + if (pattern.charAt(i) == pattern.charAt(j)) { + lps[i] = j + 1; + i++; + j++; + } else { + if (j != 0) { + j = lps[j - 1]; + } else { + lps[i] = 0; + i++; + } + } + } + return lps; + } + +} diff --git a/src/main/java/pr2/ads/textsearch/TestTextSearch.java b/src/main/java/pr2/ads/textsearch/TestTextSearch.java new file mode 100644 index 0000000..8f14ee5 --- /dev/null +++ b/src/main/java/pr2/ads/textsearch/TestTextSearch.java @@ -0,0 +1,21 @@ +package pr2.ads.textsearch; + +public class TestTextSearch { + public static void main(String[] args) { + + String haystack = "DASISTEINSINNLOSERTEXT"; + String needle = "SINN"; + + TextSearch bf = new BruteForceTextSearch(); + System.out.println(bf.search(haystack, needle)); + + TextSearch kmp = new KMPTextSearch(); + System.out.println(kmp.search(haystack, needle)); + + TextSearch bm = new BoyerMooreTextSearch(); + System.out.println(bm.search(haystack, needle)); + + + + } +} diff --git a/src/main/java/pr2/ads/textsearch/TextSearch.java b/src/main/java/pr2/ads/textsearch/TextSearch.java new file mode 100644 index 0000000..46bc87b --- /dev/null +++ b/src/main/java/pr2/ads/textsearch/TextSearch.java @@ -0,0 +1,8 @@ +package pr2.ads.textsearch; + +public interface TextSearch { + + int NOT_FOUND = -1; + + int search(String haystack, String needle); +} diff --git a/src/main/java/pr2/auffrischung/CodingStandard.java b/src/main/java/pr2/auffrischung/CodingStandard.java new file mode 100644 index 0000000..8dd76e1 --- /dev/null +++ b/src/main/java/pr2/auffrischung/CodingStandard.java @@ -0,0 +1,39 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.auffrischung; + +/** + * Klasse dient als Beispiel für den Coding-Standard und die Namenskonventionen + * bei Java-Programmen. + * + * @author Thomas Smits + */ +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; + + /** + * Methode, die etwas tut - oder auch nicht ;-). + * + * @param parameter Eingabewert für die Methode + * @return gibt immer 42 zurück + */ + public int methodeDieWasTut(int parameter) { + + if (parameter > 3) { + erstesFeld = 12; + } + else { + zweitesFeld = 13; + } + + return 42; + } +} diff --git a/src/main/java/pr2/auffrischung/Ding.java b/src/main/java/pr2/auffrischung/Ding.java new file mode 100644 index 0000000..171e004 --- /dev/null +++ b/src/main/java/pr2/auffrischung/Ding.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.auffrischung; + +public class Ding { + + // Dies ist ein Kommentar in einer Zeile + + /* dies ist ein Kommentar + über mehrere Zeilen */ + + /** Hier kommt JavaDoc */ + + /** Hier kommt JavaDoc, + * gerne auch Mehrzeilig */ + + private int x; + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + // Testkommentar +} \ No newline at end of file diff --git a/src/main/java/pr2/auffrischung/IsPrim.java b/src/main/java/pr2/auffrischung/IsPrim.java new file mode 100644 index 0000000..faecd1c --- /dev/null +++ b/src/main/java/pr2/auffrischung/IsPrim.java @@ -0,0 +1,22 @@ +/* (c) 2022 Thomas Smits */ +package pr2.auffrischung; + +import java.util.Scanner; + +/** + * + * + * @author Thomas Smits + */ +public class IsPrim { + + @SuppressWarnings("resource") + public static void main(String[] args) { + int r, i = 2, n = new Scanner(System.in).nextInt(); + while ((r = (n % i)) != 0 & (i++ < Math.sqrt(n))); + System.out.println(r == 0 ? "Keine Primzahl (" + --i +")" : "Primzahl"); + } + + //for (int i = 2; (r = (n % i)) != 0 && (i < Math.sqrt(n)); i++); + +} diff --git a/src/main/java/pr2/auffrischung/Kontrollstrukturen.java b/src/main/java/pr2/auffrischung/Kontrollstrukturen.java new file mode 100644 index 0000000..d6ab342 --- /dev/null +++ b/src/main/java/pr2/auffrischung/Kontrollstrukturen.java @@ -0,0 +1,201 @@ +/* + * (c) 2011 Thomas Smits + */ +package pr2.auffrischung; + +public class Kontrollstrukturen { + + public void doIt() { + + int count = getCount(); // Methode im Programm + + if (count < 0) { + System.out.println("Fehler: Zahl ist negativ."); + } else if (count > getMaxCount()) { + System.out.println("Fehler: Zahl ist zu groß."); + } else { + System.out.println("Es kommen " + count + " Leute"); + } + + int mitarbeiterArt = 5; + final int MANAGER = 1; + final int SENIOR_DEVELOPER = 2; + + switch (mitarbeiterArt) { + + case MANAGER: + addEinzelnesBuero(); + addFirmenwagen(); + addSekretaerin(); + break; + + case SENIOR_DEVELOPER: + addEinzelnesBuero(); + addFirmenwagen(); + break; + + default: + addGrossraumbuero(); + } + + // Kindersimulator + for (int i = 0; i < 10; i++) { + System.out.println("Sind wir schon da?"); + System.out.println("Ist es noch weit?"); + } + + System.out.println("Jetzt sind wir angekommen!"); + + + int i = 0; + for (; i < 10; i++) { + System.out.println("Variable außerhalb deklariert"); + } + + int j = 0; + for (; j < 10;) { + j++; + System.out.println("Variable außerhalb deklariert"); + System.out.println("Inkrement innerhalb"); + } + + for (i = 0, j = 0; j < 10; i++, j++) { } + + + int[] primzahlen = { 2, 3, 5, 7, 11, 13, 17, 19}; + int summe = 0; + + for (int primzahl : primzahlen) { + summe += primzahl; + } + + +// for (int i = 0; i < primzahlen.length; i++) { +// int primzahl = primzahlen[i]; +// summe += primzahl; +// } + + + for (;;) { + System.out.println("Endlosschleife"); + } + } + + public void switch12() { + var titel = "Senior"; + + switch (titel) { + case "Manager" -> { + addEinzelnesBuero(); + addFirmenwagen(); + addSekretaerin(); + } + case "Senior" -> { + addEinzelnesBuero(); + addFirmenwagen(); + } + default -> addGrossraumbuero(); + } + + int monat = 2; + int tage; + + switch (monat) { + case 2: + tage = 28; break; + case 4: + case 6: + case 9: + case 11: + tage = 30; break; + default: + tage = 31; + } + + int days = switch (monat) { + case 2 -> 28; + case 4, 6, 9, 11 -> 30; + default -> 31; + }; + } + + public void doit2() { + + + int i = 0; + + while (i < 10) { + System.out.println("Sind wir schon da?"); + System.out.println("Ist es noch weit?"); + i++; + } + + System.out.println("Jetzt sind wir angekommen!"); + } + + public void doit3() { + + int i = 0; + + do { + System.out.println("Sind wir schon da?"); + System.out.println("Ist es noch weit?"); + i++; + } while (i < 10); + + + System.out.println("Jetzt sind wir angekommen!"); + + do + System.out.println("Hallo"); + while (true); + } + + private void addGrossraumbuero() { + // TODO Auto-generated method stub + + } + + private void addSekretaerin() { + // TODO Auto-generated method stub + + } + + private void addFirmenwagen() { + // TODO Auto-generated method stub + + } + + private void addEinzelnesBuero() { + // TODO Auto-generated method stub + + } + + private void addEngine() { + // TODO Auto-generated method stub + + } + + private void addWheels() { + // TODO Auto-generated method stub + + } + + private void addRadio() { + // TODO Auto-generated method stub + + } + + private void addAirConditioning() { + // TODO Auto-generated method stub + + } + + int getCount() { + return 5; + } + + int getMaxCount() { + return 5; + } +} diff --git a/src/main/java/pr2/auffrischung/QuizString.java b/src/main/java/pr2/auffrischung/QuizString.java new file mode 100644 index 0000000..1acc169 --- /dev/null +++ b/src/main/java/pr2/auffrischung/QuizString.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.auffrischung; + +/** + * + * @author Thomas Smits + */ +public class QuizString { + + public static void main(String[] args) { + String s1 = "Hallo"; + String s2 = new String("World"); + String s3 = s2; + String s4 = s1.toUpperCase().trim(); + } +} diff --git a/src/main/java/pr2/auffrischung/Scope.java b/src/main/java/pr2/auffrischung/Scope.java new file mode 100644 index 0000000..70ad030 --- /dev/null +++ b/src/main/java/pr2/auffrischung/Scope.java @@ -0,0 +1,35 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.auffrischung; + +class Scope { + int i = 1; + + void ersteMethode() { + int i = 4; + int j = 5; + + { + int k = 7; + this.i = i + j; + zweiteMethode(k); + } + + { + int k = 7; + } + } + + void zweiteMethode(int i) { + int j = 8; + this.i = i + j; + } + + public static void main(String[] args) { + Scope scope = new Scope(); + scope.ersteMethode(); + System.out.println(5 * 70 % 6); + System.out.println(5 * (70 % 6)); + } +} diff --git a/src/main/java/pr2/auffrischung/Zuweisung.java b/src/main/java/pr2/auffrischung/Zuweisung.java new file mode 100644 index 0000000..bb78a64 --- /dev/null +++ b/src/main/java/pr2/auffrischung/Zuweisung.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.auffrischung; + +public class Zuweisung { + + public static void main(String args[]) { + + int x, y; + + float z = 1.712f; + + double w = 3.1415; + + boolean wahrheit = true; + + char c; + + String str; + + String str1 = "Hi"; + + c = 'Z'; + + str = "Hallo Welt"; + + x = 60; + + y = 400; + + int i = 5, j[] = { 1,2,3 }, k[][] = new int[2][3]; + } +} diff --git a/src/main/java/pr2/auffrischung/quiz/IfWithoutBlock.java b/src/main/java/pr2/auffrischung/quiz/IfWithoutBlock.java new file mode 100644 index 0000000..6fb5da3 --- /dev/null +++ b/src/main/java/pr2/auffrischung/quiz/IfWithoutBlock.java @@ -0,0 +1,14 @@ +/* (c) 2022 Thomas Smits */ +package pr2.auffrischung.quiz; + +public class IfWithoutBlock { + + public static void main(String[] args) { + + boolean b = false; + + if (b) + System.out.println("b ist true"); + System.out.println("b ist true"); + } +} diff --git a/src/main/java/pr2/auffrischung/quiz/Puzzles.java b/src/main/java/pr2/auffrischung/quiz/Puzzles.java new file mode 100644 index 0000000..c82b26c --- /dev/null +++ b/src/main/java/pr2/auffrischung/quiz/Puzzles.java @@ -0,0 +1,113 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.auffrischung.quiz; + +public class Puzzles { + + public static void main(String[] args) { + + { + // Integer Division + int i = 1; + double d; + + d = i / 2; + + System.out.println(d); + + d = (double) i / 2; + + System.out.println(d); + + d = i / 2.0; + + System.out.println(d); + } + + { + // If und Zuweisung + boolean b = false; + + if (b = true) { + System.out.println("Oops"); + } + } + + { + // Case fallthru + int x = 3; + + switch (x) { + + case 1: + System.out.println("1"); + + case 2: + System.out.println("2"); + + case 3: + System.out.println("3"); + + case 4: + System.out.println("4"); + + default: + System.out.println("Passt nicht"); + } + } + + { + // Wetebreich + byte b = 127; + b++; + + System.out.println(b); + } + + { +// // Array-Indizes +// int[] array = new int[5]; +// +// for (int i = 1; i < 6; i++) { +// array[i] = i; +// } + } + + { +// // Und / Oder ohne Kurzschluss +// String s = null; +// +// if ((s != null) & (s.length() > 0)) { +// System.out.println(s.charAt(0)); +// } + } + } + + void swap(String a, String b) { + String temp = a; + a = b; + b = temp; + } + + void testSwap() { + String a = "Hallo"; + String b = "Welt"; + swap(a, b); + System.out.println(a + " " + b); + } + + /* Swap funktioniert nicht :-( + void methodeMitKommentaren() { + // Erzeuge zwei Strings mit sinnvollem Inhalt + String a = "Hallo"; + String b = "Welt"; + + // Vertausche die Variablen + swap(a, b); + + // Drucke Ergebnis aus + System.out.println(a + " " + b); + } + */ +} diff --git a/src/main/java/pr2/auffrischung/quiz/Reference.java b/src/main/java/pr2/auffrischung/quiz/Reference.java new file mode 100644 index 0000000..fe225a2 --- /dev/null +++ b/src/main/java/pr2/auffrischung/quiz/Reference.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.auffrischung.quiz; + +import java.awt.Point; + +public class Reference { + + public static void main(String[] args) { + Point p1 = new Point(3, 4); + Point p2 = p1; + + p2.setLocation(5, 6); + + System.out.println(p1); + System.out.println(p2); + } +} diff --git a/src/main/java/pr2/auffrischung/quiz/Swap.java b/src/main/java/pr2/auffrischung/quiz/Swap.java new file mode 100644 index 0000000..9971c6f --- /dev/null +++ b/src/main/java/pr2/auffrischung/quiz/Swap.java @@ -0,0 +1,22 @@ +/* (c) 2022 Thomas Smits */ +package pr2.auffrischung.quiz; + +public class Swap { + + void swap(String s1, String s2) { + String temp = s1; + s1 = s2; + s2 = temp; + } + + void swapMe() { + String s1 = "Hallo"; + String s2 = "Welt"; + swap(s1, s2); + System.out.println(s1 + " " + s2); + } + + public static void main(String[] args) { + new Swap().swapMe(); + } +} diff --git a/src/main/java/pr2/ausnahmen/Java7.java b/src/main/java/pr2/ausnahmen/Java7.java new file mode 100644 index 0000000..effd05d --- /dev/null +++ b/src/main/java/pr2/ausnahmen/Java7.java @@ -0,0 +1,118 @@ +/* (c) 2022 Thomas Smits */ +package pr2.ausnahmen; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * + * + * @author Thomas Smits + */ +public class Java7 { + + /** + * @param args the command line arguments. + */ + public static void main(String[] args) { + + String titel = "Noob"; + + switch (titel) { + case "Manager": + break; + + case "Senior": + break; + + default: + break; + } + + List list = new ArrayList<>(); + Map map = new HashMap<>(); + Set set = new HashSet<>(); + + new Java7().catchThrow(); + + try { + openFile(); + writeToDatabase(); + } + catch (FileNotFoundException | SQLException e) { + System.out.println("Ohje, alles kapput...:" + e); + } + } + + public static void openFile() throws FileNotFoundException { + throw new FileNotFoundException(); + } + + public static void writeToDatabase() throws SQLException { + throw new SQLException(); + } + + + public void catchThrow() { + try { + try { + openFile(); + } + catch (IOException e) { + throw e; + } + } + catch (Exception e) { + System.out.println(e.getClass()); + } + } + + + public void resources() { + try(FileInputStream fis = new FileInputStream("/tmp/file1"); + FileOutputStream fos = new FileOutputStream("/tmp/file2")) { + + int i = fis.read(); + fos.write(i); + } + catch (IOException e) { + // Ausnahme behandeln + } + } + + + public void resourcesOld() throws IOException { + + FileInputStream fis = new FileInputStream("/tmp/file1"); + FileOutputStream fos = new FileOutputStream("/tmp/file2"); + + try { + int i = fis.read(); + fos.write(i); + } + catch (IOException e) { + // Ausnahme behandeln + } + finally { + try { + fis.close(); + } + catch (IOException e) { /* ignore */ } + + try { + fos.close(); + } + catch (IOException e) { /* ignore */ } + } + } + +} diff --git a/src/main/java/pr2/ausnahmen/callstack/CallStack1.java b/src/main/java/pr2/ausnahmen/callstack/CallStack1.java new file mode 100644 index 0000000..1da74c6 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/callstack/CallStack1.java @@ -0,0 +1,40 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.callstack; + +import java.io.FileInputStream; +import java.io.IOException; + +public class CallStack1 { + + public static void main(String[] args) { + + if (args.length < 1) { + System.err.println("Bitte Dateinamen angeben."); + System.exit(4); + } + + + try { + mergeData(args[0], "/tmp/output"); + } catch (IOException e) { + System.err.printf("Datei %s gibt es nicht%n%n", args[0]); + } + } + + public static void mergeData(String inFile, String outFile) + throws IOException { + writeData(inFile, outFile); + } + + public static void writeData(String inFile, String outFile) throws IOException { + aggregate(5, 5, inFile); + } + + public static int aggregate(int a, int b, String inFile) throws IOException { + FileInputStream f = new FileInputStream(inFile); + f.close(); + return 1; + } +} diff --git a/src/main/java/pr2/ausnahmen/callstack/CallStack2.java b/src/main/java/pr2/ausnahmen/callstack/CallStack2.java new file mode 100644 index 0000000..980ccc3 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/callstack/CallStack2.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.callstack; + +import java.io.FileInputStream; +import java.io.IOException; + +public class CallStack2 { + + public static void main(String[] args) throws IOException { + mergeData("/tmp/input", "/tmp/output"); + } + + public static void mergeData(String inFile, String outFile) + throws IOException { + writeData(outFile); + } + + public static void writeData(String outFile) throws IOException { + aggregate(5, 5); + } + + public static int aggregate(int a, int b) throws IOException { + FileInputStream f = new FileInputStream("/tmp/input"); + f.close(); + return 1; + } +} diff --git a/src/main/java/pr2/ausnahmen/callstack/CallStack3.java b/src/main/java/pr2/ausnahmen/callstack/CallStack3.java new file mode 100644 index 0000000..73981f3 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/callstack/CallStack3.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.callstack; + +import java.io.FileInputStream; +import java.io.IOException; + +public class CallStack3 { + + public static void main(String[] args) throws IOException { + mergeData(args[0], "/tmp/output"); + } + + public static void mergeData(String inFile, String outFile) + throws IOException { + writeData(inFile, outFile); + } + + public static void writeData(String inFile, String outFile) throws IOException { + aggregate(5, 5, inFile); + } + + public static int aggregate(int a, int b, String inFile) throws IOException { + FileInputStream f = new FileInputStream(inFile); + f.close(); + return 1; + } +} diff --git a/src/main/java/pr2/ausnahmen/callstack/HandleOrDeclare.java b/src/main/java/pr2/ausnahmen/callstack/HandleOrDeclare.java new file mode 100644 index 0000000..ef9888f --- /dev/null +++ b/src/main/java/pr2/ausnahmen/callstack/HandleOrDeclare.java @@ -0,0 +1,53 @@ + +package pr2.ausnahmen.callstack; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.SQLException; + +/** + * @author Thomas Smits + */ +public class HandleOrDeclare { + + public void openFile(String dateiName) + throws IOException, FileNotFoundException { + // Datei öffnen + } + + public void datenSchreiben(String dateiName, String sqlStatement) + throws FileNotFoundException, IOException, SQLException { + + openFile(dateiName); + // Mit Datenbank arbeiten + } + + public void dateiAnlegen(String dateiName) + throws FileNotFoundException, IOException { + + try { + datenSchreiben(dateiName, "SELECT * FROM x"); + } + catch (SQLException ex) { + // Datenbank Problem beheben ;-) + } + } + + public void userInterface() { + String dateiName = askUser(); + + try { + dateiAnlegen(dateiName); + } + catch (FileNotFoundException ex) { + // Benutzer erneut nach namen Fragen + } + catch (IOException ex) { + // Benutzer auf Problem hinweisen + } + } + + public String askUser() { + return null; + } +} diff --git a/src/main/java/pr2/ausnahmen/eigene/Client.java b/src/main/java/pr2/ausnahmen/eigene/Client.java new file mode 100644 index 0000000..03fa669 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/eigene/Client.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.eigene; + +public class Client { + + public void doIt() { + + try { + Server.connect("server1", 22); + } + catch (ServerException e1) { + System.err.println("Keine Verbindung zum server " + + e1.getHost() + " auf Port " + e1.getPort()); + + try { + Server.connect("server2", 22); + } + catch (ServerException e2) { + System.err.println("Ersatzserver geht auch nicht: " + + e2.getHost() + " auf Port " + e2.getPort()); + } + } + } +} diff --git a/src/main/java/pr2/ausnahmen/eigene/Server.java b/src/main/java/pr2/ausnahmen/eigene/Server.java new file mode 100644 index 0000000..715e646 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/eigene/Server.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.eigene; + +public class Server { + + public static void connect(String host, int port) throws ServerException { + + int result = connectInternal(host, port); + + if (result == -1) { + throw new ServerException("Cannot connect to " + host + ":" + port, host, port); + } + + // ... + + } + + private static native int connectInternal(String host, int port); +} diff --git a/src/main/java/pr2/ausnahmen/eigene/ServerException.java b/src/main/java/pr2/ausnahmen/eigene/ServerException.java new file mode 100644 index 0000000..6ef6ecb --- /dev/null +++ b/src/main/java/pr2/ausnahmen/eigene/ServerException.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.eigene; + +public class ServerException extends Exception { + + private int port; + private String host; + + public ServerException(String message, String host, int port) { + super(message); + this.host = host; + this.port = port; + } + + public int getPort() { + return port; + } + + public String getHost() { + return host; + } +} diff --git a/src/main/java/pr2/ausnahmen/eigene/SimpleExample2.java b/src/main/java/pr2/ausnahmen/eigene/SimpleExample2.java new file mode 100644 index 0000000..0e9435e --- /dev/null +++ b/src/main/java/pr2/ausnahmen/eigene/SimpleExample2.java @@ -0,0 +1,27 @@ +/* (c) 2022 Thomas Smits */ +package pr2.ausnahmen.eigene; + +import java.io.IOException; + +public class SimpleExample2 { + + public static void main(String[] args) { + new SimpleExample2().method(); + } + + public void method() { + try { + System.out.println("A"); + thrower(); + System.out.println("B"); + } + catch (IOException e) { + System.out.println("Fehler: " + e.getMessage()); + } + } + + + private void thrower() throws IOException { + throw new IOException("Festplatte explodiert"); + } +} diff --git a/src/main/java/pr2/ausnahmen/eigene/SimpleExamples.java b/src/main/java/pr2/ausnahmen/eigene/SimpleExamples.java new file mode 100644 index 0000000..07a7f36 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/eigene/SimpleExamples.java @@ -0,0 +1,25 @@ +/* (c) 2022 Thomas Smits */ +package pr2.ausnahmen.eigene; + +import java.io.IOException; + +public class SimpleExamples { + + public static void main(String[] args) { + + boolean b = true; + + try { + System.out.println("A"); + + if (b) { + throw new IOException("Festplatte abgebrannt"); + } + + System.out.println("B"); + } + catch (IOException e) { + System.out.println("Fehler: " + e.getMessage()); + } + } +} diff --git a/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample.java b/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample.java new file mode 100644 index 0000000..c957faf --- /dev/null +++ b/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample.java @@ -0,0 +1,46 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.finall; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class TryCatchFinallyExample { + + public static void main(String[] args) { + + Connection connection = getConnection(); + + try { + ResultSet rs = readData(connection); + + while (rs.next()) { + // Daten bearbeiten + } + + return; + } + catch (SQLException ex) { + // Datenbank Problem behandeln + } + finally { + releaseConnection(connection); + } + + } + + private static ResultSet readData(Connection connection) throws SQLException { + return null; + } + + private static Connection getConnection() { + return null; + } + + private static void releaseConnection(Connection connection) { + + } + +} diff --git a/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample2.java b/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample2.java new file mode 100644 index 0000000..f7f9706 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/finall/TryCatchFinallyExample2.java @@ -0,0 +1,51 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.finall; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class TryCatchFinallyExample2 { + + public static void main(String[] args) { + + Connection connection = getConnection(); + + try { + ResultSet rs = readData(connection); + + while (rs.next()) { + // Daten bearbeiten + } + + return; + } + catch (SQLException ex) { + // Datenbank Problem behandeln + } + finally { + try { + releaseConnection(connection); + } + catch (SQLException ex) { + // Hier kann man nichts mehr machen + } + } + + } + + private static ResultSet readData(Connection connection) throws SQLException { + return null; + } + + private static Connection getConnection() { + return null; + } + + private static void releaseConnection(Connection connection) throws SQLException { + + } + +} diff --git a/src/main/java/pr2/ausnahmen/junit/BrokenTest.java b/src/main/java/pr2/ausnahmen/junit/BrokenTest.java new file mode 100644 index 0000000..0ded0a7 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/junit/BrokenTest.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.junit; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Assertions; + +public class BrokenTest { + + @Test + public void testKaputt() { + String s1 = new String("Hallo"); + assertEquals("Hallo", s1); + + Assertions.assertThrows(NullPointerException.class, () -> { + new String((String) null); + new String((char[]) null); + }); + + String s3 = new String("World"); + assertEquals("world", s3); + } +} diff --git a/src/main/java/pr2/ausnahmen/junit/TestMitExpected.java b/src/main/java/pr2/ausnahmen/junit/TestMitExpected.java new file mode 100644 index 0000000..9514c12 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/junit/TestMitExpected.java @@ -0,0 +1,18 @@ +package pr2.ausnahmen.junit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +/* + * (c) 2009 Thomas Smits + */ + +public class TestMitExpected { + + @Test + public void testAufAusnahme() { + Assertions.assertThrows(NullPointerException.class, () -> { + new String((String) null); + }); + } +} diff --git a/src/main/java/pr2/ausnahmen/junit/TestMitTryCatch.java b/src/main/java/pr2/ausnahmen/junit/TestMitTryCatch.java new file mode 100644 index 0000000..f5aa47a --- /dev/null +++ b/src/main/java/pr2/ausnahmen/junit/TestMitTryCatch.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.junit; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.Test; + +public class TestMitTryCatch { + + @Test + public void testAufAusnahme() { + + try { + new String((String) null); + fail(); + + } + catch (NullPointerException ex) { + assertTrue(true); + } + } +} diff --git a/src/main/java/pr2/ausnahmen/quiz/Finally.java b/src/main/java/pr2/ausnahmen/quiz/Finally.java new file mode 100644 index 0000000..1e8a962 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/quiz/Finally.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.quiz; + +public class Finally { + + @SuppressWarnings("finally") + public static boolean f() { + try { + return true; + } + finally { + return false; + } + } + + public static void main(String[] args) { + System.out.println(f()); + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/BlossNicht.java b/src/main/java/pr2/ausnahmen/trycatch/BlossNicht.java new file mode 100644 index 0000000..82a9abc --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/BlossNicht.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch; + +import java.io.FileInputStream; +import java.io.IOException; + +/** + * + * + * @author Thomas Smits + */ +public class BlossNicht { + + @SuppressWarnings("null") + public static void main(String[] args) throws IOException { + + try { + FileInputStream fis = new FileInputStream("/tmp/gibtsnicht"); + String s = null; + s = s.toUpperCase(); + fis.close(); + } + catch (Exception e) { + // Fehlerbehandlung + } + + FileInputStream fis = null; + + try { + fis = new FileInputStream("/tmp/gibtsnicht"); + } + catch (Exception e) { + e.printStackTrace(); + } + + fis.read(); + + + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/ExceptionExample.java b/src/main/java/pr2/ausnahmen/trycatch/ExceptionExample.java new file mode 100644 index 0000000..99f835d --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/ExceptionExample.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch; + +public class ExceptionExample { + + public static void main(String[] args) { + + String[] namen = { "Franz", "Hans", "Alfons" }; + + for (int i = 0; i < 4; i++) { + System.out.print(namen[i] + ", "); + } + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/FinallyMitSeiteneffekt.java b/src/main/java/pr2/ausnahmen/trycatch/FinallyMitSeiteneffekt.java new file mode 100644 index 0000000..88c958f --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/FinallyMitSeiteneffekt.java @@ -0,0 +1,23 @@ +/* (c) 2022 Thomas Smits */ +package pr2.ausnahmen.trycatch; + +public class FinallyMitSeiteneffekt { + + private static int changeMe = 41; + + public static void main(String[] args) { + System.out.println(m()); + System.out.println(changeMe); + } + + @SuppressWarnings("finally") + static int m() { + try { + return changeMe++; + } + finally { + return changeMe++; + } + } + +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/GanzBoese.java b/src/main/java/pr2/ausnahmen/trycatch/GanzBoese.java new file mode 100644 index 0000000..1ef4ad1 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/GanzBoese.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch; + +public class GanzBoese { + + public static void printArray(int[] array) { + + try { + for (int i = 0;;i++) { + System.out.println(array[i]); + } + } + catch (ArrayIndexOutOfBoundsException ex) { + } + } + + public static void main(String[] args) { + printArray(new int[] { 1,2,3,4,5,6,7,8,9,10 }); + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/Netzwerk.java b/src/main/java/pr2/ausnahmen/trycatch/Netzwerk.java new file mode 100644 index 0000000..6b6beb4 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/Netzwerk.java @@ -0,0 +1,36 @@ +/* (c) 2022 Thomas Smits */ +package pr2.ausnahmen.trycatch; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketTimeoutException; + +/** + * Beispiel für Netzwerk-Verbindung mit Java-Fehlerbehandlung. + * + * @author Thomas Smits + */ +public class Netzwerk { + + @SuppressWarnings("resource") + public static void main(String[] args) { + + try { + ServerSocket servSocket = new ServerSocket(); + servSocket.bind(new InetSocketAddress(8080)); + + for (;;) { + Socket clntSocket = servSocket.accept(); + // ... + } + } + catch (SocketTimeoutException e) { + // Fehlerbehandlung + } + catch (IOException e) { + // Fehlerbehandlung + } + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/TryCatchExample.java b/src/main/java/pr2/ausnahmen/trycatch/TryCatchExample.java new file mode 100644 index 0000000..f7ddf0a --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/TryCatchExample.java @@ -0,0 +1,40 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class TryCatchExample { + + public static void main(String[] args) { + + Connection connection = getConnection(); + + try { + ResultSet rs = readData(connection); + + while (rs.next()) { + // Daten bearbeiten + } + } + catch (SQLException ex) { + // Datenbank Problem behandeln + String sqlStatus = ex.getSQLState(); + // ... + ex.printStackTrace(); + } + + } + + private static ResultSet readData(Connection connection) throws SQLException { + return null; + } + + private static Connection getConnection() { + return null; + } + +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/TryCatchExampleZwei.java b/src/main/java/pr2/ausnahmen/trycatch/TryCatchExampleZwei.java new file mode 100644 index 0000000..9d9a1ef --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/TryCatchExampleZwei.java @@ -0,0 +1,52 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class TryCatchExampleZwei { + + public static void main(String[] args) { + + Connection connection = getConnection(); + + try { + ResultSet rs = readData(connection); + + while (rs.next()) { + storeToFile(rs.getString(0), rs); + } + } + catch (SQLException ex) { + // Datenbank-Problem + ex.printStackTrace(); + } + catch (FileNotFoundException ex) { + // Datei nicht gefunden + System.out.println(ex.getMessage()); + } + catch (IOException ex) { + // Allgemeiner IO-Fehler + System.out.println(ex.getMessage()); + } + + } + + private static ResultSet readData(Connection connection) throws SQLException { + return null; + } + + private static Connection getConnection() { + return null; + } + + public static void storeToFile(String fileName, ResultSet rs) throws FileNotFoundException, IOException {} + + + +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/polymorphie/A.java b/src/main/java/pr2/ausnahmen/trycatch/polymorphie/A.java new file mode 100644 index 0000000..2362e0c --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/polymorphie/A.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch.polymorphie; + +import java.io.IOException; + +public class A { + + public void method() throws IOException { + } +} diff --git a/src/main/java/pr2/ausnahmen/trycatch/polymorphie/B.java b/src/main/java/pr2/ausnahmen/trycatch/polymorphie/B.java new file mode 100644 index 0000000..e0affa3 --- /dev/null +++ b/src/main/java/pr2/ausnahmen/trycatch/polymorphie/B.java @@ -0,0 +1,31 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.ausnahmen.trycatch.polymorphie; + +import java.io.FileNotFoundException; +import java.io.IOException; + +class B extends A { + + public void method() throws IOException, FileNotFoundException { + } +} + +class C extends A { + + public void method() throws FileNotFoundException { + } +} + +class D extends A { + + public void method() { + } +} + +//class E extends A { +// +// public void method() throws SQLException { +// } +//} diff --git a/src/main/java/pr2/collections/StackDemo.java b/src/main/java/pr2/collections/StackDemo.java new file mode 100644 index 0000000..bd2fe69 --- /dev/null +++ b/src/main/java/pr2/collections/StackDemo.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections; + +import java.util.Stack; + +public class StackDemo { + + public static void main(String[] args) { + Stack stack = new Stack<>(); + } +} diff --git a/src/main/java/pr2/collections/interfaces/collection/CollectionDemo.java b/src/main/java/pr2/collections/interfaces/collection/CollectionDemo.java new file mode 100644 index 0000000..34e836c --- /dev/null +++ b/src/main/java/pr2/collections/interfaces/collection/CollectionDemo.java @@ -0,0 +1,54 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.interfaces.collection; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +public class CollectionDemo { + + public static void main(String[] args) { + + Collection col1 = new ArrayList<>(); + + col1.add("Element 1"); + col1.add("Element 2"); + col1.add("Element 3"); + col1.add("Element 4"); + + System.out.println(col1.size()); // --> 4 + System.out.println(col1.contains("Element 3")); // --> true + + col1.remove("Element 3"); + + System.out.println(col1.size()); // --> 3 + + Collection col2 = new ArrayList<>(); + + col2.addAll(col1); + + System.out.println(col2); // --> [Element 1, Element 2, Element 4] + + col1.clear(); + System.out.println(col1.size()); // --> 0 + + col1.add("Element 2"); + col2.removeAll(col1); + + System.out.println(col2); // --> [Element 1, Element 4] + + String[] str1 = new String[col1.size()]; + str1 = col1.toArray(str1); + + String[] str2 = col2.toArray(new String[0]); + + System.out.println(Arrays.asList(str1)); // --> [Element 2] + System.out.println(Arrays.asList(str2)); // --> [Element 1, Element 4] + + + + + } +} diff --git a/src/main/java/pr2/collections/interfaces/collection/CollectionsDemo.java b/src/main/java/pr2/collections/interfaces/collection/CollectionsDemo.java new file mode 100644 index 0000000..7211a6d --- /dev/null +++ b/src/main/java/pr2/collections/interfaces/collection/CollectionsDemo.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.interfaces.collection; + +public class CollectionsDemo { + + public static void main(String[] args) { + + } +} diff --git a/src/main/java/pr2/collections/interfaces/list/ListDemo.java b/src/main/java/pr2/collections/interfaces/list/ListDemo.java new file mode 100644 index 0000000..b71cb43 --- /dev/null +++ b/src/main/java/pr2/collections/interfaces/list/ListDemo.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.interfaces.list; + +import java.util.ArrayList; +import java.util.List; + +public class ListDemo { + + public static void main(String[] args) { + + List list = new ArrayList<>(); + + list.add("Element A"); + list.add("Element B"); + list.add("Element C"); + list.add("Element A"); + list.add("Element B"); + + for (String element : list) { + System.out.println(element); + } + } +} diff --git a/src/main/java/pr2/collections/interfaces/list/ListDemo2.java b/src/main/java/pr2/collections/interfaces/list/ListDemo2.java new file mode 100644 index 0000000..65d05b9 --- /dev/null +++ b/src/main/java/pr2/collections/interfaces/list/ListDemo2.java @@ -0,0 +1,41 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.interfaces.list; + +import java.util.ArrayList; +import java.util.List; + +public class ListDemo2 { + + public static void main(String[] args) { + + List list1 = new ArrayList<>(); + + list1.add("Element 1"); + list1.add("Element 2"); + list1.add("Element 3"); + list1.add("Element 4"); + + System.out.println(list1.get(3)); // --> Element 4 + list1.remove(0); + System.out.println(list1); // --> [Element 2, Element 3, Element 4] + + list1.add(2, "Eigefuegt"); + System.out.println(list1); // --> [Element 2, Element 3, Eigefuegt, Element 4] + + List list2 = list1.subList(1, 3); + System.out.println(list2); // --> [Element 3, Eigefuegt] + + list2.set(1, "Ausgetauscht"); + System.out.println(list2); // --> [Element 3, Ausgetauscht] + + list2.add(2, "Eingefuegt"); + System.out.println(list2); // --> [Element 3, Ausgetauscht, Eingefuegt] + + System.out.println(list2.indexOf("Element 3")); // --> 0 + + + + } +} diff --git a/src/main/java/pr2/collections/interfaces/set/SetDemo.java b/src/main/java/pr2/collections/interfaces/set/SetDemo.java new file mode 100644 index 0000000..042c57f --- /dev/null +++ b/src/main/java/pr2/collections/interfaces/set/SetDemo.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.interfaces.set; + +import java.util.HashSet; +import java.util.Set; + +public class SetDemo { + + public static void main(String[] args) { + + Set set = new HashSet<>(); + + set.add("Element A"); + set.add("Element B"); + set.add("Element C"); + set.add("Element A"); + set.add("Element B"); + + for (String element : set) { + System.out.println(element); + } + } +} diff --git a/src/main/java/pr2/collections/iterable/IterableFor.java b/src/main/java/pr2/collections/iterable/IterableFor.java new file mode 100644 index 0000000..60c2ea6 --- /dev/null +++ b/src/main/java/pr2/collections/iterable/IterableFor.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.iterable; + +import java.util.ArrayList; + +public class IterableFor { + + public static void main(String[] args) { + + ArrayList list = new ArrayList<>(); + list.add("Eintrag 1"); + list.add("Eintrag 2"); + list.add("Eintrag 3"); + + for (String wert : list) { + System.out.println(wert); + } + } +} diff --git a/src/main/java/pr2/collections/iterable/IterableWhile.java b/src/main/java/pr2/collections/iterable/IterableWhile.java new file mode 100644 index 0000000..3d16a0a --- /dev/null +++ b/src/main/java/pr2/collections/iterable/IterableWhile.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.iterable; + +import java.util.ArrayList; +import java.util.Iterator; + +public class IterableWhile { + + public static void main(String[] args) { + + ArrayList list = new ArrayList<>(); + list.add("Eintrag 1"); + list.add("Eintrag 2"); + list.add("Eintrag 3"); + + Iterable iterable = list; + + Iterator iterator = iterable.iterator(); + + while (iterator.hasNext()) { + String wert = iterator.next(); + System.out.println(wert); + } + } +} diff --git a/src/main/java/pr2/collections/iterable/ListIteratorDemo.java b/src/main/java/pr2/collections/iterable/ListIteratorDemo.java new file mode 100644 index 0000000..32c1b2b --- /dev/null +++ b/src/main/java/pr2/collections/iterable/ListIteratorDemo.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.iterable; + +import java.util.ArrayList; +import java.util.ListIterator; + +public class ListIteratorDemo { + + public static void main(String[] args) { + + ArrayList list = new ArrayList<>(); + list.add("Eintrag 1"); + list.add("Eintrag 2"); + list.add("Eintrag 3"); + + ListIterator iterator = list.listIterator(); + + iterator.next(); + iterator.next(); + iterator.previous(); + iterator.set("Neuer Eintrag 2"); + iterator.next(); + iterator.next(); + iterator.add("Neuer Eintrag 4"); + + iterator = list.listIterator(); + + for (String wert : list) { + System.out.println(wert); + } + } +} diff --git a/src/main/java/pr2/collections/iterable/eigenes/IterableInnereKlasse.java b/src/main/java/pr2/collections/iterable/eigenes/IterableInnereKlasse.java new file mode 100644 index 0000000..b4265f1 --- /dev/null +++ b/src/main/java/pr2/collections/iterable/eigenes/IterableInnereKlasse.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.iterable.eigenes; + +import java.util.Iterator; + +public class IterableInnereKlasse implements Iterable { + + private String[] stringListe; + + public IterableInnereKlasse(String[] stringListe) { + this.stringListe = stringListe; + } + + public Iterator iterator() { + + return new Iterator() { + private int pos = 0; + + public boolean hasNext() { + return (pos < stringListe.length); + } + + public String next() { + return stringListe[pos++]; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + public static void main(String[] args) { + String[] strings = new String[] { "A", "B", "C", "D" }; + IterableInnereKlasse liste = new IterableInnereKlasse(strings); + for (String string : liste) { + System.out.println(string); + } + } +} diff --git a/src/main/java/pr2/collections/iterable/eigenes/IterableMitKlasse.java b/src/main/java/pr2/collections/iterable/eigenes/IterableMitKlasse.java new file mode 100644 index 0000000..694c40b --- /dev/null +++ b/src/main/java/pr2/collections/iterable/eigenes/IterableMitKlasse.java @@ -0,0 +1,50 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.iterable.eigenes; + +import java.util.Iterator; + +public class IterableMitKlasse implements Iterable { + + private String[] stringListe; + + public IterableMitKlasse(String[] stringListe) { + this.stringListe = stringListe; + } + + public Iterator iterator() { + return new MyIterator(stringListe); + } + + public static void main(String[] args) { + String[] strings = new String[] { "A", "B", "C", "D" }; + IterableMitKlasse liste = new IterableMitKlasse(strings); + for (String string : liste) { + System.out.println(string); + } + } +} + +class MyIterator implements Iterator { + + private int pos = 0; + + private String[] stringListe; + + public MyIterator(String[] stringListe) { + this.stringListe = stringListe; + } + + public boolean hasNext() { + return (pos < stringListe.length); + } + + public String next() { + return stringListe[pos++]; + } + + public void remove() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/pr2/collections/iterable/eigenes/IterableSelbst.java b/src/main/java/pr2/collections/iterable/eigenes/IterableSelbst.java new file mode 100644 index 0000000..af3b817 --- /dev/null +++ b/src/main/java/pr2/collections/iterable/eigenes/IterableSelbst.java @@ -0,0 +1,41 @@ +/* + * + */ +package pr2.collections.iterable.eigenes; + +import java.util.Iterator; + +public class IterableSelbst implements Iterable, Iterator { + + private String[] stringListe; + + public IterableSelbst(String[] stringListe) { + this.stringListe = stringListe; + } + + public Iterator iterator() { + return this; + } + + public static void main(String[] args) { + String[] strings = new String[] { "A", "B", "C", "D" }; + IterableSelbst liste = new IterableSelbst(strings); + for (String string : liste) { + System.out.println(string); + } + } + + private int pos = 0; + + public boolean hasNext() { + return (pos < stringListe.length); + } + + public String next() { + return stringListe[pos++]; + } + + public void remove() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/pr2/collections/iterable/eigenes/IterableSelbstKorrekt.java b/src/main/java/pr2/collections/iterable/eigenes/IterableSelbstKorrekt.java new file mode 100644 index 0000000..1fc99eb --- /dev/null +++ b/src/main/java/pr2/collections/iterable/eigenes/IterableSelbstKorrekt.java @@ -0,0 +1,45 @@ +/* + * + */ +package pr2.collections.iterable.eigenes; + +import java.util.Iterator; + +public class IterableSelbstKorrekt implements Iterable, Iterator { + + private String[] stringListe; + + public IterableSelbstKorrekt(int size) { + stringListe = new String[size]; + } + + private IterableSelbstKorrekt(String[] stringListe) { + this.stringListe = stringListe; + } + + public Iterator iterator() { + return new IterableSelbstKorrekt(stringListe); + } + + public static void main(String[] args) { + String[] strings = new String[] { "A", "B", "C", "D" }; + IterableSelbstKorrekt liste = new IterableSelbstKorrekt(strings); + for (String string : liste) { + System.out.println(string); + } + } + + private int pos = 0; + + public boolean hasNext() { + return (pos < stringListe.length); + } + + public String next() { + return stringListe[pos++]; + } + + public void remove() { + throw new UnsupportedOperationException(); + } +} diff --git a/src/main/java/pr2/collections/maps/MapBeispiel.java b/src/main/java/pr2/collections/maps/MapBeispiel.java new file mode 100644 index 0000000..f082451 --- /dev/null +++ b/src/main/java/pr2/collections/maps/MapBeispiel.java @@ -0,0 +1,41 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.maps; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class MapBeispiel { + + public static void main(String[] args) { + + Map map = new HashMap<>(); + + map.put("go", "gehen"); + map.put("walk", "gehen"); + map.put("sleep", "schlafen"); + map.put("learn", "lernen"); + + System.out.println(map); // --> {learn=lernen, sleep=schlafen, walk=gehen, go=gehen} + + System.out.println(map.get("go")); // --> gehen + System.out.println(map.get("walk")); // --> gehen + System.out.println(map.get("learn")); // --> lernen + + System.out.println(map.keySet()); // --> [learn, sleep, walk, go] + System.out.println(map.values()); // --> [lernen, schlafen, gehen, gehen] + + Set> entrySet = map.entrySet(); + + for (Map.Entry entry : entrySet) { + String key = entry.getKey(); + String value = entry.getValue(); + + System.out.printf("%s engl. fuer %s%n", key, value); + } + + + } +} \ No newline at end of file diff --git a/src/main/java/pr2/collections/maps/MapBeispiel2.java b/src/main/java/pr2/collections/maps/MapBeispiel2.java new file mode 100644 index 0000000..1ac7912 --- /dev/null +++ b/src/main/java/pr2/collections/maps/MapBeispiel2.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.maps; + +import java.util.HashMap; +import java.util.Map; + +public class MapBeispiel2 { + + public static void main(String[] args) { + + Map map = new HashMap<>(); + + Mitarbeiter meier = new Mitarbeiter("Meier", 1200.00); + Mitarbeiter mueller = new Mitarbeiter("Mueller", 2400.00); + Mitarbeiter schulze = new Mitarbeiter("Schulze", 1200.00); + + map.put(new Personalnummer("4711"), meier); + map.put(new Personalnummer("4242"), mueller); + map.put(new Personalnummer("0007"), schulze); + + System.out.println(map.containsKey(new Personalnummer("0007"))); // --> true + System.out.println(map.containsValue(meier)); // --> true + } +} diff --git a/src/main/java/pr2/collections/maps/MapBeispiel3.java b/src/main/java/pr2/collections/maps/MapBeispiel3.java new file mode 100644 index 0000000..a4d9cc9 --- /dev/null +++ b/src/main/java/pr2/collections/maps/MapBeispiel3.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.maps; + +import java.util.HashMap; +import java.util.Map; + +public class MapBeispiel3 { + + public static void main(String[] args) { + + Map map = new HashMap<>(); + + map.put("gehen", "go"); + map.put("gehen", "walk"); + map.put("schlafen", "sleep"); + map.put("lernen", "learn"); + + System.out.println(map); + + } +} \ No newline at end of file diff --git a/src/main/java/pr2/collections/maps/Mitarbeiter.java b/src/main/java/pr2/collections/maps/Mitarbeiter.java new file mode 100644 index 0000000..beec55d --- /dev/null +++ b/src/main/java/pr2/collections/maps/Mitarbeiter.java @@ -0,0 +1,68 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.maps; + +import java.util.Date; + +public class Mitarbeiter { + + public String name; + public double gehalt; + public Date geburtsdatum; + + public Mitarbeiter(String name, double gehalt, Date geburtsdatum) { + super(); + this.name = name; + this.gehalt = gehalt; + this.geburtsdatum = geburtsdatum; + } + + public Mitarbeiter(String name, double gehalt) { + super(); + this.name = name; + this.gehalt = gehalt; + } + + public boolean equals(Object o) { + + if (o == null) { + return false; + } + else if (o == this) { + return true; + } + else if (o instanceof Mitarbeiter) { + Mitarbeiter mi = (Mitarbeiter) o; + + return mi.name.equals(name) + && mi.gehalt == gehalt + && mi.geburtsdatum.equals(geburtsdatum); + } + else { + return false; + } + } + + public int hashCode() { + return name.hashCode() + ^ (int) Double.doubleToLongBits(gehalt) + ^ geburtsdatum.hashCode(); + } + + public void doIt(Mitarbeiter m) { + System.out.println(m + " hat ein Gehalt von " + gehalt); + System.out.println(m.toString() + " hat ein Gehalt von " + Double.toString(gehalt)); + } + + public String toString() { + return "Mitarbeiter [name: " + name + ", " + + "gehalt: " + gehalt + ", " + + "geburtsdatum " + geburtsdatum + "]"; + } + + public static void main(String[] args) { + Mitarbeiter m = new Mitarbeiter("Franz Meier", 5000.00, new Date(new Date().getTime() - 1000L*1000*1000*1000)); + m.doIt(m); + } +} diff --git a/src/main/java/pr2/collections/maps/Personalnummer.java b/src/main/java/pr2/collections/maps/Personalnummer.java new file mode 100644 index 0000000..acfabfd --- /dev/null +++ b/src/main/java/pr2/collections/maps/Personalnummer.java @@ -0,0 +1,45 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.maps; + +public class Personalnummer { + private String nummer; + + @Override + public String toString() { + return "Personalnummer [" + nummer + "]"; + } + + public Personalnummer(String nummer) { + super(); + this.nummer = nummer; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((nummer == null) ? 0 : nummer.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Personalnummer other = (Personalnummer) obj; + if (nummer == null) { + if (other.nummer != null) + return false; + } else if (!nummer.equals(other.nummer)) + return false; + return true; + } + + +} diff --git a/src/main/java/pr2/collections/sorting/ListSortierung.java b/src/main/java/pr2/collections/sorting/ListSortierung.java new file mode 100644 index 0000000..cbe0acf --- /dev/null +++ b/src/main/java/pr2/collections/sorting/ListSortierung.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +class ReverseComparator implements Comparator { + public int compare(String o1, String o2) { + return o1.compareTo(o2) * -1; + } +} + +public class ListSortierung { + + public static void main(String[] args) { + + List list = new ArrayList<>(); + + list.add("Seeler"); + list.add("Mueller"); + list.add("Zander"); + list.add("Beckenbauer"); + list.add("Schumacher"); + + Collections.sort(list); + System.out.println(list); + + Collections.sort(list, new ReverseComparator()); + System.out.println(list); + + } +} diff --git a/src/main/java/pr2/collections/sorting/SortedSetDemo.java b/src/main/java/pr2/collections/sorting/SortedSetDemo.java new file mode 100644 index 0000000..e3a3329 --- /dev/null +++ b/src/main/java/pr2/collections/sorting/SortedSetDemo.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting; + +import java.util.SortedSet; +import java.util.TreeSet; + +public class SortedSetDemo { + + public static void main(String[] args) { + + SortedSet set = new TreeSet<>(); + + set.add("Seeler"); + set.add("Mueller"); + set.add("Zander"); + set.add("Beckenbauer"); + set.add("Schumacher"); + + System.out.printf("Elemente: %s%n", set); + System.out.printf("Erstes Element: %s%n", set.first()); + System.out.printf("Letztes Element: %s%n", set.last()); + + + } +} diff --git a/src/main/java/pr2/collections/sorting/comparable/Student.java b/src/main/java/pr2/collections/sorting/comparable/Student.java new file mode 100644 index 0000000..6b1b342 --- /dev/null +++ b/src/main/java/pr2/collections/sorting/comparable/Student.java @@ -0,0 +1,31 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting.comparable; + +public class Student implements Comparable { + + private int matrikelNummer; + private String name; + + public Student(int matrikelNummer, String name) { + this.matrikelNummer = matrikelNummer; + this.name = name; + } + + public int compareTo(Student o) { + if (o.matrikelNummer == matrikelNummer) { + return 0; + } + else if (o.matrikelNummer > matrikelNummer) { + return -1; + } + else { + return 1; + } + } + + public String toString() { + return matrikelNummer + " - " + name; + } +} diff --git a/src/main/java/pr2/collections/sorting/comparable/StudentDemo.java b/src/main/java/pr2/collections/sorting/comparable/StudentDemo.java new file mode 100644 index 0000000..8418cb6 --- /dev/null +++ b/src/main/java/pr2/collections/sorting/comparable/StudentDemo.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting.comparable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class StudentDemo { + + public static void main(String[] args) { + { + Set studenten = new TreeSet<>(); + studenten.add(new Student(4411, "Franz Müller")); + studenten.add(new Student(5711, "Albert Meier")); + studenten.add(new Student(4711, "Xaver Meier")); + + for (Student student : studenten) { + System.out.println(student); + } + } + + { + List studenten = new ArrayList<>(); + + studenten.add(new Student(4411, "Franz Müller")); + studenten.add(new Student(5711, "Albert Meier")); + studenten.add(new Student(4711, "Xaver Meier")); + + Collections.sort(studenten); + + for (Student student : studenten) { + System.out.println(student); + } + } + } +} diff --git a/src/main/java/pr2/collections/sorting/comparator/Student.java b/src/main/java/pr2/collections/sorting/comparator/Student.java new file mode 100644 index 0000000..9f6be1b --- /dev/null +++ b/src/main/java/pr2/collections/sorting/comparator/Student.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting.comparator; + +public class Student { + + int matrikelNummer; + String name; + + public Student(int matrikelNummer, String name) { + this.matrikelNummer = matrikelNummer; + this.name = name; + } + + public String toString() { + return matrikelNummer + " - " + name; + } +} diff --git a/src/main/java/pr2/collections/sorting/comparator/StudentComparator.java b/src/main/java/pr2/collections/sorting/comparator/StudentComparator.java new file mode 100644 index 0000000..43e5010 --- /dev/null +++ b/src/main/java/pr2/collections/sorting/comparator/StudentComparator.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting.comparator; + +import java.util.Comparator; + +public class StudentComparator implements Comparator { + + public int compare(Student o1, Student o2) { + if (o2.matrikelNummer == o1.matrikelNummer) { + return 0; + } + else if (o2.matrikelNummer > o1.matrikelNummer) { + return -1; + } + else { + return 1; + } + } +} diff --git a/src/main/java/pr2/collections/sorting/comparator/StudentDemo.java b/src/main/java/pr2/collections/sorting/comparator/StudentDemo.java new file mode 100644 index 0000000..8a6cf63 --- /dev/null +++ b/src/main/java/pr2/collections/sorting/comparator/StudentDemo.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.sorting.comparator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + + +public class StudentDemo { + + public static void main(String[] args) { + + { + Set studenten = new TreeSet(new StudentComparator()); + + studenten.add(new Student(4411, "Franz Müller")); + studenten.add(new Student(5711, "Albert Meier")); + studenten.add(new Student(4711, "Xaver Meier")); + + for (Student student : studenten) { + System.out.println(student); + } + } + + { + List studenten = new ArrayList<>(); + + studenten.add(new Student(4411, "Franz Müller")); + studenten.add(new Student(5711, "Albert Meier")); + studenten.add(new Student(4711, "Xaver Meier")); + + Collections.sort(studenten, new StudentComparator()); + + for (Student student : studenten) { + System.out.println(student); + } + } + } +} diff --git a/src/main/java/pr2/collections/threads/ConcurrentModificationException.java b/src/main/java/pr2/collections/threads/ConcurrentModificationException.java new file mode 100644 index 0000000..9b7f1de --- /dev/null +++ b/src/main/java/pr2/collections/threads/ConcurrentModificationException.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.collections.threads; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; + +public class ConcurrentModificationException { + + public static void main(String[] args) { + + List list = new ArrayList<>(); + Collections.addAll(list, "A", "B", "C", "D", "E", "X", "F"); + + for (String element : list) { + if ("X".equals(element)) { + list.remove(element); + } + } + + ListIterator it = list.listIterator(); + + while (it.hasNext()) { + String element = it.next(); + + if ("X".equals(element)) { + it.remove(); + } + } + } +} diff --git a/src/main/java/pr2/ds/avl/AVLTree.java b/src/main/java/pr2/ds/avl/AVLTree.java new file mode 100644 index 0000000..64f128d --- /dev/null +++ b/src/main/java/pr2/ds/avl/AVLTree.java @@ -0,0 +1,118 @@ +package pr2.ds.avl; + +class Node { + int key, height; + Node left, right; + + public Node(int key) { + this.key = key; + height = 1; + } +} + +public class AVLTree { + Node root; + + int height(Node N) { + if (N == null) + return 0; + + return N.height; + } + + int max(int a, int b) { + return (a > b) ? a : b; + } + + Node rightRotate(Node y) { + Node x = y.left; + Node T2 = x.right; + + x.right = y; + y.left = T2; + + y.height = max(height(y.left), height(y.right)) + 1; + x.height = max(height(x.left), height(x.right)) + 1; + + return x; + } + + Node leftRotate(Node x) { + Node y = x.right; + Node T2 = y.left; + + y.left = x; + x.right = T2; + + x.height = max(height(x.left), height(x.right)) + 1; + y.height = max(height(y.left), height(y.right)) + 1; + + return y; + } + + int getBalance(Node N) { + if (N == null) + return 0; + + return height(N.left) - height(N.right); + } + + Node insert(Node node, int key) { + + if (node == null) + return (new Node(key)); + + if (key < node.key) + node.left = insert(node.left, key); + else if (key > node.key) + node.right = insert(node.right, key); + else + return node; + + node.height = 1 + max(height(node.left), + height(node.right)); + + int balance = getBalance(node); + + if (balance > 1 && key < node.left.key) + return rightRotate(node); + + if (balance < -1 && key > node.right.key) + return leftRotate(node); + + if (balance > 1 && key > node.left.key) { + node.left = leftRotate(node.left); + return rightRotate(node); + } + + if (balance < -1 && key < node.right.key) { + node.right = rightRotate(node.right); + return leftRotate(node); + } + + return node; + } + + void preOrder(Node node) { + if (node != null) { + System.out.print(node.key + " "); + preOrder(node.left); + preOrder(node.right); + } + } + public static void main(String[] args) { + AVLTree tree = new AVLTree(); + + /* Constructing tree given in the above figure */ + tree.root = tree.insert(tree.root, 10); + tree.root = tree.insert(tree.root, 20); + tree.root = tree.insert(tree.root, 30); + tree.root = tree.insert(tree.root, 40); + tree.root = tree.insert(tree.root, 50); + tree.root = tree.insert(tree.root, 25); + + System.out.println("Preorder traversal" + + " of constructed tree is : "); + tree.preOrder(tree.root); + } +} diff --git a/src/main/java/pr2/ds/bt/BinaryTree.java b/src/main/java/pr2/ds/bt/BinaryTree.java new file mode 100644 index 0000000..c882614 --- /dev/null +++ b/src/main/java/pr2/ds/bt/BinaryTree.java @@ -0,0 +1,66 @@ +package pr2.ds.bt; + +class Node { + int data; + Node left; + Node right; + + public Node(int item) { + data = item; + left = right = null; + } +} + +public class BinaryTree { + // Wurzel des Baums + Node root; + + public BinaryTree() { + root = null; + } + + public void insert(int key) { + root = insertRec(root, key); + } + + private Node insertRec(Node root, int key) { + + if (root == null) { + root = new Node(key); + return root; + } + + if (key < root.data) + root.left = insertRec(root.left, key); + else if (key > root.data) + root.right = insertRec(root.right, key); + + return root; + } + + public void inorder() { + inorderRec(root); + } + + void inorderRec(Node root) { + if (root != null) { + inorderRec(root.left); + System.out.println(root.data); + inorderRec(root.right); + } + } + + public static void main(String[] args) { + BinaryTree tree = new BinaryTree(); + + tree.insert(50); + tree.insert(30); + tree.insert(20); + tree.insert(40); + tree.insert(70); + tree.insert(60); + tree.insert(80); + + tree.inorder(); + } +} diff --git a/src/main/java/pr2/ds/bt/SimpleTree.java b/src/main/java/pr2/ds/bt/SimpleTree.java new file mode 100644 index 0000000..f7fd5a8 --- /dev/null +++ b/src/main/java/pr2/ds/bt/SimpleTree.java @@ -0,0 +1,94 @@ +package pr2.ds.bt; + +import java.util.LinkedList; +import java.util.function.Consumer; + +public class SimpleTree { + private static class Node { + private Node left; + private Node right; + private String data; + + public Node(String data, Node left, Node right) { + this.left = left; + this.right = right; + this.data = data; + } + + public Node(String data) { + this.left = null; + this.right = null; + this.data = data; + } + } + + private Node root; + + public SimpleTree(Node root) { + this.root = root; + } + + public void preorder(Consumer f) { + preorder(f, this.root); + } + + public void preorder(Consumer f, Node node) { + if (node == null) return; + f.accept(node.data); // Aktion auf dem Knoten ausführen + preorder(f, node.left); // Linker Teilbaum + preorder(f, node.right); // Rechter Teilbaum + } + + public void inorder(Consumer f) { + inorder(f, this.root); + } + + public void inorder(Consumer f, Node node) { + if (node == null) return; + preorder(f, node.left); // Linker Teilbaum + f.accept(node.data); // Aktion auf dem Knoten ausführen + preorder(f, node.right); // Rechter Teilbaum + } + + public void postorder(Consumer f) { + postorder(f, this.root); + } + + public void postorder(Consumer f, Node node) { + if (node == null) return; + postorder(f, node.left); // Linker Teilbaum + postorder(f, node.right); // Rechter Teilbaum + f.accept(node.data); // Aktion auf dem Knoten ausführen + } + + public void levelorder(Consumer f) { + var list = new LinkedList(); + list.add(root); + while (!list.isEmpty()) { + Node n = list.pollFirst(); + f.accept(n.data); // Aktion ausführen + if (n.left != null) { + list.add(n.left); + } + if (n.right != null) { + list.add(n.right); + } + } + } + + public static void main(String[] args) { + SimpleTree t = new SimpleTree( + new Node("A", + new Node("B", new Node("D"), new Node("E")), + new Node("C", new Node("F"), new Node("G")))); + + System.out.print("Pre-Order: "); + t.preorder(System.out::print); + System.out.print("\nIn-Order: "); + t.inorder(System.out::print); + System.out.print("\nPost-Order: "); + t.postorder(System.out::print); + System.out.print("\nLevel-Order: "); + t.levelorder(System.out::print); + } +} diff --git a/src/main/java/pr2/ds/buffer/RingBuffer.java b/src/main/java/pr2/ds/buffer/RingBuffer.java new file mode 100644 index 0000000..1c35668 --- /dev/null +++ b/src/main/java/pr2/ds/buffer/RingBuffer.java @@ -0,0 +1,55 @@ +package pr2.ds.buffer; + +public class RingBuffer { + private T[] elements; + private int head = 0; + private int tail = 0; + private int size = 0; + + public RingBuffer(int capacity) { + elements = (T[]) new Object[capacity]; + } + + // Element hinzufügen + public void add(T element) { + if (size == elements.length) { + head = (head + 1) % elements.length; + } else { + size++; + } + elements[tail] = element; + tail = (tail + 1) % elements.length; + } + + // erstes Element zurückgeben + public T first() { + if (size == 0) { + return null; + } + return elements[head]; + } + + // letztes Element zurückgeben + public T last() { + if (size == 0) { + return null; + } + return elements[(tail - 1 + elements.length) % elements.length]; + } + + // Puffer leer? + public boolean isEmpty() { + return size == 0; + } + + // Puffer voll? + public boolean isFull() { + return size == elements.length; + } + + // Anzahl der Elemente im Puffer + public int size() { + return size; + } +} + diff --git a/src/main/java/pr2/ds/graph/Graph.java b/src/main/java/pr2/ds/graph/Graph.java new file mode 100644 index 0000000..cbaefa8 --- /dev/null +++ b/src/main/java/pr2/ds/graph/Graph.java @@ -0,0 +1,54 @@ +package pr2.ds.graph; + +import java.util.LinkedList; + +class Graph { + private int V; // Anzahl der Knoten + private LinkedList adj[]; // Adjazenzliste + + Graph(int v) { + V = v; + adj = new LinkedList[v]; + for (int i=0; i true + System.out.println(sh.contains("Januar")); // -> true + + } +} diff --git a/src/main/java/pr2/ds/hash/HashTable.java b/src/main/java/pr2/ds/hash/HashTable.java new file mode 100644 index 0000000..e389c68 --- /dev/null +++ b/src/main/java/pr2/ds/hash/HashTable.java @@ -0,0 +1,51 @@ +package pr2.ds.hash; + +import java.util.LinkedList; + +public class HashTable { + private int SIZE = 10; + private LinkedList[] table; + + HashTable() { + table = new LinkedList[SIZE]; + for (int i = 0; i < SIZE; i++) + table[i] = new LinkedList(); + } + + public void put(int key, int value) { + int index = hash(key); + table[index].add(new Node(key, value)); + } + + public int get(int key) { + int index = hash(key); + for (Node node : table[index]) + if (node.key == key) + return node.value; + return -1; + } + + public void remove(int key) { + int index = hash(key); + for (Node node : table[index]) + if (node.key == key) { + table[index].remove(node); + break; + } + } + + private int hash(int key) { + return key % SIZE; + } + + private static class Node { + int key; + int value; + + public Node(int key, int value) { + this.key = key; + this.value = value; + } + } +} + diff --git a/src/main/java/pr2/ds/hash/SimpleHashTable.java b/src/main/java/pr2/ds/hash/SimpleHashTable.java new file mode 100644 index 0000000..721191d --- /dev/null +++ b/src/main/java/pr2/ds/hash/SimpleHashTable.java @@ -0,0 +1,45 @@ +package pr2.ds.hash; + +public class SimpleHashTable { + + private static final int SIZE = 10; + private final String[] buckets = new String[SIZE]; + + public int idx(String s) { + return Math.abs(s.hashCode()) % SIZE; + } + public void add(String s) { + buckets[idx(s)] = s; + } + + public void remove(String s) { + buckets[idx(s)] = null; + } + + public boolean contains(String s) { + return s.equals(buckets[idx(s)]); + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SIZE; i++) { + sb.append(String.format("[%d]: %s\n", i, + buckets[i] == null ? "" : buckets[i])); + } + return sb.toString(); + } + + public static void main(String[] args) { + SimpleHashTable sh = new SimpleHashTable(); + sh.add("Januar"); + sh.add("Februar"); + sh.add("März"); + sh.add("April"); + sh.add("Mai"); + + System.out.println(sh.toString()); + System.out.println(sh.contains("Februar")); + System.out.println(sh.contains("Januar")); + + } +} diff --git a/src/main/java/pr2/ds/liste/ArrayIterator.java b/src/main/java/pr2/ds/liste/ArrayIterator.java new file mode 100644 index 0000000..455a21f --- /dev/null +++ b/src/main/java/pr2/ds/liste/ArrayIterator.java @@ -0,0 +1,24 @@ +package pr2.ds.liste; + +public class ArrayIterator { + + private final int[] array; + private int pos; + + public ArrayIterator(int[] array) { + this.array = array.clone(); + pos = 0; + } + + public boolean hasNext() { + return pos < array.length; + } + + public int next() { + return array[pos++]; // ArrayIndexOutOfBoundsException + } + + public void reset() { + pos = 0; + } +} diff --git a/src/main/java/pr2/ds/liste/ArrayIteratorTest.java b/src/main/java/pr2/ds/liste/ArrayIteratorTest.java new file mode 100644 index 0000000..7f3a1ee --- /dev/null +++ b/src/main/java/pr2/ds/liste/ArrayIteratorTest.java @@ -0,0 +1,14 @@ +package pr2.ds.liste; + +public class ArrayIteratorTest { + + public static void main(String[] args) { + int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 }; + ArrayIterator ai1 = new ArrayIterator(array); + ArrayIterator ai2 = new ArrayIterator(array); + + while (ai1.hasNext()) { + System.out.println(ai1.next()); + } + } +} diff --git a/src/main/java/pr2/ds/liste/DoubleLinkedList.java b/src/main/java/pr2/ds/liste/DoubleLinkedList.java new file mode 100644 index 0000000..440d81a --- /dev/null +++ b/src/main/java/pr2/ds/liste/DoubleLinkedList.java @@ -0,0 +1,112 @@ +package pr2.ds.liste; + + +class DoubleLinkedList implements IntList { + + private static class Node { + int data; + Node next; + Node prev; + + Node(int data) { + this.data = data; + this.next = null; + this.prev = null; + } + } + + Node head; + Node tail; + + + + + public void append(int data) { + Node newNode = new Node(data); + + if (head == null) { + head = newNode; + tail = newNode; + return; + } + + tail.next = newNode; + newNode.prev = tail; + tail = newNode; + } + + public void prepend(int data) { + Node newNode = new Node(data); + + if (head == null) { + head = newNode; + tail = newNode; + return; + } + + newNode.next = head; + head.prev = newNode; + head = newNode; + } + + public void deleteWithValue(int data) { + if (head == null) { + return; + } + + if (head.data == data) { + head = head.next; + head.prev = null; + return; + } + + Node current = head; + while (current.next != null) { + if (current.next.data == data) { + current.next = current.next.next; + if (current.next != null) { + current.next.prev = current; + } + return; + } + current = current.next; + } + } + + public void printList() { + Node current = head; + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[ "); + + Node current = head; + while (current != null) { + sb.append(current.data); + current = current.next; + if (current != null) { + sb.append(", "); + } + } + + sb.append(" ]"); + return sb.toString(); + } + + public int size() { + Node current = head; + int count = 0; + while (current != null) { + count++; + current = current.next; + } + return count; + } +} + diff --git a/src/main/java/pr2/ds/liste/IntList.java b/src/main/java/pr2/ds/liste/IntList.java new file mode 100644 index 0000000..3298e52 --- /dev/null +++ b/src/main/java/pr2/ds/liste/IntList.java @@ -0,0 +1,9 @@ +package pr2.ds.liste; + +public interface IntList { + void append(int data); + + void prepend(int data); + + int size(); +} diff --git a/src/main/java/pr2/ds/liste/LinkedList.java b/src/main/java/pr2/ds/liste/LinkedList.java new file mode 100644 index 0000000..16d4bf7 --- /dev/null +++ b/src/main/java/pr2/ds/liste/LinkedList.java @@ -0,0 +1,120 @@ +package pr2.ds.liste; + +class LinkedList implements IntList { + + private static class Node { + int data; + Node next; + + public Node(int data) { + this.data = data; + this.next = null; + } + } + + Node head; + + private Node last() { + Node result = head; + + while (result != null && result.next != null) { + result = result.next; + } + + return result; + } + + public void append(int data) { + Node newNode = new Node(data); + Node last = last(); + + if (last == null) { + head = newNode; + } + else { + last.next = newNode; + } + } + + public void prepend(int data) { + Node newNode = new Node(data); + + if (head == null) { + head = newNode; + return; + } + + newNode.next = head; + head = newNode; + } + + public int size() { + Node current = head; + int count = 0; + while (current != null) { + count++; + current = current.next; + } + return count; + } + + public int remove(int index) { + Node current = head; + Node prev = head; + + if (current == null) { + throw new RuntimeException("List is empty"); + } + + if (index >= size()) { + throw new IndexOutOfBoundsException(); + } + + int count = 0; + while (current != null) { + if (index == count) { + int data = current.data; + prev.next = current.next; + return data; + } + prev = current; + current = current.next; + } + return count; + } + public void delete(int data) { + Node current = head, prev = null; + if (current != null && current.data == data) { + head = current.next; + return; + } + + while (current != null && current.data != data) { + prev = current; + current = current.next; + } + + if (current == null) + return; + + prev.next = current.next; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[ "); + + Node current = head; + while (current != null) { + sb.append(current.data); + current = current.next; + if (current != null) { + sb.append(", "); + } + } + + sb.append(" ]"); + return sb.toString(); + } +} diff --git a/src/main/java/pr2/ds/liste/Pr2LinkedList.java b/src/main/java/pr2/ds/liste/Pr2LinkedList.java new file mode 100644 index 0000000..cf4f615 --- /dev/null +++ b/src/main/java/pr2/ds/liste/Pr2LinkedList.java @@ -0,0 +1,168 @@ +package pr2.ds.liste; + +public class Pr2LinkedList implements Pr2List { + + /** Start node. */ + private Pr2Node first; + + /** Last Node. */ + private Pr2Node last; + + @Override + public void add(T e) { + if (first == null) { + first = new Pr2Node<>(e); + last = first; + } + else { + last.next = new Pr2Node<>(e); + last = last.next; + } + } + + private Pr2Node at(int index) { + + if (index == 0 && first != null) { + return first; + } + + Pr2Node n = first; + int count = 0; + while (n != null) { + if (count == index) { + return n; + } + n = n.next; + count++; + } + + throw new ArrayIndexOutOfBoundsException(); + } + + @Override + public T get(int index) { + // no negative indices + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Negative index"); + } + + return at(index).element; + } + + @Override + public T remove(int index) { + T e; + + // no negative indices + if (index < 0) { + throw new ArrayIndexOutOfBoundsException("Negative index"); + } + + // Case first node + if (index == 0 && first != null) { + if (last == first) { + last = null; + } + e = first.element; + first = first.next; + return e; + } + + + Pr2Node prevNode = at(index - 1); + if (prevNode.next == null) { + // index not present + throw new ArrayIndexOutOfBoundsException(); + } + + e = prevNode.next.element; + prevNode.next = prevNode.next.next; + + if (prevNode.next == null) { + last = prevNode; + } + + return e; + } + + @Override + public int length() { + Pr2Node n = first; + int count = 0; + while (n != null) { + n = n.next; + count++; + } + return count; + } + + @Override + public T removeLast() { + T e; + + if (last == null) { + throw new ArrayIndexOutOfBoundsException("Empty List"); + } + + if (first == last) { + e = last.element; + first = null; + last = null; + } + else { + Pr2Node n = first; + while (n.next != last) { + n = n.next; + } + e = n.next.element; + n.next = null; + last = n; + } + + return e; + } + + @Override + public T removeFirst() { + if (last == null) { + throw new ArrayIndexOutOfBoundsException("Empty List"); + } + + T e = first.element; + + if (first == last) { + first = null; + last = null; + } + else { + first = first.next; + } + + return e; + } + + @Override + public void free() { + first = null; + last = null; + } + + /** + * @see Object#toString() + */ + @Override + public String toString() { + StringBuilder b = new StringBuilder("[ "); + Pr2Node n = first; + + while (n != null) { + b.append(n.element); + b.append(", "); + n = n.next; + } + + b.append(']'); + + return b.toString(); + } +} diff --git a/src/main/java/pr2/ds/liste/Pr2LinkedListTest.java b/src/main/java/pr2/ds/liste/Pr2LinkedListTest.java new file mode 100644 index 0000000..c6e4426 --- /dev/null +++ b/src/main/java/pr2/ds/liste/Pr2LinkedListTest.java @@ -0,0 +1,124 @@ +package pr2.ds.liste; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Testklasse für die Listen. + */ +public class Pr2LinkedListTest { + @Test + public void testLength() { + Pr2List list = new Pr2LinkedList<>(); + + assertEquals(0, list.length()); + list.add("A"); + assertEquals(1, list.length()); + list.add("B"); + assertEquals(2, list.length()); + list.removeLast(); + assertEquals(1, list.length()); + list.removeLast(); + assertEquals(0, list.length()); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + list::removeLast); + assertEquals(0, list.length()); + list.add("A"); + assertEquals(1, list.length()); + list.add("B"); + } + + @Test + public void testAddRemove() { + Pr2List list = new Pr2LinkedList<>(); + + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + list::removeLast); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(0)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(1)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(-1)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(99)); + + list.add("A"); + assertEquals("A", list.removeLast()); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + list::removeLast); + + list.add("A"); + list.add("B"); + list.add("C"); + assertEquals("A", list.removeFirst()); + assertEquals("C", list.removeLast()); + assertEquals("B", list.removeLast()); + assertEquals(0, list.length()); + + list.add("A"); + list.add("B"); + list.add("C"); + assertEquals("A", list.remove(0)); + assertEquals("B", list.removeFirst()); + assertEquals("C", list.remove(0)); + assertEquals(0, list.length()); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(0)); + + list.add("A"); + list.add("B"); + list.add("C"); + list.add("D"); + list.add("E"); + System.out.println(list); + assertEquals("C", list.remove(2)); + list.add("F"); + assertEquals("D", list.remove(2)); + assertEquals("F", list.get(3)); + assertEquals("E", list.remove(2)); + assertEquals("A", list.remove(0)); + assertEquals("B", list.remove(0)); + assertEquals("F", list.remove(0)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.remove(0)); + + list.add("A"); + assertEquals("A", list.remove(0)); + } + + @Test + public void testGet() { + Pr2List list = new Pr2LinkedList<>(); + + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.get(0)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.get(-9)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.get(999)); + + list.add("A"); + assertEquals("A", list.get(0)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.get(1)); + + list.add("B"); + assertEquals("A", list.get(0)); + assertEquals("B", list.get(1)); + Assertions.assertThrows(ArrayIndexOutOfBoundsException.class, + () -> list.get(2)); + + list.free(); + assertEquals(0, list.length()); + + list.add("A"); + list.add("B"); + list.add("C"); + assertEquals("C", list.get(2)); + assertEquals("A", list.get(0)); + assertEquals("B", list.get(1)); + } +} diff --git a/src/main/java/pr2/ds/liste/Pr2List.java b/src/main/java/pr2/ds/liste/Pr2List.java new file mode 100644 index 0000000..f8b9394 --- /dev/null +++ b/src/main/java/pr2/ds/liste/Pr2List.java @@ -0,0 +1,61 @@ +package pr2.ds.liste; + +/** + * Interface für eine Liste. + * + * @param Tyo der gespeicherten Daten. + */ +public interface Pr2List { + + /** + * Fügt das Element e an das Ende der Liste an. + * + * @param e das anzufügende Element + */ + void add(T e); + + /** + * Liefert das Element an der gegebenen Position zurück. + * + * @param index Index des Elements. + * @return das gefundene Element. + * @throws IndexOutOfBoundsException wenn der Index zu groß + * oder zu klein ist + */ + T get(int index); + + /** + * Entfernt das Element an der gegebenen Stelle. + * + * @param index der Index, des Elements. + * @return Das entfernte Element. + * @throws IndexOutOfBoundsException wenn der Index zu groß + * oder zu klein ist + */ + T remove(int index); + + /** + * Gibt die Länge der Liste zurück. + * @return Länge der Liste. + */ + int length(); + + /** + * Entfernt das letzte Element aus der Liste. + * @return Das entfernte Element. + * @throws IndexOutOfBoundsException wenn die Liste leer ist + */ + T removeLast(); + + /** + * Entfernt das erste Element aus der Liste. + * @return Das entfernte Element. + * @throws IndexOutOfBoundsException wenn die Liste leer ist + */ + T removeFirst(); + + /** + * Leert die komplette Liste. + */ + void free(); +} diff --git a/src/main/java/pr2/ds/liste/Pr2Node.java b/src/main/java/pr2/ds/liste/Pr2Node.java new file mode 100644 index 0000000..59e6f28 --- /dev/null +++ b/src/main/java/pr2/ds/liste/Pr2Node.java @@ -0,0 +1,23 @@ +package pr2.ds.liste; + +/** + * Ein Knoten in der Liste. + * + * @param Tyo der gespeicherten Daten. + */ +public class Pr2Node { + + /** + * Das gespeicherte Element. + */ + T element; + + /** + * Der Nachfolger. + */ + Pr2Node next; + + public Pr2Node(T e) { + element = e; + } +} diff --git a/src/main/java/pr2/ds/liste/TestLists.java b/src/main/java/pr2/ds/liste/TestLists.java new file mode 100644 index 0000000..b240627 --- /dev/null +++ b/src/main/java/pr2/ds/liste/TestLists.java @@ -0,0 +1,31 @@ +package pr2.ds.liste; + +public class TestLists { + + public static void main(String[] args) { + IntList ll = new LinkedList(); + + ll.append(1); + ll.append(2); + ll.append(3); + ll.append(4); + ll.append(5); + ll.prepend(0); + + System.out.println(ll); + System.out.println(ll.size()); + + IntList dll = new DoubleLinkedList(); + + dll.append(1); + dll.append(2); + dll.append(3); + dll.append(4); + dll.append(5); + dll.prepend(0); + + System.out.println(dll); + System.out.println(dll.size()); + + } +} diff --git a/src/main/java/pr2/ds/queue/Queue.java b/src/main/java/pr2/ds/queue/Queue.java new file mode 100644 index 0000000..97569aa --- /dev/null +++ b/src/main/java/pr2/ds/queue/Queue.java @@ -0,0 +1,42 @@ +package pr2.ds.queue; + +import java.util.LinkedList; + +public class Queue { + private LinkedList elements; + + public Queue() { + elements = new LinkedList(); + } + + // Element in die Queue legen + public void enqueue(T element) { + elements.addLast(element); + } + + // erstes Element aus der Queue entfernen und zurückgeben + public T dequeue() { + if (elements.isEmpty()) { + return null; + } + return elements.removeFirst(); + } + + // erstes Element zurückgeben, ohne es zu entfernen + public T peek() { + if (elements.isEmpty()) { + return null; + } + return elements.getFirst(); + } + + // Queue leer? + public boolean isEmpty() { + return elements.isEmpty(); + } + + // Anzahl der Elemente in der Queue + public int size() { + return elements.size(); + } +} diff --git a/src/main/java/pr2/ds/search/BoyerMoore.java b/src/main/java/pr2/ds/search/BoyerMoore.java new file mode 100644 index 0000000..b3ffc06 --- /dev/null +++ b/src/main/java/pr2/ds/search/BoyerMoore.java @@ -0,0 +1,52 @@ +package pr2.ds.search; + +public class BoyerMoore { + + private final int R; // the radix + private int[] right; // the bad-character skip array + + private char[] pattern; // store the pattern as a character array + private String pat; // or as a string + + public BoyerMoore(String pat) { + this.R = 256; + this.pat = pat; + + // position of rightmost occurrence of c in the pattern + right = new int[R]; + for (int c = 0; c < R; c++) + right[c] = -1; + for (int j = 0; j < pat.length(); j++) + right[pat.charAt(j)] = j; + } + + public int search(String txt) { + int M = pat.length(); + int N = txt.length(); + int skip; + for (int i = 0; i <= N - M; i += skip) { + skip = 0; + for (int j = M-1; j >= 0; j--) { + if (pat.charAt(j) != txt.charAt(i+j)) { + skip = Math.max(1, j - right[txt.charAt(i+j)]); + break; + } + } + if (skip == 0) return i; // found + } + return N; // not found + } + + public static void main(String[] args) { + String txt = "This is a simple example text"; + String pat = "example"; + BoyerMoore bm = new BoyerMoore(pat); + int offset = bm.search(txt); + // print results + System.out.println("text: " + txt); + System.out.print("pattern: "); + for (int i = 0; i < offset; i++) + System.out.print(" "); + System.out.println(pat); + } +} diff --git a/src/main/java/pr2/ds/search/KMP.java b/src/main/java/pr2/ds/search/KMP.java new file mode 100644 index 0000000..ebd67f6 --- /dev/null +++ b/src/main/java/pr2/ds/search/KMP.java @@ -0,0 +1,38 @@ +package pr2.ds.search; + +public class KMP { + private final int[] prefixFunction; + private final String pattern; + + public KMP(String pattern) { + this.pattern = pattern; + prefixFunction = new int[pattern.length()]; + int j = 0; + for (int i = 1; i < pattern.length(); i++) { + while (j > 0 && pattern.charAt(i) != pattern.charAt(j)) { + j = prefixFunction[j - 1]; + } + if (pattern.charAt(i) == pattern.charAt(j)) { + j++; + } + prefixFunction[i] = j; + } + } + + public int search(String text) { + int j = 0; + for (int i = 0; i < text.length(); i++) { + while (j > 0 && text.charAt(i) != pattern.charAt(j)) { + j = prefixFunction[j - 1]; + } + if (text.charAt(i) == pattern.charAt(j)) { + j++; + } + if (j == pattern.length()) { + return i - pattern.length() + 1; + } + } + return -1; + } +} + diff --git a/src/main/java/pr2/ds/stack/Stack.java b/src/main/java/pr2/ds/stack/Stack.java new file mode 100644 index 0000000..47d0bfa --- /dev/null +++ b/src/main/java/pr2/ds/stack/Stack.java @@ -0,0 +1,42 @@ +package pr2.ds.stack; + +import java.util.ArrayList; + +public class Stack { + private final ArrayList elements; + + public Stack() { + elements = new ArrayList(); + } + + // Element auf den Stack legen + public void push(T element) { + elements.add(element); + } + + // oberstes Element vom Stack entfernen und zurückgeben + public T pop() { + if (elements.isEmpty()) { + return null; + } + return elements.remove(elements.size() - 1); + } + + // oberstes Element zurückgeben, ohne es zu entfernen + public T peek() { + if (elements.isEmpty()) { + return null; + } + return elements.get(elements.size() - 1); + } + + // Stack leer? + public boolean isEmpty() { + return elements.isEmpty(); + } + + // Anzahl der Elemente im Stack + public int size() { + return elements.size(); + } +} diff --git a/src/main/java/pr2/enumerationen/abstraktemethoden/Operation.java b/src/main/java/pr2/enumerationen/abstraktemethoden/Operation.java new file mode 100644 index 0000000..3f8acfd --- /dev/null +++ b/src/main/java/pr2/enumerationen/abstraktemethoden/Operation.java @@ -0,0 +1,33 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.abstraktemethoden; + +public enum Operation { + + ADDITION { + @Override + public double anwenden(double op1, double op2) { + return op1 + op2; + } + }, + SUBTRAKTION { + @Override + public double anwenden(double op1, double op2) { + return op1 - op2; + } + }, + MULTIPLIKATION { + @Override + public double anwenden(double op1, double op2) { + return op1 * op2; + } + }, + DIVISION { + public double anwenden(double op1, double op2) { + return op1 / op2; + } + }; + + public abstract double anwenden(double op1, double op2); +} diff --git a/src/main/java/pr2/enumerationen/abstraktemethoden/Rechner.java b/src/main/java/pr2/enumerationen/abstraktemethoden/Rechner.java new file mode 100644 index 0000000..9a89814 --- /dev/null +++ b/src/main/java/pr2/enumerationen/abstraktemethoden/Rechner.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.abstraktemethoden; + +public class Rechner { + + public double rechne(double op1, double op2, Operation operation) { + return operation.anwenden(op1, op2); + } + + public static void main(String[] args) { + Rechner r = new Rechner(); + + double summe = r.rechne(5, 5, Operation.ADDITION); + double differenz = r.rechne(6, 3, Operation.SUBTRAKTION); + double produkt = r.rechne(6, 3, Operation.MULTIPLIKATION); + + System.out.printf("%.0f, %.0f, %.0f", summe, differenz, produkt); + } +} diff --git a/src/main/java/pr2/enumerationen/classic/DateJuggler.java b/src/main/java/pr2/enumerationen/classic/DateJuggler.java new file mode 100644 index 0000000..f0dd291 --- /dev/null +++ b/src/main/java/pr2/enumerationen/classic/DateJuggler.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.classic; + +public class DateJuggler { + + public void setWochentag(int wochentag) { + // ... + } + + public int getWochentag() { + // ... + return 0; + } +} diff --git a/src/main/java/pr2/enumerationen/classic/Verwender.java b/src/main/java/pr2/enumerationen/classic/Verwender.java new file mode 100644 index 0000000..ba7df18 --- /dev/null +++ b/src/main/java/pr2/enumerationen/classic/Verwender.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.classic; + +public class Verwender { + + public static void main(String[] args) { + DateJuggler dj = new DateJuggler(); + dj.setWochentag(Wochentag.FREITAG); + dj.setWochentag(Wochentag.FREITAG + 100); + + int wochentag = dj.getWochentag(); + } +} diff --git a/src/main/java/pr2/enumerationen/classic/Wochentag.java b/src/main/java/pr2/enumerationen/classic/Wochentag.java new file mode 100644 index 0000000..43c3b21 --- /dev/null +++ b/src/main/java/pr2/enumerationen/classic/Wochentag.java @@ -0,0 +1,14 @@ +package pr2.enumerationen.classic; + +/** + * Konstanten für die Wochentage. + */ +public class Wochentag { + public static final int MONTAG = 1; + public static final int DIENSTAG = 2; + public static final int MITTWOCH = 3; + public static final int DONNERSTAG = 4; + public static final int FREITAG = 5; + public static final int SAMSTAG = 6; + public static final int SONNTAG = 7; +} diff --git a/src/main/java/pr2/enumerationen/enummethoden/Planet.java b/src/main/java/pr2/enumerationen/enummethoden/Planet.java new file mode 100644 index 0000000..c9200ef --- /dev/null +++ b/src/main/java/pr2/enumerationen/enummethoden/Planet.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enummethoden; + +public enum Planet { + MERKUR, + VENUS, + ERDE, + MARS, + JUPITER, + SATURN, + URANUS, + NEPTUN; +} diff --git a/src/main/java/pr2/enumerationen/enummethoden/Verwender.java b/src/main/java/pr2/enumerationen/enummethoden/Verwender.java new file mode 100644 index 0000000..4810ee5 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enummethoden/Verwender.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enummethoden; + +public class Verwender { + + public static void main(String[] args) { + String name = Planet.ERDE.name(); + Planet planet = Planet.valueOf(name); + System.out.printf("Wir leben auf der %s, sie ist " + + "der %d Planet%n", planet, planet.ordinal() + 1); + + System.out.printf("Wir haben %d Planeten im Sonnensystem%n", + Planet.values().length); + } +} diff --git a/src/main/java/pr2/enumerationen/enumset/EnumSetDemo.java b/src/main/java/pr2/enumerationen/enumset/EnumSetDemo.java new file mode 100644 index 0000000..87e1705 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/EnumSetDemo.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset; + +import java.util.EnumSet; + +public class EnumSetDemo { + + public static void main(String[] args) { + EnumSet planeten = EnumSet.allOf(Planet.class); + System.out.println("allOf: " + planeten); + + EnumSet dieAnderen = EnumSet.complementOf(EnumSet.of(Planet.ERDE)); + System.out.println("complementOf: " + dieAnderen); + + EnumSet leer = EnumSet.noneOf(Planet.class); + System.out.println("noneOf: " + leer); + + EnumSet aeussere = EnumSet.range(Planet.MARS, Planet.NEPTUN); + System.out.println("range: " + aeussere); + } +} diff --git a/src/main/java/pr2/enumerationen/enumset/Planet.java b/src/main/java/pr2/enumerationen/enumset/Planet.java new file mode 100644 index 0000000..aa2947a --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/Planet.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset; + +public enum Planet { + MERKUR, + VENUS, + ERDE, + MARS, + JUPITER, + SATURN, + URANUS, + NEPTUN; + // PLUTO; +} diff --git a/src/main/java/pr2/enumerationen/enumset/Textauszeichnung.java b/src/main/java/pr2/enumerationen/enumset/Textauszeichnung.java new file mode 100644 index 0000000..fc15bd6 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/Textauszeichnung.java @@ -0,0 +1,10 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset; + +public enum Textauszeichnung { + FETT, + UNTERSTRICHEN, + KURSIV; +} diff --git a/src/main/java/pr2/enumerationen/enumset/Textverarbeitung.java b/src/main/java/pr2/enumerationen/enumset/Textverarbeitung.java new file mode 100644 index 0000000..22f0018 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/Textverarbeitung.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset; + +import java.util.EnumSet; + +import static pr2.enumerationen.enumset.Textauszeichnung.*; + +public class Textverarbeitung { + + public void formatiere(String text, EnumSet formatierungen) { + // .. + } + + public static void main(String[] args) { + Textverarbeitung tv = new Textverarbeitung(); + tv.formatiere("Hello World!", EnumSet.of(FETT, KURSIV)); + } +} diff --git a/src/main/java/pr2/enumerationen/enumset/Todesstern.java b/src/main/java/pr2/enumerationen/enumset/Todesstern.java new file mode 100644 index 0000000..2f9dd9c --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/Todesstern.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset; + +import java.util.EnumSet; + +import static pr2.enumerationen.enumset.Planet.*; + +public class Todesstern { + + public void zerstoere(EnumSet planeten) { + for (Planet planet : planeten) { + richteTodesstrahlAus(planet); + } + } + + public static void main(String[] args) { + Todesstern ts = new Todesstern(); + ts.zerstoere(EnumSet.of(MARS, VENUS, JUPITER)); + } + + private void richteTodesstrahlAus(Planet planet) { + // TODO Auto-generated method stub + } +} diff --git a/src/main/java/pr2/enumerationen/enumset/alt/Auszeichnungen.java b/src/main/java/pr2/enumerationen/enumset/alt/Auszeichnungen.java new file mode 100644 index 0000000..e5a1cce --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/alt/Auszeichnungen.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset.alt; + +public class Auszeichnungen { + public static final int BOLD = 0x01; + public static final int ITALIC = 0x02; + public static final int UNDERLINE = 0x04; + public static final int STRIKEOUT = 0x08; + public static final int OUTLINED = 0x10; +} diff --git a/src/main/java/pr2/enumerationen/enumset/alt/Textprogramm.java b/src/main/java/pr2/enumerationen/enumset/alt/Textprogramm.java new file mode 100644 index 0000000..13f42f6 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/alt/Textprogramm.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset.alt; + +import static pr2.enumerationen.enumset.alt.Auszeichnungen.*; + +public class Textprogramm { + + public void setAuszeichnung(int auszeichnung) { + if ((auszeichnung & BOLD) > 0) { + System.out.println("Fett"); + } + if ((auszeichnung & ITALIC) > 0) { + System.out.println("Kursiv"); + } + if ((auszeichnung & UNDERLINE) > 0) { + System.out.println("Unterstrichen"); + } + if ((auszeichnung & STRIKEOUT) > 0) { + System.out.println("Durchgestrichen"); + } + if ((auszeichnung & OUTLINED) > 0) { + System.out.println("Umriss"); + } + } + + public static void main(String[] args) { + Textprogramm t = new Textprogramm(); + t.setAuszeichnung(BOLD | UNDERLINE | STRIKEOUT); + } +} diff --git a/src/main/java/pr2/enumerationen/enumset/neu/Auszeichnungen.java b/src/main/java/pr2/enumerationen/enumset/neu/Auszeichnungen.java new file mode 100644 index 0000000..94cd544 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/neu/Auszeichnungen.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset.neu; + +public enum Auszeichnungen { + BOLD, ITALIC, UNDERLINE, STRIKEOUT, OUTLINED; +} diff --git a/src/main/java/pr2/enumerationen/enumset/neu/Textprogramm.java b/src/main/java/pr2/enumerationen/enumset/neu/Textprogramm.java new file mode 100644 index 0000000..25bcee5 --- /dev/null +++ b/src/main/java/pr2/enumerationen/enumset/neu/Textprogramm.java @@ -0,0 +1,35 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.enumset.neu; + +import static pr2.enumerationen.enumset.neu.Auszeichnungen.*; + +import java.util.EnumSet; + +public class Textprogramm { + + public void setAuszeichnung(EnumSet auszeichnung) { + + if (auszeichnung.contains(BOLD)) { + System.out.println("Fett"); + } + if (auszeichnung.contains(ITALIC)) { + System.out.println("Kursiv"); + } + if (auszeichnung.contains(UNDERLINE)) { + System.out.println("Unterstrichen"); + } + if (auszeichnung.contains(STRIKEOUT)) { + System.out.println("Durchgestrichen"); + } + if (auszeichnung.contains(OUTLINED)) { + System.out.println("Umriss"); + } + } + + public static void main(String[] args) { + Textprogramm t = new Textprogramm(); + t.setAuszeichnung(EnumSet.of(BOLD, UNDERLINE, STRIKEOUT)); + } +} diff --git a/src/main/java/pr2/enumerationen/mitenums/DateJuggler.java b/src/main/java/pr2/enumerationen/mitenums/DateJuggler.java new file mode 100644 index 0000000..03c537e --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitenums/DateJuggler.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitenums; + +public class DateJuggler { + + public void setWochentag(Wochentag wochentag) { + // ... + } + + public Wochentag getWochentag() { + // ... + return null; + } +} diff --git a/src/main/java/pr2/enumerationen/mitenums/Verwender.java b/src/main/java/pr2/enumerationen/mitenums/Verwender.java new file mode 100644 index 0000000..4b98872 --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitenums/Verwender.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitenums; + +public class Verwender { + + public static void main(String[] args) { + DateJuggler dj = new DateJuggler(); + dj.setWochentag(Wochentag.FREITAG); + // dj.setWochentag(Wochentag.FREITAG + 100); + + Wochentag wochentag = dj.getWochentag(); + } +} diff --git a/src/main/java/pr2/enumerationen/mitenums/Wochentag.java b/src/main/java/pr2/enumerationen/mitenums/Wochentag.java new file mode 100644 index 0000000..133a8e1 --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitenums/Wochentag.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitenums; + +public enum Wochentag { + MONTAG, DIENSTAG, MITTWOCH, DONNERSTAG, FREITAG, SAMSTAG, SONNTAG; +} diff --git a/src/main/java/pr2/enumerationen/mitkonstruktor/Ausgabe.java b/src/main/java/pr2/enumerationen/mitkonstruktor/Ausgabe.java new file mode 100644 index 0000000..352c98e --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitkonstruktor/Ausgabe.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitkonstruktor; + +public class Ausgabe { + + public static void main(String[] args) { + for (Planet planet : Planet.values()) { + System.out.printf("%s -> %,.0f km/h%n", + planet.name(), planet.bahngeschwindigkeit()); + } + } +} diff --git a/src/main/java/pr2/enumerationen/mitkonstruktor/Planet.java b/src/main/java/pr2/enumerationen/mitkonstruktor/Planet.java new file mode 100644 index 0000000..ecbbd47 --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitkonstruktor/Planet.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitkonstruktor; + +public enum Planet { + + MERKUR (59e+6,88), + VENUS (108e+6,225), + ERDE (150e+6, 365), + MARS (228e+6, 1.9*365), + JUPITER (778e+6, 11.9*365), + SATURN (1427e+6, 29.5*365), + URANUS (2870e+6, 84*365), + NEPTUN (4497e+6, 165*365); + + private final double entfernungsonne; // in km + private final double umlaufzeit; // in tagen + + Planet(double entfernungsonne, double umlaufzeit) { + this.entfernungsonne = entfernungsonne; + this.umlaufzeit = umlaufzeit; + } + + public double bahngeschwindigkeit() { + return 2 * entfernungsonne * Math.PI / umlaufzeit / 24; + } +} diff --git a/src/main/java/pr2/enumerationen/mitkonstruktor/SpaceTravel.java b/src/main/java/pr2/enumerationen/mitkonstruktor/SpaceTravel.java new file mode 100644 index 0000000..b388551 --- /dev/null +++ b/src/main/java/pr2/enumerationen/mitkonstruktor/SpaceTravel.java @@ -0,0 +1,48 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.mitkonstruktor; + +public class SpaceTravel { + + private static final Planet HOME_PLANET = Planet.ERDE; + + public void travel(Planet zielPlanet) { + + if (zielPlanet == HOME_PLANET) { + return; // wir sind doch schon da + } + + switch (zielPlanet) { + + case JUPITER: + // langer Flug + break; + + case MARS: + // kurzer Flug + break; + + case MERKUR: + // mittellanger Flug + break; + + case NEPTUN: + // ewiglanger Flug + break; + + case SATURN: + break; + + case URANUS: + break; + + case VENUS: + break; + + default: + throw new IllegalArgumentException("Unbekannter Wert" + zielPlanet); + + } + } +} diff --git a/src/main/java/pr2/enumerationen/quiz/Geschlecht.java b/src/main/java/pr2/enumerationen/quiz/Geschlecht.java new file mode 100644 index 0000000..e764f82 --- /dev/null +++ b/src/main/java/pr2/enumerationen/quiz/Geschlecht.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.quiz; + +public enum Geschlecht { + MAENNLICH, WEIBLICH; +} diff --git a/src/main/java/pr2/enumerationen/quiz/Verwender.java b/src/main/java/pr2/enumerationen/quiz/Verwender.java new file mode 100644 index 0000000..923a68d --- /dev/null +++ b/src/main/java/pr2/enumerationen/quiz/Verwender.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.quiz; + +public class Verwender { + + public static void main(String[] args) { + Geschlecht g1 = Geschlecht.valueOf("MAENNLICH"); + Geschlecht g2 = Geschlecht.values()[0]; + Geschlecht g3 = Geschlecht.MAENNLICH; + + System.out.println(g1 == g2); + System.out.println(g1 == g3); + System.out.println(g1.equals(g3)); + } +} diff --git a/src/main/java/pr2/enumerationen/quiz/Wochentage.java b/src/main/java/pr2/enumerationen/quiz/Wochentage.java new file mode 100644 index 0000000..0909077 --- /dev/null +++ b/src/main/java/pr2/enumerationen/quiz/Wochentage.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.quiz; + +public enum Wochentage { + MONTAG, DIENSTAG, MITTWOCH, DONNERSTAG, FREITAG, + SAMSTAG, SONNTAG(false); + + @SuppressWarnings("unused") + private final boolean werktag; + + private Wochentage() { + werktag = true; + } + + private Wochentage(boolean werktag) { + this.werktag = werktag; + } +} diff --git a/src/main/java/pr2/enumerationen/simple/Planet.java b/src/main/java/pr2/enumerationen/simple/Planet.java new file mode 100644 index 0000000..6ae36b9 --- /dev/null +++ b/src/main/java/pr2/enumerationen/simple/Planet.java @@ -0,0 +1,10 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.enumerationen.simple; + +public enum Planet { + MERKUR, VENUS, ERDE, MARS, + JUPITER,SATURN, URANUS, NEPTUN; + // PLUTO; +} diff --git a/src/main/java/pr2/generics/Pair.java b/src/main/java/pr2/generics/Pair.java new file mode 100644 index 0000000..8e86102 --- /dev/null +++ b/src/main/java/pr2/generics/Pair.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics; + +public class Pair { + + private T o1; + private V o2; + + public Pair(T o1, V o2) { + this.o1 = o1; + this.o2 = o2; + } + + public T getFirst() { + return o1; + } + + public V getSecond() { + return o2; + } +} diff --git a/src/main/java/pr2/generics/PairUser.java b/src/main/java/pr2/generics/PairUser.java new file mode 100644 index 0000000..0fb7b68 --- /dev/null +++ b/src/main/java/pr2/generics/PairUser.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics; + +public class PairUser { + public static void main(String[] args) { + Pair p1 = new Pair<>("Hello", "World"); + Pair geld = new Pair<>(100, "EUR"); + } +} diff --git a/src/main/java/pr2/generics/boundparameter/Form.java b/src/main/java/pr2/generics/boundparameter/Form.java new file mode 100644 index 0000000..f4c92d4 --- /dev/null +++ b/src/main/java/pr2/generics/boundparameter/Form.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundparameter; + +public abstract class Form { + + public abstract double flaeche(); +} diff --git a/src/main/java/pr2/generics/boundparameter/FormenStack.java b/src/main/java/pr2/generics/boundparameter/FormenStack.java new file mode 100644 index 0000000..742cebc --- /dev/null +++ b/src/main/java/pr2/generics/boundparameter/FormenStack.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundparameter; + +public class FormenStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public FormenStack(int size) { + stack = (T[]) new Form[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } + + public int getSize() { + return pos - 1; + } + + public double flaeche() { + + double summe = 0; + + for (int i = getSize(); i >= 0; i--) { + summe += pop().flaeche(); + } + + return summe; + } +} diff --git a/src/main/java/pr2/generics/boundparameter/Kreis.java b/src/main/java/pr2/generics/boundparameter/Kreis.java new file mode 100644 index 0000000..98256e2 --- /dev/null +++ b/src/main/java/pr2/generics/boundparameter/Kreis.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundparameter; + +public class Kreis extends Form { + + private double radius; + + public Kreis(double radius) { + this.radius = radius; + } + + @Override + public double flaeche() { + return 2 * Math.PI * radius * radius; + } +} diff --git a/src/main/java/pr2/generics/boundparameter/Rechteck.java b/src/main/java/pr2/generics/boundparameter/Rechteck.java new file mode 100644 index 0000000..e195fe2 --- /dev/null +++ b/src/main/java/pr2/generics/boundparameter/Rechteck.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundparameter; + +public class Rechteck extends Form { + + private double a; + private double b; + + public Rechteck(double a, double b) { + this.a = a; + this.b = b; + } + + @Override + public double flaeche() { + return a * b; + } +} diff --git a/src/main/java/pr2/generics/boundparameter/Verwender.java b/src/main/java/pr2/generics/boundparameter/Verwender.java new file mode 100644 index 0000000..95e12f8 --- /dev/null +++ b/src/main/java/pr2/generics/boundparameter/Verwender.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundparameter; + +public class Verwender { + + public static void main(String[] args) { + + var s1 = new FormenStack(10); + s1.push(new Kreis(0.4)); + s1.push(new Kreis(0.7)); + + var s2 = new FormenStack(10); + s2.push(new Rechteck(0.4, 0.4)); + s2.push(new Rechteck(0.7, 0.3)); + + var s3 = new FormenStack
(10); + s3.push(new Rechteck(0.4, 0.4)); + s3.push(new Rechteck(0.7, 0.3)); + s3.push(new Kreis(0.4)); + s3.push(new Kreis(0.7)); + + System.out.printf("Gesamtflaeche: %g%n", s1.flaeche()); + System.out.printf("Gesamtflaeche: %g%n", s2.flaeche()); + System.out.printf("Gesamtflaeche: %g%n", s3.flaeche()); + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/Berechner.java b/src/main/java/pr2/generics/boundwildcard/Berechner.java new file mode 100644 index 0000000..07ce462 --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/Berechner.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +public class Berechner { + + public double berechneFlaeche(SimpleStack formen) { + + double summe = 0; + + for (int i = formen.getSize(); i >= 0; i--) { + summe += formen.pop().flaeche(); + } + + return summe; + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/Form.java b/src/main/java/pr2/generics/boundwildcard/Form.java new file mode 100644 index 0000000..98b4ca9 --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/Form.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +public abstract class Form { + + public abstract double flaeche(); +} diff --git a/src/main/java/pr2/generics/boundwildcard/Kreis.java b/src/main/java/pr2/generics/boundwildcard/Kreis.java new file mode 100644 index 0000000..720decb --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/Kreis.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +public class Kreis extends Form { + + private double radius; + + public Kreis(double radius) { + this.radius = radius; + } + + @Override + public double flaeche() { + return 2 * Math.PI * radius * radius; + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/Rechteck.java b/src/main/java/pr2/generics/boundwildcard/Rechteck.java new file mode 100644 index 0000000..3ebd3cf --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/Rechteck.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +public class Rechteck extends Form { + + private double a; + private double b; + + public Rechteck(double a, double b) { + this.a = a; + this.b = b; + } + + @Override + public double flaeche() { + return a * b; + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/SimpleStack.java b/src/main/java/pr2/generics/boundwildcard/SimpleStack.java new file mode 100644 index 0000000..03f79f1 --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/SimpleStack.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +import java.util.Collection; + +public class SimpleStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } + + public int getSize() { + return pos - 1; + } + + public void popAll(Collection collection) { + while (pos > 0) { + collection.add(pop()); + } + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/SuperDemo.java b/src/main/java/pr2/generics/boundwildcard/SuperDemo.java new file mode 100644 index 0000000..af644af --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/SuperDemo.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +import java.util.ArrayList; +import java.util.List; + +public class SuperDemo { + + public static void main(String[] args) { + SimpleStack s = new SimpleStack<>(10); + s.push(new Rechteck(0.4, 0.4)); + s.push(new Rechteck(0.7, 0.3)); + s.push(new Kreis(0.4)); + s.push(new Kreis(0.7)); + + List l1 = new ArrayList<>(); + s.popAll(l1); + } +} diff --git a/src/main/java/pr2/generics/boundwildcard/Verwender.java b/src/main/java/pr2/generics/boundwildcard/Verwender.java new file mode 100644 index 0000000..895516b --- /dev/null +++ b/src/main/java/pr2/generics/boundwildcard/Verwender.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.boundwildcard; + +public class Verwender { + + public static void main(String[] args) { + + var s1 = new SimpleStack(10); + s1.push(new Kreis(0.4)); + s1.push(new Kreis(0.7)); + + var s2 = new SimpleStack(10); + s2.push(new Rechteck(0.4, 0.4)); + s2.push(new Rechteck(0.7, 0.3)); + + var s3 = new SimpleStack(10); + s3.push(new Rechteck(0.4, 0.4)); + s3.push(new Rechteck(0.7, 0.3)); + s3.push(new Kreis(0.4)); + s3.push(new Kreis(0.7)); + + Berechner b = new Berechner(); + + System.out.printf("Gesamtflaeche: %g%n", b.berechneFlaeche(s1)); + System.out.printf("Gesamtflaeche: %g%n", b.berechneFlaeche(s2)); + System.out.printf("Gesamtflaeche: %g%n", b.berechneFlaeche(s3)); + } +} diff --git a/src/main/java/pr2/generics/covariance/Covariance.java b/src/main/java/pr2/generics/covariance/Covariance.java new file mode 100644 index 0000000..5e90841 --- /dev/null +++ b/src/main/java/pr2/generics/covariance/Covariance.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.covariance; + +import java.util.ArrayList; + +class A { /* empty */ } +class B extends A { /* empty */ } + +public class Covariance { + + public static void main(String[] args) { + A[] a1 = new A[10]; + A[] a2 = new B[10]; + B[] b1 = (B[]) a2; + + a1[0] = new B(); + a1[1] = new A(); + + a2[0] = new B(); + b1[0] = new B(); + + ArrayList l1 = new ArrayList<>(); +// ArrayList l2 = new ArrayList(); + + l1.add(new B()); + } + +} diff --git a/src/main/java/pr2/generics/covariance/Refied.java b/src/main/java/pr2/generics/covariance/Refied.java new file mode 100644 index 0000000..d5658d7 --- /dev/null +++ b/src/main/java/pr2/generics/covariance/Refied.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.covariance; + +public class Refied { + + public static void main(String[] args) { + Object[] array = new String[10]; + array[0] = "Hallo"; + array[1] = Integer.valueOf(15); + +// ArrayList list = new ArrayList(); +// list.add(new Integer(15)); + + } +} diff --git a/src/main/java/pr2/generics/erasure/ShowErasure.java b/src/main/java/pr2/generics/erasure/ShowErasure.java new file mode 100644 index 0000000..b99ddc0 --- /dev/null +++ b/src/main/java/pr2/generics/erasure/ShowErasure.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.erasure; + +public class ShowErasure { + + @SuppressWarnings("rawtypes") + public static void main(String[] args) { + + var s1 = new SimpleStack(4); + var s2 = new SimpleStack(4); + SimpleStack sRaw = new SimpleStack(4); + + System.out.println(s1.getClass()); + System.out.println(s2.getClass()); + System.out.println(sRaw.getClass()); + } +} diff --git a/src/main/java/pr2/generics/erasure/SimpleStack.java b/src/main/java/pr2/generics/erasure/SimpleStack.java new file mode 100644 index 0000000..08eba3f --- /dev/null +++ b/src/main/java/pr2/generics/erasure/SimpleStack.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.erasure; + +public class SimpleStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/generics/inheritance/A.java b/src/main/java/pr2/generics/inheritance/A.java new file mode 100644 index 0000000..be2dcff --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/A.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +public class A { + +} diff --git a/src/main/java/pr2/generics/inheritance/B.java b/src/main/java/pr2/generics/inheritance/B.java new file mode 100644 index 0000000..2cdbad6 --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/B.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +public class B extends A { + +} diff --git a/src/main/java/pr2/generics/inheritance/FormStack.java b/src/main/java/pr2/generics/inheritance/FormStack.java new file mode 100644 index 0000000..26653cf --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/FormStack.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +import pr2.generics.boundwildcard.Form; +import pr2.generics.mitgenerics.SimpleStack; + +public class FormStack extends SimpleStack { + + public FormStack(int size) { + super(size); + } + + public Form peek() { + Form wert = pop(); + push(wert); + return wert; + } +} diff --git a/src/main/java/pr2/generics/inheritance/Generic.java b/src/main/java/pr2/generics/inheritance/Generic.java new file mode 100644 index 0000000..0a684b6 --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/Generic.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +public class Generic { + + public void doIt(T param) { + // ... + } +} diff --git a/src/main/java/pr2/generics/inheritance/MyStack.java b/src/main/java/pr2/generics/inheritance/MyStack.java new file mode 100644 index 0000000..cf824a8 --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/MyStack.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +import pr2.generics.mitgenerics.SimpleStack; + +public class MyStack extends SimpleStack { + + public MyStack(int size) { + super(size); + } + + public T peek() { + T wert = pop(); + push(wert); + return wert; + } +} diff --git a/src/main/java/pr2/generics/inheritance/Verwender.java b/src/main/java/pr2/generics/inheritance/Verwender.java new file mode 100644 index 0000000..00764a3 --- /dev/null +++ b/src/main/java/pr2/generics/inheritance/Verwender.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.inheritance; + +public class Verwender { + + public static void main(String[] args) { + + A a1 = new B(); + A[] a2 = new B[1]; + + // Generic g3 = new Generic(); + } +} diff --git a/src/main/java/pr2/generics/methoden/SimpleStack.java b/src/main/java/pr2/generics/methoden/SimpleStack.java new file mode 100644 index 0000000..2e6889f --- /dev/null +++ b/src/main/java/pr2/generics/methoden/SimpleStack.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.methoden; + +public class SimpleStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } + + public int getSize() { + return pos - 1; + } +} diff --git a/src/main/java/pr2/generics/methoden/Unifier.java b/src/main/java/pr2/generics/methoden/Unifier.java new file mode 100644 index 0000000..dd06941 --- /dev/null +++ b/src/main/java/pr2/generics/methoden/Unifier.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.methoden; + +import java.util.HashSet; +import java.util.Set; + +public class Unifier { + + public Set unify(Set s1, Set s2) { + Set s = new HashSet(); + s.addAll(s1); + s.addAll(s2); + + return s; + } + + public static void main(String[] args) { + Set s1 = new HashSet<>(); + s1.add(1); s1.add(3); + Set s2 = new HashSet<>(); + s2.add(10.0); s2.add(30.0); + + Set union = new Unifier().unify(s1, s2); + System.out.println(union); + } +} diff --git a/src/main/java/pr2/generics/methoden/Verwender.java b/src/main/java/pr2/generics/methoden/Verwender.java new file mode 100644 index 0000000..95da45b --- /dev/null +++ b/src/main/java/pr2/generics/methoden/Verwender.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.methoden; + +public class Verwender { + + public static void main(String[] args) { + SimpleStack stack1 = new SimpleStack<>(10); + stack1.push("Hello"); + stack1.push("World"); + + SimpleStack stack2 = new SimpleStack<>(10); + stack2.push("!"); + + SimpleStack ergebnis = Zusammenfasser2.fasseZusammen(stack1, stack2); + } +} diff --git a/src/main/java/pr2/generics/methoden/Zusammenfasser1.java b/src/main/java/pr2/generics/methoden/Zusammenfasser1.java new file mode 100644 index 0000000..783000a --- /dev/null +++ b/src/main/java/pr2/generics/methoden/Zusammenfasser1.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.methoden; + +public class Zusammenfasser1 { + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public static SimpleStack fasseZusammen(SimpleStack s1, SimpleStack s2) { + + SimpleStack ergebnis = new SimpleStack(s1.getSize() + s2.getSize()); + + + for (int i = s1.getSize(); i >= 0; i--) { + ergebnis.push(s1.pop()); + } + + for (int i = s2.getSize(); i >= 0; i--) { + ergebnis.push(s2.pop()); + } + + return ergebnis; + } +} diff --git a/src/main/java/pr2/generics/methoden/Zusammenfasser2.java b/src/main/java/pr2/generics/methoden/Zusammenfasser2.java new file mode 100644 index 0000000..baffffe --- /dev/null +++ b/src/main/java/pr2/generics/methoden/Zusammenfasser2.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.methoden; + +public class Zusammenfasser2 { + + public static SimpleStack fasseZusammen(SimpleStack s1, SimpleStack s2) { + + SimpleStack ergebnis = new SimpleStack(s1.getSize() + s2.getSize()); + + + for (int i = s1.getSize(); i >= 0; i--) { + ergebnis.push(s1.pop()); + } + + for (int i = s2.getSize(); i >= 0; i--) { + ergebnis.push(s2.pop()); + } + + return ergebnis; + } +} diff --git a/src/main/java/pr2/generics/mitgenerics/SimpleStack.java b/src/main/java/pr2/generics/mitgenerics/SimpleStack.java new file mode 100644 index 0000000..b3af5e1 --- /dev/null +++ b/src/main/java/pr2/generics/mitgenerics/SimpleStack.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.mitgenerics; + +public class SimpleStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/generics/mitgenerics/Verwender.java b/src/main/java/pr2/generics/mitgenerics/Verwender.java new file mode 100644 index 0000000..5bcbdeb --- /dev/null +++ b/src/main/java/pr2/generics/mitgenerics/Verwender.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.mitgenerics; + +public class Verwender { + + public static void main(String[] args) { + var stack = new SimpleStack(10); + + stack.push("Hello"); + String s = stack.pop(); + System.out.println(s); + + // stack.push(42); + s = stack.pop(); + System.out.println(s); + } +} diff --git a/src/main/java/pr2/generics/ohnegenerics/SimpleStack.java b/src/main/java/pr2/generics/ohnegenerics/SimpleStack.java new file mode 100644 index 0000000..fb7d607 --- /dev/null +++ b/src/main/java/pr2/generics/ohnegenerics/SimpleStack.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.ohnegenerics; + +public class SimpleStack { + + private Object[] stack; + private int pos; + + public SimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public void push(Object o) { + stack[pos++] = o; + } + + public Object pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/generics/ohnegenerics/SimpleStackInteger.java b/src/main/java/pr2/generics/ohnegenerics/SimpleStackInteger.java new file mode 100644 index 0000000..8f76d8e --- /dev/null +++ b/src/main/java/pr2/generics/ohnegenerics/SimpleStackInteger.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.generics.ohnegenerics; + +public class SimpleStackInteger { + + private Integer[] stack; + private int pos; + + public SimpleStackInteger(int size) { + stack = new Integer[size]; + pos = 0; + } + + public void push(Integer o) { + stack[pos++] = o; + } + + public Integer pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/generics/ohnegenerics/SimpleStackString.java b/src/main/java/pr2/generics/ohnegenerics/SimpleStackString.java new file mode 100644 index 0000000..25cba0b --- /dev/null +++ b/src/main/java/pr2/generics/ohnegenerics/SimpleStackString.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.generics.ohnegenerics; + +public class SimpleStackString { + + private String[] stack; + private int pos; + + public SimpleStackString(int size) { + stack = new String[size]; + pos = 0; + } + + public void push(String o) { + stack[pos++] = o; + } + + public String pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/generics/ohnegenerics/Verwender.java b/src/main/java/pr2/generics/ohnegenerics/Verwender.java new file mode 100644 index 0000000..5e0dd2a --- /dev/null +++ b/src/main/java/pr2/generics/ohnegenerics/Verwender.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.ohnegenerics; + +public class Verwender { + + public static void main(String[] args) { + SimpleStack stack = new SimpleStack(10); + + stack.push("Hello"); + String s = (String) stack.pop(); + System.out.println(s); + + stack.push(42); + s = (String) stack.pop(); + System.out.println(s); + } +} diff --git a/src/main/java/pr2/generics/wildcard/A.java b/src/main/java/pr2/generics/wildcard/A.java new file mode 100644 index 0000000..cd56a0c --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/A.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class A { + // empty +} diff --git a/src/main/java/pr2/generics/wildcard/B.java b/src/main/java/pr2/generics/wildcard/B.java new file mode 100644 index 0000000..1f40a35 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/B.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class B extends A { + // empty +} diff --git a/src/main/java/pr2/generics/wildcard/C.java b/src/main/java/pr2/generics/wildcard/C.java new file mode 100644 index 0000000..42d654d --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/C.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class C extends B { + // empty +} diff --git a/src/main/java/pr2/generics/wildcard/Generic.java b/src/main/java/pr2/generics/wildcard/Generic.java new file mode 100644 index 0000000..9838f19 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/Generic.java @@ -0,0 +1,15 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class Generic { + + public void add(T obj) { + // empty + } + + public T get() { + return null; + } +} diff --git a/src/main/java/pr2/generics/wildcard/GenericUser.java b/src/main/java/pr2/generics/wildcard/GenericUser.java new file mode 100644 index 0000000..257d5e6 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/GenericUser.java @@ -0,0 +1,38 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class GenericUser { + + public static void main(String[] args) { + + A a = new A(); + B b = new B(); + C c = new C(); + + Generic ga = new Generic<>(); + Generic gb = new Generic<>(); + Generic gc = new Generic<>(); + + ga.add(a); ga.add(b); ga.add(c); + gb.add(b); gb.add(c); + gc.add(c); + + Generic gea = ga; + gea = gb; gea = gc; + + Generic geb = gb; + geb = gc; + + Generic gec = gc; + + Generic gsa = ga; + + Generic gsb = gb; + gsb = ga; + + Generic gsc = gc; + gsc = gb; gsc = ga; + } +} diff --git a/src/main/java/pr2/generics/wildcard/GenericUser2.java b/src/main/java/pr2/generics/wildcard/GenericUser2.java new file mode 100644 index 0000000..8265a24 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/GenericUser2.java @@ -0,0 +1,33 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.generics.wildcard; + +public class GenericUser2 { + + public void adder(Generic g) { + g.add(new C()); + g.add(new B()); + //g.add(new A()); // könnte ein Generic sein + + //B b = g.get(); // könnte ein Generic sein + } + + public void getter(Generic g) { + A a = g.get(); + B b = g.get(); + //C c = g.get(); // könnte ein Generic sein + + //g.add(new B()); // könnte ein Generic sein + } + + public void doIt() { + //adder(new Generic()); // A, B, C + adder(new Generic()); // A, B + adder(new Generic()); // A + + getter(new Generic()); // A, B, C + getter(new Generic()); // A, B + //getter(new Generic()); // A + } +} diff --git a/src/main/java/pr2/generics/wildcard/SimpleStack.java b/src/main/java/pr2/generics/wildcard/SimpleStack.java new file mode 100644 index 0000000..92abfb7 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/SimpleStack.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.wildcard; + +public class SimpleStack { + + private T[] stack; + private int pos; + + @SuppressWarnings("unchecked") + public SimpleStack(int size) { + stack = (T[]) new Object[size]; + pos = 0; + } + + public void push(T o) { + stack[pos++] = o; + } + + public T pop() { + return stack[--pos]; + } + + public int getSize() { + return pos - 1; + } +} diff --git a/src/main/java/pr2/generics/wildcard/StackPrinter.java b/src/main/java/pr2/generics/wildcard/StackPrinter.java new file mode 100644 index 0000000..dc62d84 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/StackPrinter.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.wildcard; + +public class StackPrinter { + + public void printStack(SimpleStack stack) { + + for (int i = stack.getSize(); i >= 0; i--) { + System.out.printf("%d %s%n", i, stack.pop()); + } + } +} diff --git a/src/main/java/pr2/generics/wildcard/Verwender.java b/src/main/java/pr2/generics/wildcard/Verwender.java new file mode 100644 index 0000000..8554b33 --- /dev/null +++ b/src/main/java/pr2/generics/wildcard/Verwender.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.generics.wildcard; + +public class Verwender { + + public static void main(String[] args) { + var stack = new SimpleStack(10); + + stack.push("!"); + stack.push("World"); + stack.push("Hello"); + + StackPrinter printer = new StackPrinter(); + printer.printStack(stack); + + SimpleStack wildcard1 = new SimpleStack(10); + // SimpleStack wildcard2 = new SimpleStack(10); + } +} diff --git a/src/main/java/pr2/io/console/Echo.java b/src/main/java/pr2/io/console/Echo.java new file mode 100644 index 0000000..e904b15 --- /dev/null +++ b/src/main/java/pr2/io/console/Echo.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.console; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Echo { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader( + new InputStreamReader(System.in)); + + String line; + + while ((line = br.readLine()) != null) { + System.out.println(line); + } + } +} diff --git a/src/main/java/pr2/io/console/Einzelzeichen.java b/src/main/java/pr2/io/console/Einzelzeichen.java new file mode 100644 index 0000000..d20a160 --- /dev/null +++ b/src/main/java/pr2/io/console/Einzelzeichen.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.console; + +import java.io.IOException; + +public class Einzelzeichen { + + public static void main(String[] args) throws IOException { + + while (true) { + System.out.println(System.in.read()); + } + } +} diff --git a/src/main/java/pr2/io/console/Umleitung.java b/src/main/java/pr2/io/console/Umleitung.java new file mode 100644 index 0000000..939889d --- /dev/null +++ b/src/main/java/pr2/io/console/Umleitung.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.console; + +import java.io.FileNotFoundException; +import java.io.PrintStream; + +public class Umleitung { + + public static void main(String[] args) throws FileNotFoundException { + + PrintStream ps = new PrintStream("/tmp/umleitung.txt"); + System.setOut(ps); + System.out.println("Hallo, das kommt ja gar nicht raus..."); + ps.close(); + } +} diff --git a/src/main/java/pr2/io/file/CreateDir.java b/src/main/java/pr2/io/file/CreateDir.java new file mode 100644 index 0000000..9105f2c --- /dev/null +++ b/src/main/java/pr2/io/file/CreateDir.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.file; + +import java.io.File; +import java.io.IOException; + +public class CreateDir { + + public static void main(String[] args) throws IOException { + + File[] roots = File.listRoots(); + File tmpDir = new File(roots[0], "tmp"); + File newDir = new File(tmpDir, "new"); + File subDir = new File(newDir, "subdir"); + File txtFile = new File(subDir, "test.txt"); + subDir.mkdirs(); + txtFile.createNewFile(); + + + + + } +} diff --git a/src/main/java/pr2/io/file/DirLister.java b/src/main/java/pr2/io/file/DirLister.java new file mode 100644 index 0000000..66bdac2 --- /dev/null +++ b/src/main/java/pr2/io/file/DirLister.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.file; + +import java.io.File; + +public class DirLister { + + public static void main(String[] args) { + + File tempDir = new File("/"); + + File[] filesInTemp = tempDir.listFiles(); + + for (File file : filesInTemp) { + System.out.printf(file.isDirectory() ? "d" : "-"); + System.out.printf(file.canRead() ? "r" : "-"); + System.out.printf(file.canWrite() ? "w" : "-"); + System.out.printf(file.canExecute() ? "x" : "-"); + System.out.printf(file.isHidden() ? "h" : "-"); + System.out.printf(" %,10d ", file.length()); + System.out.printf(" %1$tY-%1$tm-%1$td ", file.lastModified()); + System.out.printf(" %-18s%n", file.getName()); + } + } +} diff --git a/src/main/java/pr2/io/nio/FileReadExample.java b/src/main/java/pr2/io/nio/FileReadExample.java new file mode 100644 index 0000000..4f0a1e2 --- /dev/null +++ b/src/main/java/pr2/io/nio/FileReadExample.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.nio; + +import java.io.FileInputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +public class FileReadExample { + + public static void main(String[] args) throws Exception { + FileInputStream fis = new FileInputStream("/tmp/test.txt"); + FileChannel fc = fis.getChannel(); + + ByteBuffer buffer = ByteBuffer.allocate(1024); + + fc.read(buffer); + + fis.close(); + } +} diff --git a/src/main/java/pr2/io/nio/FileWriteExample.java b/src/main/java/pr2/io/nio/FileWriteExample.java new file mode 100644 index 0000000..1307c07 --- /dev/null +++ b/src/main/java/pr2/io/nio/FileWriteExample.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.nio; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +public class FileWriteExample { + + public static void main(String[] args) throws IOException { + + FileOutputStream fos = new FileOutputStream("/tmp/output.txt"); + FileChannel fc = fos.getChannel(); + + ByteBuffer buffer = ByteBuffer.allocate(128); + System.out.println(buffer.position()); + System.out.println(buffer.limit()); + + buffer.put("Hello World\n".getBytes()); + buffer.flip(); + fc.write(buffer); + + fos.close(); + + } +} diff --git a/src/main/java/pr2/io/nio/KopierprogrammNIO.java b/src/main/java/pr2/io/nio/KopierprogrammNIO.java new file mode 100644 index 0000000..c10a12c --- /dev/null +++ b/src/main/java/pr2/io/nio/KopierprogrammNIO.java @@ -0,0 +1,43 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.nio; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +public class KopierprogrammNIO { + + public static void main(String[] args) throws Exception { + + // Quell und Zieldatei + String quelle = "/tmp/test.txt"; + String ziel = "/tmp/ziel.txt"; + + // Streams für Quell und Zieldatei + FileInputStream in = new FileInputStream(quelle); + FileOutputStream out = new FileOutputStream(ziel); + + // Channels für die Dateien + FileChannel fcin = in.getChannel(); + FileChannel fcout = out.getChannel(); + + // Puffer für Dateiinhalt + ByteBuffer buffer = ByteBuffer.allocateDirect(1024); + + // Daten aus Quell- in Zieldatei kopieren + while (fcin.read(buffer) >= 0) { + buffer.flip(); + fcout.write(buffer); + buffer.clear(); + } + + // Streams schließen (channels werden automatisch auch geschlossen) + in.close(); + out.close(); + + + } +} diff --git a/src/main/java/pr2/io/nio/KopierprogrammNIODirectBuffer.java b/src/main/java/pr2/io/nio/KopierprogrammNIODirectBuffer.java new file mode 100644 index 0000000..3493a12 --- /dev/null +++ b/src/main/java/pr2/io/nio/KopierprogrammNIODirectBuffer.java @@ -0,0 +1,40 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.nio; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +public class KopierprogrammNIODirectBuffer { + + public static void main(String[] args) throws Exception { + + // Quell und Zieldatei + String quelle = "/tmp/test.txt"; + String ziel = "/tmp/ziel.txt"; + + // Streams für Quell und Zieldatei + FileInputStream in = new FileInputStream(quelle); + FileOutputStream out = new FileOutputStream(ziel); + + // Channels für die Dateien + FileChannel fcin = in.getChannel(); + FileChannel fcout = out.getChannel(); + + ByteBuffer buffer = ByteBuffer.allocate(1024); // Puffer für Dateiinhalt + + // Daten aus Quell- in Zieldatei kopieren + while (fcin.read(buffer) >= 0) { + buffer.flip(); + fcout.write(buffer); + buffer.clear(); + } + + // Streams schließen (channels werden automatisch auch geschlossen) + in.close(); + out.close(); + } +} diff --git a/src/main/java/pr2/io/nio/MemoryMappedFile.java b/src/main/java/pr2/io/nio/MemoryMappedFile.java new file mode 100644 index 0000000..3a4df13 --- /dev/null +++ b/src/main/java/pr2/io/nio/MemoryMappedFile.java @@ -0,0 +1,18 @@ +/* (c) 2022 Thomas Smits */ +package pr2.io.nio; + +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; + +public class MemoryMappedFile { + + public static void main(String[] args) throws Exception { + RandomAccessFile fis = new RandomAccessFile( + "/Users/thomas/Temp/bigmama", "rw"); + FileChannel fc = fis.getChannel(); + fc.map(MapMode.READ_WRITE, 0, 1024L*1024*1024*8); + fc.close(); + fis.close(); + } +} diff --git a/src/main/java/pr2/io/nio/SelectorExample.java b/src/main/java/pr2/io/nio/SelectorExample.java new file mode 100644 index 0000000..ab3f158 --- /dev/null +++ b/src/main/java/pr2/io/nio/SelectorExample.java @@ -0,0 +1,93 @@ +/* (c) 2022 Thomas Smits */ +package pr2.io.nio; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.util.Arrays; +import java.util.Iterator; +import java.util.Set; + + +public class SelectorExample { + + static final int SERVER_PORT = 9999; + + public static void main(String[] args) throws IOException { + + // Server Socket öffnen, damit sich Clients von außen verbinden können + ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); + serverSocketChannel.bind(new InetSocketAddress(SERVER_PORT)); + serverSocketChannel.configureBlocking(false); + + // Selector für die Client Sockets + Selector selector = Selector.open(); + + while (true) { + + // Server-Socket dem Selector hinzufügen, damit die Verbindungen + // von außen erkannt werden + serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); + + // auf dem Selector schlafen legen + selector.select(); + + // hier geht es erst weiter, wenn eines der Sockets aus der + // Select-Maske gefallen ist + Set selectedKeys = selector.selectedKeys(); + + Iterator keyIterator = selectedKeys.iterator(); + + while (keyIterator.hasNext()) { + + SelectionKey key = keyIterator.next(); + + if (key.isReadable()) { + // Ein Client-Socket hat Daten + SocketChannel channel = (SocketChannel) key.channel(); + + // ID des clients lesen + String clientInfo = (String) key.attachment(); + + // Daten vom Socket holen + ByteBuffer buf = ByteBuffer.allocate(1024); + channel.read(buf); + + // Daten testweise ausgeben. Hier müsste der Hand-Over + // zum verarbeitenden Thread erfolgen + System.out.println(clientInfo + " " + Arrays.toString(buf.array())); + + } else if (key.isWritable()) { + // Ein Client-Socket kann beschrieben werden + + } else if (key.isAcceptable()) { + + // für jede Client-Verbindung entsteht hier ein neuer Channel + SocketChannel channel = serverSocketChannel.accept(); + channel.configureBlocking(false); + + String clientInfo = "Client " + channel.getRemoteAddress(); + System.out.println("Anfrage von " + clientInfo); + + // Das Client-Socket muss in die Select-Maske eingetragen werden + // Ob man ein Write braucht, hängt vom Nutzungszweck an + SelectionKey clientKey = channel.register(selector, + SelectionKey.OP_READ | SelectionKey.OP_WRITE); + + // Daten an den Key hängen, damit wir das Socket später + // problemlos wieder einem Client zuordnen können. In + // echt würde man hier ein Objekt und keinen String nehmen. + clientKey.attach(clientInfo); + } + + // Key wurde berarbeitet, er kann entfernt werden + keyIterator.remove(); + } + + } + } +} diff --git a/src/main/java/pr2/io/randomaccess/Beispiel.java b/src/main/java/pr2/io/randomaccess/Beispiel.java new file mode 100644 index 0000000..555d6a6 --- /dev/null +++ b/src/main/java/pr2/io/randomaccess/Beispiel.java @@ -0,0 +1,62 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.randomaccess; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +public class Beispiel { + + static class Student { + long matrikelNr; + int note; + + public Student(long matrikelNr, int note) { + super(); + this.matrikelNr = matrikelNr; + this.note = note; + } + } + + public static void main(String[] args) throws IOException { + + List studenten = new ArrayList(); + studenten.add(new Student(100001L, 29)); + studenten.add(new Student(100002L, 30)); + studenten.add(new Student(100003L, 31)); + studenten.add(new Student(100004L, 24)); + studenten.add(new Student(100005L, 21)); + studenten.add(new Student(100006L, 17)); + studenten.add(new Student(100007L, 13)); + studenten.add(new Student(100008L, 12)); + studenten.add(new Student(100009L, 15)); + + RandomAccessFile db = new RandomAccessFile("/tmp/datenbank", "rw"); + + // Schreibe alle Matrikelnummern und Noten + for (Student student : studenten) { + db.writeLong(student.matrikelNr); + db.writeInt(student.note); + } + + long length = db.length(); + long datensatz = 0; + int besteNote = Integer.MAX_VALUE; + + // Suche die beste Note + while (datensatz + 8 < length) { + db.seek(datensatz + 8); + int note = db.readInt(); + besteNote = Math.min(note, besteNote); + datensatz += 12; + } + + db.close(); + + System.out.println("Beste Note: " + (double) besteNote / 10); + + } +} diff --git a/src/main/java/pr2/io/reader/ArrayReaderDemo.java b/src/main/java/pr2/io/reader/ArrayReaderDemo.java new file mode 100644 index 0000000..ce84e59 --- /dev/null +++ b/src/main/java/pr2/io/reader/ArrayReaderDemo.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +public class ArrayReaderDemo { + + public static void main(String[] args) throws IOException { + String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" + + "Nulla laoreet, sem vel mollis imperdiet, sapien mauris\n" + + "sollicitudin arcu, sed viverra nulla dui at est.\n" + + "Nunc est erat, semper id sollicitudin ut, pretium ac eros.\n"; + + Reader sr = new StringReader(text); + + int gelesen; + while ((gelesen = sr.read()) > -1) { + System.out.print((char) gelesen); + } + + sr.close(); + } +} diff --git a/src/main/java/pr2/io/reader/ArrayWriterDemo.java b/src/main/java/pr2/io/reader/ArrayWriterDemo.java new file mode 100644 index 0000000..18354c3 --- /dev/null +++ b/src/main/java/pr2/io/reader/ArrayWriterDemo.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.CharArrayWriter; +import java.io.PrintWriter; + +public class ArrayWriterDemo { + + public static void main(String[] args) { + + CharArrayWriter cw = new CharArrayWriter(); + PrintWriter pw = new PrintWriter(cw); + + pw.printf("%s%n", "Toller Titel"); + pw.printf("%s", "Ganz viel Body"); + + pw.close(); + + String webseite = cw.toString(); + + System.out.println(webseite); + } +} diff --git a/src/main/java/pr2/io/reader/InputStreamReaderExample.java b/src/main/java/pr2/io/reader/InputStreamReaderExample.java new file mode 100644 index 0000000..c7bf1a4 --- /dev/null +++ b/src/main/java/pr2/io/reader/InputStreamReaderExample.java @@ -0,0 +1,41 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.Socket; + +public class InputStreamReaderExample { + + public static void main(String[] args) throws IOException { + + Socket socket = new Socket("www.web.de", 80); + OutputStream os = socket.getOutputStream(); + InputStream is = socket.getInputStream(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + Writer writer = new OutputStreamWriter(os); + + writer.write("GET / HTTP/1.0\r\n\r\n"); + writer.flush(); + + String line; + + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + + reader.close(); + writer.close(); + socket.close(); + + + } +} diff --git a/src/main/java/pr2/io/reader/LineNumberReader.java b/src/main/java/pr2/io/reader/LineNumberReader.java new file mode 100644 index 0000000..30bf3b3 --- /dev/null +++ b/src/main/java/pr2/io/reader/LineNumberReader.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class LineNumberReader { + + public static void main(String[] args) throws IOException { + + String dateiname = "/tmp/text.txt"; + + BufferedReader reader = new BufferedReader( + new FileReader(dateiname)); + + String zeile; + int nummer = 1; + while ((zeile = reader.readLine()) != null) { + System.out.printf("%03d %s%n", nummer, zeile); + nummer++; + } + + reader.close(); + } +} diff --git a/src/main/java/pr2/io/reader/ReadChar.java b/src/main/java/pr2/io/reader/ReadChar.java new file mode 100644 index 0000000..5661532 --- /dev/null +++ b/src/main/java/pr2/io/reader/ReadChar.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; + +public class ReadChar { + + public static void main(String[] args) throws IOException { + + Reader fr = new FileReader("/tmp/test.txt"); + + int daten; + + while ((daten = fr.read()) > -1) { + char c = (char) daten; + // jetzt kann man etwas sinnvolles mit den Zeichen machen, + // die aus der Datei gelesen wurden + } + + fr.close(); + + } + + public static void falsch(String[] args) throws IOException { + + FileReader fr = new FileReader("/tmp/test.txt"); + + char b; + + while ((b = (char) fr.read()) > -1) { + // jetzt kann man etwas sinnvolles mit den Zeichen machen, + // die aus der Datei gelesen wurden + } + + fr.close(); + + } +} diff --git a/src/main/java/pr2/io/reader/ReadCharArrays.java b/src/main/java/pr2/io/reader/ReadCharArrays.java new file mode 100644 index 0000000..dc58da2 --- /dev/null +++ b/src/main/java/pr2/io/reader/ReadCharArrays.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; + +public class ReadCharArrays { + + public static void main(String[] args) throws IOException { + + Reader fr = new FileReader("/tmp/test.txt"); + + char[] daten = new char[1024]; + int charactersRead; + + while ((charactersRead = fr.read(daten)) > -1) { + // jetzt kann man etwas sinnvolles mit den Zeichen machen, + // die aus der Datei gelesen wurden + } + + fr.close(); + + } +} diff --git a/src/main/java/pr2/io/reader/UpperCaseReader.java b/src/main/java/pr2/io/reader/UpperCaseReader.java new file mode 100644 index 0000000..c445b95 --- /dev/null +++ b/src/main/java/pr2/io/reader/UpperCaseReader.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.FilterReader; +import java.io.IOException; +import java.io.Reader; + +public class UpperCaseReader extends FilterReader { + + protected UpperCaseReader(Reader in) { + super(in); + } + + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + int result = super.read(cbuf, off, len); + + for (int i = off; i < off + result; i++) { + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + + return result; + } +} diff --git a/src/main/java/pr2/io/reader/UpperCaseReaderTest.java b/src/main/java/pr2/io/reader/UpperCaseReaderTest.java new file mode 100644 index 0000000..dbdfc50 --- /dev/null +++ b/src/main/java/pr2/io/reader/UpperCaseReaderTest.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class UpperCaseReaderTest { + + public static void main(String[] args) throws IOException { + + String dateiname = "/tmp/text.txt"; + + BufferedReader reader = new BufferedReader( + new UpperCaseReader( + new FileReader(dateiname))); + + String zeile; + while ((zeile = reader.readLine()) != null) { + System.out.println(zeile); + } + + reader.close(); + } +} diff --git a/src/main/java/pr2/io/reader/WriteChar.java b/src/main/java/pr2/io/reader/WriteChar.java new file mode 100644 index 0000000..f274ab7 --- /dev/null +++ b/src/main/java/pr2/io/reader/WriteChar.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; + +public class WriteChar { + + public static void main(String[] args) throws IOException { + + Writer fw = new FileWriter("/tmp/mytext"); + + fw.write('T'); + fw.write('e'); + fw.write('x'); + fw.write('t'); + fw.write('\n'); + + fw.close(); + + } +} diff --git a/src/main/java/pr2/io/reader/WriteCharArrays.java b/src/main/java/pr2/io/reader/WriteCharArrays.java new file mode 100644 index 0000000..10e0725 --- /dev/null +++ b/src/main/java/pr2/io/reader/WriteCharArrays.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.reader; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; + +public class WriteCharArrays { + + public static void main(String[] args) throws IOException { + + Writer fw = new FileWriter("/tmp/mytext"); + + String daten = "Dies ist ein Text"; + + fw.write(daten); + fw.write(daten, 0, 5); + fw.write(daten, 0, 5); + fw.write(daten, 12, 5); + fw.write(daten, 12, 5); + + fw.write("\n"); + + fw.close(); + + } +} diff --git a/src/main/java/pr2/io/serialization/Manager.java b/src/main/java/pr2/io/serialization/Manager.java new file mode 100644 index 0000000..b274d3e --- /dev/null +++ b/src/main/java/pr2/io/serialization/Manager.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization; + +import java.io.Serializable; +import java.util.Date; + +public class Manager extends Mitarbeiter implements Serializable { + public String abteilung; + + public Manager(String name, double gehalt, Date geburtsdatum, String abteilung) { + super(name, gehalt, geburtsdatum); + this.abteilung = abteilung; + } + + public String getDetails() { + return super.getDetails() + + " und leitet die Abteilung " + abteilung; + } +} diff --git a/src/main/java/pr2/io/serialization/Mitarbeiter.java b/src/main/java/pr2/io/serialization/Mitarbeiter.java new file mode 100644 index 0000000..0d84574 --- /dev/null +++ b/src/main/java/pr2/io/serialization/Mitarbeiter.java @@ -0,0 +1,37 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization; + +import java.io.Serializable; +import java.util.Date; + +public class Mitarbeiter implements Serializable { + + public String name; + public double gehalt; + public Date geboren; + + public Mitarbeiter(String name, double gehalt, Date geboren) { + this.name = name; + this.gehalt = gehalt; + this.geboren = geboren; + } + + public Mitarbeiter(String name, double gehalt) { + this(name, gehalt, null); + } + + public Mitarbeiter(String name) { + this(name, 0.0d, null); + } + /* + public Mitarbeiter() { + + } + */ + public String getDetails() { + return name + ", geb. am " + geboren + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/io/serialization/SimpleExample.java b/src/main/java/pr2/io/serialization/SimpleExample.java new file mode 100644 index 0000000..d80db24 --- /dev/null +++ b/src/main/java/pr2/io/serialization/SimpleExample.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.Date; + +public class SimpleExample { + + public static void main(String[] args) throws IOException, ClassNotFoundException { + + Manager m = new Manager("Hans Alberts", 10000.0, + new Date(0x2222222222L), "Buchhaltung"); + + ObjectOutputStream os = new ObjectOutputStream( + new FileOutputStream("/tmp/albert.ser")); + + os.writeObject(m); + os.close(); + + ObjectInputStream is = new ObjectInputStream( + new FileInputStream("/tmp/albert.ser")); + + Manager albert = (Manager) is.readObject(); + + is.close(); + + System.out.println(albert.getDetails()); + + } +} diff --git a/src/main/java/pr2/io/serialization/readobject/Liste.java b/src/main/java/pr2/io/serialization/readobject/Liste.java new file mode 100644 index 0000000..52610fc --- /dev/null +++ b/src/main/java/pr2/io/serialization/readobject/Liste.java @@ -0,0 +1,77 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.readobject; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +public class Liste implements Serializable { + + class Element implements Serializable { + + Element next; + String daten; + + Element(String daten) { + this.daten = daten; + } + } + + transient Element erstes; + transient Element aktuelles; + + public Liste add(String daten) { + if (erstes == null) { + erstes = new Element(daten); + aktuelles = erstes; + } + else { + Element neu = new Element(daten); + aktuelles.next = neu; + aktuelles = neu; + } + + return this; + } + + @Override + public String toString() { + + if (erstes == null) { + return "null"; + } + + StringBuilder sb = new StringBuilder(); + + Element e = erstes; + + while (e != null) { + sb.append(e.daten).append(", "); + e = e.next; + } + + return sb.toString(); + } + + private void writeObject(ObjectOutputStream os) throws IOException { + os.defaultWriteObject(); + + for (Element e = erstes; e != null; e = e.next) { + os.writeUTF(e.daten); + } + os.writeUTF(""); + } + + private void readObject(ObjectInputStream is) + throws IOException, ClassNotFoundException { + is.defaultReadObject(); + String string; + + while ((string = is.readUTF()).length() > 0) { + add(string); + } + } +} diff --git a/src/main/java/pr2/io/serialization/readobject/ReadObjectDemo.java b/src/main/java/pr2/io/serialization/readobject/ReadObjectDemo.java new file mode 100644 index 0000000..454e175 --- /dev/null +++ b/src/main/java/pr2/io/serialization/readobject/ReadObjectDemo.java @@ -0,0 +1,33 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.readobject; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class ReadObjectDemo { + public static void main(String[] args) throws IOException, ClassNotFoundException { + { + Liste liste = new Liste(); + liste.add("A").add("B").add("C").add("D"); + System.out.println(liste); + + ObjectOutputStream os = new ObjectOutputStream( + new FileOutputStream("/tmp/liste.ser")); + os.writeObject(liste); + os.close(); + } + { + ObjectInputStream is = new ObjectInputStream( + new FileInputStream("/tmp/liste.ser")); + Liste liste = (Liste) is.readObject(); + is.close(); + System.out.println(liste); + } + + } +} diff --git a/src/main/java/pr2/io/serialization/readresolve/Demo.java b/src/main/java/pr2/io/serialization/readresolve/Demo.java new file mode 100644 index 0000000..3da2c5c --- /dev/null +++ b/src/main/java/pr2/io/serialization/readresolve/Demo.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.readresolve; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class Demo { + + public static void main(String[] args) throws IOException, ClassNotFoundException { + + ObjectOutputStream os = new ObjectOutputStream( + new FileOutputStream("/tmp/singleton.ser")); + + os.writeObject(TrueSingleton.getInstance()); + os.close(); + + ObjectInputStream is = new ObjectInputStream( + new FileInputStream("/tmp/singleton.ser")); + + TrueSingleton singleton = (TrueSingleton) is.readObject(); + is.close(); + + System.out.println(singleton == TrueSingleton.getInstance()); // --> false + } +} diff --git a/src/main/java/pr2/io/serialization/readresolve/TrueSingleton.java b/src/main/java/pr2/io/serialization/readresolve/TrueSingleton.java new file mode 100644 index 0000000..3e473bd --- /dev/null +++ b/src/main/java/pr2/io/serialization/readresolve/TrueSingleton.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.readresolve; + +import java.io.ObjectStreamException; +import java.io.Serializable; + +public class TrueSingleton implements Serializable { + + private static final TrueSingleton instanz = new TrueSingleton(); + + private TrueSingleton() { + // keine Instanzen zulassen + } + + public static TrueSingleton getInstance() { + return instanz; + } + + private Object readResolve() throws ObjectStreamException { + return TrueSingleton.instanz; + } +} diff --git a/src/main/java/pr2/io/serialization/trans/A.java b/src/main/java/pr2/io/serialization/trans/A.java new file mode 100644 index 0000000..9bb450c --- /dev/null +++ b/src/main/java/pr2/io/serialization/trans/A.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.trans; + +import java.io.Serializable; + +public class A implements Serializable { + + private String s1; + private String s2; + private transient String s3; + + public A(String s1, String s2, String s3) { + if ((s1 == null) || (s2 == null) || (s3 == null)) { + throw new NullPointerException(); + } + + this.s1 = s1; + this.s2 = s2; + this.s3 = s3; + } + + @Override + public String toString() { + return String.format("A [s1=%s, s2=%s, s3=%s]", s1, s2, s3); + } +} diff --git a/src/main/java/pr2/io/serialization/trans/TransientDemo.java b/src/main/java/pr2/io/serialization/trans/TransientDemo.java new file mode 100644 index 0000000..6322c1c --- /dev/null +++ b/src/main/java/pr2/io/serialization/trans/TransientDemo.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.serialization.trans; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class TransientDemo { + + public static void main(String[] args) throws IOException, ClassNotFoundException { + + // Object schreiben + ObjectOutputStream os = new ObjectOutputStream( + new FileOutputStream("/tmp/transient.ser")); + + os.writeObject(new A("String1", "String2", "String3")); + os.close(); + + // Object lesen + ObjectInputStream is = new ObjectInputStream( + new FileInputStream("/tmp/transient.ser")); + + A a = (A) is.readObject(); + is.close(); + + System.out.println(a); + } +} diff --git a/src/main/java/pr2/io/streams/AppendStream.java b/src/main/java/pr2/io/streams/AppendStream.java new file mode 100644 index 0000000..13def88 --- /dev/null +++ b/src/main/java/pr2/io/streams/AppendStream.java @@ -0,0 +1,33 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * + * + * @author Thomas Smits + */ +public class AppendStream { + + public static void main(String[] args) throws IOException { + + byte[] daten = { (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe }; + { + OutputStream fos = new FileOutputStream("/tmp/myfile"); + fos.write(daten); + fos.close(); + } + + { + OutputStream fos2 = new FileOutputStream("/tmp/myfile", true); + fos2.write(daten); + fos2.close(); + } + + } +} diff --git a/src/main/java/pr2/io/streams/BufferedFileCopy.java b/src/main/java/pr2/io/streams/BufferedFileCopy.java new file mode 100644 index 0000000..5d4ffb4 --- /dev/null +++ b/src/main/java/pr2/io/streams/BufferedFileCopy.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class BufferedFileCopy { + public static void mainX(String[] args) throws IOException { + + String quelle = "/tmp/quelle.txt"; + String ziel = "/tmp/ziel.txt"; + + InputStream in = new BufferedInputStream( + new FileInputStream(quelle)); + + OutputStream out = new BufferedOutputStream( + new FileOutputStream(ziel)); + + byte[] buffer = new byte[1024]; + int gelesen; + + while ((gelesen = in.read(buffer)) > -1) { + out.write(buffer, 0, gelesen); + } + + in.close(); + out.close(); + } +} diff --git a/src/main/java/pr2/io/streams/DataOutput.java b/src/main/java/pr2/io/streams/DataOutput.java new file mode 100644 index 0000000..466c5a9 --- /dev/null +++ b/src/main/java/pr2/io/streams/DataOutput.java @@ -0,0 +1,47 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Date; + +public class DataOutput { + + public static void main(String[] args) throws IOException { + + DataOutputStream out = new DataOutputStream( + new BufferedOutputStream( + new FileOutputStream("/tmp/daten"))); + + out.writeUTF("** Datendatei **"); + out.writeUTF("Datum"); + out.writeLong(new Date().getTime()); + out.writeUTF("PI"); + out.writeDouble(Math.PI); + + out.close(); + + DataInputStream dis = new DataInputStream( + new BufferedInputStream( + new FileInputStream("/tmp/daten"))); + + String header = dis.readUTF(); + String datumTag = dis.readUTF(); + Date datum = new Date(dis.readLong()); + String PITag = dis.readUTF(); + double pi = dis.readDouble(); + + dis.close(); + + System.out.println(header); + System.out.println(datumTag + " " + datum); + System.out.println(PITag + " " + pi); + } +} diff --git a/src/main/java/pr2/io/streams/FileCopy.java b/src/main/java/pr2/io/streams/FileCopy.java new file mode 100644 index 0000000..0f6d8e9 --- /dev/null +++ b/src/main/java/pr2/io/streams/FileCopy.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class FileCopy { + + public static void main(String[] args) throws IOException { + + // Quell und Zieldatei + String quelle = "/tmp/quelle.txt"; + String ziel = "/tmp/ziel.txt"; + + // Streams für Quell und Zieldatei + InputStream in = new FileInputStream(quelle); + OutputStream out = new FileOutputStream(ziel); + + byte[] buffer = new byte[1024]; // Puffer für Dateiinhalt + int gelesen; // Anzahl der gelesenen Bytes + + // Daten aus Quell- in Zieldatei kopieren + while ((gelesen = in.read(buffer)) > -1) { + out.write(buffer, 0, gelesen); + } + + // Streams schließen + in.close(); + out.close(); + } +} diff --git a/src/main/java/pr2/io/streams/ReadByteArrays.java b/src/main/java/pr2/io/streams/ReadByteArrays.java new file mode 100644 index 0000000..3d05324 --- /dev/null +++ b/src/main/java/pr2/io/streams/ReadByteArrays.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class ReadByteArrays { + + public static void main(String[] args) throws IOException { + + InputStream fis = new FileInputStream("/tmp/test.txt"); + + byte[] daten = new byte[1024]; + int bytesRead; + + while ((bytesRead = fis.read(daten)) > -1) { + // jetzt kann man etwas sinnvolles mit den Bytes machen, die aus + // der Datei gelesen wurden + } + + fis.close(); + + } +} diff --git a/src/main/java/pr2/io/streams/ReadBytes.java b/src/main/java/pr2/io/streams/ReadBytes.java new file mode 100644 index 0000000..3fbbee8 --- /dev/null +++ b/src/main/java/pr2/io/streams/ReadBytes.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class ReadBytes { + + public static void main(String[] args) throws IOException { + + InputStream fis = new FileInputStream("/tmp/test.txt"); + + int daten; + + while ((daten = fis.read()) > -1) { + byte b = (byte) daten; + // jetzt kann man etwas sinnvolles mit den Bytes machen, die aus + // der Datei gelesen wurden + } + + fis.close(); + + } + + public static void falsch(String[] args) throws IOException { + + FileInputStream fis = new FileInputStream("/tmp/test.txt"); + + byte b; + + while ((b = (byte) fis.read()) > -1) { + // jetzt kann man etwas sinnvolles mit den Bytes machen, die aus + // der Datei gelesen wurden + } + + fis.close(); + + } +} diff --git a/src/main/java/pr2/io/streams/SimpleHash.java b/src/main/java/pr2/io/streams/SimpleHash.java new file mode 100644 index 0000000..addc208 --- /dev/null +++ b/src/main/java/pr2/io/streams/SimpleHash.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class SimpleHash { + + public static void main(String[] args) throws IOException { + + String datei = "/tmp/quelle.txt"; + + InputStream in = new FileInputStream(datei); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + int daten; + + while ((daten = in.read()) > -1) { + out.write(daten); + } + + in.close(); + out.close(); + + byte[] bytes = out.toByteArray(); + long sum = 0; + + for (int i = 0; i < bytes.length; i++) { + sum += bytes[i]; + } + + System.out.println(sum); + } +} diff --git a/src/main/java/pr2/io/streams/WriteByteArrays.java b/src/main/java/pr2/io/streams/WriteByteArrays.java new file mode 100644 index 0000000..493b6f9 --- /dev/null +++ b/src/main/java/pr2/io/streams/WriteByteArrays.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class WriteByteArrays { + + public static void main(String[] args) throws IOException { + + OutputStream fos = new FileOutputStream("/tmp/myfile"); + + byte[] daten = { (byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe }; + + fos.write(daten); + fos.write(daten, 0, 2); + fos.write(daten, 0, 2); + fos.write(daten, 2, 2); + fos.write(daten, 2, 2); + + fos.close(); + + } +} diff --git a/src/main/java/pr2/io/streams/WriteBytes.java b/src/main/java/pr2/io/streams/WriteBytes.java new file mode 100644 index 0000000..2033e8b --- /dev/null +++ b/src/main/java/pr2/io/streams/WriteBytes.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.io.streams; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class WriteBytes { + + public static void main(String[] args) throws IOException { + + OutputStream fos = new FileOutputStream("/tmp/myfile"); + + fos.write(0xca); + fos.write(0xfe); + fos.write(0xba); + fos.write(0xbe); + + fos.close(); + + } +} diff --git a/src/main/java/pr2/junit/KomplexTest.java b/src/main/java/pr2/junit/KomplexTest.java new file mode 100644 index 0000000..fae2af9 --- /dev/null +++ b/src/main/java/pr2/junit/KomplexTest.java @@ -0,0 +1,63 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.junit; + +public class KomplexTest { + + public void testAddition() { + KomplexeZahl k1 = new KomplexeZahl(3, 2); + KomplexeZahl k2 = new KomplexeZahl(5, 5); + + if (k1.addiere(k2).equals(new KomplexeZahl(8, 7))) { + System.out.println("OK: Addition"); + } + else { + System.err.println("Fehler: Addition"); + } + } + + public void testSubtraktion() { + KomplexeZahl k1 = new KomplexeZahl(5, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 2); + + if (k1.subtrahiere(k2).equals(new KomplexeZahl(2, 3))) { + System.out.println("OK: Subtraktion"); + } + else { + System.err.println("Fehler: Subtraktion"); + } + } + + public void testMultiplikation() { + KomplexeZahl k1 = new KomplexeZahl(2, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 7); + + if (k1.multipliziere(k2).equals(new KomplexeZahl(-29, 29))) { + System.out.println("OK: Multiplikation"); + } + else { + System.err.println("Fehler: Multiplikation"); + } + } + + public void testDivision() { + KomplexeZahl k1 = new KomplexeZahl(2, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 7); + + if (k1.dividiere(k2).equals(new KomplexeZahl(41.0/58.0, 1.0/58.0))) { + System.out.println("OK: Division"); + } + else { + System.err.println("Fehler: Divison"); + } + } + + public static void main(String[] args) { + KomplexTest test = new KomplexTest(); + test.testAddition(); + test.testSubtraktion(); + test.testMultiplikation(); + test.testDivision(); + } +} diff --git a/src/main/java/pr2/junit/KomplexeZahl.java b/src/main/java/pr2/junit/KomplexeZahl.java new file mode 100644 index 0000000..7645e60 --- /dev/null +++ b/src/main/java/pr2/junit/KomplexeZahl.java @@ -0,0 +1,87 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.junit; + +public class KomplexeZahl { + + /** + * Realanteil. + */ + private double re; + + /** + * Imaginäranteil. + */ + private double im; + + /** + * Erzeugt eine neue Komplexe Zahl. + * + * @param re Realanteil + * @param im Imaginaeranteil + */ + public KomplexeZahl(double re, double im) { + this.re = re; + this.im = im; + } + + public double getRe() { + return re; + } + + public double getIm() { + return im; + } + + public KomplexeZahl addiere(KomplexeZahl z) { + return new KomplexeZahl(re + z.re, im + z.im); + } + + public KomplexeZahl subtrahiere(KomplexeZahl z) { + return new KomplexeZahl(re - z.re, im - z.im); + } + + public KomplexeZahl multipliziere(KomplexeZahl z) { + return new KomplexeZahl(re * z.re - im * z.im, re * z.im + im * z.re); + } + + public KomplexeZahl dividiere(KomplexeZahl z) { + return new KomplexeZahl( + (re * z.re + im * z.im) / (z.re * z.re + z.im * z.im), + (im * z.re - re * z.im) / (z.re * z.re + z.im * z.im)); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(im); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(re); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + KomplexeZahl other = (KomplexeZahl) obj; + if (Double.doubleToLongBits(im) != Double.doubleToLongBits(other.im)) + return false; + if (Double.doubleToLongBits(re) != Double.doubleToLongBits(other.re)) + return false; + return true; + } + + @Override + public String toString() { + return String.format("(%f, %fi)", re, im); + } +} diff --git a/src/main/java/pr2/junit/KomplexeZahlTest.java b/src/main/java/pr2/junit/KomplexeZahlTest.java new file mode 100644 index 0000000..d634175 --- /dev/null +++ b/src/main/java/pr2/junit/KomplexeZahlTest.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.junit; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class KomplexeZahlTest { + + @Test + public void testAddition() { + KomplexeZahl k1 = new KomplexeZahl(3, 2); + KomplexeZahl k2 = new KomplexeZahl(5, 5); + assertEquals(new KomplexeZahl(8, 7), k1.addiere(k2)); + } + + @Test + public void testSubtraktion() { + KomplexeZahl k1 = new KomplexeZahl(5, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 2); + assertEquals(new KomplexeZahl(2, 3), k1.subtrahiere(k2)); + } + + @Test + public void testMultiplikation() { + KomplexeZahl k1 = new KomplexeZahl(2, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 7); + assertEquals(new KomplexeZahl(-29, 29), k1.multipliziere(k2)); + } + + @Test + public void testDivision() { + KomplexeZahl k1 = new KomplexeZahl(2, 5); + KomplexeZahl k2 = new KomplexeZahl(3, 7); + assertEquals(new KomplexeZahl(41.0/58.0, 1.0/58.0), k1.dividiere(k2)); + } +} diff --git a/src/main/java/pr2/nestedclasses/MagicButton.java b/src/main/java/pr2/nestedclasses/MagicButton.java new file mode 100644 index 0000000..e62f188 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/MagicButton.java @@ -0,0 +1,53 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * + * @author Thomas Smits + */ +public class MagicButton { + + public static void main(String[] args) { + final JFrame jf = new JFrame("Isch bin magisch"); + final JPanel jp = new JPanel(); + final JButton jb = new JButton("Click me!"); + final JButton jb2 = new JButton("Don't click me!"); + final JLabel jl = new JLabel("JLO"); + + jb.addActionListener((e) -> jl.setText("Can I haz cookies")); + + jb2.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + System.out.println("Don't press any buttons"); + } + }); + + jf.addWindowListener(new WindowAdapter() { + /** + * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent) + */ + @Override + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + jp.add(jb); + jp.add(jb2); + jp.add(jl); + jf.getContentPane().add(jp); + jf.pack(); + jf.setVisible(true); + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v1/AnonymBeispiel.java b/src/main/java/pr2/nestedclasses/anonym/v1/AnonymBeispiel.java new file mode 100644 index 0000000..50bf0bc --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v1/AnonymBeispiel.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.anonym.v1; + + +public class AnonymBeispiel { + + public static void main(String[] args) { + + Object o = new Object() { + public String toString() { + return "Anonym..."; + } + }; + + System.out.println(o.toString()); + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v1/Escape.java b/src/main/java/pr2/nestedclasses/anonym/v1/Escape.java new file mode 100644 index 0000000..ea8b1ec --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v1/Escape.java @@ -0,0 +1,19 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.anonym.v1; + +public class Escape { + + public static Object escape() { + + return new Object() { + public String toString() { + return "Ich bin geflohen"; + } + }; + } + + public static void main(String[] args) { + Object o = Escape.escape(); + System.out.println(o.toString()); + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v1/Outer.java b/src/main/java/pr2/nestedclasses/anonym/v1/Outer.java new file mode 100644 index 0000000..d6299af --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v1/Outer.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.anonym.v1; + +public class Outer { + + void methode() { + Object o = new Object() { }; + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v2/Anonymous.java b/src/main/java/pr2/nestedclasses/anonym/v2/Anonymous.java new file mode 100644 index 0000000..4657729 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v2/Anonymous.java @@ -0,0 +1,44 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.anonym.v2; + +import java.util.Date; + +public class Anonymous { + + public static void main(String[] args) { + + final String welt = "Welt"; + + A a = new A("Hallo") { + + Date date = new Date(); + + { + // Konstruktor der anonymen Klasse + this.s = this.s.toUpperCase() + " " + welt; + } + + @Override + public void methode() { + System.out.println(this.s + " am " + date); + } + + }; + + a.methode(); + } +} + +class A { + String s; + + public A(String s) { + this.s = s; + } + + public void methode() { + System.out.println(s); + } +} \ No newline at end of file diff --git a/src/main/java/pr2/nestedclasses/anonym/v2/Outer.java b/src/main/java/pr2/nestedclasses/anonym/v2/Outer.java new file mode 100644 index 0000000..fa4ef4c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v2/Outer.java @@ -0,0 +1,40 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.anonym.v2; + +import java.util.Arrays; +import java.util.Comparator; + +public class Outer { + + private int i = 1; + + public void sortSpecial(String[] strings) { + + final int k = 1; + + Comparator c = new Comparator() { + + public int compare(String s1, String s2) { + int result = s1.toUpperCase().compareTo(s2.toUpperCase()); + result = result * i * k; + return result; + } + }; + + Arrays.sort(strings, c); + + Arrays.sort(strings, new Comparator() { + + public int compare(String s1, String s2) { + int result = s1.toUpperCase().compareTo(s2.toUpperCase()); + result = result * i * k; + return result; + } + }); + + Arrays.sort(strings, (s1, s2) -> s1.toUpperCase().compareTo(s2.toUpperCase())); + + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v2/Outer2.java b/src/main/java/pr2/nestedclasses/anonym/v2/Outer2.java new file mode 100644 index 0000000..33ab6ec --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v2/Outer2.java @@ -0,0 +1,33 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.anonym.v2; + +import java.util.Arrays; +import java.util.Comparator; + +public class Outer2 { + + private int i; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + public void sortSpecial(String[] strings) { + + final int k = 1; + + class DontCare implements Comparator { + + public int compare(Object o1, Object o2) { + String s1 = (String) o1; + String s2 = (String) o2; + int result = s1.toUpperCase().compareTo(s2.toUpperCase()); + result = result * i * k; + return result; + } + } + + Comparator c = new DontCare(); + + Arrays.sort(strings, c); + } +} diff --git a/src/main/java/pr2/nestedclasses/anonym/v3/WasIstDennDas.java b/src/main/java/pr2/nestedclasses/anonym/v3/WasIstDennDas.java new file mode 100644 index 0000000..e6c1a94 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/anonym/v3/WasIstDennDas.java @@ -0,0 +1,14 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.nestedclasses.anonym.v3; + +import java.util.Date; + +public class WasIstDennDas { + + public static void main(String[] args) { + Date date = new Date() {{ setTime(400000000000L); }}; + System.out.println(date); + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/anonym/A.java b/src/main/java/pr2/nestedclasses/basics/anonym/A.java new file mode 100644 index 0000000..fe7ea5c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/anonym/A.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.anonym; + +class A { + + void methode() { + Object o = new Object() { }; + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/anzahl/A.java b/src/main/java/pr2/nestedclasses/basics/anzahl/A.java new file mode 100644 index 0000000..a4be4c9 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/anzahl/A.java @@ -0,0 +1,8 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.anzahl; + +public class A { + class B { } + class C { } + class D { } +} diff --git a/src/main/java/pr2/nestedclasses/basics/arten/A.java b/src/main/java/pr2/nestedclasses/basics/arten/A.java new file mode 100644 index 0000000..942986b --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/arten/A.java @@ -0,0 +1,8 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.arten; + +class A { + abstract class B { } + interface C { } + class D { } +} diff --git a/src/main/java/pr2/nestedclasses/basics/local/A.java b/src/main/java/pr2/nestedclasses/basics/local/A.java new file mode 100644 index 0000000..6af66e7 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/local/A.java @@ -0,0 +1,8 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.local; + +class A { + void methode() { + class B { } + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/priv/A.java b/src/main/java/pr2/nestedclasses/basics/priv/A.java new file mode 100644 index 0000000..ab58450 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/priv/A.java @@ -0,0 +1,11 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.priv; + +class A { + + private int i = 12; + + class B { + private int k = i; + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/priv/C.java b/src/main/java/pr2/nestedclasses/basics/priv/C.java new file mode 100644 index 0000000..bdf73d1 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/priv/C.java @@ -0,0 +1,14 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.priv; + +class C { + + C() { + D d = new D(); + d.i = 100; + } + + class D { + private int i = 10; + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/sichtbarkeit/A.java b/src/main/java/pr2/nestedclasses/basics/sichtbarkeit/A.java new file mode 100644 index 0000000..6ac1195 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/sichtbarkeit/A.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.sichtbarkeit; + +class A { + private class B { } + class C { } + protected class D { } + public class E { } +} diff --git a/src/main/java/pr2/nestedclasses/basics/tiefe/A.java b/src/main/java/pr2/nestedclasses/basics/tiefe/A.java new file mode 100644 index 0000000..a2f6feb --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/tiefe/A.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.tiefe; + +class A { + class B { + class C { + class D { } + } + } +} diff --git a/src/main/java/pr2/nestedclasses/basics/vererbung/A.java b/src/main/java/pr2/nestedclasses/basics/vererbung/A.java new file mode 100644 index 0000000..e487608 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/vererbung/A.java @@ -0,0 +1,12 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.vererbung; + +import java.io.Serializable; +import java.util.ArrayList; + +class A { + class B { } + class C extends B { } + class D extends ArrayList { } + class E implements Serializable { } +} diff --git a/src/main/java/pr2/nestedclasses/basics/vererbunginnen/A.java b/src/main/java/pr2/nestedclasses/basics/vererbunginnen/A.java new file mode 100644 index 0000000..95eefd8 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/basics/vererbunginnen/A.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.basics.vererbunginnen; + +public class A { + class B extends A { } +} diff --git a/src/main/java/pr2/nestedclasses/callback/Berechnung.java b/src/main/java/pr2/nestedclasses/callback/Berechnung.java new file mode 100644 index 0000000..1385bb3 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/callback/Berechnung.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.callback; + +public class Berechnung { + + public static int berechne(int input1, int input2, Funktion funktion) { + int ergebnis = funktion.apply(input1, input2); + return ergebnis; + } +} diff --git a/src/main/java/pr2/nestedclasses/callback/Funktion.java b/src/main/java/pr2/nestedclasses/callback/Funktion.java new file mode 100644 index 0000000..fc748a2 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/callback/Funktion.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.callback; + +public interface Funktion { + public abstract int apply(int o1, int o2); +} diff --git a/src/main/java/pr2/nestedclasses/callback/TestBerechnung.java b/src/main/java/pr2/nestedclasses/callback/TestBerechnung.java new file mode 100644 index 0000000..7ebbb6f --- /dev/null +++ b/src/main/java/pr2/nestedclasses/callback/TestBerechnung.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.callback; + +public class TestBerechnung { + + public static void main(String[] args) { + + int ergebnis = Berechnung.berechne(7, 3, new Funktion() { + public int apply(int o1, int o2) { + return o1 - o2; + } + }); + + ergebnis = Berechnung.berechne(5, 6, new Funktion() { + public int apply(int o1, int o2) { + return o1 + o2; + } + }); + } +} diff --git a/src/main/java/pr2/nestedclasses/decompiled/A$B.java b/src/main/java/pr2/nestedclasses/decompiled/A$B.java new file mode 100644 index 0000000..42a328c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/decompiled/A$B.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.decompiled; + +public class A$B { + private int i; + private int j; + + private void methodB(String s) { + i = A.access$0(this$0) + A.access$1(this$0) + j; // i = A.this.i + A.this.j + j; + A.access$2(this$0, "Welt"); // methodA("Welt"); + } + + public A$B(A a) { + this$0 = a; + } + + final A this$0; + static void access$0(A$B b, int i) { b.i = i; } + static void access$1(A$B b, String s) { b.methodB(s); } + static int access$2(A$B b) { return b.i; } + static int access$3(A$B b) { return b.j; } +} diff --git a/src/main/java/pr2/nestedclasses/decompiled/A.java b/src/main/java/pr2/nestedclasses/decompiled/A.java new file mode 100644 index 0000000..03fca08 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/decompiled/A.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.decompiled; + +public class A { + + private int i; + private int j; + + public A() {} + + private int methodA(String s) { + A$B b = new A$B(this); // B b = new B(); + A$B.access$0(b, 5); // b.i = 5; + A$B.access$1(b, "Hallo"); // b.methodB("Hallo"); + return A$B.access$2(b) + A$B.access$3(b); // return b.i + b.j; + } + + static int access$0(A a) { return a.i; } + static int access$1(A a) { return a.j; } + static int access$2(A a, String s) { return a.methodA(s); } +} \ No newline at end of file diff --git a/src/main/java/pr2/nestedclasses/lambda/Aktion.java b/src/main/java/pr2/nestedclasses/lambda/Aktion.java new file mode 100644 index 0000000..63ff24c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Aktion.java @@ -0,0 +1,32 @@ +package pr2.nestedclasses.lambda; + +@FunctionalInterface +public interface Aktion { + public void run(String param); +} + +class Main { + public static void main(String[] args) { + // Nur ein Statement, Block unnötig + Aktion a1 = s -> System.out.println(s); + + // Mehrere Statements, Block + Aktion a2 = s -> { + System.out.println(s); + System.out.println("Nochmal" + s); + }; + + a1.run("Hallo"); + } +} + + +class X { + public static void executor(Aktion a) { + a.run("Hallo"); + } + + public static void main(String[] args) { + executor(s -> System.out.println(s)); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Berechnung.java b/src/main/java/pr2/nestedclasses/lambda/Berechnung.java new file mode 100644 index 0000000..a07c202 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Berechnung.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.lambda; + +public class Berechnung { + + public static int berechne(int input1, int input2, Funktion funktion) { + int ergebnis = funktion.apply(input1, input2); + + return ergebnis; + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Capture.java b/src/main/java/pr2/nestedclasses/lambda/Capture.java new file mode 100644 index 0000000..00068e7 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Capture.java @@ -0,0 +1,16 @@ +package pr2.nestedclasses.lambda; + +import java.util.Arrays; +import java.util.Comparator; + +public class Capture { + + public static void main(String[] args) { + int minus_eins = -1; + Comparator cmp = (a, b) -> a.compareTo(b) * minus_eins; + + String[] s = { "D", "A", "C", "B" }; + Arrays.sort(s, cmp); + System.out.println(Arrays.toString(s)); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/ComparatorFactory.java b/src/main/java/pr2/nestedclasses/lambda/ComparatorFactory.java new file mode 100644 index 0000000..3e89262 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/ComparatorFactory.java @@ -0,0 +1,20 @@ +package pr2.nestedclasses.lambda; + +import java.util.Comparator; + +public class ComparatorFactory { + + public Comparator createComparator() { + return (a, b) -> a.compareTo(b); + } + + public Comparator createComparatorClassic() { + return new Comparator() { + + @Override + public int compare(String a, String b) { + return a.compareTo(b); + } + }; + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Funktion.java b/src/main/java/pr2/nestedclasses/lambda/Funktion.java new file mode 100644 index 0000000..93e0e37 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Funktion.java @@ -0,0 +1,5 @@ +package pr2.nestedclasses.lambda; + +public interface Funktion { + public int apply(int a, int b); +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Rechner.java b/src/main/java/pr2/nestedclasses/lambda/Rechner.java new file mode 100644 index 0000000..76c9de9 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Rechner.java @@ -0,0 +1,35 @@ +package pr2.nestedclasses.lambda; + + +public class Rechner { + public static void main(String[] args) { + int ergebnis = Berechnung.berechne(5, 7, new Funktion() { + + @Override + public int apply(int a, int b) { + return a + b; + } + }); + + System.out.println(ergebnis); + + Funktion add = new Funktion() { + @Override + public int apply(int a, int b) { + return a + b; + } + }; + + Funktion sub = new Funktion() { + @Override + public int apply(int a, int b) { + return a - b; + } + }; + + int e1 = add.apply(5, 5); + int e2 = sub.apply(10, 8); + + + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/RechnerLambda.java b/src/main/java/pr2/nestedclasses/lambda/RechnerLambda.java new file mode 100644 index 0000000..f371e03 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/RechnerLambda.java @@ -0,0 +1,14 @@ +package pr2.nestedclasses.lambda; + +public class RechnerLambda { + public static void main(String[] args) { + int ergebnis = Berechnung.berechne(5, 5, (x, y) -> x + y); + System.out.println(ergebnis); + + Funktion add = (x, y) -> x + y; + Funktion sub = (x, y) -> x - y; + + int e1 = add.apply(5, 5); + int e2 = sub.apply(10, 8); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Scoping.java b/src/main/java/pr2/nestedclasses/lambda/Scoping.java new file mode 100644 index 0000000..137e943 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Scoping.java @@ -0,0 +1,17 @@ +package pr2.nestedclasses.lambda; + +public class Scoping { + + @Override + public String toString() { + return "Scoping"; + } + + public Runnable createRunner() { + return () -> System.out.println(toString()); + } + + public static void main(String[] args) { + new Scoping().createRunner().run(); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/Scoping2.java b/src/main/java/pr2/nestedclasses/lambda/Scoping2.java new file mode 100644 index 0000000..749cc35 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/Scoping2.java @@ -0,0 +1,20 @@ +package pr2.nestedclasses.lambda; + +public class Scoping2 { + + @Override + public String toString() { + return "Scoping"; + } + + public Runnable createRunner() { + return new Runnable() { + public void run() { + System.out.println(toString()); } + }; + } + + public static void main(String[] args) { + new Scoping2().createRunner().run(); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/function/Rechner.java b/src/main/java/pr2/nestedclasses/lambda/function/Rechner.java new file mode 100644 index 0000000..200f5a1 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/function/Rechner.java @@ -0,0 +1,20 @@ +package pr2.nestedclasses.lambda.function; + +import java.util.function.IntBinaryOperator; + +public class Rechner { + + public static int berechne(int input1, int input2, + IntBinaryOperator funktion) { + int ergebnis = funktion.applyAsInt(input1, input2); + return ergebnis; + } + + public static void main(String[] args) { + IntBinaryOperator sub = (a, b) -> a - b; + IntBinaryOperator add = (a, b) -> a + b; + + System.out.println(berechne(5, 3, sub)); + System.out.println(berechne(1, 7, add)); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/function/RechnerKurz.java b/src/main/java/pr2/nestedclasses/lambda/function/RechnerKurz.java new file mode 100644 index 0000000..9fb4b59 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/function/RechnerKurz.java @@ -0,0 +1,17 @@ +package pr2.nestedclasses.lambda.function; + +import java.util.function.IntBinaryOperator; + +public class RechnerKurz { + + public static int berechne(int input1, int input2, + IntBinaryOperator funktion) { + int ergebnis = funktion.applyAsInt(input1, input2); + return ergebnis; + } + + public static void main(String[] args) { + System.out.println(berechne(5, 3, (a, b) -> a - b)); + System.out.println(berechne(1, 7, (a, b) -> a + b)); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/methodref/Funktion.java b/src/main/java/pr2/nestedclasses/lambda/methodref/Funktion.java new file mode 100644 index 0000000..54d403a --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/methodref/Funktion.java @@ -0,0 +1,9 @@ +package pr2.nestedclasses.lambda.methodref; + +public interface Funktion { + public int apply(int a, int b); + + public static int rechne(int a, int b, Funktion fkt) { + return fkt.apply(a, b); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/methodref/Funktionssammlung.java b/src/main/java/pr2/nestedclasses/lambda/methodref/Funktionssammlung.java new file mode 100644 index 0000000..e49adf9 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/methodref/Funktionssammlung.java @@ -0,0 +1,12 @@ +package pr2.nestedclasses.lambda.methodref; + +public class Funktionssammlung { + + public static int add(int a, int b) { + return a + b; + } + + public static int sub(int a, int b) { + return a - b; + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/methodref/Main.java b/src/main/java/pr2/nestedclasses/lambda/methodref/Main.java new file mode 100644 index 0000000..987f353 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/methodref/Main.java @@ -0,0 +1,8 @@ +package pr2.nestedclasses.lambda.methodref; + +public class Main { + public static void main(String[] args) { + System.out.println(Funktion.rechne(5, 5, Funktionssammlung::add)); + System.out.println(Funktion.rechne(10, 8, Funktionssammlung::sub)); + } +} diff --git a/src/main/java/pr2/nestedclasses/lambda/typing/Caller.java b/src/main/java/pr2/nestedclasses/lambda/typing/Caller.java new file mode 100644 index 0000000..0864a6c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/typing/Caller.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.lambda.typing; + +public interface Caller { + public int call(); +} diff --git a/src/main/java/pr2/nestedclasses/lambda/typing/Runner.java b/src/main/java/pr2/nestedclasses/lambda/typing/Runner.java new file mode 100644 index 0000000..886c9d6 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/typing/Runner.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.lambda.typing; + +public interface Runner { + public void run(); +} diff --git a/src/main/java/pr2/nestedclasses/lambda/typing/TargetTyping.java b/src/main/java/pr2/nestedclasses/lambda/typing/TargetTyping.java new file mode 100644 index 0000000..2c70869 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/lambda/typing/TargetTyping.java @@ -0,0 +1,22 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.lambda.typing; + +public class TargetTyping { + public void doIt(Runner r) { + r.run(); + } + + public void doIt(Caller c) { + c.call(); + } + + public static void main(String[] args) { + TargetTyping t = new TargetTyping(); + + // implementiert Runner + t.doIt(() -> { return; }); + + // implementiert Caller + t.doIt(() -> { return 1; }); + } +} diff --git a/src/main/java/pr2/nestedclasses/local/Escape.java b/src/main/java/pr2/nestedclasses/local/Escape.java new file mode 100644 index 0000000..b4cfb63 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/local/Escape.java @@ -0,0 +1,20 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.local; + +public class Escape { + + public static Object escape() { + + class Papillon { + public String toString() { + return "Ich bin geflohen"; + } + } + + return new Papillon(); + } + + public static void main(String[] args) { + System.out.println(escape()); + } +} diff --git a/src/main/java/pr2/nestedclasses/local/LocalBeispiel.java b/src/main/java/pr2/nestedclasses/local/LocalBeispiel.java new file mode 100644 index 0000000..8cf2c1a --- /dev/null +++ b/src/main/java/pr2/nestedclasses/local/LocalBeispiel.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.local; + +public class LocalBeispiel { + + public static void main(String[] args) { + + class LocalClass { + public String toString() { + return "Ich bin lokal"; + } + } + + LocalClass local = new LocalClass(); + System.out.println(local.toString()); + } +} diff --git a/src/main/java/pr2/nestedclasses/local/LocalMitVariable.java b/src/main/java/pr2/nestedclasses/local/LocalMitVariable.java new file mode 100644 index 0000000..f826ef4 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/local/LocalMitVariable.java @@ -0,0 +1,19 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.local; + +public class LocalMitVariable { + + public static void main(String[] args) { + + final String ausgabe = "Ich bin lokal"; + + class LocalClass { + public String toString() { + return ausgabe; + } + } + + LocalClass local = new LocalClass(); + System.out.println(local); + } +} diff --git a/src/main/java/pr2/nestedclasses/local/LocalMitVariableUndInstanz.java b/src/main/java/pr2/nestedclasses/local/LocalMitVariableUndInstanz.java new file mode 100644 index 0000000..cce2a67 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/local/LocalMitVariableUndInstanz.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.local; + +public class LocalMitVariableUndInstanz { + + private String variable = "Ich bin nicht lokal"; + + public void doIt() { + + final String ausgabe = "Ich bin lokal"; + + class LocalClass { + public String toString() { + return ausgabe + ", " + variable; + } + } + + LocalClass local = new LocalClass(); + System.out.println(local); + } +} diff --git a/src/main/java/pr2/nestedclasses/local/Outer.java b/src/main/java/pr2/nestedclasses/local/Outer.java new file mode 100644 index 0000000..5a46e9a --- /dev/null +++ b/src/main/java/pr2/nestedclasses/local/Outer.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.local; + +public class Outer { + + void methode() { + class Inner { } + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer.java b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer.java new file mode 100644 index 0000000..6330b78 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.nonstatic.computer; + +public class Computer { + + String hersteller = "HP"; + + class Hauptspeicher { + int groesse = 1024; + } + + public static void main(String[] args) { + Computer computer = new Computer(); + Computer.Hauptspeicher h1 = computer.new Hauptspeicher(); + Computer.Hauptspeicher h2 = computer.new Hauptspeicher(); + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer2.java b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer2.java new file mode 100644 index 0000000..6c5356f --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer2.java @@ -0,0 +1,16 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.nonstatic.computer; + +public class Computer2 { + + Hauptspeicher hauptspeicher = new Hauptspeicher(); + + class Hauptspeicher { + + Speicherzelle zelle = new Speicherzelle(); + + class Speicherzelle { + + } + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer3.java b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer3.java new file mode 100644 index 0000000..b86d190 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer3.java @@ -0,0 +1,16 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.nonstatic.computer; + +public class Computer3 { + + String hersteller = "HP"; + Hauptspeicher speicher = new Hauptspeicher(); + + class Hauptspeicher { + int groesse = 1024; + + void printHersteller() { + System.out.println(Computer3.this.hersteller); + } + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer4.java b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer4.java new file mode 100644 index 0000000..f5373ae --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/computer/Computer4.java @@ -0,0 +1,34 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.nonstatic.computer; + +public class Computer4 { + + String hersteller = "HP"; + + class Hauptspeicher { + + int groesse = 1024; + + class Speicherzelle { + + int kapazitaet = 10; + + void doIt() { + System.out.println(Computer4.this.hersteller); + System.out.println(Hauptspeicher.this.groesse); + System.out.println(kapazitaet); + } + } + } +} + +class X { + public static void main(String[] args) { + Computer4 computer = new Computer4(); + Computer4.Hauptspeicher speicher = computer.new Hauptspeicher(); + Computer4.Hauptspeicher.Speicherzelle zelle = speicher.new Speicherzelle(); + + Computer4.Hauptspeicher.Speicherzelle z2 = new Computer4().new Hauptspeicher().new Speicherzelle(); + + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/computer/Outer.java b/src/main/java/pr2/nestedclasses/nonstatic/computer/Outer.java new file mode 100644 index 0000000..915a3a1 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/computer/Outer.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.nonstatic.computer; + +public class Outer { + class Inner { } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/v1/Outer.java b/src/main/java/pr2/nestedclasses/nonstatic/v1/Outer.java new file mode 100644 index 0000000..3afa43e --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/v1/Outer.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.nonstatic.v1; + +public class Outer { + + private int i = 0; + private int j = 5; + + public class Inner { + + public int doSomething(int i) { + Outer.this.i += i; + j++; + return Outer.this.i; + } + } + + public void testInner() { + Inner inner = new Inner(); + inner.doSomething(5); + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/v1/UseOuter.java b/src/main/java/pr2/nestedclasses/nonstatic/v1/UseOuter.java new file mode 100644 index 0000000..c7949ff --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/v1/UseOuter.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.nonstatic.v1; + +public class UseOuter { + + public static void main(String[] args) { + Outer outer = new Outer(); + outer.testInner(); + + Outer.Inner inner = outer.new Inner(); + inner.doSomething(10); + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/v2/Outer.java b/src/main/java/pr2/nestedclasses/nonstatic/v2/Outer.java new file mode 100644 index 0000000..fb5c20f --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/v2/Outer.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.nonstatic.v2; + +public class Outer { + + private int i; + + public void methode(int i) { + + final int j = 0; + + class Inner { + private int k; + + public void doSomething(int i) { + k = i + j + Outer.this.i; + } + } + + Inner inner = new Inner(); + inner.doSomething(i); + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/v2/UseOuter.java b/src/main/java/pr2/nestedclasses/nonstatic/v2/UseOuter.java new file mode 100644 index 0000000..16183de --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/v2/UseOuter.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.nonstatic.v2; + +public class UseOuter { + + public static void main(String[] args) { + Outer outer = new Outer(); + outer.methode(5); + } +} diff --git a/src/main/java/pr2/nestedclasses/nonstatic/v3/A.java b/src/main/java/pr2/nestedclasses/nonstatic/v3/A.java new file mode 100644 index 0000000..c7ce8c4 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/nonstatic/v3/A.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.nonstatic.v3; + +public class A { + + private int i; + private int j; + + private class B { + private int i; + private int j; + + private void methodB(String s) { + i = A.this.i + A.this.j + j; + methodA("Welt"); + } + } + + private int methodA(String s) { + B b = new B(); + b.i = 5; + b.methodB("Hallo"); + return b.i + b.j; + } +} \ No newline at end of file diff --git a/src/main/java/pr2/nestedclasses/quiz/Outer.java b/src/main/java/pr2/nestedclasses/quiz/Outer.java new file mode 100644 index 0000000..6358285 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/quiz/Outer.java @@ -0,0 +1,6 @@ +package pr2.nestedclasses.quiz; + +public class Outer { + public class Inner1 extends Outer {} + // public class Inner2 extends Inner1 {} +} diff --git a/src/main/java/pr2/nestedclasses/quiz/Twister.java b/src/main/java/pr2/nestedclasses/quiz/Twister.java new file mode 100644 index 0000000..ad7bf26 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/quiz/Twister.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.quiz; + +public class Twister { + + private final String name; + + Twister(String name) { this.name = name; } + + private String name() { return name; } + + private void reproduce() { + new Twister("reproduce") { + void printName() { + System.out.println(name()); + } + }.printName(); + } + + public static void main(String[] args) { + new Twister("main").reproduce(); + } +} diff --git a/src/main/java/pr2/nestedclasses/statisch/v1/Outer.java b/src/main/java/pr2/nestedclasses/statisch/v1/Outer.java new file mode 100644 index 0000000..91a0a4e --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v1/Outer.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.statisch.v1; + +public class Outer { + static class Inner { } +} diff --git a/src/main/java/pr2/nestedclasses/statisch/v1/StaticMember.java b/src/main/java/pr2/nestedclasses/statisch/v1/StaticMember.java new file mode 100644 index 0000000..6d9af04 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v1/StaticMember.java @@ -0,0 +1,25 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.statisch.v1; + +public class StaticMember { + + public static class InnereKlasse { + + @Override + public String toString() { + return "Ich bin innen"; + } + } + + @Override + public String toString() { + return "Ich bin außen"; + } + + public static void main(String[] args) { + StaticMember aeussere = new StaticMember(); + StaticMember.InnereKlasse innere = new StaticMember.InnereKlasse(); + System.out.println(aeussere); // -> "Ich bin außen" + System.out.println(innere); // -> "Ich bin innen" + } +} diff --git a/src/main/java/pr2/nestedclasses/statisch/v2/Outer.java b/src/main/java/pr2/nestedclasses/statisch/v2/Outer.java new file mode 100644 index 0000000..6f1313c --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v2/Outer.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.statisch.v2; + +public class Outer { + + private static int CONSTANT = 15; + + public static class Inner { + + public static final double PI = 3.14159265; + + public int doSomething(int i) { + return i * CONSTANT; + } + } +} diff --git a/src/main/java/pr2/nestedclasses/statisch/v2/PrivaterZugriff.java b/src/main/java/pr2/nestedclasses/statisch/v2/PrivaterZugriff.java new file mode 100644 index 0000000..f7b4661 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v2/PrivaterZugriff.java @@ -0,0 +1,25 @@ +/* (c) 2022 Thomas Smits */ +package pr2.nestedclasses.statisch.v2; + +public class PrivaterZugriff { + + private static int a = 5; + + static class InnereKlasse { + + private static int b = 7; + + private static void innen() { + b = a; + aussen2(); + } + } + + private static void aussen1() { + InnereKlasse.innen(); + } + + private static void aussen2() { + InnereKlasse.b = 17; + } +} \ No newline at end of file diff --git a/src/main/java/pr2/nestedclasses/statisch/v2/UseOuter.java b/src/main/java/pr2/nestedclasses/statisch/v2/UseOuter.java new file mode 100644 index 0000000..8286e8d --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v2/UseOuter.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.statisch.v2; + +public class UseOuter { + + public static void main(String[] args) { + Outer.Inner inner = new Outer.Inner(); + inner.doSomething(15); + double a = 2 * Outer.Inner.PI * 5.0 * 5.0; + } +} diff --git a/src/main/java/pr2/nestedclasses/statisch/v2/Zugriff.java b/src/main/java/pr2/nestedclasses/statisch/v2/Zugriff.java new file mode 100644 index 0000000..ed51110 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/statisch/v2/Zugriff.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.statisch.v2; + +public class Zugriff { + + static class Inner1 { + private int i; + + private void doIt(Inner2 inner2) { + inner2.k++; + } + } + + static class Inner2 { + private int k; + + private void doIt(Inner1 inner1) { + inner1.i++; + } + } + + public void doIt(Inner1 i1, Inner2 i2) { + i1.i = i2.k; + i1.doIt(i2); + i2.doIt(i1); + } +} diff --git a/src/main/java/pr2/nestedclasses/vererbung/Outer.java b/src/main/java/pr2/nestedclasses/vererbung/Outer.java new file mode 100644 index 0000000..2879417 --- /dev/null +++ b/src/main/java/pr2/nestedclasses/vererbung/Outer.java @@ -0,0 +1,37 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.nestedclasses.vererbung; + +public class Outer { + + protected String s; + private String t; + + protected void methode() { + System.out.println("Outer:methode()"); + } + + public class Inner extends Outer { + + protected void methode() { + System.out.println("Inner:methode()"); + } + + public void doIt() { + s = "geerbter String"; + Outer.this.s = "String von außen"; + t = "nicht über Vererbung sichtbar"; + Outer.this.methode(); + methode(); + } + } + + public static void main(String[] args) { + Outer o = new Outer(); + Outer.Inner i = o.new Inner(); + i.doIt(); + System.out.println(o.s); + System.out.println(i.s); + } +} \ No newline at end of file diff --git a/src/main/java/pr2/oo1/konstruktor/hierarchie/A.java b/src/main/java/pr2/oo1/konstruktor/hierarchie/A.java new file mode 100644 index 0000000..22aff5b --- /dev/null +++ b/src/main/java/pr2/oo1/konstruktor/hierarchie/A.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.konstruktor.hierarchie; + +public class A extends Object { + + protected int i; + + public A(int i) { + this.i = i; + } + +} diff --git a/src/main/java/pr2/oo1/konstruktor/hierarchie/B.java b/src/main/java/pr2/oo1/konstruktor/hierarchie/B.java new file mode 100644 index 0000000..b88062c --- /dev/null +++ b/src/main/java/pr2/oo1/konstruktor/hierarchie/B.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.konstruktor.hierarchie; + +public class B extends A { + + protected int k; + + public B(int k) { + this(k, 0); + } + + public B(int k, int i) { + super(i); + } +} diff --git a/src/main/java/pr2/oo1/konstruktor/probleme/A.java b/src/main/java/pr2/oo1/konstruktor/probleme/A.java new file mode 100644 index 0000000..6f54b52 --- /dev/null +++ b/src/main/java/pr2/oo1/konstruktor/probleme/A.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.konstruktor.probleme; + +public class A { + + protected String name; + + public A() { + name = getDetails(); + } + + protected String getDetails() { + return "Hans"; + } +} diff --git a/src/main/java/pr2/oo1/konstruktor/probleme/B.java b/src/main/java/pr2/oo1/konstruktor/probleme/B.java new file mode 100644 index 0000000..667001b --- /dev/null +++ b/src/main/java/pr2/oo1/konstruktor/probleme/B.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.konstruktor.probleme; + +public class B extends A { + + protected String info = "Eine Information"; + + public B() { + } + + protected String getDetails() { + return name + info.length(); + } + + public static void main(String[] args) { + B b = new B(); + System.out.println(b.getDetails()); + } +} diff --git a/src/main/java/pr2/oo1/object/clone/CloneDemo.java b/src/main/java/pr2/oo1/object/clone/CloneDemo.java new file mode 100644 index 0000000..a04af66 --- /dev/null +++ b/src/main/java/pr2/oo1/object/clone/CloneDemo.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.clone; + +public class CloneDemo { + + public static void main(String[] args) throws CloneNotSupportedException { + Mitarbeiter m1 = new Mitarbeiter("Heinz", 5000.00, null); + Mitarbeiter m2 = (Mitarbeiter) m1.clone(); + + System.out.println(m1 == m2); + System.out.println(m2.name); + System.out.println(m2.gehalt); + } +} diff --git a/src/main/java/pr2/oo1/object/clone/Mitarbeiter.java b/src/main/java/pr2/oo1/object/clone/Mitarbeiter.java new file mode 100644 index 0000000..31a11fb --- /dev/null +++ b/src/main/java/pr2/oo1/object/clone/Mitarbeiter.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.clone; + +import java.util.Date; + +public class Mitarbeiter implements Cloneable { + public String name; + public double gehalt; + public Date geburtsdatum; + + public Mitarbeiter(String name, double gehalt, Date geburtsdatum) { + this.name = name; + this.gehalt = gehalt; + this.geburtsdatum = geburtsdatum; + } + + protected Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + throw new AssertionError("Cannot happen"); + } + } +} diff --git a/src/main/java/pr2/oo1/object/clone2/Main.java b/src/main/java/pr2/oo1/object/clone2/Main.java new file mode 100644 index 0000000..2819b80 --- /dev/null +++ b/src/main/java/pr2/oo1/object/clone2/Main.java @@ -0,0 +1,29 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo1.object.clone2; + +/** + * + * + * @author Thomas Smits + */ +public class Main { + + public static void main(String[] args) throws CloneNotSupportedException { + Schaf dolly = new Schaf("Dolly"); + Schaf shawn = new Schaf("Shawn"); + dolly.setFreund(shawn); + shawn.setFreund(dolly); + + + Schaf dolly2 = (Schaf) dolly.clone(); + dolly2.ohrmarkennummer = 42; + + System.out.println(dolly); + System.out.println("Ist befreundet mit " + dolly.getFreund()); + System.out.println("Cloning in Progress... all your sheep belong to us"); + System.out.println(dolly2); + System.out.println("Ist befreundet mit " + dolly2.getFreund()); + + + } +} diff --git a/src/main/java/pr2/oo1/object/clone2/Schaf.java b/src/main/java/pr2/oo1/object/clone2/Schaf.java new file mode 100644 index 0000000..65e4c88 --- /dev/null +++ b/src/main/java/pr2/oo1/object/clone2/Schaf.java @@ -0,0 +1,48 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo1.object.clone2; + +import java.util.Random; + +/** + * + * + * @author Thomas Smits + */ +public class Schaf implements Cloneable { + + public static int schafCounter = 0; + + private String name; + private Schaf freund; + public int ohrmarkennummer = new Random().nextInt(10000); + + public Schaf(String name) { + this.name = name; + System.out.printf("Konstruktor gerufen für Schaf %s%n", name); + } + + public void setFreund(Schaf freund) { + this.freund = freund; + } + + public Schaf getFreund() { + return freund; + } + + @Override + public String toString() { + return String.format("Ich heisse %s (Nummer %d, code %d), " + + "mein Freund heisst %s", + name, ohrmarkennummer, System.identityHashCode(this), + freund != null ? freund.name : "Du hast keine Freunde!!!!"); + } + + @Override + protected Object clone() throws CloneNotSupportedException { + schafCounter++; + System.out.println(schafCounter); + Schaf miniMi = (Schaf) super.clone(); + miniMi.freund = ((freund == null) ? null : (Schaf) freund.clone()); + return miniMi; + } +} diff --git a/src/main/java/pr2/oo1/object/equals/EqualsDemo.java b/src/main/java/pr2/oo1/object/equals/EqualsDemo.java new file mode 100644 index 0000000..68d1ef4 --- /dev/null +++ b/src/main/java/pr2/oo1/object/equals/EqualsDemo.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.equals; + +public class EqualsDemo { + + public static void main(String[] args) { + MeinDatum d1 = new MeinDatum(5, 12, 2009); + MeinDatum d2 = new MeinDatum(5, 12, 2009); + MeinDatum d3 = new MeinDatum(3, 7, 1970); + + System.out.println(d1 == d2); + System.out.println(d1.equals(d2)); + System.out.println(d1.equals(d3)); + + d2 = d1; + + System.out.println(d1 == d2); + } +} diff --git a/src/main/java/pr2/oo1/object/equals/MeinDatum.java b/src/main/java/pr2/oo1/object/equals/MeinDatum.java new file mode 100644 index 0000000..943387a --- /dev/null +++ b/src/main/java/pr2/oo1/object/equals/MeinDatum.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.equals; + +public class MeinDatum { + int tag; + int monat; + int jahr; + + public MeinDatum(int tag, int monat, int jahr) { + this.tag = tag; + this.monat = monat; + this.jahr = jahr; + } + + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + jahr; + result = prime * result + monat; + result = prime * result + tag; + return result; + } + + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MeinDatum other = (MeinDatum) obj; + if (jahr != other.jahr) + return false; + if (monat != other.monat) + return false; + if (tag != other.tag) + return false; + return true; + } +} diff --git a/src/main/java/pr2/oo1/object/equals/Mitarbeiter.java b/src/main/java/pr2/oo1/object/equals/Mitarbeiter.java new file mode 100644 index 0000000..aa64ff8 --- /dev/null +++ b/src/main/java/pr2/oo1/object/equals/Mitarbeiter.java @@ -0,0 +1,62 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.equals; + +import java.util.Date; + +public class Mitarbeiter { + + public String name; + public double gehalt; + public Date geburtsdatum; + + public Mitarbeiter(String name, double gehalt, Date geburtsdatum) { + super(); + this.name = name; + this.gehalt = gehalt; + this.geburtsdatum = geburtsdatum; + } + + public boolean equals(Object o) { + + if (o == null) { + return false; + } + else if (o == this) { + return true; + } + else if (o instanceof Mitarbeiter) { + Mitarbeiter mi = (Mitarbeiter) o; + + return mi.name.equals(name) + && mi.gehalt == gehalt + && mi.geburtsdatum.equals(geburtsdatum); + } + else { + return false; + } + } + + public int hashCode() { + return name.hashCode() + ^ (int) Double.doubleToLongBits(gehalt) + ^ geburtsdatum.hashCode(); + } + + public void doIt(Mitarbeiter m) { + System.out.println(m + " hat ein Gehalt von " + gehalt); + System.out.println(m.toString() + " hat ein Gehalt von " + Double.toString(gehalt)); + } + + public String toString() { + return "Mitarbeiter [name: " + name + ", " + + "gehalt: " + gehalt + ", " + + "geburtsdatum " + geburtsdatum + "]"; + } + + public static void main(String[] args) { + Mitarbeiter m = new Mitarbeiter("Franz Meier", 5000.00, new Date(new Date().getTime() - 1000L*1000*1000*1000)); + m.doIt(m); + } +} diff --git a/src/main/java/pr2/oo1/object/intern/InternDemo.java b/src/main/java/pr2/oo1/object/intern/InternDemo.java new file mode 100644 index 0000000..2aa514b --- /dev/null +++ b/src/main/java/pr2/oo1/object/intern/InternDemo.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.object.intern; + +public class InternDemo { + public static void main(String[] args) { + String s1 = "Hallo"; + String s2 = "Hallo"; + String s3 = new String("Hallo"); + String s4 = s3.intern(); + + System.out.println(s1 == s2); // -> true + System.out.println(s1 == s3); // -> false + System.out.println(s1 == s4); // -> true + + String s = ""; + + if ((s != null) && (s.equals("text"))) {} + if ("text".equals(s)) {} + } +} diff --git a/src/main/java/pr2/oo1/quiz/binding/BindingQuiz.java b/src/main/java/pr2/oo1/quiz/binding/BindingQuiz.java new file mode 100644 index 0000000..15a9b54 --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/binding/BindingQuiz.java @@ -0,0 +1,31 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.binding; + +class A { +} + +class B extends A { +} + +public class BindingQuiz { + + public static void f(A a) { + System.out.println("A"); + } + + public static void f(B b) { + System.out.println("B"); + } + + public static void main(String[] args) { + A a1 = new A(); + A a2 = new B(); + B b = new B(); + + f(a1); + f(a2); + f(b); + } +} diff --git a/src/main/java/pr2/oo1/quiz/boxing/BoxingQuiz.java b/src/main/java/pr2/oo1/quiz/boxing/BoxingQuiz.java new file mode 100644 index 0000000..1f7eccb --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/boxing/BoxingQuiz.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.boxing; + +public class BoxingQuiz { + + public static void main(String[] args) { + Long l1 = 150L; + Long l2 = 200L; + + l2 = l2 - 50; + + System.out.println(l1 == l2); + } +} diff --git a/src/main/java/pr2/oo1/quiz/constructor/ConstructorQuiz.java b/src/main/java/pr2/oo1/quiz/constructor/ConstructorQuiz.java new file mode 100644 index 0000000..43a4b49 --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/constructor/ConstructorQuiz.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.constructor; + +class A { + public A() { System.out.println("A"); } +} + +class B extends A { + public B() { System.out.println("B"); } +} + +public class ConstructorQuiz { + + public static void main(String[] args) { + new B(); + } +} diff --git a/src/main/java/pr2/oo1/quiz/constructor/NameHolder.java b/src/main/java/pr2/oo1/quiz/constructor/NameHolder.java new file mode 100644 index 0000000..0aff6fa --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/constructor/NameHolder.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.constructor; + +public class NameHolder { + + private String name; + + public void NameHolder() { + name = "Hallo"; + } + + @Override + public String toString() { + return name; + } + + public static void main(String[] args) { + NameHolder s = new NameHolder(); + System.out.println(s); + } +} diff --git a/src/main/java/pr2/oo1/quiz/equals/A.java b/src/main/java/pr2/oo1/quiz/equals/A.java new file mode 100644 index 0000000..1caa7a7 --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/equals/A.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.equals; + +public class A { + + public int i; + + public boolean equals(A obj) { + return ((obj != null) + && (getClass() == obj.getClass()) + && (i == ((A) obj).i)); + } + + public int hashCode() { + return i; + } + + public static void main(String[] args) { + A a1 = new A(); + a1.i = 5; + + A a2 = new A(); + a2.i = 5; + + Object o = a2; + + System.out.println("a1.equals(a2): " + a1.equals(a2)); + System.out.println("a1.equals(o): " + a1.equals(o)); + } +} diff --git a/src/main/java/pr2/oo1/quiz/equals2/A.java b/src/main/java/pr2/oo1/quiz/equals2/A.java new file mode 100644 index 0000000..2bd2fe7 --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/equals2/A.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.equals2; + +public class A { + + public int i; + + public boolean equals(Object obj) { + return ((obj != null) + && (getClass() == obj.getClass()) + && (i == ((A) obj).i)); + } + + public int hashCode() { + return i; + } + + public static void main(String[] args) { + A a1 = new A(); + a1.i = 5; + + A a2 = new A(); + a2.i = 5; + + Object o = a2; + + System.out.println("a1.equals(a2): " + a1.equals(a2)); + System.out.println("a1.equals(o): " + a1.equals(o)); + } +} diff --git a/src/main/java/pr2/oo1/quiz/polymorphie/Polymorphie.java b/src/main/java/pr2/oo1/quiz/polymorphie/Polymorphie.java new file mode 100644 index 0000000..09fc3cc --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/polymorphie/Polymorphie.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.polymorphie; + +class A { + public void f() { + System.out.println("A"); + } +} + +class B extends A { + public void f() { + System.out.println("B"); + } +} + +public class Polymorphie { + + public static void main(String[] args) { + A a1 = new A(); + A a2 = new B(); + B b = new B(); + + a1.f(); + a2.f(); + b.f(); + } +} diff --git a/src/main/java/pr2/oo1/quiz/verdecken/A.java b/src/main/java/pr2/oo1/quiz/verdecken/A.java new file mode 100644 index 0000000..a20772e --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/verdecken/A.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.verdecken; + +class A { + + private int k = 10; + public int j = 20; + + public void f() { + System.out.println(k); + } +} diff --git a/src/main/java/pr2/oo1/quiz/verdecken/B.java b/src/main/java/pr2/oo1/quiz/verdecken/B.java new file mode 100644 index 0000000..846b78b --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/verdecken/B.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.verdecken; + +public class B extends A { + + private int j = 3000; + + public static void main(String[] args) { + + A a = new A(); + A b = new B(); + B b2 = new B(); + + System.out.printf("%d %d %d%n", a.j, b.j, b2.j); + a.f(); + } +} diff --git a/src/main/java/pr2/oo1/quiz/vererbung/C.java b/src/main/java/pr2/oo1/quiz/vererbung/C.java new file mode 100644 index 0000000..67928aa --- /dev/null +++ b/src/main/java/pr2/oo1/quiz/vererbung/C.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.quiz.vererbung; + +class A { + +} + +class B extends A { + +} + +//public class C extends A, B { +// +// public C() { +// super(null); +// } +//} diff --git a/src/main/java/pr2/oo1/ueberschreiben/A.java b/src/main/java/pr2/oo1/ueberschreiben/A.java new file mode 100644 index 0000000..cad21bc --- /dev/null +++ b/src/main/java/pr2/oo1/ueberschreiben/A.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.ueberschreiben; + +class A { + + public void method(Object o) { + // ... + } +} + +class B extends A { + +// @Override + public void method(String o) { + // ... + } +} \ No newline at end of file diff --git a/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/A.java b/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/A.java new file mode 100644 index 0000000..655a499 --- /dev/null +++ b/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/A.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.ueberschreiben.rueckgabetyp; + +public class A { + + protected int methode() { + return 0; + } +} diff --git a/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/B.java b/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/B.java new file mode 100644 index 0000000..faf30ba --- /dev/null +++ b/src/main/java/pr2/oo1/ueberschreiben/rueckgabetyp/B.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.ueberschreiben.rueckgabetyp; + +public class B extends A { + + public int methode() { + return 0; + } +} diff --git a/src/main/java/pr2/oo1/verdecken/A.java b/src/main/java/pr2/oo1/verdecken/A.java new file mode 100644 index 0000000..de3b145 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/A.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken; + +public class A { + public int i = 5; + + public void doIt() { + System.out.println(i); + } + + public static void main(String[] args) { + A a = new A(); + B b = new B(); + a.doIt(); + b.doIt(); + b.doItNew(); + } +} diff --git a/src/main/java/pr2/oo1/verdecken/B.java b/src/main/java/pr2/oo1/verdecken/B.java new file mode 100644 index 0000000..3d3eb30 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/B.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken; + +public class B extends A { + public String i = "Ein String in B"; + + public void doItNew() { + System.out.println(i); + System.out.println(super.i); + } +} diff --git a/src/main/java/pr2/oo1/verdecken/C.java b/src/main/java/pr2/oo1/verdecken/C.java new file mode 100644 index 0000000..536e03d --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/C.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken; + +public class C { + public int i = 1; +} diff --git a/src/main/java/pr2/oo1/verdecken/D.java b/src/main/java/pr2/oo1/verdecken/D.java new file mode 100644 index 0000000..740c2a2 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/D.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken; + +public class D extends C { + private int i; // verdeckt i aus C mit private Sichtbarkeit +} diff --git a/src/main/java/pr2/oo1/verdecken/E.java b/src/main/java/pr2/oo1/verdecken/E.java new file mode 100644 index 0000000..7941404 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/E.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken; + +public class E extends D { +// public void doIt() { +// i++; // Fehler, i wurde von D mit private verdeckt und +// // ist daher nicht mehr sichtbar. +// super.i++; // Fehler, da es in D kein sichtbares i gibt +// } +} diff --git a/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDemo.java b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDemo.java new file mode 100644 index 0000000..a0e937e --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDemo.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken.ellipse; + +public class EllipseDemo { + + public void flexibleMethode(String name, int ... attributes) { + int summe = 0; + + for (int i = 0; i < attributes.length; i++) { + summe += attributes[i]; + } + + System.out.println(name + " ist " + summe); + } + + public static void main(String[] args) { + EllipseDemo demo = new EllipseDemo(); + demo.flexibleMethode("Primsumme bis 13", 1, 3, 5, 7, 13); + demo.flexibleMethode("Gar nichts"); + } +} diff --git a/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetails.java b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetails.java new file mode 100644 index 0000000..bc85df7 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetails.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken.ellipse; + +public class EllipseDetails { + + public void flexMethod(int ... werte) { + for (int wert : werte) { + // tu was + } + } + + public void verwender() { + flexMethod(3, 5, 7, 13, 21, 23); + } +} diff --git a/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetailsEcht.java b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetailsEcht.java new file mode 100644 index 0000000..e66466b --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/ellipse/EllipseDetailsEcht.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken.ellipse; + +public class EllipseDetailsEcht { + + public void flexMethod(int[] werte) { + for (int wert : werte) { + // tu was + } + } + + public void verwender() { + flexMethod(new int[] { 3, 5, 7, 13, 21, 23 }); + } +} diff --git a/src/main/java/pr2/oo1/verdecken/ueberladen/UeberladenUndPolymorphie.java b/src/main/java/pr2/oo1/verdecken/ueberladen/UeberladenUndPolymorphie.java new file mode 100644 index 0000000..5f0a233 --- /dev/null +++ b/src/main/java/pr2/oo1/verdecken/ueberladen/UeberladenUndPolymorphie.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.verdecken.ueberladen; + +public class UeberladenUndPolymorphie { + + public void method(A a) { + System.out.println("method(C a) gerufen"); + } + + public void method(B b) { + System.out.println("method(B b) gerufen"); + } + + public static void main(String[] args) { + UeberladenUndPolymorphie u = new UeberladenUndPolymorphie(); + + A a1 = new A(); + A a2 = new B(); + + u.method(a1); + u.method(a2); + } +} + +class A {} +class B extends A {} +class C extends B {} diff --git a/src/main/java/pr2/oo1/vererbung/privat/Kampfechse.java b/src/main/java/pr2/oo1/vererbung/privat/Kampfechse.java new file mode 100644 index 0000000..06adcbd --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/privat/Kampfechse.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo1.vererbung.privat; + +public class Kampfechse extends Rennechse { + + public Kampfechse() { + super("Mike Tyson"); + } +} diff --git a/src/main/java/pr2/oo1/vererbung/privat/Rennechse.java b/src/main/java/pr2/oo1/vererbung/privat/Rennechse.java new file mode 100644 index 0000000..e9016be --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/privat/Rennechse.java @@ -0,0 +1,23 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo1.vererbung.privat; + +public class Rennechse { + + private String name; + + public Rennechse(String name) { + this.name = name; + } + + private void adeln() { + name = "von " + name; + } + + public String getName() { + return name; + } + + public void guttenbergeisieren() { + adeln(); + } +} diff --git a/src/main/java/pr2/oo1/vererbung/privat/Terrarium.java b/src/main/java/pr2/oo1/vererbung/privat/Terrarium.java new file mode 100644 index 0000000..ef3fc22 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/privat/Terrarium.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo1.vererbung.privat; + +import java.lang.reflect.Field; + +public class Terrarium { + + @SuppressWarnings("rawtypes") + public static void main(String[] args) throws NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + Kampfechse k = new Kampfechse(); + Class ke = k.getClass(); + + Field f = Rennechse.class.getDeclaredField("name"); + f.setAccessible(true); + System.out.println(f.get(k)); + k.guttenbergeisieren(); + System.out.println(f.get(k)); + + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v1/Geschaeftsfuehrer.java b/src/main/java/pr2/oo1/vererbung/v1/Geschaeftsfuehrer.java new file mode 100644 index 0000000..a0df0a5 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v1/Geschaeftsfuehrer.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v1; + +public class Geschaeftsfuehrer extends Manager { + public boolean prokura = true; +} diff --git a/src/main/java/pr2/oo1/vererbung/v1/GeschaeftsfuehrerBean.java b/src/main/java/pr2/oo1/vererbung/v1/GeschaeftsfuehrerBean.java new file mode 100644 index 0000000..5c350c0 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v1/GeschaeftsfuehrerBean.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v1; + +import java.util.Date; + +public class GeschaeftsfuehrerBean extends Manager { + + private boolean prokura = true; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public double getGehalt() { + return gehalt; + } + public void setGehalt(double gehalt) { + this.gehalt = gehalt; + } + public Date getGeburtsdatum() { + return geburtsdatum; + } + public void setGeburtsdatum(Date geburtsdatum) { + this.geburtsdatum = geburtsdatum; + } + public boolean isProkura() { + return prokura; + } + public void setProkura(boolean prokura) { + this.prokura = prokura; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v1/Manager.java b/src/main/java/pr2/oo1/vererbung/v1/Manager.java new file mode 100644 index 0000000..aa7119f --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v1/Manager.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v1; + +public class Manager extends Mitarbeiter { + public String abteilung; +} diff --git a/src/main/java/pr2/oo1/vererbung/v1/Mitarbeiter.java b/src/main/java/pr2/oo1/vererbung/v1/Mitarbeiter.java new file mode 100644 index 0000000..96d82a9 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v1/Mitarbeiter.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v1; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geburtsdatum; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v1/MitarbeiterBean.java b/src/main/java/pr2/oo1/vererbung/v1/MitarbeiterBean.java new file mode 100644 index 0000000..0afddc4 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v1/MitarbeiterBean.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v1; + +import java.util.Date; + +public class MitarbeiterBean { + private String name; + private double gehalt; + private Date geburtsdatum; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public double getGehalt() { + return gehalt; + } + public void setGehalt(double gehalt) { + this.gehalt = gehalt; + } + public Date getGeburtsdatum() { + return geburtsdatum; + } + public void setGeburtsdatum(Date geburtsdatum) { + this.geburtsdatum = geburtsdatum; + } + + + +} diff --git a/src/main/java/pr2/oo1/vererbung/v2/Geschaeftsfuehrer.java b/src/main/java/pr2/oo1/vererbung/v2/Geschaeftsfuehrer.java new file mode 100644 index 0000000..3ada69b --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v2/Geschaeftsfuehrer.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v2; + +public class Geschaeftsfuehrer extends Manager { + public boolean prokura = true; +} diff --git a/src/main/java/pr2/oo1/vererbung/v2/Manager.java b/src/main/java/pr2/oo1/vererbung/v2/Manager.java new file mode 100644 index 0000000..074c501 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v2/Manager.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v2; + +import java.util.Date; + +public class Manager { + public String abteilung; + public String name; + public double gehalt; + public Date geburtsdatum; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v2/Mitarbeiter.java b/src/main/java/pr2/oo1/vererbung/v2/Mitarbeiter.java new file mode 100644 index 0000000..58ed4e5 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v2/Mitarbeiter.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v2; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geburtsdatum; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v3/Geschaeftsfuehrer.java b/src/main/java/pr2/oo1/vererbung/v3/Geschaeftsfuehrer.java new file mode 100644 index 0000000..8e291ab --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v3/Geschaeftsfuehrer.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v3; + +public class Geschaeftsfuehrer extends Manager { + public boolean prokura = true; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt + + " und leitet die Abteilung " + abteilung + + " und hat Prokura " + prokura; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v3/Manager.java b/src/main/java/pr2/oo1/vererbung/v3/Manager.java new file mode 100644 index 0000000..6a00a52 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v3/Manager.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v3; + +public class Manager extends Mitarbeiter { + public String abteilung; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt + + " und leitet die Abteilung " + abteilung; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v3/Mitarbeiter.java b/src/main/java/pr2/oo1/vererbung/v3/Mitarbeiter.java new file mode 100644 index 0000000..d70017f --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v3/Mitarbeiter.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v3; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geburtsdatum; + + public String getDetails() { + return name + ", geb. am " + geburtsdatum + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v4/Geschaeftsfuehrer.java b/src/main/java/pr2/oo1/vererbung/v4/Geschaeftsfuehrer.java new file mode 100644 index 0000000..977ca58 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v4/Geschaeftsfuehrer.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v4; + +public class Geschaeftsfuehrer extends Manager { + public boolean prokura = true; + + public String getDetails() { + return super.getDetails() + + " und hat Prokura " + prokura; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v4/Manager.java b/src/main/java/pr2/oo1/vererbung/v4/Manager.java new file mode 100644 index 0000000..7b54752 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v4/Manager.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v4; + +public class Manager extends Mitarbeiter { + public String abteilung; + + public String getDetails() { + return super.getDetails() + + " und leitet die Abteilung " + abteilung; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v4/Methodenaufruf.java b/src/main/java/pr2/oo1/vererbung/v4/Methodenaufruf.java new file mode 100644 index 0000000..43b19ca --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v4/Methodenaufruf.java @@ -0,0 +1,144 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v4; + +import java.util.Date; + +public class Methodenaufruf { + + public void main() { + + { + Mitarbeiter mi = new Mitarbeiter(); + Manager ma = new Manager(); + + System.out.println(mi.getDetails()); + System.out.println(ma.getDetails()); + } + + { + Mitarbeiter mi = new Manager(); + System.out.println(mi.getDetails()); + } + + { + Mitarbeiter[] array = new Mitarbeiter[3]; + array[0] = new Mitarbeiter(); + array[1] = new Manager(); + array[2] = new Geschaeftsfuehrer(); + } + + { + Manager ma = new Manager(); + Mitarbeiter mi = new Mitarbeiter(); + + gehaltserhöhung(0.12, ma); + gehaltserhöhung(0.02, mi); + } + + { +// Manager ma = new Mitarbeiter(); + } + + { + Mitarbeiter mi = new Manager(); + + Manager ma = (Manager) mi; + ma.abteilung = "Buchhaltung"; + + ((Manager) mi).abteilung = "Buchhaltung"; + } + + { + Mitarbeiter mi = new Manager(); + if (mi instanceof Manager) {} + if (mi instanceof Mitarbeiter) {} + if (mi instanceof Geschaeftsfuehrer) {} + if (null instanceof Mitarbeiter) {} + + Object o = new Manager(); + o = new String(); + + Object[] os = new Object[2]; + os[0] = new Manager(); + os[1] = new Date(); + } + + Manager ma = new Manager(); + gehaltserhöhung(0.05d, ma); + } + + public void gehaltserhöhung(double prozente, Mitarbeiter mi) { + mi.gehalt *= 1.0 + prozente; + } + + public void automatischeErhöhung(Mitarbeiter mi) { + if (mi instanceof Geschaeftsfuehrer) { + gehaltserhöhung(0.10, mi); + } + else if (mi instanceof Manager) { + gehaltserhöhung(0.05, mi); + } + else { + gehaltserhöhung(0.0003, mi); + } + } + + public void gehaltserhöhung(Geschaeftsfuehrer mi) { + mi.gehalt = mi.gehalt * (1.0 + 0.2); + } + + public void gehaltserhöhung(Manager mi) { + mi.gehalt = mi.gehalt * (1.0 + 0.1); + } + + public void gehaltserhöhung(Mitarbeiter mi) { + mi.gehalt = mi.gehalt * (1.0 + 0.002); + } + + public int average(int a1, int a2) { return 0; } + public int average(int a1, int a2, int a3) { return 0; } + public int average(int a1, int a2, int a3, int a4) { return 0; } + + public void automatischeErhöhung2(Mitarbeiter mi) { + if (mi instanceof Geschaeftsfuehrer) { + + Geschaeftsfuehrer g = (Geschaeftsfuehrer) mi; + + if (g.prokura) { + mi.gehalt = mi.gehalt * (1 + 0.2); + } + else { + mi.gehalt = mi.gehalt * (1 + 0.1); + } + } + else if (mi instanceof Manager) { + mi.gehalt = mi.gehalt * (1 + 0.05); + } + else { + mi.gehalt = mi.gehalt * (1 + 0.002); + } + } + + public void automatischeErhöhung3(Mitarbeiter mi) { + + if (mi.getClass() == Geschaeftsfuehrer.class) { + + Geschaeftsfuehrer g = (Geschaeftsfuehrer) mi; + + if (g.prokura) { + mi.gehalt = mi.gehalt * (1 + 0.2); + } + else { + mi.gehalt = mi.gehalt * (1 + 0.1); + } + } + else if (mi.getClass() == Manager.class) { + mi.gehalt = mi.gehalt * (1 + 0.05); + } + else { + mi.gehalt = mi.gehalt * (1 + 0.002); + } + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v4/Mitarbeiter.java b/src/main/java/pr2/oo1/vererbung/v4/Mitarbeiter.java new file mode 100644 index 0000000..85d1fed --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v4/Mitarbeiter.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v4; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geboren; + + public String getDetails() { + return name + ", geb. am " + geboren + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v5/Klasse.java b/src/main/java/pr2/oo1/vererbung/v5/Klasse.java new file mode 100644 index 0000000..b5f36c2 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v5/Klasse.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v5; + +public class Klasse { + int i; +} diff --git a/src/main/java/pr2/oo1/vererbung/v5/Klasse2.java b/src/main/java/pr2/oo1/vererbung/v5/Klasse2.java new file mode 100644 index 0000000..1060f38 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v5/Klasse2.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v5; + +public class Klasse2 extends Object { + + int i; + + public Klasse2() { + super(); + i = 0; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v5/Manager.java b/src/main/java/pr2/oo1/vererbung/v5/Manager.java new file mode 100644 index 0000000..a2b8fb9 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v5/Manager.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v5; + +import java.util.Date; + +public class Manager extends Mitarbeiter { + public String abteilung; + + public Manager(String name, double gehalt, Date geburtsdatum, String abteilung) { + super(name, gehalt, geburtsdatum); + this.abteilung = abteilung; + } + + public String getDetails() { + return super.getDetails() + + " und leitet die Abteilung " + abteilung; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/v5/Mitarbeiter.java b/src/main/java/pr2/oo1/vererbung/v5/Mitarbeiter.java new file mode 100644 index 0000000..f3fb135 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/v5/Mitarbeiter.java @@ -0,0 +1,35 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.v5; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geboren; + + public Mitarbeiter(String name, double gehalt, Date geboren) { + this.name = name; + this.gehalt = gehalt; + this.geboren = geboren; + } + + public Mitarbeiter(String name, double gehalt) { + this(name, gehalt, null); + } + + public Mitarbeiter(String name) { + this(name, 0.0d, null); + } + + public Mitarbeiter() { + + } + + public String getDetails() { + return name + ", geb. am " + geboren + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack1/A.java b/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack1/A.java new file mode 100644 index 0000000..ed7c535 --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack1/A.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.wasprotectedwirklichheisst.pack1; + +public class A { + + protected int i; + + protected void method() { + } +} diff --git a/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack2/B.java b/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack2/B.java new file mode 100644 index 0000000..e13bd0e --- /dev/null +++ b/src/main/java/pr2/oo1/vererbung/wasprotectedwirklichheisst/pack2/B.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo1.vererbung.wasprotectedwirklichheisst.pack2; + +import pr2.oo1.vererbung.wasprotectedwirklichheisst.pack1.A; + +public class B extends A { + + protected void method() { + i++; + } + + public void doIt(A a) { + // a.i++; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v1/Ente.java b/src/main/java/pr2/oo2/abstrakt/v1/Ente.java new file mode 100644 index 0000000..82b8f34 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v1/Ente.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v1; + +public class Ente extends Tier { + + public void schwimmen() { + // mit den Füßen paddeln und mit Flügeln schlagen + // und überhaupt tolle Ente + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v1/Hund.java b/src/main/java/pr2/oo2/abstrakt/v1/Hund.java new file mode 100644 index 0000000..f9ea18a --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v1/Hund.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v1; + +public class Hund extends Tier { + + public void bellen() { + // Maul auf und los + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v1/Tier.java b/src/main/java/pr2/oo2/abstrakt/v1/Tier.java new file mode 100644 index 0000000..8b3fa1c --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v1/Tier.java @@ -0,0 +1,11 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v1; + +public class Tier { + + private int alter; + + public void fortbewegen() { /* mhmm... Tiere bewegen sich, aber wie? */ } + + public void fressen() { /* mhmm... Tiere fressen, aber wie? */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v1/Verwender.java b/src/main/java/pr2/oo2/abstrakt/v1/Verwender.java new file mode 100644 index 0000000..87a26c2 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v1/Verwender.java @@ -0,0 +1,15 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v1; + +public class Verwender { + + public static void main(String[] args) { + Hund h = new Hund(); + h.fortbewegen(); + + Tier t = new Tier(); + + Tier e = new Ente(); + e.fressen(); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/A.java b/src/main/java/pr2/oo2/abstrakt/v10/A.java new file mode 100644 index 0000000..5ee7962 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/A.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public abstract class A { + + public abstract void abstrakt1(); + + public abstract void abstrakt2(); + + public void konkret() { + System.out.println("Konkret Alter!"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/B.java b/src/main/java/pr2/oo2/abstrakt/v10/B.java new file mode 100644 index 0000000..a263af3 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/B.java @@ -0,0 +1,15 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public class B extends A { + + @Override + public void abstrakt1() { + System.out.println("Abstrakt 1 aus B"); + } + + @Override + public void abstrakt2() { + System.out.println("Abstrakt 2 aus B"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/C.java b/src/main/java/pr2/oo2/abstrakt/v10/C.java new file mode 100644 index 0000000..c97f622 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/C.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public abstract class C extends A { + + public void abstrakt1() { + System.out.println("Abstrakt 1 aus C"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/D.java b/src/main/java/pr2/oo2/abstrakt/v10/D.java new file mode 100644 index 0000000..8921266 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/D.java @@ -0,0 +1,4 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public abstract class D extends C { /* empty */ } diff --git a/src/main/java/pr2/oo2/abstrakt/v10/E.java b/src/main/java/pr2/oo2/abstrakt/v10/E.java new file mode 100644 index 0000000..025206c --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/E.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public class E extends D { + + public void abstrakt2() { + System.out.println("Abstrakt 2 aus E"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/F.java b/src/main/java/pr2/oo2/abstrakt/v10/F.java new file mode 100644 index 0000000..bfc86d2 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/F.java @@ -0,0 +1,4 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public abstract class F extends A { /* empty */ } diff --git a/src/main/java/pr2/oo2/abstrakt/v10/G.java b/src/main/java/pr2/oo2/abstrakt/v10/G.java new file mode 100644 index 0000000..a3a7a56 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/G.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public class G extends F { + + public void abstrakt1() { + System.out.println("Abstrakt 1 aus G"); + } + + public void abstrakt2() { + System.out.println("Abstrakt 2 aus G"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v10/Verwender.java b/src/main/java/pr2/oo2/abstrakt/v10/Verwender.java new file mode 100644 index 0000000..8e4e070 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v10/Verwender.java @@ -0,0 +1,22 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v10; + +public class Verwender { + + public static void main(String[] args) { + A b = new B(); + + b.konkret(); + b.abstrakt1(); + b.abstrakt2(); + + A d = new E(); + d.konkret(); + d.abstrakt1(); + d.abstrakt2(); + + A g = new G(); g.konkret(); g.abstrakt1(); g.abstrakt2(); + + + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v11/A.java b/src/main/java/pr2/oo2/abstrakt/v11/A.java new file mode 100644 index 0000000..4a6b5f3 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v11/A.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v11; + +public abstract class A { + + public abstract void abstrakt1(); + + public abstract void abstrakt2(); + + public void konkret() { + System.out.println("Konkret Alter!"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v11/B.java b/src/main/java/pr2/oo2/abstrakt/v11/B.java new file mode 100644 index 0000000..ef10a17 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v11/B.java @@ -0,0 +1,16 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v11; + +public abstract class B extends A { + + @Override + public void abstrakt1() { + System.out.println("Abstrakt 1 aus B"); + } + + public abstract void abstrakt3(); + + public void konkret2() { + System.out.println("Konkret 2 aus B"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v11/C.java b/src/main/java/pr2/oo2/abstrakt/v11/C.java new file mode 100644 index 0000000..7dd74c8 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v11/C.java @@ -0,0 +1,15 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v11; + +public class C extends B { + + @Override + public void abstrakt2() { + System.out.println("Abstrakt 2 aus C"); + } + + @Override + public void abstrakt3() { + System.out.println("Abstrakt 3 aus C"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v11/D.java b/src/main/java/pr2/oo2/abstrakt/v11/D.java new file mode 100644 index 0000000..a3abb1c --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v11/D.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v11; + +public class D extends C { + + @Override + public void abstrakt1() { + System.out.println("Abstrakt 1 aus D"); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v2/Ente.java b/src/main/java/pr2/oo2/abstrakt/v2/Ente.java new file mode 100644 index 0000000..b8b2b3b --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v2/Ente.java @@ -0,0 +1,11 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v2; + +public class Ente extends Tier { + + public void schwimmen() { /* mit den Füßen paddeln */ } + + public void fortbewegen() { /* watscheln */ } + + public void fressen() { /* abtauchen und Schnabel auf */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v2/Hund.java b/src/main/java/pr2/oo2/abstrakt/v2/Hund.java new file mode 100644 index 0000000..f7f64a7 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v2/Hund.java @@ -0,0 +1,11 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v2; + +public class Hund extends Tier { + + public void bellen() { /* Maul auf und los */ } + + public void fortbewegen() { /* auf allen Vieren dackeln */ } + + public void fressen() { /* Maul auf und runter mit dem Futter */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v2/Tier.java b/src/main/java/pr2/oo2/abstrakt/v2/Tier.java new file mode 100644 index 0000000..a69d782 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v2/Tier.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v2; + +public class Tier { + private int alter; +} diff --git a/src/main/java/pr2/oo2/abstrakt/v2/Verwender.java b/src/main/java/pr2/oo2/abstrakt/v2/Verwender.java new file mode 100644 index 0000000..c23b214 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v2/Verwender.java @@ -0,0 +1,23 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v2; + +public class Verwender { + + public void main(String[] args) { + Hund h = new Hund(); + füttern(h); + + Ente e = new Ente(); + füttern(e); + } + + public void füttern(Tier t) { + + if (t instanceof Hund) { + ((Hund) t).fressen(); + } + else if (t instanceof Ente) { + ((Ente) t).fressen(); + } + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v3/Ente.java b/src/main/java/pr2/oo2/abstrakt/v3/Ente.java new file mode 100644 index 0000000..7869bab --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v3/Ente.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v3; + +public class Ente extends Tier { + + public void schwimmen() { /* mit den Füßen paddeln */ } + + @Override + public void fortbewegen() { /* watscheln */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v3/Hund.java b/src/main/java/pr2/oo2/abstrakt/v3/Hund.java new file mode 100644 index 0000000..76308d8 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v3/Hund.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v3; + +public class Hund extends Tier { + + public void bellen() { /* Maul auf und los */ } + + @Override + public void fortbewegen() { /* auf allen Vieren dackeln */ } + + @Override + public void fressen() { /* Maul auf und runter mit dem Futter */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v3/Tier.java b/src/main/java/pr2/oo2/abstrakt/v3/Tier.java new file mode 100644 index 0000000..492ed98 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v3/Tier.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v3; + +public class Tier { + private int alter; + + public void fortbewegen() { /* mhmm... Tiere bewegen sich, aber wie? */ } + + public void fressen() { /* mhmm... Tiere fressen, aber wie? */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v3/Verwender.java b/src/main/java/pr2/oo2/abstrakt/v3/Verwender.java new file mode 100644 index 0000000..74e1fed --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v3/Verwender.java @@ -0,0 +1,32 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v3; + +public class Verwender { + + public void main(String[] args) { + + { + Hund h = new Hund(); + h.fortbewegen(); + + Tier t = new Tier(); + + Tier e = new Ente(); + e.fressen(); + } + + { + Hund h = new Hund(); + füttern(h); + + Ente e = new Ente(); + füttern(e); + } + + } + + public void füttern(Tier t) { + // Futter bereitstellen + t.fressen(); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v4/Ente.java b/src/main/java/pr2/oo2/abstrakt/v4/Ente.java new file mode 100644 index 0000000..30a6473 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v4/Ente.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v4; + +public class Ente extends Tier { + + public void schwimmen() { /* mit den Füßen paddeln */ } + + @Override + public void fortbewegen() { /* watscheln */ } + + @Override + public void fressen() { /* abtauchen und Schnabel auf */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v4/Hund.java b/src/main/java/pr2/oo2/abstrakt/v4/Hund.java new file mode 100644 index 0000000..578f906 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v4/Hund.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v4; + +public class Hund extends Tier { + + public void bellen() { /* Maul auf und los */ } + + @Override + public void fortbewegen() { /* auf allen Vieren dackeln */ } + + @Override + public void fressen() { /* Maul auf und runter mit dem Futter */ } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v4/Tier.java b/src/main/java/pr2/oo2/abstrakt/v4/Tier.java new file mode 100644 index 0000000..080e03b --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v4/Tier.java @@ -0,0 +1,11 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v4; + +public abstract class Tier { + + private int alter; + + public abstract void fortbewegen(); + + public abstract void fressen(); +} diff --git a/src/main/java/pr2/oo2/abstrakt/v4/Verwender.java b/src/main/java/pr2/oo2/abstrakt/v4/Verwender.java new file mode 100644 index 0000000..e77b422 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v4/Verwender.java @@ -0,0 +1,32 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v4; + +public class Verwender { + + public void main(String[] args) { + + { + Hund h = new Hund(); + h.fortbewegen(); + + // Tier t = new Tier(); + + Tier e = new Ente(); + e.fressen(); + } + + { + Hund h = new Hund(); + füttern(h); + + Ente e = new Ente(); + füttern(e); + } + + } + + public void füttern(Tier t) { + // Futter bereitstellen + t.fressen(); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v5/Angestellter.java b/src/main/java/pr2/oo2/abstrakt/v5/Angestellter.java new file mode 100644 index 0000000..64d90fe --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v5/Angestellter.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v5; + +public class Angestellter extends Mitarbeiter { + + private int monatsgehalt; + + public int berechneZahlung() { + return monatsgehalt; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v5/Arbeiter.java b/src/main/java/pr2/oo2/abstrakt/v5/Arbeiter.java new file mode 100644 index 0000000..5975985 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v5/Arbeiter.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v5; + +public class Arbeiter extends Mitarbeiter { + + private int akkordlohn; + private int grundlohn; + private int akkordStueckzahl; + + public int berechneZahlung() { + return grundlohn + akkordlohn * akkordStueckzahl; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v5/Lohnbuchhaltung.java b/src/main/java/pr2/oo2/abstrakt/v5/Lohnbuchhaltung.java new file mode 100644 index 0000000..9db3483 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v5/Lohnbuchhaltung.java @@ -0,0 +1,31 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v5; + +public class Lohnbuchhaltung { + + public void ueberweisung(Mitarbeiter[] mitarbeiter) { + + for (int i = 0; i < mitarbeiter.length; i++) { + Mitarbeiter m = mitarbeiter[i]; + + int zahlbetrag; + + if (m instanceof Arbeiter) { + Arbeiter a = (Arbeiter) m; + zahlbetrag = a.berechneZahlung(); + } + else if (m instanceof Angestellter) { + Angestellter a = (Angestellter) m; + zahlbetrag = a.berechneZahlung(); + } + else { + System.out.println("Fehler"); + } + + // Zahlung durchf�hren + + } + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v5/Mitarbeiter.java b/src/main/java/pr2/oo2/abstrakt/v5/Mitarbeiter.java new file mode 100644 index 0000000..a77081e --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v5/Mitarbeiter.java @@ -0,0 +1,7 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v5; + +public class Mitarbeiter { +} diff --git a/src/main/java/pr2/oo2/abstrakt/v5/Vorstand.java b/src/main/java/pr2/oo2/abstrakt/v5/Vorstand.java new file mode 100644 index 0000000..06e5b66 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v5/Vorstand.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v5; + +public class Vorstand extends Mitarbeiter { + private int monatsgehalt; + private int aktienoptionen; +} diff --git a/src/main/java/pr2/oo2/abstrakt/v6/Angestellter.java b/src/main/java/pr2/oo2/abstrakt/v6/Angestellter.java new file mode 100644 index 0000000..e0e3df2 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v6/Angestellter.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v6; + +public class Angestellter extends Mitarbeiter { + + private int monatsgehalt; + + public int berechneZahlung() { + return monatsgehalt; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v6/Arbeiter.java b/src/main/java/pr2/oo2/abstrakt/v6/Arbeiter.java new file mode 100644 index 0000000..3750ace --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v6/Arbeiter.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v6; + +public class Arbeiter extends Mitarbeiter { + + private int akkordlohn; + private int grundlohn; + private int akkordStueckzahl; + + public int berechneZahlung() { + return grundlohn + akkordlohn * akkordStueckzahl; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v6/Lohnbuchhaltung.java b/src/main/java/pr2/oo2/abstrakt/v6/Lohnbuchhaltung.java new file mode 100644 index 0000000..afc88d7 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v6/Lohnbuchhaltung.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v6; + +public class Lohnbuchhaltung { + + public void ueberweisung(Mitarbeiter[] mitarbeiter) { + + for (int i = 0; i < mitarbeiter.length; i++) { + int zahlbetrag = mitarbeiter[i].berechneZahlung(); + // Zahlung durchf�hren + } + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v6/Mitarbeiter.java b/src/main/java/pr2/oo2/abstrakt/v6/Mitarbeiter.java new file mode 100644 index 0000000..0b48466 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v6/Mitarbeiter.java @@ -0,0 +1,12 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v6; + +public class Mitarbeiter { + + public int berechneZahlung() { + System.out.println("Fehler..."); + return 0; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v6/Vorstand.java b/src/main/java/pr2/oo2/abstrakt/v6/Vorstand.java new file mode 100644 index 0000000..9823da1 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v6/Vorstand.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v6; + +public class Vorstand extends Mitarbeiter { + private int monatsgehalt; + private int aktienoptionen; +} diff --git a/src/main/java/pr2/oo2/abstrakt/v7/Angestellter.java b/src/main/java/pr2/oo2/abstrakt/v7/Angestellter.java new file mode 100644 index 0000000..fc11efd --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v7/Angestellter.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v7; + +public class Angestellter extends Mitarbeiter { + + private int monatsgehalt; + + public int berechneZahlung() { + return monatsgehalt; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v7/Arbeiter.java b/src/main/java/pr2/oo2/abstrakt/v7/Arbeiter.java new file mode 100644 index 0000000..d8b16d0 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v7/Arbeiter.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v7; + +public class Arbeiter extends Mitarbeiter { + + private int akkordlohn; + private int grundlohn; + private int akkordStueckzahl; + + public int berechneZahlung() { + return grundlohn + akkordlohn * akkordStueckzahl; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v7/Lohnbuchhaltung.java b/src/main/java/pr2/oo2/abstrakt/v7/Lohnbuchhaltung.java new file mode 100644 index 0000000..1e2ef33 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v7/Lohnbuchhaltung.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v7; + +public class Lohnbuchhaltung { + + public void ueberweisung(Mitarbeiter[] mitarbeiter) { + + for (int i = 0; i < mitarbeiter.length; i++) { + int zahlbetrag = mitarbeiter[i].berechneZahlung(); + // Zahlung durchführen + } + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v7/Mitarbeiter.java b/src/main/java/pr2/oo2/abstrakt/v7/Mitarbeiter.java new file mode 100644 index 0000000..91d1d3b --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v7/Mitarbeiter.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v7; + +public abstract class Mitarbeiter { + + protected abstract int berechneZahlung(); +} diff --git a/src/main/java/pr2/oo2/abstrakt/v7/Vorstand.java b/src/main/java/pr2/oo2/abstrakt/v7/Vorstand.java new file mode 100644 index 0000000..fa3523b --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v7/Vorstand.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v7; + +public abstract class Vorstand extends Mitarbeiter { + +} diff --git a/src/main/java/pr2/oo2/abstrakt/v8/Angestellter.java b/src/main/java/pr2/oo2/abstrakt/v8/Angestellter.java new file mode 100644 index 0000000..79a30d8 --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v8/Angestellter.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v8; + +public class Angestellter extends Mitarbeiter { + + private double zielbonus = 1000.0; + private double zielerreichung = 0.98; + + public double zielbonus() { + return zielbonus; + } + + public double zielerreichung() { + return zielerreichung; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v8/Main.java b/src/main/java/pr2/oo2/abstrakt/v8/Main.java new file mode 100644 index 0000000..1980a9d --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v8/Main.java @@ -0,0 +1,14 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v8; + +/** + * + * + * @author Thomas Smits + */ +public class Main { + public static void main(String[] args) { + Mitarbeiter m = new Vorstand(); + System.out.println(m.bonus()); + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v8/Mitarbeiter.java b/src/main/java/pr2/oo2/abstrakt/v8/Mitarbeiter.java new file mode 100644 index 0000000..582fd9b --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v8/Mitarbeiter.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v8; + +public abstract class Mitarbeiter { + + public final double bonus() { + return zielbonus() * zielerreichung(); + } + + public abstract double zielerreichung(); + public abstract double zielbonus(); +} diff --git a/src/main/java/pr2/oo2/abstrakt/v8/Vorstand.java b/src/main/java/pr2/oo2/abstrakt/v8/Vorstand.java new file mode 100644 index 0000000..c85afcf --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v8/Vorstand.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.abstrakt.v8; + +public class Vorstand extends Mitarbeiter { + + private double firmengewinn = 300000.0; + + public double zielbonus() { + return 0.5 * firmengewinn; + } + + public double zielerreichung() { + return 1.00; + } +} diff --git a/src/main/java/pr2/oo2/abstrakt/v9/Mitarbeiter.java b/src/main/java/pr2/oo2/abstrakt/v9/Mitarbeiter.java new file mode 100644 index 0000000..fb1da7e --- /dev/null +++ b/src/main/java/pr2/oo2/abstrakt/v9/Mitarbeiter.java @@ -0,0 +1,23 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.abstrakt.v9; + +import java.util.Date; + +public abstract class Mitarbeiter { + + protected String name; + protected double gehalt; + protected Date geboren; + + public Mitarbeiter(String name, double gehalt, Date geboren) { + this.name = name; + this.gehalt = gehalt; + this.geboren = geboren; + } + + public Date getGeburtsdatum() { + return geboren; + } + + protected abstract int berechneZahlung(); +} diff --git a/src/main/java/pr2/oo2/finale/blankfinal/Kunde.java b/src/main/java/pr2/oo2/finale/blankfinal/Kunde.java new file mode 100644 index 0000000..81fa78b --- /dev/null +++ b/src/main/java/pr2/oo2/finale/blankfinal/Kunde.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.blankfinal; + +public class Kunde { + + private final int kundenNummer; + + public Kunde(int kundenNummer) { + this.kundenNummer = kundenNummer; + } +} diff --git a/src/main/java/pr2/oo2/finale/v1/DeadCodeElemination.java b/src/main/java/pr2/oo2/finale/v1/DeadCodeElemination.java new file mode 100644 index 0000000..4c2399f --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v1/DeadCodeElemination.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v1; + +public class DeadCodeElemination { + + public static final boolean SCHALTER = false; + + public static void main(String[] args) { + if (SCHALTER) { + System.out.println("Schalter AN"); + } + else { + System.out.println("Schalter AUS"); + } + } +} diff --git a/src/main/java/pr2/oo2/finale/v1/Konstanten.java b/src/main/java/pr2/oo2/finale/v1/Konstanten.java new file mode 100644 index 0000000..a8b0aad --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v1/Konstanten.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v1; + +public class Konstanten { + public static final double PI = 3.14159265; + + public static final double NA = 6.0221415E23; + + public static final String XML_HEADER = ""; + + public static final int[] primzahlen = { 2, 3, 5, 7, 11, 13, 17, 19 }; +} diff --git a/src/main/java/pr2/oo2/finale/v1/NoDeadCodeElemination.java b/src/main/java/pr2/oo2/finale/v1/NoDeadCodeElemination.java new file mode 100644 index 0000000..2393d19 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v1/NoDeadCodeElemination.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v1; + +public class NoDeadCodeElemination { + + public static boolean schalter = false; + + public static void main(String[] args) { + if (schalter) { + System.out.println("Schalter AN"); + } + else { + System.out.println("Schalter AUS"); + } + } +} diff --git a/src/main/java/pr2/oo2/finale/v2/A.java b/src/main/java/pr2/oo2/finale/v2/A.java new file mode 100644 index 0000000..7671cb4 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v2/A.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v2; + +public class A { + public void methode() {} + public final void finalMethode() {} +} diff --git a/src/main/java/pr2/oo2/finale/v2/B.java b/src/main/java/pr2/oo2/finale/v2/B.java new file mode 100644 index 0000000..ae0c916 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v2/B.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v2; + +public class B extends A { + + @Override + public void methode() {} + /* + @Override + public void finalMethode() {} + */ +} diff --git a/src/main/java/pr2/oo2/finale/v2/FinaleVariablen.java b/src/main/java/pr2/oo2/finale/v2/FinaleVariablen.java new file mode 100644 index 0000000..aa2fb47 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v2/FinaleVariablen.java @@ -0,0 +1,37 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v2; + +public class FinaleVariablen { +/* + public void methode1(final int k) { + k++; + } + + public void methode2() { + final boolean b = true; + final String s; + + if (b) { + s = "TRUE"; + } + else { + s = "FALSE"; + } + + b = false; + s.toUpperCase(); + } +public static void main(String[] args) { + FinaleVariablen f = new FinaleVariablen(); + f.methode2(true); +} + public void methode3() { + final boolean b = true; + final String s; + + s = b ? "TRUE" : "FALSE"; + } + */ +} diff --git a/src/main/java/pr2/oo2/finale/v2/Konstanten.java b/src/main/java/pr2/oo2/finale/v2/Konstanten.java new file mode 100644 index 0000000..cd1e905 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v2/Konstanten.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v2; + +public class Konstanten { + public static final int[] PRIMZAHLEN = { 2, 3, 5, 7, 11, 13, 17, 19 }; +} diff --git a/src/main/java/pr2/oo2/finale/v2/Konstantenverwender.java b/src/main/java/pr2/oo2/finale/v2/Konstantenverwender.java new file mode 100644 index 0000000..55a8db9 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v2/Konstantenverwender.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v2; + +import java.util.Arrays; + +public class Konstantenverwender { + + public static void main(String[] args) { + System.out.println(Arrays.toString(Konstanten.PRIMZAHLEN)); + Konstanten.PRIMZAHLEN[2] = 8; + System.out.println(Arrays.toString(Konstanten.PRIMZAHLEN)); + } +} diff --git a/src/main/java/pr2/oo2/finale/v3/A.java b/src/main/java/pr2/oo2/finale/v3/A.java new file mode 100644 index 0000000..b671747 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v3/A.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v3; + +public final class A { + +} diff --git a/src/main/java/pr2/oo2/finale/v3/B.java b/src/main/java/pr2/oo2/finale/v3/B.java new file mode 100644 index 0000000..94cfa77 --- /dev/null +++ b/src/main/java/pr2/oo2/finale/v3/B.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.finale.v3; + +public class B /* extends A */ { + +} diff --git a/src/main/java/pr2/oo2/interfaces/callback/Addition.java b/src/main/java/pr2/oo2/interfaces/callback/Addition.java new file mode 100644 index 0000000..5b5a265 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/callback/Addition.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.callback; + +public class Addition implements Funktion { + + public int apply(int o1, int o2) { + return o1 + o2; + } +} diff --git a/src/main/java/pr2/oo2/interfaces/callback/Berechnung.java b/src/main/java/pr2/oo2/interfaces/callback/Berechnung.java new file mode 100644 index 0000000..ae10729 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/callback/Berechnung.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.callback; + +public class Berechnung { + + public static int berechne(int input1, int input2, Funktion funktion) { + int ergebnis = funktion.apply(input1, input2); + return ergebnis; + } + + public static void main(String[] args) { + Funktion sub = new Subtraktion(); + Funktion add = new Addition(); + + System.out.println(berechne(5, 3, sub)); + System.out.println(berechne(1, 7, add)); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/callback/Funktion.java b/src/main/java/pr2/oo2/interfaces/callback/Funktion.java new file mode 100644 index 0000000..dd64582 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/callback/Funktion.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.callback; + +public interface Funktion { + public abstract int apply(int o1, int o2); +} diff --git a/src/main/java/pr2/oo2/interfaces/callback/Subtraktion.java b/src/main/java/pr2/oo2/interfaces/callback/Subtraktion.java new file mode 100644 index 0000000..cddc6c8 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/callback/Subtraktion.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.callback; + +public class Subtraktion implements Funktion { + + public int apply(int o1, int o2) { + return o1 - o2; + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/ExtFlieger.java b/src/main/java/pr2/oo2/interfaces/java8/ExtFlieger.java new file mode 100644 index 0000000..fe16e2f --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/ExtFlieger.java @@ -0,0 +1,17 @@ +package pr2.oo2.interfaces.java8; + +public interface ExtFlieger extends Flieger { + + @Override + public default void steigen() { + // ersetzt die steigen Methode aus Flieger + System.out.println("Schnell steigen"); + } + + public void sinken(); // Methode ist jetzt abstrakt + + public default void starten() { + // Methode ist jetzt nicht mehr abstrakt + System.out.println("Kommt"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/Flieger.java b/src/main/java/pr2/oo2/interfaces/java8/Flieger.java new file mode 100644 index 0000000..60edd2d --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/Flieger.java @@ -0,0 +1,12 @@ +package pr2.oo2.interfaces.java8; + +public interface Flieger { + public void starten(); + public void landen(); + public default void sinken() { + System.out.println("sinke"); + } + public default void steigen() { + System.out.println("steige"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/Flugzeug.java b/src/main/java/pr2/oo2/interfaces/java8/Flugzeug.java new file mode 100644 index 0000000..1dcda02 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/Flugzeug.java @@ -0,0 +1,11 @@ +package pr2.oo2.interfaces.java8; + +public class Flugzeug implements Flieger { + public void starten() { + System.out.println("starte"); + } + + public void landen() { + System.out.println("lande"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/IA.java b/src/main/java/pr2/oo2/interfaces/java8/IA.java new file mode 100644 index 0000000..e303807 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/IA.java @@ -0,0 +1,5 @@ +package pr2.oo2.interfaces.java8; + +public interface IA { + public default void m() { } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/IB.java b/src/main/java/pr2/oo2/interfaces/java8/IB.java new file mode 100644 index 0000000..b5e755b --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/IB.java @@ -0,0 +1,5 @@ +package pr2.oo2.interfaces.java8; + +public interface IB { + public default void m() { } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/Impl.java b/src/main/java/pr2/oo2/interfaces/java8/Impl.java new file mode 100644 index 0000000..c792bdf --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/Impl.java @@ -0,0 +1,5 @@ +package pr2.oo2.interfaces.java8; + +public class Impl implements IA, IB { + public void m() { } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/Main.java b/src/main/java/pr2/oo2/interfaces/java8/Main.java new file mode 100644 index 0000000..2c20165 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/Main.java @@ -0,0 +1,11 @@ +package pr2.oo2.interfaces.java8; + +public class Main { + public static void main(String[] args) { + Flugzeug flugzeug = new Flugzeug(); + flugzeug.starten(); + flugzeug.steigen(); + flugzeug.sinken(); + flugzeug.landen(); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/Rechner.java b/src/main/java/pr2/oo2/interfaces/java8/Rechner.java new file mode 100644 index 0000000..80a5a3e --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/Rechner.java @@ -0,0 +1,10 @@ +package pr2.oo2.interfaces.java8; + +public interface Rechner { + + public int apply(int a, int b); + + public static int rechne(Rechner r, int a, int b) { + return r.apply(a, b); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/A.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/A.java new file mode 100644 index 0000000..027a445 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/A.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public class A implements I1 { + + public void methode() { + System.out.println("A"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/B.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/B.java new file mode 100644 index 0000000..e61531a --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/B.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public class B extends A { + + @Override + public void methode() { + System.out.println("B"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/C.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/C.java new file mode 100644 index 0000000..54a3c8a --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/C.java @@ -0,0 +1,6 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public class C extends B implements I2 { + +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/I1.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/I1.java new file mode 100644 index 0000000..1c3eea6 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/I1.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public interface I1 { + + public default void methode() { + System.out.println("I1"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/I2.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/I2.java new file mode 100644 index 0000000..4117297 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/I2.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public interface I2 extends I1 { + + @Override + public default void methode() { + System.out.println("I2"); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/java8/diamond/Main.java b/src/main/java/pr2/oo2/interfaces/java8/diamond/Main.java new file mode 100644 index 0000000..1d251b3 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/java8/diamond/Main.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.java8.diamond; + +public class Main { + + public static void main(String[] args) { + C c = new C(); + c.methode(); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/A.java b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/A.java new file mode 100644 index 0000000..c00eaed --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/A.java @@ -0,0 +1,13 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.mehrfachvererbung; + +public class A implements I1, I2 { + + public static void main(String[] args) { + //System.out.println(KONSTANTE); + System.out.println(I1.KONSTANTE); + System.out.println(I2.KONSTANTE); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I1.java b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I1.java new file mode 100644 index 0000000..d3936ea --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I1.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.mehrfachvererbung; + +public interface I1 { + public static final int KONSTANTE = 42; +} diff --git a/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I2.java b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I2.java new file mode 100644 index 0000000..4d3168a --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/mehrfachvererbung/I2.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.mehrfachvererbung; + +public interface I2 { + public static final int KONSTANTE = 23; +} diff --git a/src/main/java/pr2/oo2/interfaces/v1/Flieger.java b/src/main/java/pr2/oo2/interfaces/v1/Flieger.java new file mode 100644 index 0000000..e28747b --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v1/Flieger.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v1; + +public interface Flieger { + + public void starten(); + + public void landen(); + + public void fliegen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v1/Flughafen.java b/src/main/java/pr2/oo2/interfaces/v1/Flughafen.java new file mode 100644 index 0000000..0549710 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v1/Flughafen.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v1; + +public class Flughafen { + + public static void main(String[] args) { + Flieger flieger = new Flugzeug(); + + flieger.landen(); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v1/Flugzeug.java b/src/main/java/pr2/oo2/interfaces/v1/Flugzeug.java new file mode 100644 index 0000000..744f038 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v1/Flugzeug.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v1; + +public class Flugzeug implements Flieger { + + public void fliegen() { + // ... + } + + public void landen() { + // ... + } + + public void starten() { + // .. + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v2/Flieger.java b/src/main/java/pr2/oo2/interfaces/v2/Flieger.java new file mode 100644 index 0000000..0efb9bf --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v2/Flieger.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v2; + +public interface Flieger { + + int constant = 12; + + void starten(); + + void landen(); + + void fliegen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v2/Flieger2.java b/src/main/java/pr2/oo2/interfaces/v2/Flieger2.java new file mode 100644 index 0000000..e443865 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v2/Flieger2.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v2; + +public interface Flieger2 { + + public static final int constant = 12; + + public abstract void starten(); + + public abstract void landen(); + + public abstract void fliegen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v2/Flugzeug.java b/src/main/java/pr2/oo2/interfaces/v2/Flugzeug.java new file mode 100644 index 0000000..93dbe9c --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v2/Flugzeug.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v2; + +public class Flugzeug implements Flieger { + + public void fliegen() { + // ... + } + + public void landen() { + // ... + } + + public void starten() { + // .. + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Airbus.java b/src/main/java/pr2/oo2/interfaces/v3/Airbus.java new file mode 100644 index 0000000..3dbf6a9 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Airbus.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Airbus extends Flugzeug { + +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Ente.java b/src/main/java/pr2/oo2/interfaces/v3/Ente.java new file mode 100644 index 0000000..8c9554b --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Ente.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Ente extends Vogel implements Schwimmer, Flieger { + + public void eierLegen() { // ... + } + + public void schwimmen() { // ... + } + + public void fliegen() { // ... + } + + public void landen() { // ... + } + + public void starten() { // ... + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Flieger.java b/src/main/java/pr2/oo2/interfaces/v3/Flieger.java new file mode 100644 index 0000000..d243e13 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Flieger.java @@ -0,0 +1,10 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public interface Flieger { + public void starten(); + public void landen(); + public void fliegen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Flugzeug.java b/src/main/java/pr2/oo2/interfaces/v3/Flugzeug.java new file mode 100644 index 0000000..951dcd6 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Flugzeug.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Flugzeug extends Maschine implements Flieger { + + public void fliegen() { + // ... + } + + public void landen() { + // ... + } + + public void starten() { + // .. + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Maschine.java b/src/main/java/pr2/oo2/interfaces/v3/Maschine.java new file mode 100644 index 0000000..5a90b44 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Maschine.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Maschine { + +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Schwimmer.java b/src/main/java/pr2/oo2/interfaces/v3/Schwimmer.java new file mode 100644 index 0000000..9e8fc85 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Schwimmer.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public interface Schwimmer { + + public void schwimmen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Superman.java b/src/main/java/pr2/oo2/interfaces/v3/Superman.java new file mode 100644 index 0000000..87a862e --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Superman.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Superman implements Flieger { + + public void fliegen() { // ... + } + + public void landen() { // ... + } + + public void starten() { // ... + } + + public void weltRetten() { // ... + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Tier.java b/src/main/java/pr2/oo2/interfaces/v3/Tier.java new file mode 100644 index 0000000..aa4cd45 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Tier.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public abstract class Tier { + public void schlafen() { // ... + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Vogel.java b/src/main/java/pr2/oo2/interfaces/v3/Vogel.java new file mode 100644 index 0000000..11f222d --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Vogel.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public abstract class Vogel extends Tier { + public abstract void eierLegen(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v3/Wasserflugzeug.java b/src/main/java/pr2/oo2/interfaces/v3/Wasserflugzeug.java new file mode 100644 index 0000000..f38f10d --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v3/Wasserflugzeug.java @@ -0,0 +1,10 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v3; + +public class Wasserflugzeug extends Flugzeug implements Schwimmer { + + public void schwimmen() { // ... + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v4/Konstanten.java b/src/main/java/pr2/oo2/interfaces/v4/Konstanten.java new file mode 100644 index 0000000..2e0c998 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v4/Konstanten.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v4; + +public interface Konstanten { + public static final double PI = 3.14159265; +} diff --git a/src/main/java/pr2/oo2/interfaces/v4/Mathe.java b/src/main/java/pr2/oo2/interfaces/v4/Mathe.java new file mode 100644 index 0000000..d27d66d --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v4/Mathe.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v4; + +public class Mathe implements Konstanten { + + public double kreisFlaeche(double radius) { + return PI*radius*radius; + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v4/Mathe2.java b/src/main/java/pr2/oo2/interfaces/v4/Mathe2.java new file mode 100644 index 0000000..bdc0703 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v4/Mathe2.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v4; + +import static pr2.oo2.interfaces.v4.Konstanten.*; + +public class Mathe2 { + + public double kreisFlaeche(double radius) { + return PI*radius*radius; + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v5/Sorter.java b/src/main/java/pr2/oo2/interfaces/v5/Sorter.java new file mode 100644 index 0000000..cfffc45 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v5/Sorter.java @@ -0,0 +1,19 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.interfaces.v5; + +import java.util.Arrays; + +public class Sorter { + + public static void main(String[] args) { + Telefonnummer[] nummern = { + new Telefonnummer(6221, 72643), + new Telefonnummer(7262, 5558912), + new Telefonnummer(6221, 71263), + new Telefonnummer(7261, 5558912) }; + + Arrays.sort(nummern); + + System.out.println(Arrays.asList(nummern).toString()); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v5/Telefonnummer.java b/src/main/java/pr2/oo2/interfaces/v5/Telefonnummer.java new file mode 100644 index 0000000..5b7a4d1 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v5/Telefonnummer.java @@ -0,0 +1,40 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.interfaces.v5; + +@SuppressWarnings("rawtypes") +public class Telefonnummer implements Comparable { + + private int vorwahl; + private int nummer; + + public int compareTo(Object o) { + Telefonnummer n = (Telefonnummer) o; + + if (vorwahl > n.vorwahl) { + return 1; + } else if (vorwahl < n.vorwahl) { + return -1; + } else { + // vorwahl == n.vorwahl + if (nummer > n.nummer) { + return 1; + } else if (nummer < n.nummer) { + return -1; + } + } + + return 0; + } + + public Telefonnummer(int vorwahl, int nummer) { + this.vorwahl = vorwahl; + this.nummer = nummer; + } + + @Override + public String toString() { + return String.format("(%d) %d", vorwahl, nummer); + } +} diff --git a/src/main/java/pr2/oo2/interfaces/v6/A.java b/src/main/java/pr2/oo2/interfaces/v6/A.java new file mode 100644 index 0000000..2f9b4a2 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v6/A.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.v6; + +public class A implements I1 { + public void m1() { /* leer */ } +} diff --git a/src/main/java/pr2/oo2/interfaces/v6/B.java b/src/main/java/pr2/oo2/interfaces/v6/B.java new file mode 100644 index 0000000..efb925d --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v6/B.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.v6; + +public class B extends A implements I2 { + public void m2() { /* leer */ } +} diff --git a/src/main/java/pr2/oo2/interfaces/v6/I1.java b/src/main/java/pr2/oo2/interfaces/v6/I1.java new file mode 100644 index 0000000..35197c8 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v6/I1.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.v6; + +public interface I1 { + public abstract void m1(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v6/I2.java b/src/main/java/pr2/oo2/interfaces/v6/I2.java new file mode 100644 index 0000000..4154b81 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v6/I2.java @@ -0,0 +1,8 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.v6; + +public interface I2 { + public abstract void m2(); +} diff --git a/src/main/java/pr2/oo2/interfaces/v6/Main.java b/src/main/java/pr2/oo2/interfaces/v6/Main.java new file mode 100644 index 0000000..4dce735 --- /dev/null +++ b/src/main/java/pr2/oo2/interfaces/v6/Main.java @@ -0,0 +1,18 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.oo2.interfaces.v6; + +public class Main { + public static void main(String[] args) { + A a = new A(); + B b = new B(); + + System.out.println(a instanceof I1); // --> true + System.out.println(a instanceof I2); // --> false + System.out.println(b instanceof A); // --> true + System.out.println(b instanceof I1); // --> true + System.out.println(b instanceof I2); // --> true + + } +} diff --git a/src/main/java/pr2/oo2/quiz/Ente.java b/src/main/java/pr2/oo2/quiz/Ente.java new file mode 100644 index 0000000..97b8cb3 --- /dev/null +++ b/src/main/java/pr2/oo2/quiz/Ente.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.quiz; + +public class Ente { + + private static final Ente INSTANCE = new Ente(); + private static final int NORMAL_GROESSE = Integer.parseInt("12"); + + private int groesse = NORMAL_GROESSE - 10; + + public int getGroesse() { + return groesse; + } + + public static void main(String[] args) { + System.out.println(INSTANCE.getGroesse()); + } +} diff --git a/src/main/java/pr2/oo2/quiz/Initializer.java b/src/main/java/pr2/oo2/quiz/Initializer.java new file mode 100644 index 0000000..b4d1700 --- /dev/null +++ b/src/main/java/pr2/oo2/quiz/Initializer.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.quiz; + +public class Initializer { + + static { + initialize(); + } + + private static boolean initialized = false; + + private static void initialize() { + initialized = true; + } + + public static void main(String[] args) { + System.out.println(initialized); + } +} diff --git a/src/main/java/pr2/oo2/quiz/QuizStatic.java b/src/main/java/pr2/oo2/quiz/QuizStatic.java new file mode 100644 index 0000000..ff7945a --- /dev/null +++ b/src/main/java/pr2/oo2/quiz/QuizStatic.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.quiz; + +public class QuizStatic { + + @SuppressWarnings("static-access") + public static void main(String[] args) { + String b = null; + System.out.println(b.valueOf(15)); + + System.out.println(((String) null).valueOf(17)); + + } +} diff --git a/src/main/java/pr2/oo2/statisch/apiimpl/Calculator.java b/src/main/java/pr2/oo2/statisch/apiimpl/Calculator.java new file mode 100644 index 0000000..3e53e3f --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/apiimpl/Calculator.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.apiimpl; + +public interface Calculator { + public abstract int add(int a, int b); + public abstract int sub(int a, int b); +} diff --git a/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorFactory.java b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorFactory.java new file mode 100644 index 0000000..6f1abb8 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorFactory.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.apiimpl; + +public class CalculatorFactory { + + private CalculatorFactory() {} + + public static Calculator createCalculator() { + return new CalculatorImpl(); + } +} diff --git a/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImpl.java b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImpl.java new file mode 100644 index 0000000..b254b96 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImpl.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.apiimpl; + +public class CalculatorImpl implements Calculator { + + public int add(int a, int b) { + return a + b; + } + + public int sub(int a, int b) { + return a - b; + } +} diff --git a/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImplQuantumSuperDuper.java b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImplQuantumSuperDuper.java new file mode 100644 index 0000000..ddaa873 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/apiimpl/CalculatorImplQuantumSuperDuper.java @@ -0,0 +1,22 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.apiimpl; + +public class CalculatorImplQuantumSuperDuper implements Calculator { + + @Override + public int add(int a, int b) { + Integer ai = a; + Integer bi = b; + Integer r = ai + bi; + return r; + } + + @Override + public int sub(int a, int b) { + Integer ai = a; + Integer bi = b; + Integer r = ai - bi; + return r; + } + +} diff --git a/src/main/java/pr2/oo2/statisch/apiimpl/Verwender.java b/src/main/java/pr2/oo2/statisch/apiimpl/Verwender.java new file mode 100644 index 0000000..8c2ded5 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/apiimpl/Verwender.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.apiimpl; + +public class Verwender { + + public static void main(String[] args) { + + Calculator calculator = CalculatorFactory.createCalculator(); + int ergebnis = calculator.add(1, 5); + System.out.println(ergebnis); + } +} diff --git a/src/main/java/pr2/oo2/statisch/cycle/A.java b/src/main/java/pr2/oo2/statisch/cycle/A.java new file mode 100644 index 0000000..0244de5 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/cycle/A.java @@ -0,0 +1,16 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.cycle; + +public class A { + + static B b; + + static { + b = new B(); + System.out.println("static A"); + } + + A() { + System.out.println("A():" + b); + } +} diff --git a/src/main/java/pr2/oo2/statisch/cycle/B.java b/src/main/java/pr2/oo2/statisch/cycle/B.java new file mode 100644 index 0000000..17cf10f --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/cycle/B.java @@ -0,0 +1,16 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.cycle; + +public class B { + + static A a; + + static { + a = new A(); + System.out.println("static B"); + } + + B() { + System.out.println("B():" + a); + } +} diff --git a/src/main/java/pr2/oo2/statisch/cycle/C.java b/src/main/java/pr2/oo2/statisch/cycle/C.java new file mode 100644 index 0000000..3404825 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/cycle/C.java @@ -0,0 +1,10 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.cycle; + +public class C { + + public static void main(String[] args) throws Exception { + System.out.println("main: A.b = " + A.b); + System.out.println("main: B.a = " + B.a); + } +} diff --git a/src/main/java/pr2/oo2/statisch/factory/Erzeugter.java b/src/main/java/pr2/oo2/statisch/factory/Erzeugter.java new file mode 100644 index 0000000..48f0865 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/factory/Erzeugter.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.factory; + +public class Erzeugter { + + int wert; + + Erzeugter(int wert) { + this.wert = wert; + } +} diff --git a/src/main/java/pr2/oo2/statisch/factory/Factory.java b/src/main/java/pr2/oo2/statisch/factory/Factory.java new file mode 100644 index 0000000..8491ffe --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/factory/Factory.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.factory; + +public class Factory { + + public static Erzeugter create(int wert) { + return new Erzeugter(wert); + } +} diff --git a/src/main/java/pr2/oo2/statisch/initializer/Crash.java b/src/main/java/pr2/oo2/statisch/initializer/Crash.java new file mode 100644 index 0000000..484f94d --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/initializer/Crash.java @@ -0,0 +1,13 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.initializer; + +public class Crash { + + static { + int x = 5 / 0; + } + + public static int add(int a, int b) { + return a + b; + } +} diff --git a/src/main/java/pr2/oo2/statisch/initializer/CrashUser.java b/src/main/java/pr2/oo2/statisch/initializer/CrashUser.java new file mode 100644 index 0000000..1a3f57e --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/initializer/CrashUser.java @@ -0,0 +1,9 @@ +/* (c) 2022 Thomas Smits */ +package pr2.oo2.statisch.initializer; + +public class CrashUser { + + public static void main(String[] args) { + System.out.println(Crash.add(5, 5)); + } +} diff --git a/src/main/java/pr2/oo2/statisch/singleton/Broken.java b/src/main/java/pr2/oo2/statisch/singleton/Broken.java new file mode 100644 index 0000000..884154c --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/singleton/Broken.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.singleton; + +public class Broken { + + private static Broken instanz; + + private Broken() { + // Keine Instanzen! + } + + public static Broken getInstance() { + if (instanz == null) { + synchronized (Broken.class) { + if (instanz == null) { + instanz = new Broken(); + } + } + } + + return instanz; + } +} diff --git a/src/main/java/pr2/oo2/statisch/singleton/Firma.java b/src/main/java/pr2/oo2/statisch/singleton/Firma.java new file mode 100644 index 0000000..9cfdeef --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/singleton/Firma.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.singleton; + +public class Firma { + + private static final Firma instanz = new Firma(); + + public static Firma getInstance() { + return instanz; + } + + private Firma() { + // Keine Instanzen dieser Klasse! + } +} diff --git a/src/main/java/pr2/oo2/statisch/singleton/Firma2.java b/src/main/java/pr2/oo2/statisch/singleton/Firma2.java new file mode 100644 index 0000000..a509126 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/singleton/Firma2.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.singleton; + +public class Firma2 { + + private static final Firma2 instanz; + + static { + instanz = new Firma2(); + } + + public static Firma2 getInstance() { + return instanz; + } + + private Firma2() { + // Keine Instanzen dieser Klasse! + } +} diff --git a/src/main/java/pr2/oo2/statisch/v1/Ticket.java b/src/main/java/pr2/oo2/statisch/v1/Ticket.java new file mode 100644 index 0000000..f25bade --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v1/Ticket.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v1; + +public class Ticket { + + public static int zaehler = 0; + + private int serienNummer; + private String spiel; + + public Ticket(String spiel) { + zaehler++; + serienNummer = zaehler; + this.spiel = spiel; + } +} diff --git a/src/main/java/pr2/oo2/statisch/v2/TestTicket.java b/src/main/java/pr2/oo2/statisch/v2/TestTicket.java new file mode 100644 index 0000000..857baff --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v2/TestTicket.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v2; + +public class TestTicket { + public static void main(String[] args) { + + Ticket t1 = new Ticket("Dortmund-Schalke"); + System.out.println(Ticket.getZaehler()); + + Ticket t2 = new Ticket("Hoffenheim-Leverkusen"); + System.out.println(Ticket.getZaehler()); + } +} diff --git a/src/main/java/pr2/oo2/statisch/v2/Ticket.java b/src/main/java/pr2/oo2/statisch/v2/Ticket.java new file mode 100644 index 0000000..3da21c1 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v2/Ticket.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v2; + +public class Ticket { + + private static int zaehler = 0; + + private int serienNummer; + private String spiel; + + public Ticket(String spiel) { + zaehler++; + serienNummer = zaehler; + this.spiel = spiel; + } + + public static int getZaehler() { + return zaehler; + } +} diff --git a/src/main/java/pr2/oo2/statisch/v3/TestTicket.java b/src/main/java/pr2/oo2/statisch/v3/TestTicket.java new file mode 100644 index 0000000..33deb09 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v3/TestTicket.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v3; + +public class TestTicket { + public static void main(String[] args) { + + Ticket t1 = new Ticket("Dortmund-Schalke"); + System.out.println(Ticket.getZaehler()); + Ticket t2 = new Ticket("Hoffenheim-Leverkusen"); + System.out.println(Ticket.getZaehler()); + } +} diff --git a/src/main/java/pr2/oo2/statisch/v3/Ticket.java b/src/main/java/pr2/oo2/statisch/v3/Ticket.java new file mode 100644 index 0000000..4d11e37 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v3/Ticket.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v3; + +public class Ticket { + + private static int zaehler; + + private int serienNummer; + private String spiel; + + static { + zaehler = Integer.parseInt(System.getProperty("zaehler_start")); + } + + public Ticket(String spiel) { + zaehler++; + serienNummer = zaehler; + this.spiel = spiel; + } + + static { + System.out.println("Ticket initialisiert: zaehler=" + zaehler); + } + + public static int getZaehler() { + return zaehler; + } +} diff --git a/src/main/java/pr2/oo2/statisch/v4/A.java b/src/main/java/pr2/oo2/statisch/v4/A.java new file mode 100644 index 0000000..ee5bcd8 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v4/A.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v4; + +public class A { + + public static void tuWas() { + System.out.println("A:tuWas()"); + } +} diff --git a/src/main/java/pr2/oo2/statisch/v4/B.java b/src/main/java/pr2/oo2/statisch/v4/B.java new file mode 100644 index 0000000..84c78a3 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v4/B.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v4; + +public class B extends A { + + public static void tuWas() { + System.out.println("B:tuWas()"); + } +} diff --git a/src/main/java/pr2/oo2/statisch/v4/Test.java b/src/main/java/pr2/oo2/statisch/v4/Test.java new file mode 100644 index 0000000..19eee81 --- /dev/null +++ b/src/main/java/pr2/oo2/statisch/v4/Test.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.statisch.v4; + +public class Test { + + @SuppressWarnings("static-access") + public static void main(String[] args) { + A a1 = new A(); + B b = new B(); + A a2 = new B(); + a1.tuWas(); + b.tuWas(); + a2.tuWas(); + } +} diff --git a/src/main/java/pr2/oo2/wrapper/AutoboxingDemo.java b/src/main/java/pr2/oo2/wrapper/AutoboxingDemo.java new file mode 100644 index 0000000..399f412 --- /dev/null +++ b/src/main/java/pr2/oo2/wrapper/AutoboxingDemo.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.wrapper; + +public class AutoboxingDemo { + public static void main(String[] args) { + + Integer k = 5; + int j = k; + + Object o = true; + boolean b = Boolean.valueOf(true); + + Long l = 0L; + + for (int i = 0; i < Integer.MAX_VALUE; i++) { + l += i; + } + } +} diff --git a/src/main/java/pr2/oo2/wrapper/BoxingFailure.java b/src/main/java/pr2/oo2/wrapper/BoxingFailure.java new file mode 100644 index 0000000..d49e88b --- /dev/null +++ b/src/main/java/pr2/oo2/wrapper/BoxingFailure.java @@ -0,0 +1,21 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.wrapper; + +public class BoxingFailure { + + public static boolean compare(Integer i1, Integer i2) { + return i1 == i2; + } + + public static void main(String[] args) { + int i1 = 10; + int i2 = 10; + System.out.println(i1 + " == " + i2 + " : " + compare(i1, i2)); + + i1 = 2000; + i2 = 2000; + System.out.println(i1 + " == " + i2 + " : " + compare(i1, i2)); + } +} diff --git a/src/main/java/pr2/oo2/wrapper/WrapperDemo.java b/src/main/java/pr2/oo2/wrapper/WrapperDemo.java new file mode 100644 index 0000000..bddaaee --- /dev/null +++ b/src/main/java/pr2/oo2/wrapper/WrapperDemo.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.oo2.wrapper; + +public class WrapperDemo { + + public static void main(String[] args) { + int i = 5; + Integer i1 = Integer.valueOf(i); + int j = i1.intValue(); + + Integer i2 = Integer.valueOf("42"); + int i3 = Integer.parseInt("42"); + } +} diff --git a/src/main/java/pr2/records/newstyle/Rectangle.java b/src/main/java/pr2/records/newstyle/Rectangle.java new file mode 100644 index 0000000..1579476 --- /dev/null +++ b/src/main/java/pr2/records/newstyle/Rectangle.java @@ -0,0 +1,26 @@ +package pr2.records.newstyle; + +record Rectangle(double length, double width) { + public Rectangle { + if (length <= 0 || width <= 0) { + throw new java.lang.IllegalArgumentException( + String.format("Invalid dimensions: %f, %f", length, width)); + } + } + + public double length() { + return length; + } + + public double area() { + return length * width; + } +} + +class Demo { + public static void main(String[] args) { + Rectangle r = new Rectangle(4, 3); + System.out.println("Fläche: " + r.length() * r.width()); + System.out.println("Fläche: " + r.area()); + } +} diff --git a/src/main/java/pr2/records/plain/Rectangle.java b/src/main/java/pr2/records/plain/Rectangle.java new file mode 100644 index 0000000..b9e799b --- /dev/null +++ b/src/main/java/pr2/records/plain/Rectangle.java @@ -0,0 +1,55 @@ +package pr2.records.plain; + +public final class Rectangle { + + private final double length; + private final double width; + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + public double length() { return this.length; } + public double width() { return this.width; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Rectangle rectangle = (Rectangle) o; + + if (Double.compare(rectangle.length, length) != 0) { + return false; + } + return Double.compare(rectangle.width, width) == 0; + } + + @Override + public int hashCode() { + int result; + long temp; + temp = Double.doubleToLongBits(length); + result = (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(width); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public String toString() { + return "Rechteck{" + "length=" + length + ", width=" + width + '}'; + } +} + +class Demo { + public static void main(String[] args) { + Rectangle r = new Rectangle(4, 3); + System.out.println("Fläche: " + r.length() * r.width()); + } +} diff --git a/src/main/java/pr2/reflection/Voldemort.java b/src/main/java/pr2/reflection/Voldemort.java new file mode 100644 index 0000000..3a0244f --- /dev/null +++ b/src/main/java/pr2/reflection/Voldemort.java @@ -0,0 +1,40 @@ +/* (c) 2022 Thomas Smits */ +package pr2.reflection; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +/** + * + * + * @author Thomas Smits + */ +public class Voldemort { + + public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { + String s = new String("Voldemort"); + Class clazz = s.getClass(); + Field f = clazz.getDeclaredField("value"); + f.setAccessible(true); + char[] zeichen = (char[]) f.get(s); + zeichen[0] = 'M'; + zeichen[1] = 'u'; + zeichen[2] = 'f'; + zeichen[3] = 'l'; + System.out.println(s); + + System.out.println("Voldemort"); + + Map m = new HashMap(); + m.put("Harry", "Potter"); + + Field f2 = clazz.getDeclaredField("hash"); + f2.setAccessible(true); + f2.set("Harry", 6); + System.out.println(m.get("Harry")); + System.out.println(m); + + + } +} diff --git a/src/main/java/pr2/reflection/api/Arrays.java b/src/main/java/pr2/reflection/api/Arrays.java new file mode 100644 index 0000000..9db6911 --- /dev/null +++ b/src/main/java/pr2/reflection/api/Arrays.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.api; + +import java.lang.reflect.Array; + +public class Arrays { + + public static void main(String[] args) { + + Class elementTyp = String.class; + Object arrayInstanz = Array.newInstance(elementTyp, 4); + Array.set(arrayInstanz, 0, "Erster Eintrag"); + Array.set(arrayInstanz, 1, "Zweiter Eintrag"); + Array.set(arrayInstanz, 2, "Dritter Eintrag"); + Array.set(arrayInstanz, 3, "Vierter Eintrag"); + + int length = Array.getLength(arrayInstanz); + + for (int i = 0; i < length; i++) { + Object element = Array.get(arrayInstanz, i); + System.out.println(element); + } + } +} diff --git a/src/main/java/pr2/reflection/api/Fields.java b/src/main/java/pr2/reflection/api/Fields.java new file mode 100644 index 0000000..55162f4 --- /dev/null +++ b/src/main/java/pr2/reflection/api/Fields.java @@ -0,0 +1,46 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.api; + +import java.awt.Point; +import java.lang.reflect.Field; + +public class Fields { + + public static void main(String[] args) { + + + try { + { + Class clazz = Integer.class; + Field field = clazz.getField("MAX_VALUE"); + int value = field.getInt(null); + System.out.println(value); // -> 2147483647 + } + { + Point point = new Point(); + Class clazz = point.getClass(); + Field fieldX = clazz.getField("x"); + Field fieldY = clazz.getField("y"); + fieldX.setInt(point, 640); + fieldY.setInt(point, 480); + System.out.println(point); // -> java.awt.Point[x=640,y=480] + } + + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchFieldException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/pr2/reflection/api/Instanzen.java b/src/main/java/pr2/reflection/api/Instanzen.java new file mode 100644 index 0000000..22e4592 --- /dev/null +++ b/src/main/java/pr2/reflection/api/Instanzen.java @@ -0,0 +1,42 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.api; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Date; + +public class Instanzen { + + public static void main(String[] args) { + + Class clazz = Date.class; + + try { + Constructor constructor = clazz.getConstructor(long.class); + Object o = constructor.newInstance(222222222222L); + System.out.println(o); // -> Sun Jan 16 01:23:42 CET 1977 + } + catch (SecurityException e) { + // darf keine Reflection machen + } + catch (NoSuchMethodException e) { + // es gibt den Construktor nicht + } + catch (IllegalArgumentException e) { + // Argumente passen nicht + } + catch (InstantiationException e) { + // Klasse ist abstrakt + } + catch (IllegalAccessException e) { + // Konstruktor ist nicht public + } + catch (InvocationTargetException e) { + // Konstruktor wirft selber eine Exception + } + + System.out.println(new Date(222222222222L)); + } +} diff --git a/src/main/java/pr2/reflection/api/Methoden.java b/src/main/java/pr2/reflection/api/Methoden.java new file mode 100644 index 0000000..bc58aa3 --- /dev/null +++ b/src/main/java/pr2/reflection/api/Methoden.java @@ -0,0 +1,51 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.api; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class Methoden { + + public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { + aufObjekt(); + aufKlasse(); + } + + public static void aufKlasse() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { + Class clazz = Integer.class; + Method m = clazz.getMethod("toHexString", int.class); + String result = (String) m.invoke(null, 255); + System.out.println(result); // -> ff + } + + public static void aufObjekt() { + + String s = "Hello "; + Class clazz = s.getClass(); + + try { + Method m = clazz.getMethod("concat", String.class); + Object result = m.invoke(s, "World!"); + System.out.println(result); // -> Hello World! + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + System.out.println("Hello " + "World!"); + } +} diff --git a/src/main/java/pr2/reflection/apiimpltrennung/Verwender.java b/src/main/java/pr2/reflection/apiimpltrennung/Verwender.java new file mode 100644 index 0000000..6a77fe2 --- /dev/null +++ b/src/main/java/pr2/reflection/apiimpltrennung/Verwender.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.apiimpltrennung; + +import pr2.reflection.apiimpltrennung.api.Calculator; +import pr2.reflection.apiimpltrennung.api.CalculatorFactory; + +public class Verwender { + + public static void main(String[] args) { + + Calculator calculator = CalculatorFactory.createCalculator(); + int ergebnis = calculator.add(1, 5); + System.out.println(ergebnis); + } +} diff --git a/src/main/java/pr2/reflection/apiimpltrennung/api/Calculator.java b/src/main/java/pr2/reflection/apiimpltrennung/api/Calculator.java new file mode 100644 index 0000000..cca1b84 --- /dev/null +++ b/src/main/java/pr2/reflection/apiimpltrennung/api/Calculator.java @@ -0,0 +1,9 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.apiimpltrennung.api; + +public interface Calculator { + public abstract int add(int a, int b); + public abstract int sub(int a, int b); +} diff --git a/src/main/java/pr2/reflection/apiimpltrennung/api/CalculatorFactory.java b/src/main/java/pr2/reflection/apiimpltrennung/api/CalculatorFactory.java new file mode 100644 index 0000000..2d0d9e8 --- /dev/null +++ b/src/main/java/pr2/reflection/apiimpltrennung/api/CalculatorFactory.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.apiimpltrennung.api; + +import java.lang.reflect.InvocationTargetException; + +public class CalculatorFactory { + + private static final String IMPL_CLASS = + "pr2.reflection.apiimpltrennung.impl.CalculatorImpl"; + + private CalculatorFactory() {} + + public static Calculator createCalculator() { + Class clazz; + try { + clazz = Class.forName(IMPL_CLASS); + return (Calculator) clazz.getDeclaredConstructor().newInstance(); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(IMPL_CLASS + " nicht gefunden"); + } catch (InstantiationException e) { + throw new IllegalStateException(IMPL_CLASS + " hat einen Fehler geworfen: " + e); + } catch (IllegalAccessException e) { + throw new IllegalStateException(IMPL_CLASS + " hat keinen public Konstruktor"); + } catch (InvocationTargetException e) { + throw new IllegalStateException(IMPL_CLASS + " hat keinen public Konstruktor"); + } catch (NoSuchMethodException e) { + throw new IllegalStateException(IMPL_CLASS + " hat keinen public Konstruktor"); + } + } +} diff --git a/src/main/java/pr2/reflection/apiimpltrennung/impl/CalculatorImpl.java b/src/main/java/pr2/reflection/apiimpltrennung/impl/CalculatorImpl.java new file mode 100644 index 0000000..1d97eaf --- /dev/null +++ b/src/main/java/pr2/reflection/apiimpltrennung/impl/CalculatorImpl.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.apiimpltrennung.impl; + +import pr2.reflection.apiimpltrennung.api.Calculator; + +public class CalculatorImpl implements Calculator { + + public int add(int a, int b) { + return a + b; + } + + public int sub(int a, int b) { + return a - b; + } +} diff --git a/src/main/java/pr2/reflection/classloader/A.java b/src/main/java/pr2/reflection/classloader/A.java new file mode 100644 index 0000000..f0b85b1 --- /dev/null +++ b/src/main/java/pr2/reflection/classloader/A.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classloader; + +public class A { + + static { + System.out.println("A geladen"); + } +} diff --git a/src/main/java/pr2/reflection/classloader/Test.java b/src/main/java/pr2/reflection/classloader/Test.java new file mode 100644 index 0000000..a569024 --- /dev/null +++ b/src/main/java/pr2/reflection/classloader/Test.java @@ -0,0 +1,14 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classloader; + +public class Test { + + public static void main(String[] args) { + + Integer i; + i = Integer.valueOf(5); + + } +} diff --git a/src/main/java/pr2/reflection/classobj/Angestellter.java b/src/main/java/pr2/reflection/classobj/Angestellter.java new file mode 100644 index 0000000..26020db --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Angestellter.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +public class Angestellter extends Mitarbeiter { + + private double zielbonus; + private double zielerreichung; + + public double zielbonus() { + return zielbonus; + } + + public double zielerreichung() { + return zielerreichung; + } +} diff --git a/src/main/java/pr2/reflection/classobj/Basics.java b/src/main/java/pr2/reflection/classobj/Basics.java new file mode 100644 index 0000000..ca17923 --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Basics.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +public class Basics { + + public static void main(String[] args) throws ClassNotFoundException { + + { + Class clazz = String.class; + } + { + Class clazz = Class.forName("java.lang.String"); + } + { + Class clazz = "Hello World".getClass(); + Class a = int.class; + } + { + Class clazz = Integer.TYPE; + System.out.println(void.class.toString()); + } + { + ClassLoader cl = ClassLoader.getSystemClassLoader(); + Class clazz = ClassLoader.getSystemClassLoader().loadClass("java.lang.String"); + } + } +} diff --git a/src/main/java/pr2/reflection/classobj/ClazzDemo.java b/src/main/java/pr2/reflection/classobj/ClazzDemo.java new file mode 100644 index 0000000..07664fb --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/ClazzDemo.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class ClazzDemo { + + public static void main(String[] args) { + + Class clazz = Geschaeftsfuehrer.class; + + System.out.println(clazz.getSuperclass()); // class pr2.reflection.classobj.Manager + System.out.println(clazz.getName()); // pr2.reflection.classobj.Geschaeftsfuehrer + System.out.println(clazz.isInterface()); // false + System.out.println(clazz.getPackage()); // package pr2.reflection.classobj + System.out.println(clazz.isAssignableFrom(Mitarbeiter.class)); // false + System.out.println(Mitarbeiter.class.isAssignableFrom(clazz)); // true + System.out.println(clazz.isInstance(new Vorstand())); // false + System.out.println(clazz.isInstance(new Geschaeftsfuehrer())); // true + printArray(clazz.getFields()); // [] + printArray(clazz.getConstructors()); // [public pr2.reflection.classobj.Geschaeftsfuehrer()] + printArray(clazz.getMethods()); + // [public boolean java.lang.Object.equals(java.lang.Object), + // public java.lang.String pr2.reflection.classobj.Geschaeftsfuehrer.getDetails() + // public final native void java.lang.Object.notify(), + // ... + + } + + private static void printArray(Object[] array) { + Set set = new HashSet(); + Collections.addAll(set, array); + System.out.println(set); + } +} diff --git a/src/main/java/pr2/reflection/classobj/Geschaeftsfuehrer.java b/src/main/java/pr2/reflection/classobj/Geschaeftsfuehrer.java new file mode 100644 index 0000000..f370e57 --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Geschaeftsfuehrer.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +public class Geschaeftsfuehrer extends Manager { + public boolean prokura = true; + + public String getDetails() { + return super.getDetails() + + " und hat Prokura " + prokura; + } +} diff --git a/src/main/java/pr2/reflection/classobj/Manager.java b/src/main/java/pr2/reflection/classobj/Manager.java new file mode 100644 index 0000000..e20fd5e --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Manager.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + + +public class Manager extends Mitarbeiter { + public String abteilung; + + @Override + public String getDetails() { + return super.getDetails() + + " und leitet die Abteilung " + abteilung; + } +} diff --git a/src/main/java/pr2/reflection/classobj/Mitarbeiter.java b/src/main/java/pr2/reflection/classobj/Mitarbeiter.java new file mode 100644 index 0000000..81b4701 --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Mitarbeiter.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +import java.util.Date; + +public class Mitarbeiter { + public String name; + public double gehalt; + public Date geboren; + + public String getDetails() { + return name + ", geb. am " + geboren + + " hat ein Gehalt von " + gehalt; + } +} diff --git a/src/main/java/pr2/reflection/classobj/Vorstand.java b/src/main/java/pr2/reflection/classobj/Vorstand.java new file mode 100644 index 0000000..7ece00e --- /dev/null +++ b/src/main/java/pr2/reflection/classobj/Vorstand.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.classobj; + +public class Vorstand extends Manager { + + private double firmengewinn; + + public double zielbonus() { + return 0.5*firmengewinn; + } + + public double zielerreichung() { + return 1.00; + } +} diff --git a/src/main/java/pr2/reflection/ugly/Erdogan.java b/src/main/java/pr2/reflection/ugly/Erdogan.java new file mode 100644 index 0000000..510f796 --- /dev/null +++ b/src/main/java/pr2/reflection/ugly/Erdogan.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.ugly; + +import java.lang.reflect.Field; + +public class Erdogan { + + public static void main(String[] args) throws Exception { + + // Don't try this at home!!! + String immutable = "Erdogan ist böse"; + System.out.println(immutable); + + Class clazz = immutable.getClass(); + Field valueField = clazz.getDeclaredField("value"); + valueField.setAccessible(true); + char[] chars = (char[]) valueField.get(immutable); + chars[12] = 'g'; + chars[13] = 'u'; + chars[14] = 't'; + chars[15] = '!'; + System.out.println(immutable); + System.out.println("Ein anderer sagt:"); + System.out.println("Erdogan ist böse"); + } +} diff --git a/src/main/java/pr2/reflection/ugly/Killer.java b/src/main/java/pr2/reflection/ugly/Killer.java new file mode 100644 index 0000000..44940cf --- /dev/null +++ b/src/main/java/pr2/reflection/ugly/Killer.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.reflection.ugly; + +import java.lang.reflect.Field; + +public class Killer { + + public static void main(String[] args) throws Exception { + + // Don't try this at home!!! + String immutable = "immutable String"; + System.out.println(immutable); + System.out.println(immutable.hashCode()); + Class clazz = immutable.getClass(); + Field valueField = clazz.getDeclaredField("value"); + valueField.setAccessible(true); + char[] chars = (char[]) valueField.get(immutable); + chars[0] = ' '; + chars[1] = ' '; + System.out.println(immutable); + System.out.println(immutable.hashCode()); + } +} diff --git a/src/main/java/pr2/streams/advanced/CollectorAggregation.java b/src/main/java/pr2/streams/advanced/CollectorAggregation.java new file mode 100644 index 0000000..f05f3c5 --- /dev/null +++ b/src/main/java/pr2/streams/advanced/CollectorAggregation.java @@ -0,0 +1,27 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.advanced; + +import static java.util.stream.Collectors.averagingDouble; +import static java.util.stream.Collectors.summingDouble; + +import java.util.stream.Stream; + +public class CollectorAggregation { + + public static void main(String[] args) { + average(); + sum(); + } + + static void average() { + Stream stream = Stream.of(3.0, 2.0, 4.0, 1.0, 2.0, 2.7); + double average = stream.collect(averagingDouble(e -> e)); + System.out.printf("%.2f%n", average); // -> 2,45 + } + + static void sum() { + Stream stream = Stream.of(3.0, 2.0, 4.0, 1.0, 2.0, 2.7); + double sum = stream.collect(summingDouble(e -> e)); + System.out.printf("%.2f%n", sum); // -> 14,70 + } +} diff --git a/src/main/java/pr2/streams/advanced/CollectorType.java b/src/main/java/pr2/streams/advanced/CollectorType.java new file mode 100644 index 0000000..e5bc8f8 --- /dev/null +++ b/src/main/java/pr2/streams/advanced/CollectorType.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.advanced; + +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toCollection; + +public class CollectorType { + + public static void main(String[] args) { + Stream stream = Stream.of("A", "B", "C"); + List list = stream.collect(toCollection(LinkedList::new)); + System.out.println(list.getClass()); + } +} diff --git a/src/main/java/pr2/streams/advanced/Grouping.java b/src/main/java/pr2/streams/advanced/Grouping.java new file mode 100644 index 0000000..e3499c9 --- /dev/null +++ b/src/main/java/pr2/streams/advanced/Grouping.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.advanced; + +import static java.util.stream.Collectors.groupingBy; + +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +public class Grouping { + + public static void main(String[] args) { + Stream stream = Stream.of("USW", "GGT", "UA", "ZB", "FKK", + "MFG", "SOSE", "A", "H", "MM", "MBIT"); + + Map> result = + stream.collect(groupingBy(String::length)); + + System.out.println(result); + } +} diff --git a/src/main/java/pr2/streams/advanced/Joining.java b/src/main/java/pr2/streams/advanced/Joining.java new file mode 100644 index 0000000..4fdd453 --- /dev/null +++ b/src/main/java/pr2/streams/advanced/Joining.java @@ -0,0 +1,18 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.advanced; + +import java.util.stream.Stream; + +import static java.util.stream.Collectors.*; + +public class Joining { + + public static void main(String[] args) { + Stream stream = Stream.of("USW", "GGT", "UA", "ZB", "FKK", + "MFG", "SOSE", "A", "H", "MM", "MBIT"); + + String result = stream.collect(joining(" | ", "{ ", " }")); + + System.out.println(result); + } +} diff --git a/src/main/java/pr2/streams/advanced/Partition.java b/src/main/java/pr2/streams/advanced/Partition.java new file mode 100644 index 0000000..cea740d --- /dev/null +++ b/src/main/java/pr2/streams/advanced/Partition.java @@ -0,0 +1,24 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.advanced; + +import static java.util.stream.Collectors.partitioningBy; + +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +public class Partition { + + public static void main(String[] args) { + Stream stream = Stream.of('A', '2', 'X', '!', '3', '7'); + + Map> result = + stream.collect(partitioningBy(Character::isLetter)); + + List letters = result.get(true); + List other = result.get(false); + + System.out.println(letters); // -> [A, X] + System.out.println(other); // -> [2, !, 3, 7] + } +} diff --git a/src/main/java/pr2/streams/basics/CollectSimple.java b/src/main/java/pr2/streams/basics/CollectSimple.java new file mode 100644 index 0000000..90ee2a5 --- /dev/null +++ b/src/main/java/pr2/streams/basics/CollectSimple.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.List; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; + +public class CollectSimple { + public static void main(String[] args) { + + Stream stream = Stream.of( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + + List result = stream + .filter(name -> name.startsWith("A")) + .collect(toList()); + + System.out.println(result); // -> [Anton, Alfred] + } +} diff --git a/src/main/java/pr2/streams/basics/Filter.java b/src/main/java/pr2/streams/basics/Filter.java new file mode 100644 index 0000000..2a7e2d2 --- /dev/null +++ b/src/main/java/pr2/streams/basics/Filter.java @@ -0,0 +1,17 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.Arrays; +import java.util.List; + +public class Filter { + + public static void main(String[] args) { + List namen = Arrays.asList( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + + namen.stream() + .filter(name -> name.startsWith("A")) + .forEach(name -> System.out.println(name)); + } +} diff --git a/src/main/java/pr2/streams/basics/FlatMap.java b/src/main/java/pr2/streams/basics/FlatMap.java new file mode 100644 index 0000000..851845f --- /dev/null +++ b/src/main/java/pr2/streams/basics/FlatMap.java @@ -0,0 +1,44 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static java.util.stream.Collectors.toList; +public class FlatMap { + + public static void main(String[] args) { + flatMap(); + klassisch(); + } + + static void klassisch() { + List l1 = Arrays.asList("A", "B", "C"); + List l2 = Arrays.asList("D", "E", "F"); + List> gesamt = Arrays.asList(l1, l2); + + List result = new ArrayList<>(); + + for (List l : gesamt) { + + for (String s : l) { + result.add(s); + } + } + + System.out.println(result); // -> [A, B, C, D, E, F] + } + + static void flatMap() { + List l1 = Arrays.asList("A", "B", "C"); + List l2 = Arrays.asList("D", "E", "F"); + List> gesamt = Arrays.asList(l1, l2); + + List result = gesamt.stream() + .flatMap(e -> e.stream()) + .collect(toList()); + + System.out.println(result); // -> [A, B, C, D, E, F] + } +} diff --git a/src/main/java/pr2/streams/basics/IntStreamExample.java b/src/main/java/pr2/streams/basics/IntStreamExample.java new file mode 100644 index 0000000..8c6bee9 --- /dev/null +++ b/src/main/java/pr2/streams/basics/IntStreamExample.java @@ -0,0 +1,25 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.Arrays; +import java.util.stream.IntStream; + +public class IntStreamExample { + + public static void main(String[] args) { + example1(); + } + + static void example1() { + IntStream stream = IntStream.of(8, 9, 12, 22, 2, 3, 44, 11, 7); + + int[] even = + stream + .filter(i -> i % 2 == 0) + .toArray(); + + System.out.println(Arrays.toString(even)); + } + + +} diff --git a/src/main/java/pr2/streams/basics/Lazy.java b/src/main/java/pr2/streams/basics/Lazy.java new file mode 100644 index 0000000..5d7082b --- /dev/null +++ b/src/main/java/pr2/streams/basics/Lazy.java @@ -0,0 +1,37 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.Arrays; +import java.util.List; + +public class Lazy { + + public static void main(String[] args) { + List namen = Arrays.asList( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + + // ohne terminal operation: keine Ausgabe + namen.stream() + .filter(name -> { + System.out.println("Filter 1: " + name); + return name.startsWith("A"); + }) + .filter(name -> { + System.out.println("Filter 2: " + name); + return name.charAt(1) == 'n'; + }); + + // Mit terminal operation + namen.stream() + .filter(name -> { + System.out.println("Filter 1: " + name); + return name.startsWith("A"); + }) + .filter(name -> { + System.out.println("Filter 2: " + name); + return name.charAt(1) == 'n'; + }) + .forEach(System.out::println); + + } +} diff --git a/src/main/java/pr2/streams/basics/MapExample.java b/src/main/java/pr2/streams/basics/MapExample.java new file mode 100644 index 0000000..e877bcd --- /dev/null +++ b/src/main/java/pr2/streams/basics/MapExample.java @@ -0,0 +1,62 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.stream.Stream; + +public class MapExample { + + public static void main(String[] args) { + example1(); + example2(); + example3(); + + } + + static void example1() { + // map mit lambda + Stream stream = Stream.of( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + stream + .map(name -> name.toLowerCase()) + .forEach(System.out::println); // anton egon ... + } + + static void example2() { + // map mit Methodenreferenz + Stream stream = Stream.of( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + + stream + .map(String::toLowerCase) + .forEach(System.out::println); // anton egon ... + } + + static class Schauspieler { + private String name; + private String film; + + public Schauspieler(String name, String film) { + this.name = name; + this.film = film; + } + + public String getName() { + return name; + } + + public String getFilm() { + return film; + } + } + + static void example3() { + Stream stream = Stream.of( + new Schauspieler("John Travolta", "Pulp Fiction"), + new Schauspieler("Brad Pitt", "Inglourious Basterds")); + + stream + .map(Schauspieler::getName) + .forEach(System.out::println); + } + +} diff --git a/src/main/java/pr2/streams/basics/MaxMin.java b/src/main/java/pr2/streams/basics/MaxMin.java new file mode 100644 index 0000000..19eaad3 --- /dev/null +++ b/src/main/java/pr2/streams/basics/MaxMin.java @@ -0,0 +1,44 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.Comparator; +import java.util.stream.Stream; + +public class MaxMin { + + public static void main(String[] args) { + max1(); + max2(); + max3(); + } + + static void max1() { + Stream stream = Stream.of("K", "O", "B", "T", "I"); + + String max = stream + .max((a, b) -> a.compareTo(b)) + .orElse(""); + + System.out.println(max); // -> T + } + + static void max2() { + Stream stream = Stream.of("K", "O", "B", "T", "I"); + + String max = stream + .max(String::compareTo) + .orElse(""); + + System.out.println(max); // -> T + } + + static void max3() { + Stream stream = Stream.of("K", "O", "B", "T", "I"); + + String max = stream + .max(Comparator.comparing(a -> a)) + .orElse(""); + + System.out.println(max); // -> T + } +} diff --git a/src/main/java/pr2/streams/basics/Reduce.java b/src/main/java/pr2/streams/basics/Reduce.java new file mode 100644 index 0000000..f63e01d --- /dev/null +++ b/src/main/java/pr2/streams/basics/Reduce.java @@ -0,0 +1,25 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.stream.Stream; + +public class Reduce { + + public static void main(String[] args) { + reduce1(); + reduce2(); + } + + static void reduce1() { + Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + int summe = stream.reduce(0, (sum, value) -> sum + value); + System.out.println(summe); // -> 55 + } + + static void reduce2() { + Stream stream = Stream.of("A", "B", "C", "D"); + String ergebnis = stream.reduce("", (acc, value) -> acc + value); + + System.out.println(ergebnis); // -> ABCD + } +} diff --git a/src/main/java/pr2/streams/basics/StreamOf.java b/src/main/java/pr2/streams/basics/StreamOf.java new file mode 100644 index 0000000..b8d1071 --- /dev/null +++ b/src/main/java/pr2/streams/basics/StreamOf.java @@ -0,0 +1,12 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics; + +import java.util.stream.Stream; + +public class StreamOf { + public static void main(String[] args) { + Stream stream = Stream.of( + "Anton", "Egon", "Alfred", "Barbara", "Klaus", "Herbert"); + stream.forEach(System.out::println); + } +} diff --git a/src/main/java/pr2/streams/basics/kombi/Item.java b/src/main/java/pr2/streams/basics/kombi/Item.java new file mode 100644 index 0000000..4d1070e --- /dev/null +++ b/src/main/java/pr2/streams/basics/kombi/Item.java @@ -0,0 +1,21 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics.kombi; + +public class Item { + + private String name; + private int price; + + public Item(String name, int price) { + this.name = name; + this.price = price; + } + + public int getPrice() { + return price; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/pr2/streams/basics/kombi/Order.java b/src/main/java/pr2/streams/basics/kombi/Order.java new file mode 100644 index 0000000..576e993 --- /dev/null +++ b/src/main/java/pr2/streams/basics/kombi/Order.java @@ -0,0 +1,20 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics.kombi; + +import java.util.ArrayList; +import java.util.List; + +public class Order { + + private List items = new ArrayList<>(); + + public Order(Item ... items) { + for (Item item : items) { + this.items.add(item); + } + } + + public List getItems() { + return this.items; + } +} diff --git a/src/main/java/pr2/streams/basics/kombi/Simulation.java b/src/main/java/pr2/streams/basics/kombi/Simulation.java new file mode 100644 index 0000000..379c3ff --- /dev/null +++ b/src/main/java/pr2/streams/basics/kombi/Simulation.java @@ -0,0 +1,147 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.basics.kombi; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static java.util.stream.Collectors.toList; + +public class Simulation { + + public static void main(String[] args) { + stream(); + klassisch(); + klassisch2(); + example2(); + } + + static void stream() { + List orders = + Arrays.asList( + new Order( + new Item("Java Insel", 3980), + new Item("GoTo Java", 2240), + new Item("Planet der Affen", 1690) + ), + new Order( + new Item("Breaking Dawn", 2130), + new Item("Lord of the Rings", 1870) + ) + ); + + + List namen = + orders.stream() + .flatMap(e -> e.getItems().stream()) + .filter(e -> e.getPrice() > 2000) + .map(Item::getName) + .collect(toList()); + + System.out.println(namen); + + int summe = + orders.stream() + .flatMap(e -> e.getItems().stream()) + .map(Item::getPrice) + .reduce((a, e) -> a + e) + .orElse(0); + + System.out.println(summe); + } + + static void klassisch() { + List orders = + Arrays.asList( + new Order( + new Item("Java Insel", 3980), + new Item("GoTo Java", 2240), + new Item("Planet der Affen", 1690) + ), + new Order( + new Item("Breaking Dawn", 2130), + new Item("Lord of the Rings", 1870) + ) + ); + + // Liste "flachklopfen" + List flatList = new LinkedList<>(); + + for (Order order : orders) { + for (Item item : order.getItems()) { + flatList.add(item); + } + } + + // Liste filtern + List gefiltert = new LinkedList<>(); + + for (Item item : flatList) { + if (item.getPrice() > 2000) { + gefiltert.add(item); + } + } + + // Namen extrahieren + List namen = new ArrayList<>(gefiltert.size()); + + for (Item item : gefiltert) { + namen.add(item.getName()); + } + + System.out.println(namen); + } + + static void klassisch2() { + List orders = + Arrays.asList( + new Order( + new Item("Java Insel", 3980), + new Item("GoTo Java", 2240), + new Item("Planet der Affen", 1690) + ), + new Order( + new Item("Breaking Dawn", 2130), + new Item("Lord of the Rings", 1870) + ) + ); + + List namen = new ArrayList<>(); + + for (Order order : orders) { + for (Item item : order.getItems()) { + if (item.getPrice() > 2000) { + namen.add(item.getName()); + } + } + } + + System.out.println(namen); + } + + static void example2() { + List orders = + Arrays.asList( + new Order( + new Item("Java Insel", 3980), + new Item("GoTo Java", 2240), + new Item("Planet der Affen", 1690) + ), + new Order( + new Item("Breaking Dawn", 2130), + new Item("Lord of the Rings", 1870) + ) + ); + + int summe = + orders.stream() + .flatMap(e -> e.getItems().stream()) + .mapToInt(Item::getPrice) + .reduce((a, e) -> a + e) + .orElse(0); + + System.out.println(summe); + } + +} diff --git a/src/main/java/pr2/streams/motivation/ExternalIteration.java b/src/main/java/pr2/streams/motivation/ExternalIteration.java new file mode 100644 index 0000000..b3e0c4a --- /dev/null +++ b/src/main/java/pr2/streams/motivation/ExternalIteration.java @@ -0,0 +1,40 @@ +/* (c) 2022 Thomas Smits */ +package pr2.streams.motivation; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.function.Consumer; + +public class ExternalIteration { + + public static void main(String[] args) { + List namen = Arrays.asList("Franz", "Peter", "Jakob", "Hans"); + + // externe Iteration mit Iterator + Iterator it = namen.iterator(); + + while (it.hasNext()) { + String name = it.next(); + System.out.println(name); + } + + // externe Iteration mit foreach + for (String name : namen) { + System.out.println(name); + } + + // interne Iteration mit Lambda + namen.stream().forEach(name -> System.out.println(name)); + + // interne Iteration mit Methodenreferenz + namen.stream().forEach(System.out::println); + + // Streams ohne Lambdas (unbrauchbar) + namen.stream().forEach(new Consumer() { + public void accept(String name) { + System.out.println(name); + } + }); + } +} diff --git a/src/main/java/pr2/strukturierung/C.java b/src/main/java/pr2/strukturierung/C.java new file mode 100644 index 0000000..9e4126c --- /dev/null +++ b/src/main/java/pr2/strukturierung/C.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class A { + static int wert = 5; +} + +class B extends X { + static int wert = 6; +} + +public class C { + void method(X a) { + // System.out.println(a.wert); + } + + public static void main(String[] args) { + new C().method(new B()); + System.out.print((int) 1.5f); + } +} \ No newline at end of file diff --git a/src/main/java/pr2/strukturierung/Datum.java b/src/main/java/pr2/strukturierung/Datum.java new file mode 100644 index 0000000..41a54d2 --- /dev/null +++ b/src/main/java/pr2/strukturierung/Datum.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class Datum { + int tag; + int monat; + int jahr; +} + +class Verwender { + + void m() { + Datum d = new Datum(); + d.tag = 32; + + d.tag = 30; d.monat = 2; + + d.tag = 31; + d.tag++; + } +} + +class Datum2 { + private int tag; + private int monat; + private int jahr; + + public void setTag(int tag) { + + if (tag > 31) { + tag = 31; + } + + this.tag = tag; + } +} diff --git a/src/main/java/pr2/strukturierung/PackagePrivateClass.java b/src/main/java/pr2/strukturierung/PackagePrivateClass.java new file mode 100644 index 0000000..b1be431 --- /dev/null +++ b/src/main/java/pr2/strukturierung/PackagePrivateClass.java @@ -0,0 +1,16 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class PackagePrivateClass { + +} + +class NochEineKlasse { + +} + +class UndNochEine { + +} \ No newline at end of file diff --git a/src/main/java/pr2/strukturierung/PublicClass.java b/src/main/java/pr2/strukturierung/PublicClass.java new file mode 100644 index 0000000..a32e3d7 --- /dev/null +++ b/src/main/java/pr2/strukturierung/PublicClass.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +public class PublicClass { + +} diff --git a/src/main/java/pr2/strukturierung/Scope.java b/src/main/java/pr2/strukturierung/Scope.java new file mode 100644 index 0000000..0f07f5f --- /dev/null +++ b/src/main/java/pr2/strukturierung/Scope.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class Scope { + + int i = 1; + + void methode1() { + int i = 4; + int j = 5; + + this.i = i + j; + methode2(7); + } + + void methode2(int i) { + int j=8; + this.i = i + j; + } + + public static void main(String[] args) { + Scope s = new Scope(); + s.methode1(); + } +} diff --git a/src/main/java/pr2/strukturierung/StaticImport.java b/src/main/java/pr2/strukturierung/StaticImport.java new file mode 100644 index 0000000..ce82c4d --- /dev/null +++ b/src/main/java/pr2/strukturierung/StaticImport.java @@ -0,0 +1,13 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +import static java.lang.Math.PI; + +public class StaticImport { + + public static double umfang(double r) { + return 2*PI*r; + } +} diff --git a/src/main/java/pr2/strukturierung/Variablen.java b/src/main/java/pr2/strukturierung/Variablen.java new file mode 100644 index 0000000..ec41d1b --- /dev/null +++ b/src/main/java/pr2/strukturierung/Variablen.java @@ -0,0 +1,47 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class Variablen { + + static int klassenVariable = 10; + int member = 15; + int empty; + + void methode(int i) { + klassenVariable = 11; + member++; + empty++; + + int empty = 5; + empty++; + this.empty = 3; + + { + int k = 5; + } + + { + int k = 6; + } + } +} + +class BenutzeVariable { + @SuppressWarnings("static-access") + void doit() { + Variablen.klassenVariable = 10; + + Variablen v1 = new Variablen(); + v1.empty = 5; + + Variablen v2 = new Variablen(); + v2.empty = 6; + v2.member = 10; + + Variablen.klassenVariable = 10; + Variablen v = new Variablen(); + v.klassenVariable = 15; + } +} diff --git a/src/main/java/pr2/strukturierung/X.java b/src/main/java/pr2/strukturierung/X.java new file mode 100644 index 0000000..a556bbe --- /dev/null +++ b/src/main/java/pr2/strukturierung/X.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung; + +class X { + +} diff --git a/src/main/java/pr2/strukturierung/firma/db/DbMitarbeiter.java b/src/main/java/pr2/strukturierung/firma/db/DbMitarbeiter.java new file mode 100644 index 0000000..fda2823 --- /dev/null +++ b/src/main/java/pr2/strukturierung/firma/db/DbMitarbeiter.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.firma.db; + +public class DbMitarbeiter { + +} diff --git a/src/main/java/pr2/strukturierung/firma/domain/Angestellter.java b/src/main/java/pr2/strukturierung/firma/domain/Angestellter.java new file mode 100644 index 0000000..77e555c --- /dev/null +++ b/src/main/java/pr2/strukturierung/firma/domain/Angestellter.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.firma.domain; + +public class Angestellter extends Mitarbeiter { + +} diff --git a/src/main/java/pr2/strukturierung/firma/domain/Arbeiter.java b/src/main/java/pr2/strukturierung/firma/domain/Arbeiter.java new file mode 100644 index 0000000..10f3658 --- /dev/null +++ b/src/main/java/pr2/strukturierung/firma/domain/Arbeiter.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.firma.domain; + +public class Arbeiter extends Mitarbeiter { + +} diff --git a/src/main/java/pr2/strukturierung/firma/domain/Mitarbeiter.java b/src/main/java/pr2/strukturierung/firma/domain/Mitarbeiter.java new file mode 100644 index 0000000..6869ab0 --- /dev/null +++ b/src/main/java/pr2/strukturierung/firma/domain/Mitarbeiter.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.firma.domain; + +public class Mitarbeiter { + +} diff --git a/src/main/java/pr2/strukturierung/firma/gui/EingabeMitarbeiter.java b/src/main/java/pr2/strukturierung/firma/gui/EingabeMitarbeiter.java new file mode 100644 index 0000000..93e1f0a --- /dev/null +++ b/src/main/java/pr2/strukturierung/firma/gui/EingabeMitarbeiter.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.firma.gui; + +public class EingabeMitarbeiter { + +} diff --git a/src/main/java/pr2/strukturierung/sichtbarkeit/A.java b/src/main/java/pr2/strukturierung/sichtbarkeit/A.java new file mode 100644 index 0000000..3bb6b5b --- /dev/null +++ b/src/main/java/pr2/strukturierung/sichtbarkeit/A.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.sichtbarkeit; + +public class A { + + private int a; + int b; + public int c; + + private void m1() {} + void m2() {} + public void m3() {} +} diff --git a/src/main/java/pr2/strukturierung/sichtbarkeit/B.java b/src/main/java/pr2/strukturierung/sichtbarkeit/B.java new file mode 100644 index 0000000..9ae3264 --- /dev/null +++ b/src/main/java/pr2/strukturierung/sichtbarkeit/B.java @@ -0,0 +1,17 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.strukturierung.sichtbarkeit; + +class B { + + private int a; + int b; + public int c; + + public String s; + + private void m1() {} + void m2() {} + public void m3() {} +} diff --git a/src/main/java/pr2/swing/AdapterDemo.java b/src/main/java/pr2/swing/AdapterDemo.java new file mode 100644 index 0000000..5742dcc --- /dev/null +++ b/src/main/java/pr2/swing/AdapterDemo.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +import javax.swing.JFrame; + +public class AdapterDemo { + + public static void main(String[] args) { + + JFrame jframe = new JFrame(); + + jframe.addWindowListener(new WindowListener() { + + public void windowOpened(WindowEvent e) {} + public void windowIconified(WindowEvent e) {} + public void windowDeiconified(WindowEvent e) {} + public void windowDeactivated(WindowEvent e) {} + public void windowClosed(WindowEvent e) {} + public void windowActivated(WindowEvent e) {} + + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + + jframe.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + } +} diff --git a/src/main/java/pr2/swing/BorderLayoutDemo.java b/src/main/java/pr2/swing/BorderLayoutDemo.java new file mode 100644 index 0000000..d189879 --- /dev/null +++ b/src/main/java/pr2/swing/BorderLayoutDemo.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.BorderLayout; +import java.awt.Container; + +import javax.swing.JButton; +import javax.swing.JFrame; + +public class BorderLayoutDemo { + public static void main(String[] args) { + JFrame window = new JFrame("Border Layout"); + Container contentPane = window.getContentPane(); + contentPane.add(new JButton("SOUTH"), BorderLayout.SOUTH); + contentPane.add(new JButton("NORTH"), BorderLayout.NORTH); + contentPane.add(new JButton("WEST"), BorderLayout.WEST); + contentPane.add(new JButton("EAST"), BorderLayout.EAST); + contentPane.add(new JButton("CENTER"), BorderLayout.CENTER); + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/CardLayoutDemo.java b/src/main/java/pr2/swing/CardLayoutDemo.java new file mode 100644 index 0000000..cf23751 --- /dev/null +++ b/src/main/java/pr2/swing/CardLayoutDemo.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.Container; + +import javax.swing.JButton; +import javax.swing.JFrame; + +public class CardLayoutDemo { + + public static void main(String[] args) { + JFrame window = new JFrame("Card Layout"); + Container contentPane = window.getContentPane(); + contentPane.setLayout(new CardLayout()); + contentPane.add(new JButton("SOUTH"), BorderLayout.SOUTH); + contentPane.add(new JButton("NORTH"), BorderLayout.NORTH); + contentPane.add(new JButton("WEST"), BorderLayout.WEST); + contentPane.add(new JButton("EAST"), BorderLayout.EAST); + contentPane.add(new JButton("CENTER"), BorderLayout.CENTER); + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/ComplexLayoutDemo.java b/src/main/java/pr2/swing/ComplexLayoutDemo.java new file mode 100644 index 0000000..ecf68cd --- /dev/null +++ b/src/main/java/pr2/swing/ComplexLayoutDemo.java @@ -0,0 +1,45 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.FlowLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; + +public class ComplexLayoutDemo { + + public static void main(String[] args) { + JFrame window = new JFrame("Complex Layout"); + Container contentPane = window.getContentPane(); + + JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); + p1.add(new JLabel("Filter")); + p1.add(new JTextField(10)); + + JList list = new JList(new Object[] { + "Eintrag A", "Eintrag B", "Eintrag C", + "Eintrag D", "Eintrag E", "Eintrag F", + "Eintrag G", "Eintrag H", "Eintrag I"}); + JScrollPane scrollPane = new JScrollPane(list); + + JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + p2.add(new JButton("OK")); + p2.add(new JButton("Abbrechen")); + + contentPane.add(scrollPane, BorderLayout.CENTER); + contentPane.add(p1, BorderLayout.NORTH); + contentPane.add(p2, BorderLayout.SOUTH); + + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/EventHandlerDemo.java b/src/main/java/pr2/swing/EventHandlerDemo.java new file mode 100644 index 0000000..27c4f25 --- /dev/null +++ b/src/main/java/pr2/swing/EventHandlerDemo.java @@ -0,0 +1,49 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +public class EventHandlerDemo { + + public static void main(String[] args) { + + JFrame window = new JFrame("Fenster"); + JPanel panel = new JPanel(); + + JButton button = new JButton("Knopf"); + button.setActionCommand("knopf"); + + panel.add(button); + + window.setContentPane(panel); + window.pack(); + window.setVisible(true); + + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println(e.getActionCommand() + " wurde geklickt"); + } + }); + + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ((JButton) e.getSource()).setEnabled(false); + } + }); + + window.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + } +} diff --git a/src/main/java/pr2/swing/FlowLayoutDemo.java b/src/main/java/pr2/swing/FlowLayoutDemo.java new file mode 100644 index 0000000..f8c216c --- /dev/null +++ b/src/main/java/pr2/swing/FlowLayoutDemo.java @@ -0,0 +1,25 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.Container; +import java.awt.FlowLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; + +public class FlowLayoutDemo { + public static void main(String[] args) { + JFrame window = new JFrame("Flow Layout"); + Container contentPane = window.getContentPane(); + contentPane.setLayout(new FlowLayout()); + contentPane.add(new JButton("A")); + contentPane.add(new JButton("B")); + contentPane.add(new JButton("C")); + contentPane.add(new JButton("D")); + contentPane.add(new JButton("E")); + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/GridLayoutDemo.java b/src/main/java/pr2/swing/GridLayoutDemo.java new file mode 100644 index 0000000..d45563a --- /dev/null +++ b/src/main/java/pr2/swing/GridLayoutDemo.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.Container; +import java.awt.GridLayout; + +import javax.swing.JButton; +import javax.swing.JFrame; + +public class GridLayoutDemo { + public static void main(String[] args) { + JFrame window = new JFrame("Grid Layout"); + Container contentPane = window.getContentPane(); + contentPane.setLayout(new GridLayout(2, 3)); + contentPane.add(new JButton("A")); + contentPane.add(new JButton("B")); + contentPane.add(new JButton("C")); + contentPane.add(new JButton("D")); + contentPane.add(new JButton("E")); + contentPane.add(new JButton("F")); + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/HelloWorld.java b/src/main/java/pr2/swing/HelloWorld.java new file mode 100644 index 0000000..c9218b0 --- /dev/null +++ b/src/main/java/pr2/swing/HelloWorld.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +public class HelloWorld { + +} diff --git a/src/main/java/pr2/swing/JFrameDemo.java b/src/main/java/pr2/swing/JFrameDemo.java new file mode 100644 index 0000000..2e96bed --- /dev/null +++ b/src/main/java/pr2/swing/JFrameDemo.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JFrame; + +public class JFrameDemo { + + public static void main(String[] args) { + + JFrame window = new JFrame("Hello World"); + window.setVisible(true); + + window.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + } +} diff --git a/src/main/java/pr2/swing/MenuDemo.java b/src/main/java/pr2/swing/MenuDemo.java new file mode 100644 index 0000000..25b101e --- /dev/null +++ b/src/main/java/pr2/swing/MenuDemo.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.Menu; +import java.awt.MenuBar; +import java.awt.MenuItem; + +import javax.swing.JFrame; + +public class MenuDemo { + + public static void main(String[] args) { + JFrame window = new JFrame("Mit Menue"); + + Menu fileMenu = new Menu("Ablage"); + fileMenu.add(new MenuItem("Sichern")); + fileMenu.add(new MenuItem("Sichern unter...")); + fileMenu.addSeparator(); + fileMenu.add(new MenuItem("Drucken")); + + Menu helpMenu = new Menu("Hilfe"); + helpMenu.add(new MenuItem("Cheat sheet")); + + MenuBar menuBar = new MenuBar(); + menuBar.setHelpMenu(helpMenu); + menuBar.add(fileMenu); + window.setMenuBar(menuBar); + + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/swing/OptionPaneDemo.java b/src/main/java/pr2/swing/OptionPaneDemo.java new file mode 100644 index 0000000..6d8a43e --- /dev/null +++ b/src/main/java/pr2/swing/OptionPaneDemo.java @@ -0,0 +1,41 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import javax.swing.JFileChooser; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class OptionPaneDemo { + + public static void main(String[] args) { + JFrame window = new JFrame("Option Pane"); + window.pack(); + window.setVisible(true); + + JFileChooser fc = new JFileChooser(); + int returnVal = fc.showOpenDialog(window); + + + JOptionPane.showConfirmDialog(window, "Sind sie sicher?", "Sicherheitsabfrage", + JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); + + JOptionPane.showConfirmDialog(window, + "Alle Daten wurden gelöscht", "Hinweis", + JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE); + + JOptionPane.showInputDialog(window, + "Bitte wählen Sie Ihre Glücksmelodie", + "Eingabe", JOptionPane.PLAIN_MESSAGE); + + Object[] possibleValues = { "Eintrag A", "Eintrag B", "Eintrag C" }; + JOptionPane.showInputDialog(window, "Bitte wählen", "Eingabe", + JOptionPane.INFORMATION_MESSAGE, null, + possibleValues, possibleValues[1]); + + + + + } +} diff --git a/src/main/java/pr2/swing/XYLayout.java b/src/main/java/pr2/swing/XYLayout.java new file mode 100644 index 0000000..0a274db --- /dev/null +++ b/src/main/java/pr2/swing/XYLayout.java @@ -0,0 +1,26 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.swing; + +import java.awt.Container; + +import javax.swing.JFrame; +import javax.swing.JLabel; + +public class XYLayout { + + public static void main(String[] args) { + JFrame window = new JFrame("XY Layout"); + + JLabel label = new JLabel("Label"); + + + Container contentPane = window.getContentPane(); + contentPane.add(label); + contentPane.setLayout(null); + label.setLocation(10, 10); + window.pack(); + window.setVisible(true); + } +} diff --git a/src/main/java/pr2/threads/concurrent/atomic/AtomicExample.java b/src/main/java/pr2/threads/concurrent/atomic/AtomicExample.java new file mode 100644 index 0000000..20a9f7f --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/atomic/AtomicExample.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.atomic; + +import java.util.concurrent.atomic.AtomicInteger; + +public class AtomicExample implements Runnable { + + private final AtomicInteger counter = new AtomicInteger(1); + + public void run() { + while (counter.getAndIncrement() > 0) { + System.out.printf("Wert: %d%n", counter.get()); + Thread.yield(); + } + System.out.println("** Gestoppt **"); + } + + public static void main(String[] args) throws Exception { + AtomicExample st = new AtomicExample(); + Thread thread = new Thread(st); + + thread.start(); + Thread.sleep(100); + st.counter.set(-1); + } +} diff --git a/src/main/java/pr2/threads/concurrent/callable/CallableExample.java b/src/main/java/pr2/threads/concurrent/callable/CallableExample.java new file mode 100644 index 0000000..c40e439 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/callable/CallableExample.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.callable; + +import java.util.concurrent.Callable; + +class RechnerCallable implements Callable { + + public Integer call() throws Exception { + return 2+2; + } +} + +class RechnerRunnable implements Runnable { + + private int result; + private Exception e; + + public int getResult() { + return result; + } + + public Exception getException() { + return e; + } + + public void run() { + try { + result = 2+2; + } + catch (Exception ex) { + this.e = ex; + } + } +} diff --git a/src/main/java/pr2/threads/concurrent/collections/Cache.java b/src/main/java/pr2/threads/concurrent/collections/Cache.java new file mode 100644 index 0000000..6b52317 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/collections/Cache.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.collections; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +public class Cache { + + + public static void main(String[] args) { + + ConcurrentMap map = new ConcurrentHashMap(); + + String key = "", value = ""; + + if (!map.containsKey(key)) { + map.put(key, value); + } + else { + value = map.get(key); + } + + value = map.putIfAbsent(key, value); + } +} diff --git a/src/main/java/pr2/threads/concurrent/executors/FutureDemo.java b/src/main/java/pr2/threads/concurrent/executors/FutureDemo.java new file mode 100644 index 0000000..ac5bd95 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/executors/FutureDemo.java @@ -0,0 +1,35 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.executors; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +class FutureCallable implements Callable { + public String call() throws Exception { + Thread.sleep(1000); // unglaublich teure Berechnung ;-) + return "Ergebnis"; + } +} + +public class FutureDemo { + + public static void main(String[] args) throws Exception { + + ExecutorService executor = Executors.newCachedThreadPool(); + Future future = executor.submit(new FutureCallable()); + + System.out.println("Viele interessante Dinge parallel"); + + try { + String ergebnis = future.get(); + System.out.println(ergebnis); + } + catch (ExecutionException ex) { /* oops */ } + System.out.println("Alles fertig"); + } +} diff --git a/src/main/java/pr2/threads/concurrent/executors/TaskDemo.java b/src/main/java/pr2/threads/concurrent/executors/TaskDemo.java new file mode 100644 index 0000000..17c9fe7 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/executors/TaskDemo.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.executors; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +class Task implements Runnable { + + String name; + + public Task(String name) { + this.name = name; + } + + public void run() { + for (int i = 0; i < 100; i++) { + System.out.println(name); + Thread.yield(); + } + } +} + +public class TaskDemo { + + public static void main(String[] args) throws InterruptedException { + ExecutorService ex = Executors.newCachedThreadPool(); + ex.execute(new Task("Task 1")); + ex.execute(new Task("Task 2")); + + ScheduledExecutorService ex2 = Executors.newScheduledThreadPool(3); + ex2.schedule(new Task("Delayed 1"), 5, TimeUnit.SECONDS); + ex2.schedule(new Task("Delayed 2"), 10, TimeUnit.SECONDS); + + } +} diff --git a/src/main/java/pr2/threads/concurrent/locks/ConnectionPool.java b/src/main/java/pr2/threads/concurrent/locks/ConnectionPool.java new file mode 100644 index 0000000..914daef --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/locks/ConnectionPool.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.locks; + +import java.sql.Connection; +import java.util.concurrent.Semaphore; + +public class ConnectionPool { + + private static final int NUM_CON = 5; + private Semaphore sem = new Semaphore(NUM_CON); + + public synchronized Connection getConnection() throws InterruptedException { + sem.acquire(); + return getFreeConnection(); + } + + public synchronized void releaseConnection(Connection con) { + try { + recycleConnection(con); + } + finally { + sem.release(); + } + } + + + private void recycleConnection(Connection con) { + } + + private Connection getFreeConnection() { + return null; + } + + public static void main(String[] args) { + } +} diff --git a/src/main/java/pr2/threads/concurrent/locks/Join.java b/src/main/java/pr2/threads/concurrent/locks/Join.java new file mode 100644 index 0000000..383e444 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/locks/Join.java @@ -0,0 +1,50 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.locks; + +import java.util.concurrent.CountDownLatch; + +public class Join { + + static CountDownLatch startLatch = new CountDownLatch(1); + static CountDownLatch endLatch = new CountDownLatch(3); + + static class Runner implements Runnable { + + String text; + + Runner(String text) { + this.text = text; + } + + public void run() { + + try { + startLatch.await(); + + for (int i = 0; i < 5; i++) { + System.out.println(text); + Thread.yield(); + } + System.out.println(text + " ist fertig."); + endLatch.countDown(); + } + catch (InterruptedException e) { + // nothing to do + } + } + } + + public static void main(String[] args) throws Exception { + new Thread(new Runner("Runner 1")).start(); + new Thread(new Runner("Runner 2")).start(); + new Thread(new Runner("Runner 3")).start(); + + startLatch.countDown(); // alle laufen los + endLatch.await(); // warten bis alle fertig sind + + System.out.println("Alle fertig"); + + } +} diff --git a/src/main/java/pr2/threads/concurrent/producerconsumer/Consumer.java b/src/main/java/pr2/threads/concurrent/producerconsumer/Consumer.java new file mode 100644 index 0000000..a1b8c04 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/producerconsumer/Consumer.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.producerconsumer; + +import java.util.concurrent.BlockingQueue; + +public class Consumer implements Runnable { + + BlockingQueue queue; + + public Consumer(BlockingQueue queue) { + this.queue = queue; + } + + public void run() { + + try { + + while (true) { + String element = queue.take(); + System.out.println(Thread.currentThread().getName() + + ": " + element); + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/concurrent/producerconsumer/Producer.java b/src/main/java/pr2/threads/concurrent/producerconsumer/Producer.java new file mode 100644 index 0000000..979aaa7 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/producerconsumer/Producer.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.producerconsumer; + +import java.util.concurrent.BlockingQueue; + +public class Producer implements Runnable { + + BlockingQueue queue; + + public Producer(BlockingQueue queue) { + this.queue = queue; + } + + public void run() { + + int i = 0; + + try { + while (true) { + queue.put("P" + ++i); + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/concurrent/producerconsumer/Starter.java b/src/main/java/pr2/threads/concurrent/producerconsumer/Starter.java new file mode 100644 index 0000000..973b83b --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/producerconsumer/Starter.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.producerconsumer; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + + +public class Starter { + + public static void main(String[] args) { + + BlockingQueue queue = new LinkedBlockingQueue(); + + Producer producer = new Producer(queue); + Consumer consumer = new Consumer(queue); + + Thread tp = new Thread(producer, "Producer"); + Thread c1 = new Thread(consumer, "Consumer 1"); + Thread c2 = new Thread(consumer, "Consumer 2"); + Thread c3 = new Thread(consumer, "Consumer 3"); + + tp.start(); + c1.start(); + c3.start(); + c2.start(); + } +} diff --git a/src/main/java/pr2/threads/concurrent/timer/Countdown.java b/src/main/java/pr2/threads/concurrent/timer/Countdown.java new file mode 100644 index 0000000..414673b --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/timer/Countdown.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.timer; + +import java.util.TimerTask; + +public class Countdown extends TimerTask { + + private int zaehler; + + public Countdown(int zaehler) { + this.zaehler = zaehler; + } + + @Override + public void run() { + if (zaehler-- <= 0) { + System.out.println("Boom!"); + cancel(); + } + } +} diff --git a/src/main/java/pr2/threads/concurrent/timer/CountdownUser.java b/src/main/java/pr2/threads/concurrent/timer/CountdownUser.java new file mode 100644 index 0000000..5ef9027 --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/timer/CountdownUser.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.timer; + +import java.util.Timer; + +public class CountdownUser { + + public static void main(String[] args) throws Exception { + + Timer timer = new Timer("Countdown"); + timer.scheduleAtFixedRate(new Countdown(4), 0, 500); + + for (int i = 0; i < 10; i++) { + System.out.println(i); + Thread.sleep(1000); + } + + timer.cancel(); + } +} diff --git a/src/main/java/pr2/threads/concurrent/timer/Timebomb.java b/src/main/java/pr2/threads/concurrent/timer/Timebomb.java new file mode 100644 index 0000000..a35453b --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/timer/Timebomb.java @@ -0,0 +1,15 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.timer; + +import java.util.TimerTask; + +public class Timebomb extends TimerTask { + + @Override + public void run() { + System.out.println("Boom!"); + cancel(); + } +} diff --git a/src/main/java/pr2/threads/concurrent/timer/TimebombUser.java b/src/main/java/pr2/threads/concurrent/timer/TimebombUser.java new file mode 100644 index 0000000..4d9352a --- /dev/null +++ b/src/main/java/pr2/threads/concurrent/timer/TimebombUser.java @@ -0,0 +1,22 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.concurrent.timer; + +import java.util.Timer; + +public class TimebombUser { + + public static void main(String[] args) throws Exception { + + Timer timer = new Timer("Timebomb", true); + timer.schedule(new Timebomb(), 3000); + + for (int i = 0; i < 10; i++) { + System.out.println(i); + Thread.sleep(1000); + } + + timer.cancel(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/DaemonDemo.java b/src/main/java/pr2/threads/grundlagen/DaemonDemo.java new file mode 100644 index 0000000..d274558 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/DaemonDemo.java @@ -0,0 +1,19 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +class DaemonRunner implements Runnable { + public void run() { + while (true); + } +} + +public class DaemonDemo { + + public static void main(String[] args) { + Thread t = new Thread(new DaemonRunner()); + t.setDaemon(true); + t.start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/HeapAndStackDemo.java b/src/main/java/pr2/threads/grundlagen/HeapAndStackDemo.java new file mode 100644 index 0000000..786b8bf --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/HeapAndStackDemo.java @@ -0,0 +1,42 @@ +/* (c) 2022 Thomas Smits */ +package pr2.threads.grundlagen; + +import java.util.Random; + +class Runner3 implements Runnable { + + private HeapAndStackDemo hasd; + + public Runner3(HeapAndStackDemo hasd) { + this.hasd = hasd; + } + + private void doIt() { + int a = new Random().nextInt(10); + int b = new Random().nextInt(10); + hasd.f = hasd.calc(a, b); + } + + public void run() { + while (true) { + doIt(); + System.out.println(Thread.currentThread().getName() + ": " + hasd.f); + } + } +} + +public class HeapAndStackDemo { + + int f; + + public int calc(int i, int j) { + int temp = i + j; + return temp; + } + + public void go() { + Runnable runner = new Runner3(this); + new Thread(runner, "Thread-1").start(); + new Thread(runner, "Thread-2").start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/Interruptible.java b/src/main/java/pr2/threads/grundlagen/Interruptible.java new file mode 100644 index 0000000..4fd215b --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/Interruptible.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +public class Interruptible implements Runnable { + + public void run() { + + while (true) { + System.out.println("Thread laeuft"); + + if (Thread.currentThread().isInterrupted()) { + break; + } + Thread.yield(); + } + System.out.println("** Thread gestoppt **"); + } + + public static void main(String[] args) throws InterruptedException { + Interruptible interuptible = new Interruptible(); + Thread thread = new Thread(interuptible); + + thread.start(); + Thread.sleep(100); + thread.interrupt(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/Interruptible2.java b/src/main/java/pr2/threads/grundlagen/Interruptible2.java new file mode 100644 index 0000000..4327868 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/Interruptible2.java @@ -0,0 +1,34 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +public class Interruptible2 implements Runnable { + + public void run() { + + while (true) { + System.out.println("Thread laeuft"); + + if (Thread.currentThread().isInterrupted()) { + break; + } + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + break; + } + } + System.out.println("** Thread gestoppt **"); + } + + public static void main(String[] args) throws InterruptedException { + Interruptible2 interuptible = new Interruptible2(); + Thread thread = new Thread(interuptible); + + thread.start(); + Thread.sleep(100); + thread.interrupt(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/Interruptible3.java b/src/main/java/pr2/threads/grundlagen/Interruptible3.java new file mode 100644 index 0000000..f661d78 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/Interruptible3.java @@ -0,0 +1,29 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +public class Interruptible3 implements Runnable { + + public void run() { + + while (!Thread.currentThread().isInterrupted()) { + System.out.println("Thread laeuft"); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + System.out.println("** Thread gestoppt **"); + } + + public static void main(String[] args) throws InterruptedException { + Thread thread = new Thread(new Interruptible3()); + + thread.start(); + Thread.sleep(100); + thread.interrupt(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/Join.java b/src/main/java/pr2/threads/grundlagen/Join.java new file mode 100644 index 0000000..8df12af --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/Join.java @@ -0,0 +1,37 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +class RunnerPrinter implements Runnable { + + String text; + + RunnerPrinter(String text) { + this.text = text; + } + public void run() { + for (int i = 0; i < 5; i++) { + System.out.println(text); + Thread.yield(); + } + + System.out.println(text + " ist fertig."); + } +} + +public class Join { + + public static void main(String[] args) throws InterruptedException { + + Thread t1 = new Thread(new RunnerPrinter("Runner 1")); + Thread t2 = new Thread(new RunnerPrinter("Runner 2")); + Thread t3 = new Thread(new RunnerPrinter("Runner 3")); + + t1.start(); t2.start(); t3.start(); + + t1.join(); t2.join(); t3.join(); + + System.out.println("Alle fertig"); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/SimpleHeapAndStack.java b/src/main/java/pr2/threads/grundlagen/SimpleHeapAndStack.java new file mode 100644 index 0000000..037ea44 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/SimpleHeapAndStack.java @@ -0,0 +1,31 @@ +/* (c) 2022 Thomas Smits */ +package pr2.threads.grundlagen; + +import java.util.Random; + + +class Runner4 implements Runnable { + + private int zaehler = 0; + + @Override + public void run() { + int local = new Random().nextInt(); + + while (true) { + zaehler = local++; + } + } +} + +public class SimpleHeapAndStack { + + public static void main(String[] args) { + Runner4 runner = new Runner4(); + Thread t1 = new Thread(runner, "Thread-1"); + Thread t2 = new Thread(runner, "Thread-2"); + + t1.start(); + t2.start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/Stoppable.java b/src/main/java/pr2/threads/grundlagen/Stoppable.java new file mode 100644 index 0000000..e302e21 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/Stoppable.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +public class Stoppable implements Runnable { + + private volatile boolean cont = true; + + public void requestTermination() { + cont = false; + } + + public void run() { + while (cont) { + System.out.println("Thread laeuft"); + Thread.yield(); + } + System.out.println("** Stoppable gestoppt **"); + } + + public static void main(String[] args) throws InterruptedException { + Stoppable st = new Stoppable(); + Thread thread = new Thread(st); + + thread.start(); + Thread.sleep(100); + st.requestTermination(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/ThreadDemo.java b/src/main/java/pr2/threads/grundlagen/ThreadDemo.java new file mode 100644 index 0000000..9814eb2 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/ThreadDemo.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen; + +class Runner implements Runnable { + public void run() { + while (true) { + System.out.println("Paralleler Thread"); + Thread.yield(); + } + } +} + +public class ThreadDemo { + + public static void main(String[] args) throws InterruptedException { + + Thread t = new Thread(new Runner()); + t.start(); + + while (true) { + System.out.println("Hauptprogramm"); + Thread.yield(); + } + } +} diff --git a/src/main/java/pr2/threads/grundlagen/nested/DaemonDemo.java b/src/main/java/pr2/threads/grundlagen/nested/DaemonDemo.java new file mode 100644 index 0000000..703fad7 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/nested/DaemonDemo.java @@ -0,0 +1,14 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.threads.grundlagen.nested; + +public class DaemonDemo { + + public static void main(String[] args) { + Thread t = new Thread(() -> { while (true); }); + + t.setDaemon(true); + t.start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/nested/HeapAndStackDemo.java b/src/main/java/pr2/threads/grundlagen/nested/HeapAndStackDemo.java new file mode 100644 index 0000000..8c40b04 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/nested/HeapAndStackDemo.java @@ -0,0 +1,38 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.threads.grundlagen.nested; + +import java.util.Random; + +public class HeapAndStackDemo { + + int f; + + public int calc(int i, int j) { + int temp = i + j; + return temp; + } + + class Runner implements Runnable { + + private void doIt() { + int a = new Random().nextInt(10); + int b = new Random().nextInt(10); + f = calc(a, b); + } + + public void run() { + while (true) { + doIt(); + System.out.println(Thread.currentThread().getName() + ": " + f); + } + } + } + + public void go() { + Runnable runner = new Runner(); + new Thread(runner, "Thread-1").start(); + new Thread(runner, "Thread-2").start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/nested/ThreadDemo.java b/src/main/java/pr2/threads/grundlagen/nested/ThreadDemo.java new file mode 100644 index 0000000..f76e788 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/nested/ThreadDemo.java @@ -0,0 +1,24 @@ +/* + * (c) 2010 Thomas Smits + */ +package pr2.threads.grundlagen.nested; + +public class ThreadDemo { + + public static void main(String[] args) throws Exception { + + Thread t = new Thread( + () -> { + while (true) { + System.out.println("Paraller Thread"); + Thread.yield(); + } + }); + t.start(); + + while (true) { + System.out.println("Hauptprogramm"); + Thread.yield(); + } + } +} diff --git a/src/main/java/pr2/threads/grundlagen/runnable/BrokenRestart.java b/src/main/java/pr2/threads/grundlagen/runnable/BrokenRestart.java new file mode 100644 index 0000000..6363786 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/runnable/BrokenRestart.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen.runnable; + +class Runner2 implements Runnable { + public void run() { + } +} + +public class BrokenRestart { + + public static void main(String[] args) throws InterruptedException { + Runnable runnable = new Runner2(); + Thread t1 = new Thread(runnable, "Thread-1"); + t1.start(); + Thread.sleep(100); + t1.start(); + } +} diff --git a/src/main/java/pr2/threads/grundlagen/runnable/ThreadDemo.java b/src/main/java/pr2/threads/grundlagen/runnable/ThreadDemo.java new file mode 100644 index 0000000..4605a94 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/runnable/ThreadDemo.java @@ -0,0 +1,31 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen.runnable; + +class Runner implements Runnable { + public void run() { + while (true) { + System.out.println(Thread.currentThread().getName()); + Thread.yield(); + } + } +} + +public class ThreadDemo { + + public static void main(String[] args) throws InterruptedException { + + Runnable runner = new Runner(); + + Thread t1 = new Thread(runner, "Thread-1"); + Thread t2 = new Thread(runner, "Thread-2"); + t1.start(); + t2.start(); + + while (true) { + System.out.println("Hauptprogramm"); + Thread.yield(); + } + } +} diff --git a/src/main/java/pr2/threads/grundlagen/threadlocal/Daten.java b/src/main/java/pr2/threads/grundlagen/threadlocal/Daten.java new file mode 100644 index 0000000..e03b49d --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/threadlocal/Daten.java @@ -0,0 +1,8 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen.threadlocal; + +public class Daten { + public static final ThreadLocal tl = new ThreadLocal(); +} diff --git a/src/main/java/pr2/threads/grundlagen/threadlocal/ThreadLocalDemo.java b/src/main/java/pr2/threads/grundlagen/threadlocal/ThreadLocalDemo.java new file mode 100644 index 0000000..999fe70 --- /dev/null +++ b/src/main/java/pr2/threads/grundlagen/threadlocal/ThreadLocalDemo.java @@ -0,0 +1,24 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.grundlagen.threadlocal; + +public class ThreadLocalDemo implements Runnable { + + public void run() { + Daten.tl.set(0); + + while (true) { + int wert = Daten.tl.get(); + Daten.tl.set(++wert); + System.out.printf("Thread %s: Wert=%d%n", Thread.currentThread().getName(), wert); + Thread.yield(); + } + } + + public static void main(String[] args) { + Runnable runnable = new ThreadLocalDemo(); + new Thread(runnable, "1").start(); + new Thread(runnable, "2").start(); + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer/Consumer.java b/src/main/java/pr2/threads/koordination/producerconumer/Consumer.java new file mode 100644 index 0000000..f1589c9 --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer/Consumer.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer; + +import java.util.Queue; + +public class Consumer implements Runnable { + + Queue queue; + + public Consumer(Queue queue) { + this.queue = queue; + } + + public void run() { + + try { + + while (true) { + synchronized (queue) { + + while (queue.isEmpty()) { + queue.wait(); + } + + String element = queue.poll(); + queue.notifyAll(); + + System.out.println(Thread.currentThread().getName() + + ": " + element); + } + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer/Producer.java b/src/main/java/pr2/threads/koordination/producerconumer/Producer.java new file mode 100644 index 0000000..a80ab01 --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer/Producer.java @@ -0,0 +1,38 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer; + +import java.util.Queue; + +public class Producer implements Runnable { + + Queue queue; + + public Producer(Queue queue) { + this.queue = queue; + } + + public void run() { + + int i = 0; + + try { + while (true) { + synchronized (queue) { + + while (queue.size() >= 10) { + queue.wait(); + } + + queue.add("P" + ++i); + queue.notifyAll(); + } + + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer/Starter.java b/src/main/java/pr2/threads/koordination/producerconumer/Starter.java new file mode 100644 index 0000000..d9714c3 --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer/Starter.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer; + +import java.util.LinkedList; +import java.util.Queue; + +public class Starter { + public static void main(String[] args) { + + Queue queue = new LinkedList(); + + Producer producer = new Producer(queue); + Consumer consumer = new Consumer(queue); + + Thread tp = new Thread(producer, "Producer"); + Thread c1 = new Thread(consumer, "Consumer 1"); + Thread c2 = new Thread(consumer, "Consumer 2"); + Thread c3 = new Thread(consumer, "Consumer 3"); + + tp.start(); + c1.start(); + c3.start(); + c2.start(); + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer_broken/Consumer.java b/src/main/java/pr2/threads/koordination/producerconumer_broken/Consumer.java new file mode 100644 index 0000000..980645b --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer_broken/Consumer.java @@ -0,0 +1,46 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer_broken; + +import java.util.Queue; + +public class Consumer implements Runnable { + + Queue queue; + + public Consumer(Queue queue) { + this.queue = queue; + } + + public boolean checkQueueEmpty() { + synchronized (queue) { + return queue.isEmpty(); + } + } + + public void run() { + + try { + + while (true) { + + while (checkQueueEmpty()) { + // queue.wait(); + } + + synchronized (queue) { + + String element = queue.poll(); + queue.notifyAll(); + + System.out.println(Thread.currentThread().getName() + + ": " + element); + } + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer_broken/Producer.java b/src/main/java/pr2/threads/koordination/producerconumer_broken/Producer.java new file mode 100644 index 0000000..196266b --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer_broken/Producer.java @@ -0,0 +1,46 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer_broken; + +import java.util.Queue; + +public class Producer implements Runnable { + + Queue queue; + + public Producer(Queue queue) { + this.queue = queue; + } + + public boolean checkQueueFull() { + synchronized (queue) { + return queue.size() >= 10; + } + } + + public void run() { + + int i = 0; + + try { + while (true) { + + while (checkQueueFull()) { + queue.wait(); + } + synchronized (queue) { + + String elemnt ="P" + ++i; + queue.add(elemnt); + System.out.println(Thread.currentThread().getName() + ": Habe produziert " + elemnt); + queue.notifyAll(); + } + + Thread.sleep(100); + } + } + catch (InterruptedException e) { + } + } +} diff --git a/src/main/java/pr2/threads/koordination/producerconumer_broken/Starter.java b/src/main/java/pr2/threads/koordination/producerconumer_broken/Starter.java new file mode 100644 index 0000000..89fe5a5 --- /dev/null +++ b/src/main/java/pr2/threads/koordination/producerconumer_broken/Starter.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.koordination.producerconumer_broken; + +import java.util.LinkedList; +import java.util.Queue; + +public class Starter { + + public static void main(String[] args) { + + Queue queue = new LinkedList(); + + Producer producer = new Producer(queue); + Consumer consumer = new Consumer(queue); + + Thread tp1 = new Thread(producer, "Producer 1"); + Thread c1 = new Thread(consumer, "Consumer 1"); + Thread c2 = new Thread(consumer, "Consumer 2"); + Thread c3 = new Thread(consumer, "Consumer 3"); + + tp1.start(); + c1.start(); + c3.start(); + c2.start(); + } +} diff --git a/src/main/java/pr2/threads/schrecken/deadlock/Deadlock.java b/src/main/java/pr2/threads/schrecken/deadlock/Deadlock.java new file mode 100644 index 0000000..1dba5ab --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/deadlock/Deadlock.java @@ -0,0 +1,39 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.deadlock; + +class T1 implements Runnable { + public void run() { + synchronized (Deadlock.lock1) { + Thread.yield(); + synchronized (Deadlock.lock2) { + } + } + } +} + +class T2 implements Runnable { + public void run() { + synchronized (Deadlock.lock2) { + Thread.yield(); + synchronized (Deadlock.lock1) { + } + } + } +} + +public class Deadlock { + + static class ClassForLock1 { /* empty */ } + static class ClassForLock2 { /* empty */ } + + public static Object lock1 = new ClassForLock1(); + public static Object lock2 = new ClassForLock2(); + + public static void main(String[] args) { + Thread t1 = new Thread(new T1()); + Thread t2 = new Thread(new T2()); + t1.start(); t2.start(); + } +} diff --git a/src/main/java/pr2/threads/schrecken/hazards/Livelock.java b/src/main/java/pr2/threads/schrecken/hazards/Livelock.java new file mode 100644 index 0000000..2ba24c7 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/hazards/Livelock.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.hazards; + +class Looper implements Runnable { + + static boolean cont = true; + + public void run() { + while (cont) { + // mach was + } + } +} +public class Livelock { + + public static void main(String[] args) throws InterruptedException { + + Thread t = new Thread(new Looper()); + + t.start(); + + Thread.sleep(1000); + Looper.cont = false; + } +} diff --git a/src/main/java/pr2/threads/schrecken/hazards/SerialNumberGenerator.java b/src/main/java/pr2/threads/schrecken/hazards/SerialNumberGenerator.java new file mode 100644 index 0000000..6a453ec --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/hazards/SerialNumberGenerator.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.hazards; + +public class SerialNumberGenerator { + + private int serienNummer = 0; + + public int naechsteNummer() { + + int tmp = serienNummer; + + tmp = tmp + 1; + + serienNummer = tmp; + + return serienNummer; + } +} diff --git a/src/main/java/pr2/threads/schrecken/hazards/Singleton.java b/src/main/java/pr2/threads/schrecken/hazards/Singleton.java new file mode 100644 index 0000000..d2ed04f --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/hazards/Singleton.java @@ -0,0 +1,18 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.hazards; + +public class Singleton { + + private static Singleton instance; + + public static Singleton getInstance() { + if (instance == null) { + instance = new Singleton(); + } + + return instance; + } + +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/BrokenSimpleStack.java b/src/main/java/pr2/threads/schrecken/synchro/BrokenSimpleStack.java new file mode 100644 index 0000000..c5d9b59 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/BrokenSimpleStack.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +public class BrokenSimpleStack { + + private Object[] stack; + private int pos; + + public BrokenSimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public synchronized void push(Object o) { + stack[pos++] = o; + } + + public synchronized Object pop() { + return stack[--pos]; + } + + public Object peek() { + return stack[pos - 1]; + } +} \ No newline at end of file diff --git a/src/main/java/pr2/threads/schrecken/synchro/BrokenStack.java b/src/main/java/pr2/threads/schrecken/synchro/BrokenStack.java new file mode 100644 index 0000000..d8c83f1 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/BrokenStack.java @@ -0,0 +1,30 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +public class BrokenStack { + + private Object[] stack; + private int pos; + + public BrokenStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public synchronized void push(Object o) { + stack[pos++] = o; + } + + public synchronized Object pop() { + return stack[--pos]; + } + + public synchronized void swap() { + Object o1 = pop(); + Object o2 = pop(); + push(o1); + push(o2); + } +} \ No newline at end of file diff --git a/src/main/java/pr2/threads/schrecken/synchro/FixedLiveLock.java b/src/main/java/pr2/threads/schrecken/synchro/FixedLiveLock.java new file mode 100644 index 0000000..1485baf --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/FixedLiveLock.java @@ -0,0 +1,36 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +class Looper3 implements Runnable { + + private boolean cont = true; + + public synchronized void requestStop() { + cont = false; + } + + private synchronized boolean cont() { + return cont; + } + + public void run() { + while (cont()) { + // mach was + } + } + +} +public class FixedLiveLock { + + public static void main(String[] args) throws InterruptedException { + + Looper3 looper = new Looper3(); + Thread t = new Thread(looper); + t.start(); + + Thread.sleep(1000); + looper.requestStop(); + } +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/LiveLock2.java b/src/main/java/pr2/threads/schrecken/synchro/LiveLock2.java new file mode 100644 index 0000000..b9ed249 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/LiveLock2.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +class Looper2 implements Runnable { + + private boolean cont = true; + + public synchronized void requestStop() { + cont = false; + } + + public void run() { + while (cont) { + // mach was + } + } + +} +public class LiveLock2 { + + public static void main(String[] args) throws InterruptedException { + + Looper3 looper = new Looper3(); + Thread t = new Thread(looper); + t.start(); + + Thread.sleep(1000); + looper.requestStop(); + } +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/Livelock.java b/src/main/java/pr2/threads/schrecken/synchro/Livelock.java new file mode 100644 index 0000000..75ff32c --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/Livelock.java @@ -0,0 +1,32 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +class Looper implements Runnable { + + private boolean cont = true; + + public void requestStop() { + cont = false; + } + + public void run() { + while (cont) { + // mach was + } + } + +} +public class Livelock { + + public static void main(String[] args) throws InterruptedException { + + Looper looper = new Looper(); + Thread t = new Thread(looper); + t.start(); + + Thread.sleep(1000); + looper.requestStop(); + } +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/MemoryBarrier.java b/src/main/java/pr2/threads/schrecken/synchro/MemoryBarrier.java new file mode 100644 index 0000000..7e51f56 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/MemoryBarrier.java @@ -0,0 +1,11 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro; + +public class MemoryBarrier { + + public static void main(String[] args) { + + } +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/explizit/SimpleStack.java b/src/main/java/pr2/threads/schrecken/synchro/explizit/SimpleStack.java new file mode 100644 index 0000000..0621966 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/explizit/SimpleStack.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro.explizit; + +public class SimpleStack { + + private Object[] stack; + private int pos; + private Object lock = new Object(); + + public SimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public void push(Object o) { + synchronized (lock) { + stack[pos++] = o; + } + } + + public Object pop() { + synchronized (lock) { + return stack[--pos]; + } + } +} \ No newline at end of file diff --git a/src/main/java/pr2/threads/schrecken/synchro/methods/SimpleStack.java b/src/main/java/pr2/threads/schrecken/synchro/methods/SimpleStack.java new file mode 100644 index 0000000..57930d8 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/methods/SimpleStack.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro.methods; + +public class SimpleStack { + + private Object[] stack; + private int pos; + + public SimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public synchronized void push(Object o) { + stack[pos++] = o; + } + + public synchronized Object pop() { + return stack[--pos]; + } +} \ No newline at end of file diff --git a/src/main/java/pr2/threads/schrecken/synchro/none/SimpleStack.java b/src/main/java/pr2/threads/schrecken/synchro/none/SimpleStack.java new file mode 100644 index 0000000..6ea03aa --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/none/SimpleStack.java @@ -0,0 +1,23 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro.none; + +public class SimpleStack { + + private Object[] stack; + private int pos; + + public SimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public void push(Object o) { + stack[pos++] = o; + } + + public Object pop() { + return stack[--pos]; + } +} diff --git a/src/main/java/pr2/threads/schrecken/synchro/thi/SimpleStack.java b/src/main/java/pr2/threads/schrecken/synchro/thi/SimpleStack.java new file mode 100644 index 0000000..4bc0400 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/synchro/thi/SimpleStack.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.synchro.thi; + +public class SimpleStack { + + private Object[] stack; + private int pos; + + public SimpleStack(int size) { + stack = new Object[size]; + pos = 0; + } + + public void push(Object o) { + synchronized (this) { + stack[pos++] = o; + } + } + + public Object pop() { + synchronized (this) { + return stack[--pos]; + } + } +} \ No newline at end of file diff --git a/src/main/java/pr2/threads/schrecken/vola/LivelockFixed.java b/src/main/java/pr2/threads/schrecken/vola/LivelockFixed.java new file mode 100644 index 0000000..44ee678 --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/vola/LivelockFixed.java @@ -0,0 +1,27 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.vola; + +class Looper implements Runnable { + + static volatile boolean cont = true; + + public void run() { + while (cont) { + // mach was + } + } +} +public class LivelockFixed { + + public static void main(String[] args) throws InterruptedException { + + Thread t = new Thread(new Looper()); + + t.start(); + + Thread.sleep(1000); + Looper.cont = false; + } +} diff --git a/src/main/java/pr2/threads/schrecken/vola/PossibleReordering.java b/src/main/java/pr2/threads/schrecken/vola/PossibleReordering.java new file mode 100644 index 0000000..51f6f9a --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/vola/PossibleReordering.java @@ -0,0 +1,20 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.vola; + +public class PossibleReordering { + + static int x = 0, y = 0, a = 0, b = 0; + + public static void main(String[] args) throws Exception { + + Thread one = new Thread(() -> { a = 1; x = b; }); + Thread two = new Thread(() -> { b = 1; y = a; }); + + one.start(); two.start(); + one.join(); two.join(); + + System.out.printf("x=%d, y=%d", x, y); + } +} diff --git a/src/main/java/pr2/threads/schrecken/vola/Reordering.java b/src/main/java/pr2/threads/schrecken/vola/Reordering.java new file mode 100644 index 0000000..d93811b --- /dev/null +++ b/src/main/java/pr2/threads/schrecken/vola/Reordering.java @@ -0,0 +1,28 @@ +/* + * (c) 2009 Thomas Smits + */ +package pr2.threads.schrecken.vola; + +public class Reordering { + + public static void main(String[] args) { + { + int i, k, m; + i = 15; + i++; + k = 18; + k++; + m = i + k; + } + { + + int i, k, m; + k = 18; + i = 15; + k++; + i++; + m = i + k; + + } + } +}