Generics, Enum, Records und Änderung der Datenstruktur
parent
9bb4149c94
commit
9d3eb24eee
|
@ -6,6 +6,7 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="beispieleVL"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
package Crypter;
|
||||
|
||||
|
||||
/**
|
|
@ -1,3 +1,4 @@
|
|||
package Crypter;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
|
@ -1,3 +1,4 @@
|
|||
package Crypter;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
|
@ -0,0 +1,21 @@
|
|||
package enumeration;
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println(Planet.values());
|
||||
|
||||
for (Planet planet : Planet.values()) {
|
||||
System.out.printf("%s -> %,.0f km/h%n",
|
||||
planet.name(),
|
||||
planet.bahngeschwindigkeit());
|
||||
}
|
||||
|
||||
System.out.println(Planet.MERKUR.toString());
|
||||
System.out.println(Planet.MERKUR.name());
|
||||
System.out.println(Planet.JUPITER.toString());
|
||||
System.out.println(Planet.JUPITER.name());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package enumeration;
|
||||
|
||||
public enum Operation {
|
||||
|
||||
ADDITION {
|
||||
public double anwenden(double op1, double op2) {
|
||||
return op1 + op2;
|
||||
}
|
||||
},
|
||||
SUBTRAKTION {
|
||||
public double anwenden(double op1, double op2) {
|
||||
return op1 - op2;
|
||||
}
|
||||
},
|
||||
MULTIPLIKATION {
|
||||
public double anwenden(double op1, double op2) {
|
||||
return op1 * op2;
|
||||
}
|
||||
};
|
||||
|
||||
public abstract double anwenden(double op1, double op2);
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package enumeration;
|
||||
|
||||
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;
|
||||
private final double umlaufzeit;
|
||||
|
||||
private Planet(double entfernungsonne, double umlaufzeit) {
|
||||
this.entfernungsonne = entfernungsonne;
|
||||
this.umlaufzeit = umlaufzeit;
|
||||
}
|
||||
|
||||
public double bahngeschwindigkeit() {
|
||||
return 2 * entfernungsonne * Math.PI / umlaufzeit / 24;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name() + " ist der " + (ordinal() + 1) + " Planet im Sonnensystem.";
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package enumeration;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package enumeration;
|
||||
|
||||
public enum Wochentag {
|
||||
MONTAG, DIENSTAG, MITTWOCH, DONNERSTAG, FREITAG, SAMSTAG, SONNTAG;
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package generics;
|
||||
|
||||
public class ExtStack<T> extends SimpleStack<T> {
|
||||
|
||||
public ExtStack(int size) {
|
||||
super(size);
|
||||
}
|
||||
|
||||
/*
|
||||
* In dieser Methode passiert nichts anderes als die Ausgabe des zuletzt
|
||||
* gesetzten Wertes (in diesem Fall ein String). Das erzeugte Array wurde
|
||||
* üder den Konstruktor ExtStack angelegt, indem die Arraygröße und
|
||||
* der Datentyp in dem Konstruktoraufruf (siehe Mainmethode) übergeben wurde
|
||||
* und der Aufruf an die Superklasse SimpleStack weitergeleitet wurde.
|
||||
*/
|
||||
public T peek() {
|
||||
T wert = pop(); // Hier wird der Wert mit pop() aus dem Array geholt
|
||||
push(wert); // und in die Variable wert geschrieben
|
||||
return wert; // danach erfolgt die Rückmeldung an den Methodenaufruf
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package generics;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
var simplestack1 = new SimpleStack<String>(5);
|
||||
|
||||
simplestack1.push("Hallo");
|
||||
simplestack1.push("das ist ein Beispiel für");
|
||||
simplestack1.push("einen SimpleStack.");
|
||||
simplestack1.push("Logisch wird alles in ");
|
||||
simplestack1.push("verkehrter Reihenfolge ausgegeben.");
|
||||
|
||||
for (int i = simplestack1.getSize(); i > 0 ; i--) {
|
||||
System.out.print(simplestack1.pop() + "| |");
|
||||
}
|
||||
|
||||
var extstack1 = new ExtStack<String>(2);
|
||||
|
||||
extstack1.push("Hallo");
|
||||
extstack1.push("Hello");
|
||||
|
||||
System.out.println(extstack1.peek());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package generics;
|
||||
|
||||
public class SimpleStack<T> {
|
||||
/*
|
||||
* Der Einsatz eines Typ-Parameters hält diese Klasse generisch und
|
||||
* ermöglicht es, dass erst in der Main der Datentyp des Arrays festgelegt
|
||||
* werden kann. Somit ist die generische Klasse in ihrer Implementierung
|
||||
* unabhängig von der Main-Klasse.
|
||||
*/
|
||||
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];
|
||||
|
||||
}
|
||||
/*
|
||||
* Dies ist ziemlich schlechter Programmierstiel, da für einen gut
|
||||
* funktionierenden Stack noch weitere Methoden fehlen.
|
||||
* Diese sind zum Beispiel swap(), peek() und clear(). So das es möglich
|
||||
* ist Daten zu Tauschen (swap()), Daten zu löschen (clear()) oder nach
|
||||
* Daten zu suchen (peek())
|
||||
*/
|
||||
|
||||
public int getSize () {
|
||||
// TODO Auto-generated method stub
|
||||
return pos;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package records;
|
||||
|
||||
|
||||
public class Rechteck extends Exception{
|
||||
|
||||
record Rectangle(double laenge, double breite) {
|
||||
public Rectangle {
|
||||
if (laenge <= 0 || breite <= 0) {
|
||||
throw new java.lang.IllegalArgumentException(
|
||||
String.format("Keine 0 oder negative Werte erlaubt."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Rectangle r = new Rectangle(4.0, 3.0);
|
||||
Rectangle r2 = new Rectangle(1.0, 2.0);
|
||||
|
||||
System.out.println("Fläche: " + r.laenge() * r.breite());
|
||||
System.out.println("Fläche2: " + r2.laenge() * r2.breite());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package records;
|
||||
|
||||
public final class RechteckOhneRecords {
|
||||
|
||||
private final double laenge;
|
||||
private final double breite;
|
||||
|
||||
public RechteckOhneRecords(double laenge, double breite) {
|
||||
this.laenge = laenge;
|
||||
this.breite = breite;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
RechteckOhneRecords r = new RechteckOhneRecords(4, 3);
|
||||
|
||||
System.out.println("Fläche = " + r.laenge() * r.breite());
|
||||
}
|
||||
|
||||
private double breite() {
|
||||
// TODO Auto-generated method stub
|
||||
return laenge;
|
||||
}
|
||||
|
||||
private double laenge() {
|
||||
// TODO Auto-generated method stub
|
||||
return breite;
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ package pr2.generics.einfach;
|
|||
/**
|
||||
* Eine einfache, verkettete Liste.
|
||||
*/
|
||||
public class Liste<E> {
|
||||
public class Liste<T> {
|
||||
|
||||
/** Referenz auf den ersten Knoten. */
|
||||
private ListeNode first;
|
||||
|
@ -19,11 +19,12 @@ public class Liste<E> {
|
|||
/**
|
||||
* Fügt ein neues Element an das Ende der Liste an.
|
||||
*
|
||||
* @param data das Element
|
||||
* @param i das Element
|
||||
*/
|
||||
public void add(Object data) {
|
||||
public void add(String i) {
|
||||
|
||||
ListeNode nextNode = new ListeNode(data);
|
||||
@SuppressWarnings("unchecked")
|
||||
ListeNode nextNode = new ListeNode(i);
|
||||
|
||||
if (current == null) {
|
||||
// Liste komplett leer
|
||||
|
@ -42,17 +43,18 @@ public class Liste<E> {
|
|||
* @param index Index, beginnend bei 0.
|
||||
* @return Das Element oder {@code null}, wenn es nicht gefunden wurde.
|
||||
*/
|
||||
public Object get(int index) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public String get(int index) {
|
||||
int count = 0;
|
||||
ListeNode node = first;
|
||||
T node = (T) first;
|
||||
|
||||
while ((node != null) && (count < index)) {
|
||||
node = node.getNext();
|
||||
node = ((ListeNode<T>) node).getNext();
|
||||
count++;
|
||||
}
|
||||
|
||||
if ((count == index) && (node != null)) {
|
||||
return node.data;
|
||||
return ((ListeNode<T>) node).getData();
|
||||
}
|
||||
else {
|
||||
// index does not exist
|
||||
|
@ -75,13 +77,18 @@ public class Liste<E> {
|
|||
*/
|
||||
public int size() {
|
||||
int count = 0;
|
||||
ListeNode node = first;
|
||||
T node = (T) first;
|
||||
|
||||
while (node != null) {
|
||||
node = node.getNext();
|
||||
node = (T) ((ListeNode) node).getNext();
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public void add(int i) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,25 +3,31 @@ package pr2.generics.einfach;
|
|||
/**
|
||||
* Interne Repräsentation der Knoten in der Liste.
|
||||
*/
|
||||
class ListeNode {
|
||||
class ListeNode<E> {
|
||||
|
||||
/** Daten. */
|
||||
Object data;
|
||||
String data;
|
||||
|
||||
/** Referenz auf den nächsten Knoten. */
|
||||
ListeNode next;
|
||||
E next;
|
||||
/**
|
||||
* Legt einen neuen Knoten an.
|
||||
*
|
||||
* @param data daten, die gespeichert werden
|
||||
*/
|
||||
ListeNode(Object data) {
|
||||
this.data = data;
|
||||
protected ListeNode(String data) {
|
||||
this.setData(data);
|
||||
}
|
||||
public ListeNode getNext() {
|
||||
public E getNext() {
|
||||
return next;
|
||||
}
|
||||
public void setNext(ListeNode next) {
|
||||
public void setNext(E next) {
|
||||
this.next = next;
|
||||
}
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,12 +20,12 @@ public class ListeTest {
|
|||
|
||||
assertEquals(0, l.size());
|
||||
|
||||
l.add("Hallo");
|
||||
l.add( "Hallo");
|
||||
assertEquals(1, l.size());
|
||||
l.add(34);
|
||||
l.add( "");
|
||||
assertEquals(2, l.size());
|
||||
l.add("Peter");
|
||||
l.add("Alfons");
|
||||
l.add( "Peter");
|
||||
l.add( "Alfons");
|
||||
assertEquals(4, l.size());
|
||||
|
||||
l.clear();
|
||||
|
@ -37,7 +37,7 @@ public class ListeTest {
|
|||
*/
|
||||
@Test
|
||||
void testGet() {
|
||||
Liste l = new Liste();
|
||||
Liste<String> l = new Liste<String>();
|
||||
l.add("Hallo");
|
||||
l.add("Hugo");
|
||||
l.add("Peter");
|
||||
|
|
|
@ -2,17 +2,17 @@ package pr2.generics.pair;
|
|||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// TODO: Einkommentieren
|
||||
// var note1 = new Pair<String, Integer>("Peter", 1);
|
||||
// var note2 = new Pair<String, Integer>("Frank", 3);
|
||||
// var note3 = new Pair<String, Integer>("Sabine", 1);
|
||||
//
|
||||
// var name = new Pair<String, String>("Peter", "Meier");
|
||||
//
|
||||
// System.out.printf("%s:%d\n", note1.getFirst(), note1.getSecond());
|
||||
// System.out.printf("%s:%d\n", note2.getFirst(), note1.getSecond());
|
||||
// System.out.printf("%s:%d\n", note3.getFirst(), note1.getSecond());
|
||||
//
|
||||
// System.out.printf("%s %s\n", name.getFirst(), name.getSecond());
|
||||
|
||||
var note1 = new Pair<String, Integer>("Peter", 1);
|
||||
var note2 = new Pair<String, Integer>("Frank", 3);
|
||||
var note3 = new Pair<String, Integer>("Sabine", 1);
|
||||
|
||||
var name = new Pair<String, String>("Peter", "Meier");
|
||||
|
||||
System.out.printf("%s:%d\n", note1.getFirst(), note1.getSecond());
|
||||
System.out.printf("%s:%d\n", note2.getFirst(), note1.getSecond());
|
||||
System.out.printf("%s:%d\n", note3.getFirst(), note1.getSecond());
|
||||
|
||||
System.out.printf("%s %s\n", name.getFirst(), name.getSecond());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
package pr2.generics.pair;
|
||||
|
||||
public class Pair {
|
||||
public class Pair<K, V> {
|
||||
|
||||
private K data1;
|
||||
private V data2;
|
||||
|
||||
public Pair(K data1, V data2) {
|
||||
this.data1 = data1;
|
||||
this.data2 = data2;
|
||||
|
||||
}
|
||||
|
||||
public K getFirst() {
|
||||
return data1;
|
||||
}
|
||||
|
||||
public V getSecond() {
|
||||
return data2;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue