Aufgaben¶
Aufgabe 1 (Würfelspiel)¶
Aufgabe 1
Vorbereitung (siehe Einstieg)
- Informieren Sie sich über die Klasse
JOptionPane
aus dem Paketjavax.swing
(z.B. hier oder hier oder hier)
Sollten Sie mit dem Java-Modulsystem arbeiten, d.h. sollten Sie in Ihrem Java-Projekt eine Dateimodule-info.java
haben, dann müssen Sie in diese Datei (in den Anweisungsblock) die Anweisungrequires java.desktop;
einfügen - das ist das Modul, in dem sich das Paketjavax.swing
befindet. - Erstellen Sie insbesondere folgenden Dialog (in den Buttons kann auch
Yes
undNo
stehen) und prüfen Sie, ob derNein
- oder derJa
-Button gedrückt wurde (im Beispiel stehtA
für den Namen eines Spielers – siehe Aufgabe unten):
Aufgabe
- Implementieren Sie folgendes Würfelspiel:
- An dem Spiel können beliebig viele Spieler teilnehmen.
- Die Spieler sind nacheinander an der Reihe.
- Wenn ein Spieler an der Reihe ist, dann befindet er sich in einem Versuch.
- In einem Versuch kann der Spieler so lange würfeln, bis er entweder
- eine 6 würfelt oder er
- den Versuch freiwillig beendet.
- Hat der Spieler eine 6 gewürfelt, wird der gesamte Versuch mit
0
Punkten bewertet. - Hat der Spieler den Versuch freiwillig beendet, wird die in dem Versuch erzielte Summe aus sein Punktekonto addiert (gespeichert).
- Der Spieler, der zuerst eine bestimmte Punktzahl (z.B.
20
) erreicht hat, hat gewonnen.
Beispiel mit zwei SpielernA
undB
bis Gesamtpunktzahl20
: - Laden Sie Ihr Lösung in Moodle hoch! Viel Spaß und viel Erfolg!
eine mögliche Lösung für Aufgabe 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
Aufgabe 2 (MyInteger)¶
Aufgabe 2
Vorbereitung (Selbstudium)
- Eine statische Variable wird mit dem Schlüsselwort
static
deklariert, also z.B.static int myVariable = 0;
. Der Zugriff auf eine solche statische Variable erfolgt nicht über eine Referenzvariable, sondern über den Klassennamen. Angenommen, die VariablemyVariable
wurde in der KlasseMyClass
deklariert, dann erfolgt der Zugriff auf die Variable überMyClass.myVariable
. Für Objektvariablen gilt, dass jedes Objekt seine eigene(n) Objektvariable(n) hat. Statische Variablen gibt es in der Klasse genau einmal. Alle Objekte "teilen" sich eine statische Variable. Eine statische Variable heißt deshalb auch Klassenvariable. - Eine statische Methode wird ebenfalls mit dem Schlüsselwort
static
deklariert, also z.B.public static void myMethod() {}
. Der Zugriff auf eine solche statische Methode erfolgt nicht über eine Referenzvariable, sondern über den Klassennamen. Angenommen, die MethodemyMethod()
wurde in der KlasseMyClass
deklariert, dann erfolgt der Zugriff auf die Methode überMyClass.myMethod()
. Wir kennen solche Methoden bereits, z.B. sind alle Methoden aus der KlasseMath
statisch und wir können sie z.B. mitMath.sqrt(value)
oderMath.abs(value)
aufrufen. - siehe z.B. hier oder hier oder hier
Info: Die Klasse MyInteger
ist eine sogenannte Wrapper-Klasse. Die Idee ist, dass MyInteger
eine objektorientierte Hülle um den int
-Typ darstellt. Implementieren Sie die Klasse MyInteger
. Diese Klasse hat folgende Eigenschaften:
- Statische Konstanten vom Typ
int
MAX_VALUE
undMIN_VALUE
, welche als Wert die größte bzw. kleinsteint
-Zahl enthalten. - Eine private Objektvariable
value
vom Typint
. (Dieservalue
hat jetzt eine "objektorientierte" Hülle:MyInteger
). - Eine statische Methode
parseInt(String s)
, die den übergebenenString s
alsint
-Zahl zurückgibt, wenns
einer Zahl entspricht. Wenn nicht, wirft die Methode eineIllegalArgumentException
. Beachten Sie: a.s
kann mit+
oder–
beginnen, b.s
kann führende Nullen aufweisen, c. die Länge vons
kann mits.length()
ermittelt und jedes einzelne Zeichen auss
kann mits.charAt(index)
betrachtet werden. d. Ists
leer, wird eineIllegalArgumentException
geworfen und wenns
keiner Zahl entspricht auch. e. Die Exception wird nur weitergereicht, nicht hier behandelt. - Zwei Konstruktoren
MyInteger(int value)
undMyInteger(String s)
, die jeweils die Objektvariablevalue
initialisieren. Der zweite Konstruktor verwendetparseInt(String)
und kann ebenfalls eineIllegalArgumentException
werfen (reicht die Exception vonparseInt(String)
weiter). - Eine Objektmethode
intValue()
, die den Wert vonvalue
zurückgibt. - Eine Objektmethode
doubleValue()
, die den Wert vonvalue
alsdouble
zurückgibt. - Eine statische Methode
valueOf(String s)
, die ein Objekt vonMyInteger
erzeugt und zurückgibt (und evtl. eineIllegalArgumentException
wirft). - Eine statische Methode
valueOf(int value)
, die ein Objekt vonMyInteger
erzeugt und zurückgibt. - Überschreiben Sie außerdem die Methoden
equals()
undtoString()
(Zusatz: auchhashCode()
überschreiben). - Testen Sie Ihre Klasse ausführlich in einer
Testklasse
mitmain()
-Methode. - Laden Sie Ihr Lösung in Moodle hoch! Viel Spaß und viel Erfolg!
eine mögliche Lösung für Aufgabe 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
Aufgabe 3 (Solitaire)¶
Aufgabe 3
Information und Vorbereitung
Wir wenden Aufzählungstypen und mehrdimensionale Arrays an.
Wir beginnen, ein Englisches Solitär zu programmieren. Einige kennen es auch als Steckhalma. Ziel des Spiels ist, alle Steine bis auf einen (der am besten in der Mitte übrig bleibt), zu entfernen. Ein Zug ist wie folgt: ein Stein springt über einen anderen Stein und der übersprungene Stein wird entfernt. Es gibt viele Lösungen dafür.
Teil der Aufgabe ist es auch, "fremden" Code zu lesen und zu verstehen, denn einige Klassen sind bereits gegeben:
Klasse Point.java
package aufgaben.aufgabe3;
/*
* ein Point repreasentiert eine Position
* im Spielfeld, bestehend aus der Nummer
* fuer die Zeile (row) und der Nummer
* fuer die Spalte (col)
*/
public class Point {
private int row;
private int col;
/*
* Konstruktor zur Erzeugung einer
* Position bestehend aus row und col
*/
public Point(int row, int col)
{
this.row = row;
this.col = col;
}
public int getRow() {
return this.row;
}
public int getCol() {
return this.col;
}
@Override
public String toString()
{
return "("+ this.row + "," + this.col + ")";
}
}
Klasse Move.java
package aufgaben.aufgabe3;
/*
* diese Klasse repraesentiert einen Zug
* Variablen sind Point from
* und Point to
* es wird nicht geprueft, ob der Zug ueberhaupt
* moeglich ist
*/
public class Move {
private Point from;
private Point to;
/*
* ein Zug von dem from-Point (fromRow,fromCol)
* zum to-Point (toRow,toCol)
*/
public Move(int fromRow, int fromCol, int toRow, int toCol)
{
this.from = new Point(fromRow, fromCol);
this.to = new Point(toRow, toCol);
}
/*
* in dem Konstruktor werden in this.from und this.to nicht einfach
* die Referenzen von from und to gespeichert, sondern davon Kopien
* erstellt, damit das Programm robuster gegen das Aendern von
* Referenzen ist
*/
public Move(Point from, Point to)
{
this.from = new Point(from.getRow(), from.getCol());
this.to = new Point(to.getRow(), to.getCol());
}
/*
* der Getter fuer den Point from gibt keine Referenz auf
* den Point from zurueck, sondern eine Kopie (einen Klon)
* von from --> Referenzen koennen "verbogen" werden, aber
* die Kopien bleiben unveraendert
*/
public Point getFrom()
{
return new Point(this.from.getRow(), this.from.getCol());
}
/*
* der Getter fuer den Point to gibt keine Referenz auf
* den Point to zurueck, sondern eine Kopie (einen Klon)
* von to --> Referenzen koennen "verbogen" werden, aber
* die Kopien bleiben unveraendert
*/
public Point getTo()
{
return new Point(this.to.getRow(), this.to.getCol());
}
}
Klasse Moves.java
package aufgaben.aufgabe3;
/*
* diese Klasse repraesentiert eine Folge
* von Zuegen (Move), die in einem Array
* moves gespeichert sind
*/
public class Moves {
private Move[] moves;
/*
* der Konstruktor erstellt ein leeres moves-Array
* (d.h. noch keine Zuege (Moves) gespeichert)
*/
public Moves()
{
this.moves = new Move[0];
}
/*
* der Konstruktor erstellt ein moves-Array mit einem
* Move - dem erste Zug (firstMove)
*/
public Moves(Move firstMove)
{
this.moves = new Move[1];
this.moves[0] = firstMove;
}
/*
* Anzahl der bisher gespeicherten Zuege
*/
public int getLength()
{
return this.moves.length;
}
/*
* fuegt einen Zug (nextMove) zum moves-Array hinzu
* dazu muss das moves-Array um 1 laenger sein als zuvor
* es wird eine Kopie aller Zuege erstellt und dann
* der nextMove hinzugefuegt
*/
public void addMove(Move nextMove)
{
Move[] newMoves = new Move[this.moves.length + 1];
for (int index = 0; index < this.moves.length; index++) {
newMoves[index] = this.moves[index];
}
newMoves[newMoves.length - 1] = new Move(nextMove.getFrom(), nextMove.getTo());
this.moves = newMoves;
}
/*
* gibt den Move zurueck, der im moves-Array unter dem Index index
* gespeichert ist;
* kann sein, dass index kein korrekter Index im moves-Array ist,
* dann wird eine IllegalArgumentException geworfen
*/
public Move getMoveAtIndex(int index) throws IllegalArgumentException
{
try {
return this.moves[index];
}
catch(ArrayIndexOutOfBoundsException e)
{
throw new IllegalArgumentException("kein gueltiger Index!");
}
}
/*
* Ausgabe aller im moves-Array gespeicherten Zuege
* wird nur zum Debuggen benoetigt
*/
public void printMoves()
{
System.out.printf("%n---%n");
for (int index = 0; index < this.moves.length; index++) {
Move move = this.moves[index];
Point from = move.getFrom();
Point to = move.getTo();
System.out.println(from.toString() + " --> " + to.toString());
}
System.out.printf("%n---%n%n");
}
}
enum State.java
package aufgaben.aufgabe3;
/*
* FREE - der Platz ist ein Spielfeld, aber kein Spielstein drauf
* USED - der Platz ist ein Spielfeld mit Spielstein drauf
* NOT - der Platz gehoert nicht zum Spielfeld
*/
public enum State {
FREE, USED, NOT
}
Aufgabe
Befüllen Sie die Klasse Solitaire.java
, wie in den Kommentaren beschrieben:
Klasse Solitaire.java
package aufgaben.aufgabe3.loesung;
public class Solitaire {
private Moves game;
private State[][] field;
public Solitaire()
{
this.game = new Moves();
this.field = new State[7][7];
for(int row = 0; row < this.field.length; row++)
{
for(int col = 0; col < this.field[row].length; col++)
{
if((row < 2 || row > 4) && (col < 2 || col > 4))
{
this.field[row][col] = State.NOT;
}
else
{
this.field[row][col] = State.USED;
}
}
}
this.field[3][3] = State.FREE;
}
/*
* Geben Sie das Spielfeld aus! Am Anfang sollte auf der
* Konsole so ein Bild erscheinen:
* o o o
* o o o
* o o o o o o o
* o o o o o o
* o o o o o o o
* o o o
* o o o
*
*/
public void print()
{
}
/*
* diese Methode gibt ein true zurueck, wenn von der
* uebergebenen Position (row,col) ein Zug moeglich ist
* d.h.
* 1. auf der angegebenen Position muss ein Stein sein
* 2. zwei Steine weiter (oben, unten, rechts oder links)
* darf kein Stein sein
* 3. dazwischen muss ein Stein sein
*/
public boolean possibleFrom(int row, int col)
{
return false;
}
/*
* diese Methode gibt alle Positionen (Point) zurueck,
* AUF die von (fromRow,fromCol) aus gesprungen werden
* kann
*/
public Point[] possibleTo(int fromRow, int fromCol)
{
if(!possibleFrom(fromRow, fromCol)) return new Point[0];
/*
* naechste Zeile muss entfernt werden!
* sttatdessen muessen Sie alle Point-Objekte ermitteln AUF die
* gesprungen werden kann. Diese Point-Objekte werden in einem
* Point-Array gespeichert, welches zurückgegeben wird.
*/
return null;
}
/*
* diese Methode erzeugt ein Moves-Objekt
* in dieses Moves-Objekt werden mithilfe der
* Objektmethode addMove() (aus Moves) alle
* moeglichen Zuege hinzugefuegt
* (moeglich im aktuellen Zustand von field[][])
*/
public Moves possibleMoves()
{
Moves possibleMoves = new Moves();
// next line for debugging
possibleMoves.printMoves();
return possibleMoves;
}
/*
* gibt ein true zurueck, wenn im aktuellen Zustand
* von field[][] ueberhaupt noch ein Zug moeglich ist
* sonst false
*/
public boolean movePossible()
{
return false;
}
/*
* ruft die Methode move(Move move) auf,
* wenn ein Zug moeglich ist (dann true zurueck)
* sonst false
*/
public boolean moveFirstPossible()
{
if(!movePossible()) return false;
/*
* hier einen moeglichen Zug ausfuehren
* den ersten, den Sie finden (siehe
* possibleMoves() )
*/
return true;
}
/*
* hier wird der Zug Move move ausgefuehrt
* nach dem Zug ist
* 1. die from-Position leer
* 2. die to-Position mit einem Stein besetzt
* 3. dazwischen leer (Stein wird "entfernt")
* falls Zug nicht moeglich, wird eine
* IllegalArgumentException geworfen
*/
public void move(Move move) throws IllegalArgumentException
{
}
}
Sie können selbstverständlich beliebig viele weitere (Hilfs-)Methoden hinzufügen.
Testen Sie Ihr Spiel in einer Testklasse
. Führen Sie einige Züge aus und geben danach immer das Spielfeld auf die Konsole aus. Die Konsole könnte z.B. dann so aussehen:
mögliche Konsolenausgaben
o o o
o o o
o o o o o o o
o o o o o o
o o o o o o o
o o o
o o o
---
(1,3) --> (3,3)
(3,1) --> (3,3)
(3,5) --> (3,3)
(5,3) --> (3,3)
---
o o o
o o
o o o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(2,1) --> (2,3)
(2,5) --> (2,3)
(4,3) --> (2,3)
---
o o o
o o
o o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(0,2) --> (2,2)
(2,4) --> (2,2)
(3,3) --> (1,3)
(4,1) --> (2,1)
(4,2) --> (2,2)
---
o o
o
o o o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(0,4) --> (0,2)
(2,3) --> (2,1)
(3,2) --> (1,2)
(3,3) --> (1,3)
(4,1) --> (2,1)
---
o
o
o o o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(2,3) --> (2,1)
(2,4) --> (0,4)
(3,2) --> (1,2)
(3,3) --> (1,3)
(4,1) --> (2,1)
---
o
o
o o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(2,0) --> (2,2)
(2,4) --> (0,4)
(2,5) --> (2,3)
(4,2) --> (2,2)
(4,3) --> (2,3)
---
o
o
o o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(2,4) --> (0,4)
(2,5) --> (2,3)
(3,2) --> (1,2)
(4,0) --> (2,0)
(4,1) --> (2,1)
(4,3) --> (2,3)
---
o o
o o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(2,6) --> (2,4)
(3,2) --> (1,2)
(4,0) --> (2,0)
(4,1) --> (2,1)
(4,3) --> (2,3)
(4,4) --> (2,4)
---
o o
o o
o o o o o o o
o o o o o o o
o o o
o o o
---
(3,2) --> (1,2)
(3,4) --> (1,4)
(4,0) --> (2,0)
(4,1) --> (2,1)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
---
o o
o
o
o o o o o o
o o o o o o o
o o o
o o o
---
(0,2) --> (2,2)
(3,0) --> (3,2)
(3,4) --> (1,4)
(3,4) --> (3,2)
(4,0) --> (2,0)
(4,1) --> (2,1)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
(5,2) --> (3,2)
---
o
o o
o o o o o o
o o o o o o o
o o o
o o o
---
(3,0) --> (3,2)
(3,4) --> (1,4)
(3,4) --> (3,2)
(4,0) --> (2,0)
(4,1) --> (2,1)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
(5,2) --> (3,2)
---
o
o o
o o o o o
o o o o o o o
o o o
o o o
---
(3,2) --> (1,2)
(3,3) --> (3,1)
(3,4) --> (1,4)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
---
o
o
o
o o o o
o o o o o o o
o o o
o o o
---
(3,4) --> (1,4)
(3,4) --> (3,2)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
(5,2) --> (3,2)
---
o
o o
o o o
o o o o o o o
o o o
o o o
---
(0,4) --> (2,4)
(3,6) --> (3,4)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
(5,2) --> (3,2)
(5,4) --> (3,4)
---
o
o
o o o
o o o o o o o
o o o
o o o
---
(3,6) --> (3,4)
(4,3) --> (2,3)
(4,5) --> (2,5)
(4,6) --> (2,6)
(5,2) --> (3,2)
(5,4) --> (3,4)
---
o
o
o o
o o o o o o o
o o o
o o o
---
(3,3) --> (3,5)
(3,4) --> (1,4)
(3,4) --> (3,2)
(4,3) --> (2,3)
(5,2) --> (3,2)
---
o
o
o
o o o o o o o
o o o
o o o
---
(4,5) --> (2,5)
(5,2) --> (3,2)
(5,3) --> (3,3)
(5,4) --> (3,4)
---
o
o o
o o o o o o
o o o
o o o
---
(2,4) --> (2,6)
(2,5) --> (2,3)
(4,3) --> (4,5)
(5,2) --> (3,2)
(5,3) --> (3,3)
(5,4) --> (3,4)
---
o
o
o o o o o o
o o o
o o o
---
(4,3) --> (4,5)
(5,2) --> (3,2)
(5,3) --> (3,3)
(5,4) --> (3,4)
---
o
o
o o o o o
o o o
o o o
---
(4,1) --> (4,3)
(4,6) --> (4,4)
(5,2) --> (3,2)
(6,3) --> (4,3)
(6,4) --> (4,4)
---
o
o
o o o o
o o o
o o o
---
(4,6) --> (4,4)
(5,3) --> (3,3)
(6,2) --> (4,2)
(6,4) --> (4,4)
---
o
o
o o o
o o o
o o o
---
(4,3) --> (4,5)
(4,4) --> (4,2)
(5,3) --> (3,3)
(5,4) --> (3,4)
(6,2) --> (4,2)
---
o
o
o o
o o o
o o o
---
(6,2) --> (4,2)
(6,3) --> (4,3)
(6,4) --> (4,4)
---
o
o
o o o
o o
o o
---
(5,4) --> (5,2)
(6,3) --> (4,3)
(6,4) --> (4,4)
(6,4) --> (6,2)
---
o
o
o o o
o
o o
---
(4,2) --> (6,2)
(5,2) --> (3,2)
(6,4) --> (6,2)
---
o
o
o o
o o o
o
o
o o
o o o
Dabei steht z.B.
---
(1,3) --> (3,3)
(3,1) --> (3,3)
(3,5) --> (3,3)
(5,3) --> (3,3)
---
eine mögliche Lösung für Aufgabe 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Aufgabe 4 (Operationen über Mengen)¶
Aufgabe 4
Implementieren Sie eine Klasse SetOperations
.
- Erstellen Sie drei Objektvariablen (z.B.
numbers1
,numbers2
undboth
) vom TypSet<Integer>
. Erzeugen Sie für diese ObjektvariablenTreeSet
-Objekte. - Erstellen Sie eine Objektmethode
fill()
. In dieser Methode sollen die beiden Setsnumbers1
undnumbers2
mit Zufallszahlen aus dem Wertebereich0..99
befüllt werden. Erzeugen Sie jeweils100
Zufallszahlen (dienumber
s-Sets werden dann aber jeweils weniger als 100 Einträge haben, da doppelte Elemente nicht aufgenommen werden.)
Beachten Sie, dass dienextInt()
-Methode einint
zurückliefert. Dieses int muss zunächst in einInteger
-Objekt umgewandelt werden (Integer.valueOf(int)
) und diesesInteger
-Objekt wird dann dem Set hinzugefügt (wenn es nicht schon enthalten ist). - Erstellen Sie eine Objektmethode
fillBothUnion()
. In dieser Methode wird dieboth
-Menge derart befüllt, dassboth
einer Vereinigung der Mengennumbers1
undnumbers2
entspricht. Für Methoden auf Mengen siehe z.B. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Set.html. - Erstellen Sie eine Objektmethode
fillBothIntersection()
. In dieser Methode wird dieboth
-Menge derart befüllt, dassboth
einem Schnitt der Mengennumbers1
undnumbers2
entspricht. - Erstellen Sie eine Objektmethode
fillBothDifference()
. In dieser Methode wird dieboth
-Menge derart befüllt, dassboth
der Differenz der Mengenumbers1
minus der Mengenumbers2
entspricht. - Schreiben Sie eine Methode
print()
so, dass jede Menge als ein Quadrat (10x10) aus Punkten auf der Konsole erscheint. Es wird ein Punkt gezeichnet, wenn die entsprechende Zahl (die 100 Punkte bilden die Zahlen 0..99 ab) in der Menge enthalten ist und es wird kein Punkt gezeichnet, wenn die Zahl nicht vorhanden ist. (Anstelle von Punkten können Sie auch jedes beliebige Zeichen (o, *, #, x, ...
) verwenden).
Gestalten Sie die Ausgabe so, dass die drei Mengennumbers1
,numbers2
undboth
nebeneinander auf der Konsole erscheinen. -
Testen Sie alle drei Methoden
fillBothUnion()
,fillBothIntersection()
undfillBothDifference()
. Die Ausgabe könnte wie folgt sein (A
stellt die Mengenumbers1
dar,B
die Mengenumbers2
und rechts ist jeweils dieboth
-Menge dargestellt):
Tipps:
- Für die schwarzen Punkte habe ich das Character
'\u25cf'
verwendet (ein passendes Leerzeichen dazu ist'\u2009'
). Das Zeichen für die Vereinigung ist'\u222a'
und für den Schnitt'\u2229'
. - Schauen Sie sich auch im Skript Mengenoperationen an.
eine mögliche Lösung für Aufgabe 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Aufgabe 5 (Maps)¶
Aufgabe 5
-
Testen Sie folgenden Code:
long a = 1000; Integer b = 1000; Integer c = 1000; System.out.println(a == b); System.out.println(b == c);
Was stellen Sie fest und warum? Ändern Sie den Vergleich der Variablen
b
undc
so, dasstrue
ausgegeben wird. Die ersten drei Zeilen bleiben dabei unverändert. Sie benötigen also einen anderen Vergleich alsb == c
. -
Erstellen Sie eine Klasse
PizzaMenu
. Erzeugen Sie darin eineHashMap
und fügen Sie folgende Pizzen mit ihren Preisen ein:Pizza Preis Margherita 7,00 Veggie 7,50 Salami 8,50 Schinken 8,50 -
Schreiben Sie eine Methode
public static boolean existingPizza(Map<String,Double> menu, String pizza)
, die testet, ob eine eingegebene Pizza im Menü existiert. -
Schreiben Sie eine Methode
public static void printPrice(Map<String,Double> menu, String pizza)
, die für eine eingegebene Pizza den Preis ausgibt. Verwenden Sie dazuexistingPizza()
. Beispielausgabe:Salami: 8.50 Euro Hawaii: Diese Pizza gibt es nicht.
-
Schreiben Sie eine
printMenu()
-Methode, die ausgibt, wie viele Pizzen es im Menü gibt. Wenn das Menü nicht leer ist, sollen zudem alle Pizzen mit ihrem Preis ausgegeben werden. Beispielausgabe:// Beispiel fuer leeres Menue ############## Das Menu enthält 0 Pizzen. ##############
// Beispiel fuer Menue ############## Das Menu enthält 4 Pizzen. Pizza Margherita: 7.00 Euro Pizza Salami: 8.50 Euro Pizza Schinken: 8.50 Euro Pizza Veggie: 7.50 Euro ##############
-
Ändern Sie den Preis der
Schinken
-Pizza in derHashMap
zu8,20
. -
Schreiben Sie eine Methode
public static void affordablePizza(Map<String,Double> menu, double maxPrice)
, die alle Pizzen ausgibt, die man sich für einen bestimmten Betrag leisten kann. Beispielausgabe:Eingegebener Betrag: 4.0 Euro Leider gibt es zu diesem Preis keine Pizza. Eingegebener Betrag: 8.0 Euro Mögliche Pizzen: [Margherita, Veggie]
-
Das Menu wird überarbeitet, löschen Sie daher alle Einträge. Füllen Sie das Menü anschließend mit diesen neuen Pizzen:
Pizza Preis Verde 7,00 Hawaii 8,20 Tradizionale 8,50 -
Legen Sie ein zweites Menü an und kopieren Sie alle Einträge aus dem ersten Menü hinein. Löschen Sie im zweiten Menü die Pizza
Hawaii
und geben Sie die Namen aller noch verfügbaren Pizzen aus. -
Finden Sie in Ihrem Code ein Beispiel für Auto-Boxing und schreiben Sie einen entsprechenden Kommentar.
eine mögliche Lösung für Aufgabe 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
Aufgabe 6 (Interfaces)¶
Aufgabe 6
-
Das Observer-Entwurfsmuster gehört zu den am meisten verwendeten Designmustern/Designpattern/Pattern in der Programmierung. Es wird auch Beobachter-Muster oder Publisher-Pattern genannt. Wir werden dieses Muster in Kürze sehr häufig anwenden, wenn wir Nutzerereignisse in grafischen Oberflächen behandeln. Man kann sich dieses Pattern so vorstellen, dass der Publisher eine Zeitung oder auch Slack ist und dass Listener diese Zeitung (oder Slack) "abonnieren". Immer, wenn eine Nachricht veröffentlicht wird, dann erfahren alle Abonnenten davon. Wir werden eine (einfache) Implementierung dieses Entwurfsmusters durchführen.
-
Erstellen Sie ein Interface
Publisher
mit folgenden (abstrakten) Methoden:public boolean register(Listener listener);
public boolean unregister(Listener listener);
public void notifyListeners();
public String getUpdate(Listener listener);
-
Erstellen Sie ein weiteres Interface
Listener
mit folgenden (abstrakten) Methoden:public void update();
public void setPublisher(Publisher publisher);
public void removePublisher(Publisher publisher);
-
Erstellen Sie eine Klasse
Slack
, die dasPublisher
-Interface implementiert. Objektvariablen der Klasse sindprivate Set<Listener> listeners;
(speichert alle "Abonnenten"; kann gerne auch eine Liste sein)-
private int nrOfMessages;
(speichert die aktuelle Nummer einer veröffentlichten Nachricht - die Nachrichten, die veröffentlicht werden, sollen fortlaufend nummeriert werden) -
Im parameterlosen Konstruktor werden die Menge (oder Liste) erzeugt und die
nrOfMessages
auf `0 gesetzt. -
In der Methode
register(Listener listener)
wird derlistener
in die Setlisteners
eingefügt. Geben Sie eintrue
zurück, wennlistener
tatsächlich eingefügt wurde undfalse
sonst (falls er schon in der Menge (oder Liste) war. -
In der Methode
unregister(Listener listener)
wird derlistener
wieder aus der Setlisteners
gelöscht. Geben Sie eintrue
zurück, wennlistener
tatsächlich gelöscht wurde undfalse
sonst (falls er nicht in der Menge (oder Liste) war. -
In der Methode
notifyListeners()
wird für allelistener
aus der Mengelisteners
dieupdate()
-Methode aufgerufen (sieheListener
undStudent
). -
Die Methode
getUpdate(Listener obj)
liefert einfach folgenden String zurück:"Breaking News " + this.nrOfMessages
. -
Erstellen Sie eine Methode
public void publishNews()
, in der dienrOfMessages
um 1 erhöht und die MethodenotifyListeners()
aufgerufen wird.
-
Erstellen Sie eine Klasse
Student
, die dasListener
-Interface implementiert. Objektvariablen der Klasse sindprivate String name;
(speichert den Namen vonStudent
)-
private Publisher publisher;
(speichert denPublisher
, an den sichStudent
anmeldet) -
Im parametrisierten Konstruktor
public Student(String name)
wird der Name initalisiert. -
In der Methode
setPublisher(Publisher publisher)
wird dieregister()
-Methode despublisher
aufgerufen und der Wert der Objektvariablepublisher
gesetzt. Geben Sie bei erfolgreicher Anmeldung an denpublisher
auf die Konsolethis.name + " registered!"
aus. -
In der Methode
removePublisher(Publisher publisher)
meldet sichStudent
wieder vompublisher
ab (Aufruf vonunregister()
und Ausgabe auf die Konsolethis.name + " deregistered!"
. -
In der Methode
update()
wird diegetUpdate()
-Methode despublisher
aufgerufen und die zurückgegebene Nachrichtmsg
wie folgt auf die Konsole ausgegben:this.name + " received " + msg
. -
Implementieren Sie für
Student
auch die Methodenequals()
undhashCode()
.
-
Wenn Sie Ihre Implementierung mit folgender Klasse testen:
public class Testklasse { public static void main(String[] args) { final int NR_OF_STUDENTS = 5; Slack slack = new Slack(); Student[] students = new Student[NR_OF_STUDENTS]; Character c = 'A'; for(int index=0; index < students.length; index++) { students[index] = new Student(c.toString()); c++; students[index].setPublisher(slack); } slack.publishNews(); System.out.println(); students[1].removePublisher(slack); students[3].removePublisher(slack); System.out.println(); slack.publishNews(); System.out.println(); students[1].setPublisher(slack); students[2].removePublisher(slack); students[4].removePublisher(slack); System.out.println(); slack.publishNews(); System.out.println(); students[0].removePublisher(slack); students[1].removePublisher(slack); students[3].setPublisher(slack); System.out.println(); slack.publishNews(); } }
dann sollte die Ausgabe ungefähr so sein:
A registered! B registered! C registered! D registered! E registered! D received Breaking News 1 C received Breaking News 1 B received Breaking News 1 A received Breaking News 1 E received Breaking News 1 B deregistered! D deregistered! C received Breaking News 2 A received Breaking News 2 E received Breaking News 2 B registered! C deregistered! E deregistered! B received Breaking News 3 A received Breaking News 3 A deregistered! B deregistered! D registered! D received Breaking News 4
eine mögliche Lösung für Aufgabe 6
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Aufgabe 7 (GUI)¶
Aufgabe 7
-
Erstellen Sie die GUI für einen solchen Taschenrechner:
Kann auch schicker sein :-) Viel Spaß!
eine mögliche Lösung für Aufgabe 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
Aufgabe 8 (Ereignisbehandlung)¶
Aufgabe 8
- Implementieren Sie für den Taschenrechner aus Aufgabe 7 das
ActionListener
-Interface, so dass der Taschenrechner über möglichst viel Funktionalität verfügt. - Minimumanforderung ist, dass durch die Eingabe von Ziffern Zahlen entstehen und dass die Buttons
+
,-
,/
,+
und=
insofern funktionieren, dass dadurch tatsächlich gerechnet wird, auch wenn die Regel "Punktrechnung geht vor Strichrechnung" nicht eingehalten wird. - Eine erste Verbesserung (ist schon Zusatz) wäre, dass die Regel "Punktrechnung geht vor Strichrechnung" eingehalten wird.
- Ab dann ist alles Zusatz und bedarf in vielen Fällen der Erweiterung der GUI: Verwendung und Beachten von Klammern (ist aber sehr schwer!), Speichern von Zwischenergebnissen, komplexere Operationen durch zuätzliche Tasten (Wurzel, Potenzieren, ...). Alles, was Sie möchten - Viel Spaß!
eine mögliche Lösung für Aufgabe 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
Aufgabe 9 (Zeichnen - entfällt)¶
Aufgabe 9
-
Wir berechnen PI mithilfe der Monte-Carlo-Methode. Dazu zeichnen wir ein Quadrat und darin einen Kreisbogen (einen Viertelkreis), dessen Radius der Seitenlänge des Quadrats entspricht:
-
Nun zeichnen wir in dieses Quadrat viele kleine ausgefüllte Kreise (Durchmesser z.B.
2
Pixel). Die Positionen dieser Kreise werden zufällig ermittelt. Wenn die zufällig ermittelte Position für diesen Kreis im Kreisbogen liegt, dann sind die kleinen Kreise rot, wenn die Position außerhalb liegt, dann sind die Kreise blau. Es entsteht bei ganz vielen (z.B.50000
) Punkten so ein Bild: -
Tipp: Ob ein Punkt mit den Koordinaten
(x,y)
in einem (Viertel-)Kreis liegt, ermitteln Sie mithilfe der Kreisgleichung. Sei(xm, ym)
der Mittelpunkt undr
der Radius des Kreises, dann gilt(x-xm)^2 + (y-ym)^2 = r^2
für alle Punkte auf dem Kreis (für Punkte im Kreis gilt <= und für Punkte außerhalb des Kreises >). -
Der Quotient aus der Anzahl der roten Punkte und der Gesamtanzahl der Punkte ist eine Näherung von PI/4. Geben Sie Ihre Annäherung von PI auf der Konsole aus.
eine mögliche Lösung für Aufgabe 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|