Collection
parent
4d108c9120
commit
4c82d7cbb4
|
@ -0,0 +1,38 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class Allgemeine_Übungen {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String text = "HalloWelt";
|
||||
String o = "o";
|
||||
text.substring(0, 0);
|
||||
int x = indexof(text,o);
|
||||
System.out.println(x);
|
||||
}
|
||||
|
||||
public static int indexof(String text,String s) {
|
||||
for (int i = 0; i < text.length(); i++)
|
||||
if (text.charAt(i) == s.charAt(0))
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void find_Sub_Array(int[] arr, int target) {
|
||||
|
||||
int merker = 0;
|
||||
for (int i = 0; i < arr.length -1 ; i++) {
|
||||
merker = arr[i];
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
merker += arr[j];
|
||||
if (merker == target) {
|
||||
System.out.println("Ab Index: " + i + " bis Index: " + j + " sind die Summer der Zahl: " + target);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,54 @@
|
|||
package Algorithmus.BitweisesOperatoren;
|
||||
|
||||
public class AND {
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
* Bitweises UND (&): - Funktion: Der & Operator prüft beide Operanden bitweise
|
||||
* und gibt true zurück, wenn beide Operanden true sind (bei logischen Ausdrücken).
|
||||
*
|
||||
* - Merkmal: Beide Bedingungen (x und y) werden immer ausgewertet, unabhängig
|
||||
* davon, ob die erste Bedingung false ergibt.
|
||||
*
|
||||
* - Hinweis: Der & Operator kann auch bitweise verwendet werden, um die Bits
|
||||
* zweier Ganzzahlen zu vergleichen.
|
||||
*/
|
||||
|
||||
// Beispiel:
|
||||
boolean x = true;
|
||||
boolean y = false;
|
||||
// z ist false, weil y false ist, aber y wird dennoch ausgewertet.
|
||||
boolean z = x & y;
|
||||
|
||||
// Praktischer Einsatz:
|
||||
int a = 5;
|
||||
int b = 0;
|
||||
|
||||
// Beide Bedingungen werden überprüft. b wird erhöht, auch wenn die Bedingung a
|
||||
// > 5 false ist.
|
||||
if ((b++ < 10) & a > 5)
|
||||
System.out.println(a);
|
||||
|
||||
System.out.println(b); // b = 1
|
||||
|
||||
/*
|
||||
* Logisches UND (&&): - Funktion: Der && Operator prüft beide Operanden und
|
||||
* gibt true zurück, wenn beide true sind (bei logischen Ausdrücken).
|
||||
*
|
||||
* - Merkmal: Wenn der erste Operand false ist, wird der zweite Operand nicht
|
||||
* mehr ausgewertet. Dies spart Rechenzeit und kann in bestimmten Fällen Fehler
|
||||
* vermeiden (z. B. Division durch 0).
|
||||
*/
|
||||
|
||||
// Praktischer Einsatz:
|
||||
int a2 = 5;
|
||||
int b2 = 0;
|
||||
|
||||
// Die zweite Bedingung wird nicht überprüft, weil a2 > 5 false ist. b2 wird
|
||||
// daher nicht erhöht.
|
||||
if (a2 > 5 && (b2++ < 10))
|
||||
System.out.println(a2);
|
||||
|
||||
System.out.println(b2); // b = 0
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,45 @@
|
|||
package Algorithmus.BitweisesOperatoren;
|
||||
|
||||
public class NOT {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
* Bitweises NOT (~):
|
||||
* - Funktion: Der ~ Operator invertiert alle Bits einer Ganzzahl (bitweises NOT).
|
||||
*
|
||||
* - Beispiel: Für eine Ganzzahl invertiert ~ jedes Bit. Bei einer positiven Zahl
|
||||
* wird sie negativ (im Zweierkomplement), und bei einer negativen Zahl wird sie positiv.
|
||||
*
|
||||
* - Hinweis: Bitweises NOT kann nur auf Ganzzahlen angewendet werden, nicht auf logische Werte (booleans).
|
||||
*/
|
||||
|
||||
// Beispiel:
|
||||
int x = 5; // Binär: 00000000 00000000 00000000 00000101
|
||||
int result = ~x; // Binär: 11111111 11111111 11111111 11111010 (Das ist -6 im Zweierkomplement)
|
||||
|
||||
System.out.println(result); // Ausgabe: -6
|
||||
|
||||
|
||||
/*
|
||||
* Logisches NOT (!):
|
||||
* - Funktion: Der ! Operator invertiert den logischen Wert eines Ausdrucks.
|
||||
* Wenn der Operand true ist, wird er false, und umgekehrt.
|
||||
*
|
||||
* - Hinweis: Logisches NOT wird nur auf boolesche Werte angewendet (true/false).
|
||||
*/
|
||||
|
||||
// Beispiel:
|
||||
boolean y = true;
|
||||
// Logisches NOT invertiert y, also wird !y false
|
||||
boolean result2 = !y;
|
||||
|
||||
System.out.println(result2); // Ausgabe: false
|
||||
|
||||
// Praktischer Einsatz:
|
||||
int a = 5;
|
||||
|
||||
// Die Bedingung a > 10 ist false, aber durch das logische NOT (!) wird sie true
|
||||
if (!(a > 10))
|
||||
System.out.println("a ist nicht größer als 10");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package Algorithmus.BitweisesOperatoren;
|
||||
|
||||
public class ODER {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
* Bitweises ODER (|): - Funktion: Der | Operator prüft beide Operanden bitweise
|
||||
* und gibt true zurück, wenn mindestens einer der Operanden true ist (bei
|
||||
* logischen Ausdrücken).
|
||||
*
|
||||
* - Merkmal: Beide Bedingungen (x und y) werden immer ausgewertet, unabhängig
|
||||
* davon, ob die erste Bedingung true ergibt.
|
||||
*
|
||||
* - Hinweis: Der | Operator kann auch bitweise verwendet werden, um die Bits
|
||||
* zweier Ganzzahlen zu vergleichen.
|
||||
*/
|
||||
|
||||
// Beispiel:
|
||||
boolean x = true;
|
||||
boolean y = false;
|
||||
// z ist true, weil x true ist, aber y wird dennoch ausgewertet.
|
||||
boolean z = x | y;
|
||||
|
||||
// Praktischer Einsatz:
|
||||
int a = 5;
|
||||
int b = 0;
|
||||
|
||||
// Beide Bedingungen werden überprüft. b wird erhöht, auch wenn die erste
|
||||
// Bedingung a < 5 true ist.
|
||||
if ((b++ < 10) | a < 5)
|
||||
System.out.println(a);
|
||||
|
||||
System.out.println(b); // b = 1
|
||||
|
||||
/*
|
||||
* Logisches ODER (||): - Funktion: Der || Operator prüft beide Operanden und
|
||||
* gibt true zurück, wenn mindestens einer der Operanden true ist (bei logischen
|
||||
* Ausdrücken).
|
||||
*
|
||||
* - Merkmal: Wenn der erste Operand true ist, wird der zweite Operand nicht
|
||||
* mehr ausgewertet. Dies spart Rechenzeit und kann in bestimmten Fällen Fehler
|
||||
* vermeiden (z. B. unnötige Berechnungen oder Operationen).
|
||||
*/
|
||||
|
||||
// Praktischer Einsatz:
|
||||
int a2 = 5;
|
||||
int b2 = 0;
|
||||
|
||||
// Die zweite Bedingung wird nicht überprüft, weil a2 < 10 true ist. b2 wird
|
||||
// daher nicht erhöht.
|
||||
if (a2 < 10 || (b2++ < 10))
|
||||
System.out.println(a2);
|
||||
|
||||
System.out.println(b2); // b = 0
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,52 +0,0 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class Logischen_Operatoren {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
/* Bitweises UND (&):
|
||||
* - Funktion: Der & Operator prüft beide Operanden und gibt true zurück, wenn beide Operanden true sind.
|
||||
* - Merkmal: Beide Bedingungen (a und b) werden immer ausgewertet, unabhängig davon, ob die erste Bedingung false ergibt.
|
||||
* - Das Prinzip gilt auch für die anderen Logischen_Operatoren
|
||||
*/
|
||||
|
||||
//Beispiel:
|
||||
boolean x = true;
|
||||
boolean y = false;
|
||||
// Z ist false, weil y false ist, aber y wird dennoch ausgewertet.
|
||||
boolean z = x & y;
|
||||
|
||||
//Praktischer Einsatz:
|
||||
int a = 5;
|
||||
int b = 0;
|
||||
|
||||
//Beide Bedingungen werden überprüft, b wird erhöht, auch wenn a false ist!
|
||||
if ( a > 5 & (b++ < 10))
|
||||
System.out.println(a);
|
||||
|
||||
System.out.println(b);// b = 1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Logisches UND (&&):
|
||||
* - Funktion: Der && Operator prüft ebenfalls beide Operanden und gibt true zurück, wenn beide true sind.
|
||||
* - Merkmal: Wenn der erste Operand false ist, wird der zweite Operand nicht mehr ausgewertet. Dies spart Rechenzeit und kann in bestimmten Fällen Fehler vermeiden.
|
||||
*/
|
||||
|
||||
//Praktischer Einsatz:
|
||||
int a2 = 5;
|
||||
int b2 = 0;
|
||||
|
||||
// Beide Bedingungen werden überprüft, b wird nicht erhöht, da a false ist!
|
||||
if (a2 > 5 && (b2++ < 10))
|
||||
System.out.println(a2);
|
||||
|
||||
System.out.println(b2);// b = 0
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class MathKlasse {
|
||||
|
||||
// Alle Mathe Klasse Methoden geben 'Double' Zurück
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*((public static double random()))
|
||||
*
|
||||
* Random methode: gibt double zurück. - 0.0
|
||||
* <= Math.random() < 1.0
|
||||
*
|
||||
*/
|
||||
|
||||
double rand = Math.random();
|
||||
for (int i = 1; i <= 6; i++)
|
||||
System.out.println(" Würfel " + i + " : " + (int) (Math.random() * 6 + 1));
|
||||
|
||||
|
||||
|
||||
|
||||
/* ((public static double abs(double d)))
|
||||
*
|
||||
* - gibt immer eine positive Zahl oder 0 zurück
|
||||
* - Math.abs(10) gibt 10 zurück.
|
||||
* - Math.abs(-10) gibt ebenfalls 10 zurück.
|
||||
* - Math.abs(0) gibt 0 zurück.
|
||||
*
|
||||
* . kann mit verschiedenen DatenTypen übernehmen:
|
||||
* - double
|
||||
* - int
|
||||
* - float
|
||||
* - long
|
||||
*/
|
||||
double x = 10.5;
|
||||
double c = Math.abs(x);
|
||||
|
||||
|
||||
|
||||
|
||||
/* ((public static double ceil(double d))
|
||||
*
|
||||
* - gibt die nächstgrößere ganze Zahl zurück, die größer oder gleich
|
||||
* dem übergebenen Wert
|
||||
*
|
||||
* . Beispiele:
|
||||
* - Math.ceil(4.3) gibt 5.0 zurück.
|
||||
* - Math.ceil(7.1) gibt 8.0 zurück.
|
||||
* - Math.ceil(5.0) gibt 5.0 zurück (weil es schon eine ganze Zahl ist).
|
||||
*/
|
||||
double x2 = 10.2;
|
||||
double c2 = Math.ceil(x2);//11
|
||||
|
||||
|
||||
|
||||
|
||||
/* ((public static double floor(double d)))
|
||||
*
|
||||
* - gibt die nächstkleinere ganze Zahl zurück, die kleiner oder gleich dem übergebenen Wert d ist
|
||||
*
|
||||
* . Beispiele:
|
||||
* - Math.floor(4.8) gibt 4.0 zurück.
|
||||
* - Math.floor(7.9) gibt 7.0 zurück.
|
||||
* - Math.floor(5.0) gibt 5.0 zurück (weil es schon eine ganze Zahl ist).
|
||||
*/
|
||||
double x3 = 10.5;
|
||||
double c3 = Math.floor(x3);//10
|
||||
|
||||
|
||||
/* ((public static double rint(double d)))
|
||||
* - gibt die am nächsten gelegene ganze Zahl als double zurück
|
||||
*
|
||||
* . Beispiele:
|
||||
* - Math.rint(4.3) gibt 4.0 zurück.
|
||||
* - Math.rint(5.8) gibt 6.0 zurück.
|
||||
* - Math.rint(3.5) gibt 4.0 zurück (weil 4 die nächste gerade Zahl ist).
|
||||
* - Math.rint(2.5) gibt 2.0 zurück (weil 2 die nächste gerade Zahl ist).
|
||||
*/
|
||||
double x4 = 10.5;
|
||||
double c5 = Math.floor(x4);//11
|
||||
|
||||
|
||||
|
||||
/* ((public static double pow(double a, double b)))
|
||||
*
|
||||
* - a berechnet den Wert von a^b ) und gibt das Ergebnis als double zurück.
|
||||
*
|
||||
* . Beispiele:
|
||||
* - Math.pow(2, 3) gibt 8.0
|
||||
* - Math.pow(5, 2) gibt 25.0
|
||||
* - Math.pow(9, 0.5) gibt 3.0
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,27 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class Methoden_Der_Wrapper_Klassen {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
/* Typkonvertierungsmethoden:
|
||||
*
|
||||
* 1- public byte byteValue()
|
||||
* 2- public short shortValue()
|
||||
* 3- public int intValue()
|
||||
* 4- public long longValue()
|
||||
* 5- public float floatValue()
|
||||
* 6- public double doubleValue()
|
||||
*/
|
||||
|
||||
// Beispiel:
|
||||
Integer a = 10;
|
||||
System.out.println(a.byteValue()); // gibt a als byte aus
|
||||
System.out.println(a.shortValue()); // gibt a als short aus
|
||||
System.out.println(a.intValue()); // gibt a als int aus
|
||||
System.out.println(a.longValue()); // gibt a als long aus
|
||||
System.out.println(a.floatValue());// gibt a als float aus
|
||||
System.out.println(a.doubleValue());// gibt a als double aus
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,21 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class Ternary {
|
||||
|
||||
//(condition) ? True:False
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Ternary:
|
||||
int x = (50 > 20) ? 12: 13; //12
|
||||
System.out.println(x);
|
||||
|
||||
// Nested Ternary:
|
||||
int x2 = (50 > 20) ? ((12 > 21) ? 14: 112) : 12;
|
||||
|
||||
System.out.println(x2);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package Collection;
|
||||
public class Collections {
|
||||
|
||||
/* Collections: eine Sammlung von Werten, die vom gleichen Datentyp sein können oder auch nicht.
|
||||
* 1. Array:
|
||||
* - ist änderbar, hat fixed Size und indexed
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
Binary file not shown.
|
@ -1,46 +0,0 @@
|
|||
package Collection;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.Vector;
|
||||
|
||||
public class Lists {
|
||||
/*
|
||||
* - Duplikate von Elementen ist zulassen
|
||||
* - die Methoden alle KlassenLists sind ähnlich zueinander
|
||||
* - Stack Prinzip : LIFO (Last In, First Out)
|
||||
* - indexded Lists
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Die vier Lists sind dynamischen Lists
|
||||
ArrayList<String> arrayList = new ArrayList<>();
|
||||
|
||||
LinkedList<String> linkedList = new LinkedList<>();
|
||||
|
||||
List<String> vector = new Vector<>();
|
||||
// stack erbt von Vector
|
||||
Stack<String> stack = new Stack<>();
|
||||
|
||||
arrayList.add("arrayList");
|
||||
linkedList.add("LinkedList");
|
||||
vector.add("Vektor");
|
||||
stack.add("Stack");
|
||||
System.out.println(arrayList);
|
||||
System.out.println(linkedList);
|
||||
System.out.println(vector);
|
||||
System.out.println(stack);
|
||||
stack.push("pushed String");
|
||||
System.out.println(stack);
|
||||
stack.pop();
|
||||
System.out.println(stack);
|
||||
// gibt das oberste Element zurück
|
||||
stack.peek();
|
||||
System.out.println(stack);
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -1,71 +0,0 @@
|
|||
package Collection;
|
||||
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Deque;
|
||||
import java.util.ArrayDeque;
|
||||
|
||||
public class Queueslist {
|
||||
/*
|
||||
* - Queue Prinzip: FIFO (First In, First Out)
|
||||
* - dynamische nicht indexed lists
|
||||
*
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
|
||||
//füge ein Element am Ende hinzu
|
||||
queue.add("1");
|
||||
System.out.println(queue);
|
||||
// füge ein Element am Ende hinzu
|
||||
queue.add("2");
|
||||
System.out.println(queue);
|
||||
|
||||
// füge ein Element am Ende hinzu, gibt false zurück, wenn die Queue full ist
|
||||
queue.offer("3");
|
||||
System.out.println(queue);
|
||||
|
||||
|
||||
|
||||
//Entfernt das erste Element in der Queue und gibt es zurück.
|
||||
queue.remove();
|
||||
//Entfernt das erste Element in der Queue und gibt es zurück. Gibt null zurück, wenn die Queue leer ist,
|
||||
queue.poll();
|
||||
|
||||
|
||||
/* Spezielle Queue-Typen:
|
||||
*
|
||||
* 1. PriorityQueue: ordnet ihre Elemente in einer natürlichen Ordnung oder nach einem angegebenen Comparator.
|
||||
*/
|
||||
|
||||
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
|
||||
priorityQueue.add(10);
|
||||
priorityQueue.add(20);
|
||||
priorityQueue.add(7);
|
||||
|
||||
System.out.println(priorityQueue.poll()); // Ausgabe: 7 (niedrigster Wert zuerst)
|
||||
System.out.println(priorityQueue.poll()); // Ausgabe: 10 zweite niedrigster Wert
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Spezielle Queue-Typen:
|
||||
*
|
||||
* 1. Deque (Double-Ended Queue): erlaubt das Hinzufügen und Entfernen von Elementen an beiden Enden der Queue.
|
||||
*/
|
||||
|
||||
Deque<String> deque = new ArrayDeque<>();
|
||||
|
||||
deque.addFirst("First");
|
||||
deque.addLast("Last");
|
||||
deque.addFirst("New First");
|
||||
|
||||
System.out.println(deque.removeFirst()); // New First
|
||||
System.out.println(deque.removeLast()); // Last
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 25 KiB |
Binary file not shown.
|
@ -1,60 +0,0 @@
|
|||
package Collection;
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Setlists {
|
||||
/* - nicht indexed lists
|
||||
* - erlaubt keine doppelten Elemente
|
||||
* - HashSet, LinkedHashSet und TreeSet
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
/*
|
||||
* 1. HashSet:
|
||||
* - Verwendet eine HashMap intern, um die Elemente zu speichern
|
||||
* - Bietet konstante Zeit für die grundlegenden Operationen (add, remove, contains), solange die Hash-Funktion effizient ist.
|
||||
* - Die Reihenfolge der Elemente ist nicht garantiert.
|
||||
*/
|
||||
Set<String> hashSet = new HashSet<>();
|
||||
hashSet.add("Apple");
|
||||
hashSet.add("Banana");
|
||||
hashSet.add("Apple"); // wird ignoriert, da "Apple" bereits existiert
|
||||
|
||||
System.out.println(hashSet); // Ausgabe: Apple,Banana
|
||||
|
||||
|
||||
/*
|
||||
* 2. LinkedHashSet:
|
||||
* - Verwendet eine verknüpfte Liste (LinkedList) zusammen mit einer HashMap, um die Elemente zu speichern.
|
||||
*/
|
||||
|
||||
Set<String> linkedHashSet = new LinkedHashSet<>();
|
||||
linkedHashSet.add("Apple");
|
||||
linkedHashSet.add("Banana");
|
||||
linkedHashSet.add("Apple");
|
||||
System.out.println(linkedHashSet);// wird ignoriert, da "Apple" bereits existiert
|
||||
|
||||
|
||||
/*
|
||||
* 3. TreeSet:
|
||||
* - Verwendet eine selbstbalancierende Baumstruktur (Red-Black Tree), um die Elemente zu speichern.
|
||||
* - Elemente werden in natürlicher Ordnung (oder durch einen Comparator, falls angegeben) sortiert gespeichert.
|
||||
*/
|
||||
|
||||
Set<String> treeSet = new TreeSet<>();
|
||||
treeSet.add("Banana");
|
||||
treeSet.add("Apple");
|
||||
treeSet.add("Apple");
|
||||
|
||||
// die zweite Apple wird ignorieret und die Reihnfolge wird automatisch sortiert
|
||||
System.out.println(treeSet);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,44 @@
|
|||
package oop;
|
||||
|
||||
public class Comparable_Interface {
|
||||
/*
|
||||
* . Die Comparable_Interface hat die Methode: (public int compareTo(T o))
|
||||
* - a.compareTo(b): Vergleicht zwei Objekte und gibt:
|
||||
* 1. 0 zurück, wenn sie gleich sind.
|
||||
* 2.-1, wenn a kleiner als b ist.
|
||||
* 3. 1, wenn a größer als b ist.
|
||||
*
|
||||
* - compareTo() wird häufig bei der Sortierung von Objekten verwendet, da es die Reihenfolge bestimmt.
|
||||
*
|
||||
* . Wichtig: Das Objekt, das die Methode aufruft, und das Objekt,
|
||||
* das an sie übergeben wird, müssen vom gleichen Typ sein.
|
||||
*
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Integer a = 128;
|
||||
Integer d = 128;
|
||||
|
||||
Integer b = 220;
|
||||
Integer c = 5;
|
||||
|
||||
// Hier wird den 'Inhalt' der Objekte überprüft!!
|
||||
System.out.println(a.equals(d)); // true da a == b
|
||||
System.out.println(a.equals(b)); // false da a < b
|
||||
|
||||
System.out.println( a.compareTo(b) ); //-1 da a < b
|
||||
System.out.println( a.compareTo(c) ); // 1 da a > b
|
||||
System.out.println( a.compareTo(d) ); // 0 da a == b
|
||||
|
||||
|
||||
// Compiler Fehler: ****************************************************
|
||||
|
||||
Double zahl = 12.4;
|
||||
Integer zahl2 = 12;
|
||||
|
||||
// System.out.println(zahl.compareTo(zahl2)); // Fehler , zahl1 und zahl2 sollen om gleichen Typ sein
|
||||
|
||||
// System.out.println(zahl.equals(zahl2)); //Unlikely argument type for equals(): Integer seems to be unrelated to Double
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
@ -0,0 +1,41 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
public class Controller {
|
||||
private User user;
|
||||
private Konto konto;
|
||||
private ViewUser viewuser;
|
||||
|
||||
Controller(){
|
||||
}
|
||||
|
||||
public void login_user() {
|
||||
this.viewuser = new ViewUser();
|
||||
viewuser.user_Login();
|
||||
String name = viewuser.getName();
|
||||
String pass = viewuser.getPass();
|
||||
user = new User (name,pass);
|
||||
konto = new Konto(user);
|
||||
user.addKonto(konto);
|
||||
viewuser.show_User_Daten(user.getName(), user.getPassword(),konto.getKontostand() ,konto.getKontoNummer());
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
user.setName(name);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return user.getName();
|
||||
}
|
||||
|
||||
public void setPass(String pass) {
|
||||
user.setPassword(pass);
|
||||
}
|
||||
|
||||
public String getPass() {
|
||||
return user.getPassword();
|
||||
}
|
||||
|
||||
public void updateUser() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
//class JTest {
|
||||
// private Konto obaiKonto1;
|
||||
// private Konto obaiKonto2;
|
||||
// private User obai;
|
||||
//
|
||||
// @BeforeEach
|
||||
// void erstelleObjekt() {
|
||||
// obai = new User("Obai" , "123");
|
||||
// obaiKonto1 = new Konto(1234,0,obai);
|
||||
// obaiKonto2 = new Konto (1235,0 ,obai);
|
||||
// obai.addKonto(obaiKonto1);
|
||||
// obai.addKonto(obaiKonto2);
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Disabled
|
||||
// void testEinzahlen() throws Exception {
|
||||
// obai.einzahlen(123, obaiKonto1);
|
||||
// obai.einzahlen(124, obaiKonto2);
|
||||
// assertEquals(123,obaiKonto1.getKontostand());
|
||||
// assertEquals(124,obaiKonto2.getKontostand());
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testauszahlen() throws Exception {
|
||||
// obai.einzahlen(123, obaiKonto1);
|
||||
// obai.auszahlen(123, obaiKonto1);
|
||||
// assertEquals(0,obaiKonto1.getKontostand());
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testueberweisen() throws Exception {
|
||||
// obai.einzahlen(123, obaiKonto1);
|
||||
// obai.ueberweisen(100, obaiKonto1, obaiKonto2);
|
||||
//
|
||||
// assertEquals(23,obaiKonto1.getKontostand());
|
||||
// assertEquals(100,obaiKonto2.getKontostand());
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
|
@ -0,0 +1,41 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
public class Konto {
|
||||
|
||||
private int kontoNummer;
|
||||
private static int KONTO_NUMMER = 1000;
|
||||
private int kontostand;
|
||||
private User inhaber;
|
||||
|
||||
public Konto(User inhaber) {
|
||||
this.kontoNummer =KONTO_NUMMER++ ;
|
||||
this.kontostand = 0;
|
||||
this.inhaber = inhaber;
|
||||
}
|
||||
|
||||
public int getKontoNummer() {
|
||||
return kontoNummer;
|
||||
}
|
||||
|
||||
public void setKontoNummer(int kontoNummer) {
|
||||
this.kontoNummer = kontoNummer;
|
||||
}
|
||||
|
||||
public int getKontostand() {
|
||||
return kontostand;
|
||||
}
|
||||
|
||||
public void setKontostand(int kontostand) {
|
||||
this.kontostand = kontostand;
|
||||
}
|
||||
|
||||
public User getInhaber() {
|
||||
return inhaber;
|
||||
}
|
||||
|
||||
public void setInhaber(User inhaber) {
|
||||
this.inhaber = inhaber;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
public class Main {
|
||||
|
||||
Controller c;
|
||||
public static void main(String[] args) {
|
||||
|
||||
new Main().testDesighn();
|
||||
|
||||
}
|
||||
|
||||
public Main() {
|
||||
c = new Controller();
|
||||
}
|
||||
|
||||
public void testDesighn() {
|
||||
c.login_user();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private String password;
|
||||
private int id;
|
||||
private static int ID = 1000;
|
||||
private ArrayList<Konto> konten;
|
||||
|
||||
public User(String name, String password) {
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.id = ID++;
|
||||
konten = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addKonto(Konto neuesKonto) {
|
||||
konten.add(neuesKonto);
|
||||
}
|
||||
|
||||
public void einzahlen(int betrag, Konto konto) throws Exception {
|
||||
|
||||
if (betrag > 0)
|
||||
konto.setKontostand(konto.getKontostand() + betrag);
|
||||
else
|
||||
throw new Exception("Der Betrag muss größer 0€ sein!");
|
||||
}
|
||||
|
||||
public void auszahlen(int betrag, Konto konto) throws Exception {
|
||||
if (betrag > konto.getKontostand())
|
||||
throw new Exception("Sie haben kein genug Geld!");
|
||||
|
||||
konto.setKontostand(konto.getKontostand() - betrag);
|
||||
|
||||
}
|
||||
|
||||
public void ueberweisen(int betrag, Konto sender, Konto empfaenger) throws Exception {
|
||||
if (empfaenger == null)
|
||||
throw new Exception("Dieser Konto existiert nicht");
|
||||
|
||||
if (betrag > sender.getKontostand())
|
||||
throw new Exception("Sie haben kein genug Geld!");
|
||||
|
||||
empfaenger.setKontostand(empfaenger.getKontostand() + betrag);
|
||||
sender.setKontostand(sender.getKontostand() - betrag);
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public ArrayList<Konto> getKonten() {
|
||||
return konten;
|
||||
}
|
||||
|
||||
public void setKonten(ArrayList<Konto> konten) {
|
||||
this.konten = konten;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package Übungen.MyBankSystem;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ViewUser {
|
||||
Scanner scan;
|
||||
private String name;
|
||||
private String pass;
|
||||
|
||||
|
||||
public void show_User_Daten(String name, String password, int kontostand, int kontoNummer ){
|
||||
System.out.println("Ihre Daten sind: ");
|
||||
System.out.println("[ Name: " + name + " Password: " + password + " Kontostand: " + kontostand + " Kontonummer: " + kontoNummer);
|
||||
|
||||
}
|
||||
|
||||
public void update_User_Name(String name) {
|
||||
System.out.println("Ihre Name: " + name);
|
||||
}
|
||||
|
||||
public void user_Login() {
|
||||
scan = new Scanner(System.in);
|
||||
System.out.println("Willkommen loggen Sie sich bitte ein: ");
|
||||
System.out.println("Name: ");
|
||||
this.name = scan.nextLine();
|
||||
System.out.println("Password: ");
|
||||
pass = scan.nextLine();
|
||||
|
||||
scan.close();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public String getPass() {
|
||||
return pass;
|
||||
}
|
||||
}
|
|
@ -13,16 +13,16 @@ public class Ball extends Rectangle {
|
|||
|
||||
int yBewegung;
|
||||
int xBewegung;
|
||||
int speed = 5;
|
||||
int speed = 4;
|
||||
Random random = new Random();
|
||||
|
||||
Ball(int x, int y, int width, int height){
|
||||
super(x,y,width,height);
|
||||
xBewegung(speed);
|
||||
yBewegung(2);
|
||||
}
|
||||
public void setBewegung(int bewegung) {
|
||||
yBewegung = bewegung;
|
||||
xBewegung = random.nextInt(10) -5;
|
||||
if (xBewegung == 0)
|
||||
xBewegung++;
|
||||
xBewegung(xBewegung * speed);
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
@ -39,7 +39,7 @@ public class Ball extends Rectangle {
|
|||
}
|
||||
|
||||
public void move() {
|
||||
x -= xBewegung;
|
||||
x += xBewegung;
|
||||
y += yBewegung;
|
||||
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import javax.swing.*;
|
|||
|
||||
public class GamePanle extends JPanel implements Runnable {
|
||||
|
||||
final static int WIDTH_WINDOW = 700;
|
||||
final static int WIDTH_WINDOW = 1000;
|
||||
final static int HEIGH_WINDOW = 700;
|
||||
Dimension fensterSize = new Dimension(WIDTH_WINDOW,HEIGH_WINDOW);
|
||||
|
||||
|
@ -21,18 +21,17 @@ public class GamePanle extends JPanel implements Runnable {
|
|||
|
||||
Schläger spieler1, spieler2;
|
||||
Ball ball;
|
||||
Graphics graphics;
|
||||
Random random;
|
||||
Thread game;
|
||||
int scoreSpieler1 = 0;
|
||||
int scoreSpieler2 = 0;
|
||||
|
||||
int speed = 4;
|
||||
int speed = 2;
|
||||
GamePanle(){
|
||||
zeichneRects();
|
||||
this.setFocusable(true);
|
||||
this.setPreferredSize(fensterSize);
|
||||
this.addKeyListener(new AL());
|
||||
random = new Random();
|
||||
game = new Thread(this);
|
||||
game.start();
|
||||
|
||||
|
@ -41,7 +40,7 @@ public class GamePanle extends JPanel implements Runnable {
|
|||
public void zeichneRects() {
|
||||
spieler1 = new Schläger(0,(HEIGH_WINDOW/2) - (HEIGHT_RECT/2),WIDTH_RECT,HEIGHT_RECT,1);
|
||||
spieler2 = new Schläger(WIDTH_WINDOW-WIDTH_RECT,(HEIGH_WINDOW/2) - (HEIGHT_RECT/2),WIDTH_RECT,HEIGHT_RECT,2);
|
||||
ball = new Ball(WIDTH_WINDOW/2,HEIGH_WINDOW/2,WIDTH_BALL,HEIGHT_BALL);
|
||||
ball = new Ball(WIDTH_WINDOW/2,100,WIDTH_BALL,HEIGHT_BALL);
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
|
@ -57,24 +56,33 @@ public class GamePanle extends JPanel implements Runnable {
|
|||
spieler2.draw(g);
|
||||
ball.draw(g);
|
||||
|
||||
g.setFont(new Font("Arial", Font.BOLD, 30)); // Schriftart Arial, fett, Größe 24
|
||||
g.setFont(new Font("Arial", Font.BOLD, 30));
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString(scoreSpieler1+"", WIDTH_WINDOW/2 - 50, 35);
|
||||
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawString(scoreSpieler2 +"", WIDTH_WINDOW/2 + 50, 35);
|
||||
|
||||
g.setFont(new Font("Arial", Font.BOLD, 20));
|
||||
g.setColor(Color.YELLOW);
|
||||
g.drawString("Obai", WIDTH_WINDOW/2 - 120, 35);
|
||||
|
||||
}
|
||||
g.setFont(new Font("Arial", Font.BOLD, 20));
|
||||
g.setColor(Color.YELLOW);
|
||||
g.drawString("ComputerGegener", WIDTH_WINDOW/2 + 100, 35);
|
||||
|
||||
}
|
||||
|
||||
public void mov() {
|
||||
|
||||
spieler1.move();
|
||||
spieler2.move();
|
||||
spieler2.movegegener();
|
||||
ball.move();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void checkRänder() {
|
||||
if (spieler1.y <= 0)
|
||||
spieler1.y = 0;
|
||||
|
@ -82,19 +90,36 @@ public class GamePanle extends JPanel implements Runnable {
|
|||
if (spieler1.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||
spieler1.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||
|
||||
if (spieler2.y <= 0)
|
||||
spieler2.y = 0;
|
||||
|
||||
if (spieler2.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||
spieler2.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||
if ( ball.x > WIDTH_WINDOW/2 && ball.y < spieler2.y)
|
||||
spieler2.setBewegungGegener(-10);
|
||||
|
||||
else if (ball.x > WIDTH_WINDOW/2 && ball.y > spieler2.y)
|
||||
spieler2.setBewegungGegener(10);
|
||||
|
||||
else
|
||||
spieler2.setBewegungGegener(0);
|
||||
|
||||
|
||||
if (spieler2.y <= 0)
|
||||
spieler2.y = 0;
|
||||
|
||||
if (spieler2.y >= HEIGH_WINDOW - HEIGHT_RECT)
|
||||
spieler2.y = HEIGH_WINDOW - HEIGHT_RECT;
|
||||
|
||||
|
||||
if (ball.intersects(spieler1)) {
|
||||
ball.xBewegung = -ball.xBewegung;
|
||||
int bewegexRandom = random.nextInt(8) + 4;
|
||||
ball.xBewegung(bewegexRandom);
|
||||
int bewegeyRandom = random.nextInt(5) - 2;
|
||||
ball.yBewegung(bewegeyRandom);
|
||||
}
|
||||
if (ball.intersects(spieler2)) {
|
||||
|
||||
ball.xBewegung = -ball.xBewegung;
|
||||
int bewegexRandom = random.nextInt(8) + 4;
|
||||
ball.xBewegung(-bewegexRandom);
|
||||
int bewegeyRandom = random.nextInt(5) - 2;
|
||||
ball.yBewegung(bewegeyRandom);
|
||||
|
||||
}
|
||||
if (ball.x <= 0) {
|
||||
|
@ -125,7 +150,7 @@ public class GamePanle extends JPanel implements Runnable {
|
|||
mov();
|
||||
checkRänder();
|
||||
repaint();
|
||||
Thread.sleep(10);
|
||||
Thread.sleep(5);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class GamePlay {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameWindow game = new GameWindow();
|
||||
GameWindow game = new GameWindow();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ public class GameWindow extends JFrame{
|
|||
this.add(game);
|
||||
this.setTitle("Pong Game");
|
||||
this.setResizable(false);
|
||||
this.setBackground(Color.BLACK);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.pack();
|
||||
this.setLocationRelativeTo(null);
|
||||
|
|
|
@ -9,6 +9,8 @@ public class Schläger extends Rectangle {
|
|||
|
||||
int id;
|
||||
int yBewegung;
|
||||
|
||||
int gegnerBewegungy;
|
||||
int speed = 10;
|
||||
|
||||
Schläger(int x, int y, int width, int height, int id){
|
||||
|
@ -20,6 +22,11 @@ public class Schläger extends Rectangle {
|
|||
y += yBewegung;
|
||||
}
|
||||
|
||||
public void movegegener() {
|
||||
y += gegnerBewegungy;
|
||||
}
|
||||
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (id == 1) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_W)
|
||||
|
@ -27,29 +34,24 @@ public class Schläger extends Rectangle {
|
|||
|
||||
if (e.getKeyCode() == KeyEvent.VK_S)
|
||||
setBewegung(speed);
|
||||
} else {
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP)
|
||||
setBewegung(-speed);
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setBewegung(speed);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
if (id == 1) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S)
|
||||
setBewegung(0);
|
||||
}else
|
||||
if (e.getKeyCode() == KeyEvent.VK_UP ||e.getKeyCode() == KeyEvent.VK_DOWN)
|
||||
setBewegung(0);
|
||||
}
|
||||
}
|
||||
|
||||
public void setBewegung(int bewegung) {
|
||||
yBewegung = bewegung;
|
||||
}
|
||||
|
||||
public void setBewegungGegener(int bewegung) {
|
||||
gegnerBewegungy = bewegung;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
if (id == 1)
|
||||
g.setColor(Color.RED);
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package Übungen.Snake;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Essen extends Rectangle{
|
||||
|
||||
|
||||
Essen(int x,int y,int width,int height){
|
||||
super(x,y,width,height);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
g.setColor(Color.GREEN);
|
||||
g.fillOval(x, y, width, height);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
package Übungen.Snake;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class GameContainer extends JPanel implements Runnable {
|
||||
|
||||
final static int WIDTH_WINDOW = 1000;
|
||||
final static int HEIGHT_WINDOW = 700;
|
||||
Dimension fensterSize = new Dimension(WIDTH_WINDOW, HEIGHT_WINDOW);
|
||||
|
||||
Snake snake;
|
||||
ArrayList<Snake> speicher; // Liste für Schlangenteile
|
||||
static final int snake_Width = 20;
|
||||
static final int snake_Height = 20;
|
||||
|
||||
Essen essen;
|
||||
static final int essen_Width = 10;
|
||||
static final int essen_Height = 10;
|
||||
|
||||
Thread game;
|
||||
Random random;
|
||||
|
||||
GameContainer() {
|
||||
random = new Random();
|
||||
speicher = new ArrayList<>();
|
||||
drawSnake();
|
||||
drawEssen();
|
||||
this.setFocusable(true);
|
||||
this.setPreferredSize(fensterSize);
|
||||
this.addKeyListener(new AL()); // KeyListener für die Steuerung
|
||||
game = new Thread(this);
|
||||
game.start();
|
||||
}
|
||||
|
||||
public void checkKollision() {
|
||||
if (speicher.get(0).intersects(essen)) {
|
||||
drawEssen();
|
||||
growSnake();
|
||||
}
|
||||
|
||||
if (snake.y <= 0 || snake.x <= 0)
|
||||
game.interrupt();
|
||||
//
|
||||
// for (int i = 1 ; i < speicher.size() ; i--) {
|
||||
// if (speicher.get(0).intersects(speicher.get(i))) {
|
||||
// System.out.println("Game Over");
|
||||
// game.interrupt();
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
super.paint(g);
|
||||
g.setColor(Color.black);
|
||||
g.fillRect(0, 0, WIDTH_WINDOW, HEIGHT_WINDOW);
|
||||
|
||||
for (Snake snake : speicher)
|
||||
snake.draw(g);
|
||||
|
||||
essen.draw(g);
|
||||
}
|
||||
|
||||
public void drawSnake() {
|
||||
int startX = random.nextInt(WIDTH_WINDOW - 200);
|
||||
int startY = random.nextInt(HEIGHT_WINDOW - 200);
|
||||
snake = new Snake(startX, startY, snake_Width, snake_Height);
|
||||
speicher.add(snake);
|
||||
}
|
||||
|
||||
// Neues Essen erstellen
|
||||
public void drawEssen() {
|
||||
int essenX = random.nextInt(WIDTH_WINDOW - 200);
|
||||
int essenY = random.nextInt(HEIGHT_WINDOW - 200);
|
||||
essen = new Essen(essenX, essenY, essen_Width, essen_Height);
|
||||
}
|
||||
|
||||
public void growSnake() {
|
||||
Snake lastSegment = speicher.get(speicher.size() - 1);
|
||||
Snake newSegment = new Snake(lastSegment.x, lastSegment.y, snake_Width, snake_Height);
|
||||
speicher.add(newSegment);
|
||||
}
|
||||
|
||||
// Bewegung der Schlange
|
||||
public void move() {
|
||||
for (int i = speicher.size() - 1; i > 0; i--) {
|
||||
speicher.get(i).x = speicher.get(i - 1).x;
|
||||
speicher.get(i).y = speicher.get(i - 1).y;
|
||||
|
||||
}
|
||||
snake.moveSnake();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
checkKollision();
|
||||
repaint();
|
||||
Thread.sleep(20);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public class AL extends KeyAdapter {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
snake.keyPressed(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package Übungen.Snake;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public class GameFenster extends JFrame {
|
||||
|
||||
GameContainer gameplay;
|
||||
GameFenster(){
|
||||
gameplay = new GameContainer();
|
||||
this.add(gameplay);
|
||||
this.setTitle("SnakeGame");
|
||||
this.setResizable(false);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.pack();
|
||||
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setVisible(true);
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,196 @@
|
|||
package Übungen.Snake;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class GamePanel extends JPanel implements ActionListener {
|
||||
static final int SCREEN_WIDTH = 1300;
|
||||
static final int SCREEN_HEIGHT = 750;
|
||||
static final int UNIT_SIZE = 50;
|
||||
static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
|
||||
static final int DELAY = 175;
|
||||
final int x[] = new int[GAME_UNITS];
|
||||
final int y[] = new int[GAME_UNITS];
|
||||
int bodyParts = 6;
|
||||
int applesEaten;
|
||||
int appleX;
|
||||
int appleY;
|
||||
char direction = 'R';
|
||||
boolean running = false;
|
||||
Timer timer;
|
||||
Random random;
|
||||
|
||||
GamePanel() {
|
||||
random = new Random();
|
||||
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
|
||||
this.setBackground(Color.black);
|
||||
this.setFocusable(true);
|
||||
this.addKeyListener(new MyKeyAdapter());
|
||||
startGame();
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
newApple();
|
||||
running = true;
|
||||
timer = new Timer(DELAY, this);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
draw(g);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
if (running) {
|
||||
/*
|
||||
* for(int i=0;i<SCREEN_HEIGHT/UNIT_SIZE;i++) { g.drawLine(i*UNIT_SIZE, 0,
|
||||
* i*UNIT_SIZE, SCREEN_HEIGHT); g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH,
|
||||
* i*UNIT_SIZE); }
|
||||
*/
|
||||
g.setColor(Color.red);
|
||||
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
|
||||
|
||||
for (int i = 0; i < bodyParts; i++) {
|
||||
if (i == 0) {
|
||||
g.setColor(Color.green);
|
||||
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
|
||||
} else {
|
||||
g.setColor(new Color(45, 180, 0));
|
||||
// g.setColor(new
|
||||
// Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
|
||||
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
|
||||
}
|
||||
}
|
||||
g.setColor(Color.red);
|
||||
g.setFont(new Font("Ink Free", Font.BOLD, 40));
|
||||
FontMetrics metrics = getFontMetrics(g.getFont());
|
||||
g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: " + applesEaten)) / 2,
|
||||
g.getFont().getSize());
|
||||
} else {
|
||||
gameOver(g);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void newApple() {
|
||||
appleX = random.nextInt((int) (SCREEN_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
|
||||
appleY = random.nextInt((int) (SCREEN_HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
|
||||
}
|
||||
|
||||
public void move() {
|
||||
for (int i = bodyParts; i > 0; i--) {
|
||||
x[i] = x[i - 1];
|
||||
y[i] = y[i - 1];
|
||||
}
|
||||
|
||||
switch (direction) {
|
||||
case 'U':
|
||||
y[0] = y[0] - UNIT_SIZE;
|
||||
break;
|
||||
case 'D':
|
||||
y[0] = y[0] + UNIT_SIZE;
|
||||
break;
|
||||
case 'L':
|
||||
x[0] = x[0] - UNIT_SIZE;
|
||||
break;
|
||||
case 'R':
|
||||
x[0] = x[0] + UNIT_SIZE;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void checkApple() {
|
||||
if ((x[0] == appleX) && (y[0] == appleY)) {
|
||||
bodyParts++;
|
||||
applesEaten++;
|
||||
newApple();
|
||||
}
|
||||
}
|
||||
|
||||
public void checkCollisions() {
|
||||
// checks if head collides with body
|
||||
for (int i = bodyParts; i > 0; i--) {
|
||||
if ((x[0] == x[i]) && (y[0] == y[i])) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
// check if head touches left border
|
||||
if (x[0] < 0) {
|
||||
running = false;
|
||||
}
|
||||
// check if head touches right border
|
||||
if (x[0] > SCREEN_WIDTH) {
|
||||
running = false;
|
||||
}
|
||||
// check if head touches top border
|
||||
if (y[0] < 0) {
|
||||
running = false;
|
||||
}
|
||||
// check if head touches bottom border
|
||||
if (y[0] > SCREEN_HEIGHT) {
|
||||
running = false;
|
||||
}
|
||||
|
||||
if (!running) {
|
||||
timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void gameOver(Graphics g) {
|
||||
// Score
|
||||
g.setColor(Color.red);
|
||||
g.setFont(new Font("Ink Free", Font.BOLD, 40));
|
||||
FontMetrics metrics1 = getFontMetrics(g.getFont());
|
||||
g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Score: " + applesEaten)) / 2,
|
||||
g.getFont().getSize());
|
||||
// Game Over text
|
||||
g.setColor(Color.red);
|
||||
g.setFont(new Font("Ink Free", Font.BOLD, 75));
|
||||
FontMetrics metrics2 = getFontMetrics(g.getFont());
|
||||
g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over")) / 2, SCREEN_HEIGHT / 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
if (running) {
|
||||
move();
|
||||
checkApple();
|
||||
checkCollisions();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
|
||||
public class MyKeyAdapter extends KeyAdapter {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_LEFT:
|
||||
if (direction != 'R') {
|
||||
direction = 'L';
|
||||
}
|
||||
break;
|
||||
case KeyEvent.VK_RIGHT:
|
||||
if (direction != 'L') {
|
||||
direction = 'R';
|
||||
}
|
||||
break;
|
||||
case KeyEvent.VK_UP:
|
||||
if (direction != 'D') {
|
||||
direction = 'U';
|
||||
}
|
||||
break;
|
||||
case KeyEvent.VK_DOWN:
|
||||
if (direction != 'U') {
|
||||
direction = 'D';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package Übungen.Snake;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class Snake extends Rectangle {
|
||||
|
||||
int ybewegung;
|
||||
int xbewegung;
|
||||
int speed = 10;
|
||||
Snake(int x, int y, int width,int height){
|
||||
|
||||
super(x,y,width,height);
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(x,y,width,height);
|
||||
}
|
||||
|
||||
public void moveSnake() {
|
||||
y += ybewegung;
|
||||
x += xbewegung;
|
||||
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_W) {
|
||||
|
||||
setY(-speed);
|
||||
setX(0);
|
||||
}
|
||||
|
||||
|
||||
else if (e.getKeyCode() == KeyEvent.VK_S) {
|
||||
setY(speed);
|
||||
setX(0);
|
||||
}
|
||||
|
||||
|
||||
else if (e.getKeyCode() == KeyEvent.VK_D) {
|
||||
setX(speed);
|
||||
setY(0);
|
||||
}
|
||||
|
||||
|
||||
else if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||
setX(-speed);
|
||||
setY(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void setX(int xbe) {
|
||||
xbewegung = xbe;
|
||||
}
|
||||
|
||||
public void setY(int ybe) {
|
||||
ybewegung = ybe;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package Übungen.Snake;
|
||||
|
||||
public class Test {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameFenster game = new GameFenster();
|
||||
}
|
||||
|
||||
}
|
|
@ -48,10 +48,10 @@ public class Controller implements ActionListener {
|
|||
checkeOperator();
|
||||
textverteilen();
|
||||
textToNumber();
|
||||
String err = "";
|
||||
double ergebnis = 0;
|
||||
|
||||
switch (merker) {
|
||||
String res = "";
|
||||
Number ergebnis = 0;
|
||||
System.out.println(ergebnis.toString());
|
||||
switch (merker) {
|
||||
case '+':
|
||||
ergebnis = m1.add(zahl1, zahl2).doubleValue();
|
||||
break;
|
||||
|
@ -65,18 +65,21 @@ public class Controller implements ActionListener {
|
|||
break;
|
||||
|
||||
case '÷':
|
||||
ergebnis = m1.divid(zahl1, zahl2).doubleValue();
|
||||
break;
|
||||
|
||||
default:
|
||||
err = "Bitte geben Sie einen gültigen Operator ein.";
|
||||
try {
|
||||
ergebnis = m1.divid(zahl1, zahl2).doubleValue();
|
||||
} catch (ArithmeticException ex) {
|
||||
v1.showError("Division durch 0 ist nicht erlaubt!");
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//
|
||||
|
||||
if (!err.isEmpty())
|
||||
System.out.println(err);
|
||||
// v1.showError(err);
|
||||
else
|
||||
v1.updateErgebnisse(ergebnis);
|
||||
if (ergebnis instanceof Double || ergebnis instanceof Float) {
|
||||
res = foramtedNumber(ergebnis);
|
||||
v1.updateErgebnisse(res);
|
||||
}
|
||||
v1.updateErgebnisse(ergebnis.intValue() +"");
|
||||
}
|
||||
|
||||
if (e.getSource() == v1.clear) {
|
||||
|
@ -101,12 +104,23 @@ public class Controller implements ActionListener {
|
|||
v1.setAction(actionListener);
|
||||
}
|
||||
|
||||
private String foramtedNumber(Number n) {
|
||||
if (n.doubleValue() == n.intValue()) {
|
||||
return String.format("%d", n.intValue()); // Für Ganzzahlen
|
||||
} else {
|
||||
return String.format("%.4f", n.doubleValue()); // Für Dezimalzahlen mit 4 Nachkommastellen
|
||||
}
|
||||
}
|
||||
|
||||
private void checkeOperator() {
|
||||
merker = ' ';
|
||||
for (int i = 0; i < letzterText.length(); i++)
|
||||
for (int j = 0; j < operationen.length; j++)
|
||||
if (letzterText.charAt(i) == operationen[j])
|
||||
if (letzterText.charAt(i) == operationen[j]) {
|
||||
merker = operationen[j];
|
||||
System.out.println("Operator gefunden: " + merker); // Debug-Ausgabe
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void textverteilen() {
|
||||
|
@ -118,13 +132,12 @@ public class Controller implements ActionListener {
|
|||
}
|
||||
|
||||
public void textToNumber() {
|
||||
if (erst.contains(".") || last.contains(".")) {
|
||||
zahl1 = Double.parseDouble(erst);
|
||||
zahl2 = Double.parseDouble(last);
|
||||
} else {
|
||||
zahl1 = Integer.parseInt(erst);
|
||||
zahl2 = Integer.parseInt(last);
|
||||
}
|
||||
try {
|
||||
zahl1 = Double.parseDouble(erst.trim());
|
||||
zahl2 = Double.parseDouble(last.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
v1.showError("Ungültige Eingabe");
|
||||
}
|
||||
}
|
||||
|
||||
public void clearTextfield() {
|
||||
|
@ -148,5 +161,7 @@ public class Controller implements ActionListener {
|
|||
Modell<Number, Number> m = new Modell<>();
|
||||
View v = new View();
|
||||
Controller n1 = new Controller(m, v);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,14 +8,14 @@ import org.junit.jupiter.api.BeforeEach;
|
|||
import org.junit.jupiter.api.Disabled;
|
||||
|
||||
public class JunitTest {
|
||||
Modell<Double, Double> m1;
|
||||
// Modell<?, ?> m1;
|
||||
//
|
||||
// @BeforeEach
|
||||
// public void initi() {
|
||||
// m1 = new Modell<>();
|
||||
// }
|
||||
|
||||
@BeforeEach
|
||||
public void initi() {
|
||||
m1 = new Modell<>();
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
public void testAdd() {
|
||||
Modell<Integer, Integer> m1 = new Modell<>();
|
||||
assertEquals(15 ,m1.add(10, 5).intValue());
|
||||
|
@ -28,7 +28,7 @@ public class JunitTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testmulti() {
|
||||
Modell<Double, Double> m1 = new Modell<>();
|
||||
assertEquals(1.44 ,m1.multi(1.2, 1.2).doubleValue());
|
||||
|
@ -38,8 +38,9 @@ public class JunitTest {
|
|||
@Disabled
|
||||
public void testdivid() {
|
||||
Modell<Integer, Integer> m1 = new Modell<>();
|
||||
assertEquals(5 ,m1.divid(5, 0).intValue());
|
||||
assertEquals(1 ,m1.divid(5, 5).intValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -55,4 +55,6 @@ public class Modell <T extends Number,T2 extends Number> {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class View extends JFrame {
|
|||
this.setVisible(true);
|
||||
this.setLayout(null);
|
||||
|
||||
ergebnisse.setBounds(40, 60, 100, 30);
|
||||
ergebnisse.setBounds(40, 60, 300, 40);
|
||||
error.setBounds(40, 60, 100, 30);
|
||||
eingabe.setBounds(40, 100, buttonWidth * 6 + 20, 40);
|
||||
button1.setBounds(40,150,buttonWidth,buttonheight);
|
||||
|
@ -115,12 +115,12 @@ public class View extends JFrame {
|
|||
|
||||
}
|
||||
|
||||
public void updateErgebnisse(double erg) {
|
||||
ergebnisse.setText(erg + "");
|
||||
public void updateErgebnisse(String erg) {
|
||||
ergebnisse.setText(erg);
|
||||
}
|
||||
|
||||
public void showError(String err) {
|
||||
error.setText(err);
|
||||
ergebnisse.setText(err);
|
||||
}
|
||||
|
||||
public void setEingabe(String auswahl) {
|
||||
|
|
Loading…
Reference in New Issue