From 83463185134eb28ea66c861e9c254a336a35103d Mon Sep 17 00:00:00 2001 From: Gerd Marmitt Date: Fri, 10 May 2024 15:37:09 +0200 Subject: [PATCH] added functional programming demos --- .vscode/launch.json | 14 ++++++ FreundesListe/.vscode/settings.json | 7 +++ FreundesListe/README.md | 18 ++++++++ FreundesListe/src/Freund.java | 13 ++++++ FreundesListe/src/FreundesListe.java | 43 +++++++++++++++++++ TaschenrechnerInt3/.vscode/settings.json | 7 +++ TaschenrechnerInt3/README.md | 18 ++++++++ TaschenrechnerInt3/src/MathInteger.java | 15 +++++++ .../src/TaschenrechnerInt3.java | 15 +++++++ TaschenrechnerInt4/.vscode/settings.json | 7 +++ TaschenrechnerInt4/README.md | 18 ++++++++ .../src/TaschenrechnerInt4.java | 24 +++++++++++ 12 files changed, 199 insertions(+) create mode 100644 FreundesListe/.vscode/settings.json create mode 100644 FreundesListe/README.md create mode 100644 FreundesListe/src/Freund.java create mode 100644 FreundesListe/src/FreundesListe.java create mode 100644 TaschenrechnerInt3/.vscode/settings.json create mode 100644 TaschenrechnerInt3/README.md create mode 100644 TaschenrechnerInt3/src/MathInteger.java create mode 100644 TaschenrechnerInt3/src/TaschenrechnerInt3.java create mode 100644 TaschenrechnerInt4/.vscode/settings.json create mode 100644 TaschenrechnerInt4/README.md create mode 100644 TaschenrechnerInt4/src/TaschenrechnerInt4.java diff --git a/.vscode/launch.json b/.vscode/launch.json index 6444f54..2e5b59d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,20 @@ // Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "java", + "name": "TaschenrechnerInt4", + "request": "launch", + "mainClass": "TaschenrechnerInt4", + "projectName": "PR2-L_ada963eb" + }, + { + "type": "java", + "name": "TaschenrechnerInt3", + "request": "launch", + "mainClass": "TaschenrechnerInt3", + "projectName": "PR2-L_ada963eb" + }, { "type": "java", "name": "Teilnehmer4", diff --git a/FreundesListe/.vscode/settings.json b/FreundesListe/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/FreundesListe/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/FreundesListe/README.md b/FreundesListe/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/FreundesListe/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/FreundesListe/src/Freund.java b/FreundesListe/src/Freund.java new file mode 100644 index 0000000..f7966c1 --- /dev/null +++ b/FreundesListe/src/Freund.java @@ -0,0 +1,13 @@ +public class Freund { + int alter; + + public Freund(int alter) { + this.alter = alter; + } + + public int altersUnterschied( + Freund einFreund) + { + return alter - einFreund.alter; + } +} diff --git a/FreundesListe/src/FreundesListe.java b/FreundesListe/src/FreundesListe.java new file mode 100644 index 0000000..f200d99 --- /dev/null +++ b/FreundesListe/src/FreundesListe.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +public class FreundesListe { + public static void main(String[] args) throws Exception { + ArrayList eineListe = new ArrayList<>(); + + eineListe.add(new Freund(45)); + eineListe.add(new Freund(35)); + eineListe.add(new Freund(67)); + +/* Alternativen: + List aufsteigendesAlter = eineListe + .stream() + .sorted((freund1, freund2) -> freund1.altersUnterschied(freund2)) + .collect(Collectors.toList()); + + List aufsteigendesAlter = eineListe + .stream() + .sorted(Freund::altersUnterschied) + .collect(Collectors.toList()); +*/ + +Comparator aufsteigendAlter = (freund1, freund2) -> + freund1.altersUnterschied(freund2); +Comparator absteigendAlter = + aufsteigendAlter.reversed(); + + List aufsteigendesAlter = eineListe + .stream().sorted(absteigendAlter) + .collect(Collectors.toList()); + + ArrayList t = new ArrayList<>(aufsteigendesAlter); + + + for(Freund f : aufsteigendesAlter) + System.out.println(f.alter); + } +} diff --git a/TaschenrechnerInt3/.vscode/settings.json b/TaschenrechnerInt3/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/TaschenrechnerInt3/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/TaschenrechnerInt3/README.md b/TaschenrechnerInt3/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/TaschenrechnerInt3/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/TaschenrechnerInt3/src/MathInteger.java b/TaschenrechnerInt3/src/MathInteger.java new file mode 100644 index 0000000..e33a606 --- /dev/null +++ b/TaschenrechnerInt3/src/MathInteger.java @@ -0,0 +1,15 @@ +@FunctionalInterface +interface MathInteger +{ + int berechne(int a, int b); + + default long berechneLong(int a, int b) + { + return (long)a * (long) b; + } + + static double berechneStatisch(int a, int b) + { + return (double)a / (double) b; + } +} \ No newline at end of file diff --git a/TaschenrechnerInt3/src/TaschenrechnerInt3.java b/TaschenrechnerInt3/src/TaschenrechnerInt3.java new file mode 100644 index 0000000..15fe8ad --- /dev/null +++ b/TaschenrechnerInt3/src/TaschenrechnerInt3.java @@ -0,0 +1,15 @@ +public class TaschenrechnerInt3 implements MathInteger { + public static void main(String args []) + { + System.out.println(MathInteger.berechneStatisch(10, 20)); + TaschenrechnerInt3 meinRechner = new TaschenrechnerInt3(); + long x = meinRechner.berechneLong(100000, 200000); + System.out.println(x); + } + + @Override + public int berechne(int a, int b) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'berechne'"); + } +} diff --git a/TaschenrechnerInt4/.vscode/settings.json b/TaschenrechnerInt4/.vscode/settings.json new file mode 100644 index 0000000..0ac215c --- /dev/null +++ b/TaschenrechnerInt4/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/TaschenrechnerInt4/README.md b/TaschenrechnerInt4/README.md new file mode 100644 index 0000000..a43b9f6 --- /dev/null +++ b/TaschenrechnerInt4/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/TaschenrechnerInt4/src/TaschenrechnerInt4.java b/TaschenrechnerInt4/src/TaschenrechnerInt4.java new file mode 100644 index 0000000..54c90e2 --- /dev/null +++ b/TaschenrechnerInt4/src/TaschenrechnerInt4.java @@ -0,0 +1,24 @@ +import java.util.function.IntFunction; +import java.util.function.IntPredicate; + +public class TaschenrechnerInt4 { + @FunctionalInterface + interface MathInteger { + int berechne(int a, int b); + } + + public static void main(String args[]) + { + MathInteger addiere = (c, d) -> c + d; + MathInteger multipliziere = (x, y) -> x * y; + + System.out.println(addiere.berechne(10, 20)); + System.out.println(multipliziere.berechne(10, 20)); + + IntFunction quadriere = e -> e * e; + System.out.println(quadriere.apply(9)); + + IntPredicate geradeZahl = f -> f % 2 == 0 ? true : false; + System.out.println(geradeZahl.test(8)); + } +} \ No newline at end of file