master
3009594 2025-01-06 23:04:29 +01:00
parent ab94f3e68b
commit 2314f35393
24 changed files with 656 additions and 197 deletions

View File

@ -3,7 +3,7 @@ package Algorithmus;
import java.util.Arrays;
import java.util.stream.Stream;
public class Allgemeine_Übungen {
public class Allgemeine_Übungen extends Thread {
public static void main(String[] args) {
// int [] nums = {3, 4, -7, 3, 1, 3, 1, -4, -2, -2};

View File

@ -0,0 +1,24 @@
package Algorithmus;
import java.util.ArrayList;
public class ConcurrentModificationsException {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<>();
al.add("A");
al.add("B");
al.add("C");
al.add("D");
for (int i = 0; i < al.size(); i++) {
if (al.get(i).equals("A")) {
al.remove(i);
}
}
}
}

View File

@ -0,0 +1,30 @@
package Algorithmus;
import java.util.*;
public class Person {
String name;
int alter;
Person(String name, int alter) {
this.name = name;
this.alter = alter;
}
public static void main(String[] args) {
Person[] arr = { new Person("Anna", 25),new Person("Ben", 30), new Person("Clara", 25),new Person("Daniel", 30)};
Map<Integer, List<String>> merker = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
int alter = arr[i].alter;
if (!merker.containsKey(alter))
merker.put(alter, new ArrayList<>());
merker.get(alter).add(arr[i].name);
}
System.out.println(merker.toString());
}
}

View File

@ -2,7 +2,7 @@ package Algorithmus;
public class VariadischeFunktionen {
public static int sum(Integer... numbers) {
public static int sum(int... numbers) {
int result = 0;
for (int number : numbers)
result += number;

View File

@ -0,0 +1,45 @@
package Algorithmus;
import java.util.*;
public class WordCount {
public static void main(String[] args) {
// String[] arr = { "hallo welt", "hallo omar", "hallo obai" };
// Map<String,Integer> speicher = new HashMap<>();
// for (int i = 0; i < arr.length; i++) {
// String[] wörter = arr[i].split(" ");
//
// for (int j = 0 ; j < wörter.length; j++) {
// if (!speicher.containsKey(wörter[j])) {
// speicher.put(wörter[j], 1);
// }
// else
// speicher.put(wörter[j], speicher.get(wörter[j]) + 1); // Nur für dieses Wort erhöhen
//
//
// }
//
// }
//
// speicher.forEach((i,j) -> System.out.println(i + " " + j));
int[] zahlen = {5, 10, 15, 20, 25, 30, 35, 4};
Map<Boolean,List<Integer>> speicher = new HashMap<>();
speicher.put(true, new ArrayList<>());
speicher.put(false, new ArrayList<>());
for (int i = 0; i < zahlen.length; i++) {
boolean merker = zahlen[i] % 2 == 0;
speicher.get(merker).add(zahlen[i]);
}
System.out.println(speicher.toString());
}
}

View File

@ -0,0 +1,85 @@
package BinaryTree;
public class Baum {
private Knoten wurzel;
public Baum() {
wurzel = null;
System.out.println("Leerer Baum angelegt.");
}
public void insert(int neu) {
if (wurzel == null) {
wurzel = new Knoten(neu);
} else if (wurzel != null) {
wurzel.add(neu);
}
}
public void inorderTraversierung() {
if (wurzel == null)
System.out.println("Baum ist leer.");
else if (wurzel != null) {
System.out.print("Inorder-Reihenfolge: ");
wurzel.inorder();
}
}
class Knoten {
private int wert;
private int htb;
private Knoten links, rechts;
public Knoten(int wert) {
this.wert = wert;
this.links = null;
this.rechts = null;
this.htb = 0;
System.out.println(wert + " als Knoten angelegt.");
}
public void add(int neu) {
if (neu == this.wert)
throw new IllegalArgumentException("Wert darf nicht doppelt gespeichert werden.");
int htbL = 0;
int htbR = 0;
if (neu < this.wert) { // nach links schauen
if (links == null) {
links = new Knoten(neu);
} else if (links != null) {
links.add(neu);
}
htbL = links.htb + 1;
this.htb = Math.max(htbL, this.htb);
} else if (neu > this.wert){ // nach rechts schauen
if (rechts == null) {
rechts = new Knoten(neu);
} else if (rechts != null) {
rechts.add(neu);
}
htbR = rechts.htb + 1;
this.htb = Math.max(this.htb, htbR);
}
// this.htb = Math.max(htbL, htbR); -> Bug, da ein evtl. vorher vorhandener Wert ignoriert wird
System.out.println("Wert: " + this.wert + ", HTB: " + this.htb);
} // end add
public void inorder() {
if (links != null)
links.inorder();
System.out.print(this.wert + " ");
if (rechts != null)
rechts.inorder();
}
} // end Knoten
} // end Baum

View File

@ -1,29 +1,129 @@
package Graphen;
import java.util.ArrayList;
import java.util.*;
public class Graph {
// Graph hat Viele Knoten
ArrayList<Knote> KnotenGraph = new ArrayList<>();
//Generische Graph-Klasse
class Graph<T> {
// füge Knote hinzu
public void addknoten(Knote k) {
// Wir verwenden eine HashMap, um die Kanten im Graphen zu speichern
private Map<T, List<T>> kanten = new HashMap<>();
KnotenGraph.add(k);
}
// Diese Funktion fügt einen neuen Knoten (Vertex) zum Graphen hinzu
public void knotenHinzufügen(T k) {
kanten.put(k, new LinkedList<T>());
}
// Diese Funktion fügt eine Kante zwischen Quelle und Ziel hinzu
public void kanteHinzufügen(T quelle, T ziel, boolean bidirektional) {
if (!kanten.containsKey(quelle))
knotenHinzufügen(quelle);
public Knote getKnote(String name) {
for (Knote kAusKnotenGraph: KnotenGraph) {
if (kAusKnotenGraph.getName().equalsIgnoreCase(name))
return kAusKnotenGraph;
}
return null;
}
if (!kanten.containsKey(ziel))
knotenHinzufügen(ziel);
public void printKnotenGraph() {
for (Knote a : KnotenGraph)
System.out.println(a.getName());
}
kanten.get(quelle).add(ziel);
if (bidirektional) {
kanten.get(ziel).add(quelle);
}
}
// Diese Funktion gibt die Anzahl der Knoten im Graphen zurück
public void anzahlKnoten() {
System.out.println("Der Graph hat " + kanten.keySet().size() + " Knoten.");
}
// Diese Funktion gibt die Anzahl der Kanten im Graphen zurück
public void anzahlKanten(boolean bidirektional) {
int anzahl = 0;
for (T k : kanten.keySet()) {
anzahl += kanten.get(k).size();
}
if (bidirektional) {
anzahl = anzahl / 2;
}
System.out.println("Der Graph hat " + anzahl + " Kanten.");
}
// Diese Funktion prüft, ob ein bestimmter Knoten vorhanden ist
public void hatKnoten(T k) {
if (kanten.containsKey(k)) {
System.out.println("Der Graph enthält " + k + " als Knoten.");
} else {
System.out.println("Der Graph enthält " + k + " nicht als Knoten.");
}
}
// Diese Funktion prüft, ob eine Kante zwischen zwei Knoten existiert
public void hatKante(T quelle, T ziel) {
if (kanten.get(quelle).contains(ziel)) {
System.out.println("Der Graph hat eine Kante zwischen " + quelle + " und " + ziel + ".");
} else {
System.out.println("Der Graph hat keine Kante zwischen " + quelle + " und " + ziel + ".");
}
}
// Diese Funktion gibt die Nachbarn eines Knotens aus
public void nachbarn(T k) {
if (!kanten.containsKey(k))
return;
System.out.println("Die Nachbarn von " + k + " sind:");
for (T nachbar : kanten.get(k)) {
System.out.print(nachbar + ", ");
}
}
// Gibt die Adjazenzliste jedes Knotens aus
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (T k : kanten.keySet()) {
builder.append(k.toString() + ": ");
for (T nachbar : kanten.get(k)) {
builder.append(nachbar.toString() + " ");
}
builder.append("\n");
}
return (builder.toString());
}
}
//Hauptklasse
class Main {
public static void main(String[] args) {
// Objekt des Graphen wird erstellt
Graph<Integer> graph = new Graph<>();
// Kanten werden hinzugefügt.
// Da der Graph bidirektional ist, wird true übergeben
graph.kanteHinzufügen(0, 1, true);
graph.kanteHinzufügen(0, 4, true);
graph.kanteHinzufügen(1, 2, true);
graph.kanteHinzufügen(1, 3, true);
graph.kanteHinzufügen(1, 4, true);
graph.kanteHinzufügen(2, 3, true);
graph.kanteHinzufügen(3, 4, true);
// Graph ausgeben
System.out.println("Graph:\n" + graph.toString());
// Anzahl der Knoten im Graphen
graph.anzahlKnoten();
// Anzahl der Kanten im Graphen
graph.anzahlKanten(true);
// Prüfen, ob eine bestimmte Kante existiert
graph.hatKante(3, 4);
// Prüfen, ob ein bestimmter Knoten existiert
graph.hatKnoten(5);
// Nachbarn eines Knotens ausgeben
graph.nachbarn(1);
}
}

View File

@ -1,60 +1,33 @@
package Graphen;
public class Kanten {
public class Kanten<T> {
// jede Kante verbindet zwei Knoten
private Knote anfangsKnote;
private Knote endeKnote;
private boolean UndirectedGraph;
private int gewicht;
private Knoten anfangsKnote;
private Knoten endeKnote;
private T gewicht;
// Kante verbindet zwei Knoten (Undirected Graph)
public Kanten(Knote anfangsKnote, Knote endeKnote, boolean UndirectedGraph) {
public Kanten(Knoten anfangsKnote, Knoten endeKnote, T gewicht) {
this.anfangsKnote = anfangsKnote;
this.endeKnote = endeKnote;
this.UndirectedGraph = UndirectedGraph;
anfangsKnote.addKante(this); //speichert dann zu ihrem ArrayList diese Kante
endeKnote.addKante(this);//speichert dann auch zu ihrem ArrayList diese gleiche Kante
this.anfangsKnote.addKante(this);
this.endeKnote.addKante(this);
this.gewicht = gewicht;
}
// Kante verbindet eine Knote (Directed Graph)
public Kanten(Knote anfangsKnote, Knote endeKnote) {
this.anfangsKnote = anfangsKnote;
this.endeKnote = endeKnote;
anfangsKnote.addKante(this);
public Knoten getPartner(Knoten k) {
if (k == anfangsKnote)
return endeKnote;
else
return anfangsKnote;
}
// Kante verbindet eine Knoten und mit Gewicht (Weighted Directed Graph)
public Kanten(Knote anfangsKnote, Knote endeKnote, int gewicht) {
this.anfangsKnote = anfangsKnote;
this.endeKnote = endeKnote;
//anfangsKnote.addkanteMitGewicht(this,gewicht);
}
// Kante verbindet zwei Knoten und mit Gewicht (Weighted UnDirected Graph)
public Kanten(Knote anfangsKnote, Knote endeKnote, int gewicht, boolean UndirectedGraph) {
this.anfangsKnote = anfangsKnote;
this.endeKnote = endeKnote;
// anfangsKnote.addkanteMitGewicht(this, gewicht);
// endeKnote.addkanteMitGewicht(this, gewicht);
this.UndirectedGraph = UndirectedGraph;
}
public Knote getPartner(Knote k) {
return (k == anfangsKnote) ? endeKnote : anfangsKnote;
}
public int getGewicht() {
public T getGewicht() {
return gewicht;
}
public Knote getAnfangsKnote() {
return anfangsKnote;
}
public Knote getEndeKnote() {
return endeKnote;
}

View File

@ -1,44 +0,0 @@
package Graphen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Knote {
private String name;
private ArrayList<Kanten> kantenList = new ArrayList<>();
Knote(String name){
this.name = name;
}
//add kante zu this (knote)
public void addKante(Kanten k) {
kantenList.add(k);
}
public String getName() {
return name;
}
public ArrayList<Kanten> getKantenList() {
return kantenList;
}
public void getPartner() {
ArrayList<Kanten> merker = getKantenList();
if (merker == null)
return ;
ArrayList<Knote> verbindeteKnoten = new ArrayList<>();
for (Kanten m : merker)
verbindeteKnoten.add(m.getPartner(this));
for (Knote k: verbindeteKnoten)
System.out.println(this.name + " verbindet mit " + k.getName());
}
}

View File

@ -0,0 +1,48 @@
package Graphen;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Knoten<T> {
private T id;
private ArrayList<Kanten> kantenList;
Knoten(T id) {
this.id = id;
this.kantenList = new ArrayList<>();
}
// add kante zu this (knote)
public void addKante(Kanten k) {
kantenList.add(k);
}
public T getId() {
return id;
}
// Grad ist die Anzahl der Kanten an einem Knoten
public int getGrad() {
return kantenList.size();
}
public ArrayList<Kanten> getKantenList() {
return kantenList;
}
public void getPartner() {
if (kantenList == null)
return;
ArrayList<Knoten<T>> verbindeteKnoten = new ArrayList<>();
for (Kanten m : kantenList)
verbindeteKnoten.add(m.getPartner(this));
}
@Override
public String toString() {
return this.id + " ";
}
}

View File

@ -3,26 +3,6 @@ package Graphen;
public class Test {
public static void main(String[] args) {
Graph graph = new Graph();
Knote mannheim = new Knote("Mannheim");
Knote heidelberg = new Knote("Heidelberg");
Knote frankfourt = new Knote("Frankfourt");
Knote Maiz = new Knote("Maiz");
graph.addknoten(mannheim);
graph.addknoten(heidelberg);
graph.addknoten(frankfourt);
graph.addknoten(Maiz);
new Kanten(graph.getKnote("Mannheim"),graph.getKnote("Heidelberg"));
new Kanten(graph.getKnote("Mannheim"),graph.getKnote("Frankfourt"));
mannheim.getPartner();
heidelberg.getPartner();
frankfourt.getPartner();
Kanten k3 = new Kanten(graph.getKnote("Heidelberg"),graph.getKnote("Maiz"),25,true);
}

View File

@ -0,0 +1,55 @@
package Klausur;
public class Firma {
public static void main(String[] args) {
Mitarbeiter mi2 = new Mitarbeiter("Mayer", "12.12.1999");
((Manager)mi2).getAbteilung();
}
}
class Mitarbeiter extends Object {
private String name;
private double gehalt;
private String geburtsdatum;
public Mitarbeiter(String name, String geburtsdatum) {
this(name, 35000, geburtsdatum);
}
public Mitarbeiter(String name, double gehalt, String geburtsdatum) {
this.name = name;
this.gehalt = gehalt;
this.geburtsdatum = geburtsdatum;
}
public String getDetails() {
return name;
}
public double getGehalt() {
return gehalt;
}
public void setGehalt(double gehalt) {
this.gehalt = gehalt;
}
}
class Manager extends Mitarbeiter {
private String abteilung;
public Manager(String name, String geburtsdatum, String abteilung) {
super(name, 65000, geburtsdatum); // das MUSS zuerst gemacht werden!
this.abteilung = abteilung;
}
public String getDetails() {
return super.getDetails() + " " + abteilung;
}
public String getAbteilung() {
return abteilung;
}
}

View File

@ -0,0 +1,22 @@
package Klausur.zoo;
public abstract class Animal {
private String name;
private int birthyear;
private String species;
public Animal(String name, int birthyear) {
this.name = name;
this.birthyear = birthyear;
species = this.getClass().getName();
species = species.substring(species.lastIndexOf(".")+1);
}
public abstract void speak();
public String getSpecies() {
return species;
}
}

View File

@ -0,0 +1,15 @@
package Klausur.zoo;
public class Cat extends Animal {
public Cat(String name, int birthyear) {
super(name, birthyear);
}
@Override
public void speak() {
System.out.println(super.getSpecies() + " macht: Meow");
}
}

View File

@ -0,0 +1,14 @@
package Klausur.zoo;
public class Dog extends Animal {
public Dog(String name, int birthyear) {
super(name, birthyear);
}
@Override
public void speak() {
System.out.println(super.getSpecies() + " macht: Woof");
}
}

View File

@ -0,0 +1,24 @@
package Klausur.zoo;
import java.util.ArrayList;
public class ZooMain {
public static void main(String[] args) throws Exception {
ArrayList<Animal> tiere = new ArrayList<>();
// Animal a = new Animal("Animal", "1934"); // abstrakt, daher keine Instanzen möglich!
Animal c = new Cat("Sylvester", 1947);
c.speak();
tiere.add(c);
Animal d = new Dog("Spike", 1963);
d.speak();
tiere.add(d);
System.out.println("Anzahl der Tiere im Zoo: " + tiere.size());
}
}

View File

@ -0,0 +1,25 @@
package Lambda;
public class Addiere {
public static void main(String[] args) {
Add add = (a,b) -> a+b;
// int result = add.add(12, 12);
// System.out.println(result);
System.out.println(addieren(12,12,add));
}
public static int addieren(int a,int b,Add add) {
return add.add(a, b);
}
}
interface Add{
int add(int a, int b);
}

View File

@ -1,28 +0,0 @@
package Lambda;
public class Übungtestate2 {
public static void main(String[] args) {
Test23 t = new Test23() {
@Override
public void print () {
System.out.println("Annynom Innere Klasse");
}
};
t.print();
}
}
class Test23 {
public void print () {
System.out.println("Test");
}
}

View File

@ -0,0 +1,26 @@
package Thread;
import java.util.Random;
class Runner4 implements Runnable {
private int zaehler = 0;
public void run() {
int local = new Random().nextInt();
System.out.println(local);
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();
}
}

View File

@ -0,0 +1,34 @@
package oop.Generics;
public class Parent<T> {
private T name;
public Parent(T name) {
this.name = name;
}
public T getName() {
return name;
}
public void setName(T name) {
this.name = name;
}
@Override
public String toString() {
return "Parent [name=" + name + "]";
}
}
class Sohn<T> extends Parent<T>{
public Sohn(T name) {
super(name);
}
}

View File

@ -8,12 +8,18 @@ public class RawType{
ArrayList<Integer> t = new ArrayList<>();
t.add(22);
t.add(12);
t.add(123);
ArrayList<String> t2 = new ArrayList<>();
t2.add("dd");
add(t2);
}
public static void printList(ArrayList<Object> list) {
for (Object elem : list)
System.out.println(elem + " ");
}
public static <T extends Number> void add(ArrayList<?> list) {
for (Object o : list)
System.out.println(o.toString());
}
}

View File

@ -0,0 +1,25 @@
package streams;
import java.util.*;
public class KlausurAufgabe {
public static void main(String[] args) {
String[] arr = {"USW", "GGT", "UA", "ZB", "FKK", "MFG", "SOSE", "A", "H", "MM", "MBIT"};
Map<Integer, List<String>> speicher = new HashMap<>();
for (String s : arr) {
int length = s.length();
if (!speicher.containsKey(length))
speicher.put(length, new ArrayList<>());
speicher.get(length).add(s);
}
System.out.println(speicher);
}
}