forked from Labore/PR2-L
1
0
Fork 0

added functional programming demos

main
Gerd Marmitt 2024-05-10 15:37:09 +02:00
parent 5104e4ee3b
commit 8346318513
12 changed files with 199 additions and 0 deletions

14
.vscode/launch.json vendored
View File

@ -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",

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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;
}
}

View File

@ -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<Freund> eineListe = new ArrayList<>();
eineListe.add(new Freund(45));
eineListe.add(new Freund(35));
eineListe.add(new Freund(67));
/* Alternativen:
List<Freund> aufsteigendesAlter = eineListe
.stream()
.sorted((freund1, freund2) -> freund1.altersUnterschied(freund2))
.collect(Collectors.toList());
List<Freund> aufsteigendesAlter = eineListe
.stream()
.sorted(Freund::altersUnterschied)
.collect(Collectors.toList());
*/
Comparator<Freund> aufsteigendAlter = (freund1, freund2) ->
freund1.altersUnterschied(freund2);
Comparator<Freund> absteigendAlter =
aufsteigendAlter.reversed();
List<Freund> aufsteigendesAlter = eineListe
.stream().sorted(absteigendAlter)
.collect(Collectors.toList());
ArrayList<Freund> t = new ArrayList<>(aufsteigendesAlter);
for(Freund f : aufsteigendesAlter)
System.out.println(f.alter);
}
}

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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;
}
}

View File

@ -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'");
}
}

View File

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View File

@ -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).

View File

@ -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<Integer> quadriere = e -> e * e;
System.out.println(quadriere.apply(9));
IntPredicate geradeZahl = f -> f % 2 == 0 ? true : false;
System.out.println(geradeZahl.test(8));
}
}