Compare commits

...

41 Commits

Author SHA1 Message Date
352a9909ed Vorlesung 16 / Aufgabe 1 2024-06-11 14:47:57 +02:00
1f0c7abff6 some changes from practice group 2024-06-10 15:20:28 +02:00
e1eaf252de Vorlesung 15 / Aufgabe 1 2024-06-09 23:16:37 +02:00
b90a013f4c now using wait and notify 2024-06-03 22:04:50 +02:00
108cd3259e removed custom name for threads - using internal name instead 2024-05-30 11:37:17 +02:00
dad420e8d8 Vorlesung 14 / Aufgabe 1 2024-05-30 11:31:58 +02:00
c08f4d3196 Vorlesung 14 / Aufgabe 2 2024-05-30 11:27:24 +02:00
b96c8c7b21 removed extension Rainbow Brackets 2024-05-17 18:42:31 +02:00
9506ee39ed Vorlesung 8 / Aufgabe 3 2024-05-17 08:15:15 +02:00
ee2b5d5e1c Vorlesung 8 / Aufgabe 4 2024-05-16 16:32:58 +02:00
203e18ca10 added missing extension 2024-05-16 10:55:00 +02:00
2f0de3f778 improvements initializing arrays 2024-05-16 10:50:07 +02:00
8c527a98de small improvement and same values while running the tests 2024-05-16 10:47:47 +02:00
0b552e9d0f added README.md 2024-05-13 16:32:49 +02:00
a0a812406d small improvements 2024-05-13 16:05:20 +02:00
01bb3e9def Vorlesung 4 / Aufgabe 6 2024-05-13 16:04:59 +02:00
7e87a3bec2 Vorlesung 4 / Aufgabe 5 2024-05-13 16:04:41 +02:00
b1d4deb0e3 Vorlesung 4 / Aufgabe 4 2024-05-13 16:04:23 +02:00
bf7453a3de Vorlesung 4 / Aufgabe 3 2024-05-13 16:03:58 +02:00
cba4d08315 Vorlesung 3 / Aufgabe 4 2024-05-10 08:14:31 +02:00
4257cab1fa Vorlesung 3 / Aufgabe 3 2024-05-10 08:09:58 +02:00
d64c33dfe5 Vorlesung 3 / Aufgabe 2 2024-05-10 08:01:49 +02:00
dc781ca0bd Vorlesung 3 / Aufgabe 1 2024-05-09 15:16:52 +02:00
3e064693bf Vorlesung 5 / Aufgabe 2 2024-05-09 14:45:37 +02:00
7c8a682066 Vorlesung 4 / Aufgabe 2 2024-05-09 12:59:05 +02:00
98e7893343 Vorlesung 4 / Aufgabe 1 2024-05-09 12:58:50 +02:00
7b69a0c9dc removed old solution 2024-05-09 12:34:53 +02:00
dbca5fe6dd Vorlesung 5 / Aufgabe 3 2024-05-09 12:32:30 +02:00
a6205b597b Vorlesung 5 / Aufgabe 1 2024-05-09 12:31:36 +02:00
c14cedfcd9 Vorlesung 6 / Aufgabe 2 2024-05-08 12:45:59 +02:00
2723cb4288 small improvements 2024-05-08 12:37:30 +02:00
d31a881078 Vorlesung 6 / Aufgabe 1 2024-05-07 23:58:15 +02:00
a91b597a20 small improvements 2024-05-07 23:06:51 +02:00
353a41ba0d Vorlesung 13 / Aufgabe 3 2024-05-07 22:58:31 +02:00
e732fc7232 suppress warning 2024-05-07 20:59:14 +02:00
8a7d98881e Vorlesung 13 / Aufgabe 2 2024-05-07 20:55:35 +02:00
f6a3b314cb Vorlesung 13 / Aufgabe 1 2024-05-07 20:43:58 +02:00
12997ba989 ignore Visual Studio Code files 2024-05-06 22:22:52 +02:00
5447569e18 style guide 2024-05-06 22:19:27 +02:00
6a87bb9fd3 ignore output folders in git 2024-05-06 19:31:35 +02:00
a428740839 removed output files 2024-05-06 19:30:53 +02:00
75 changed files with 2454 additions and 695 deletions

8
.gitignore vendored
View File

@@ -3,4 +3,10 @@
# IntelliJ
.idea/
*.iml
*.iml
# Visual Studio Code
.vscode/
# output folders
**/output/

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# :chipmunk: VL-Programmieren
Lösungen zu den Aufgaben der Vorlesung "Programmieren" des ersten und zweiten Semesters.
## :hammer: Entwicklungsumgebung
- **Editor**: [Visual Studio Code](https://code.visualstudio.com/)
- [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
- [EditorConfig for VS Code](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
- **Java**: [Java Development Kit 21 (LTS)](https://www.oracle.com/de/java/technologies/downloads/#java21)
- **Style Guide**: [Google Java Style Guide](https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml)

View File

@@ -1,5 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
System.out.println("Hallo Welt");
}
}

View File

@@ -1,23 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
// Die drei Zahlen für die Prüfung definieren.
int a = 3;
int b = 4;
int c = 5;
// Die Zahlen überprüfen und das Ergebnis ausgeben.
System.out.println("Werte entsprechen dem Satz des Pythagoras: " + (check(a,b,c) ? "Ja" : "Nein"));
}
/**
* Prüfen ob die angegebenen Zahlen dem Satz des Pythagoras entsprechen.
* @param a Die erste Zahl (a).
* @param b Die zweite Zahl (b).
* @param c Das Ergebnis (c).
* @return Status ob die angegebenen Zahlen dem Satz des Pythagoras entsprechen.
*/
public static boolean check(int a, int b, int c) {
return (a*a + b*b == c*c) ? true : false;
}
}

View File

@@ -1,25 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
int i = 5;
int j = 3;
boolean b = false;
/*
* a.) (!((i<j) && b)) - boolean (true)
* b.) i/j - int (1)
* c.) (float) (i/j) - float (1.0)
* d.) (float) i/j - float (1.6)
* e.) (float) i / (float) j - float (1.6)
* f.) ((i++ == 5) || (--i == 5)) - boolean (true)
* g.) ((i++ == 5) | (--i == 5)) - int (1)
*/
System.out.println("a.) " + (!((i<j) && b)));
System.out.println("b.) " + (i/j));
System.out.println("c.) " + (float) (i/j));
System.out.println("d.) " + ((float) i/j));
System.out.println("e.) " + ((float) i / (float) j));
System.out.println("f.) " + (((i++ == 5) || (--i == 5))));
System.out.println("g.) " + (((i++ == 5) | (--i == 5))));
}
}

View File

@@ -1,21 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int MAXIMUM = 10;
System.out.println("Alle geraden Zahlen zwischen 1 und " + MAXIMUM + " (aufsteigend):");
for(int i = 1; i <= MAXIMUM; i++) {
if(i % 2 == 0) {
System.out.println(i);
}
}
System.out.println("Alle ungeraden Zahlen zwischen 1 und " + MAXIMUM + " (absteigend):");
for(int i = MAXIMUM; i >= 1; i--) {
if(i % 2 == 1) {
System.out.println(i);
}
}
}
}

View File

@@ -1,38 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int START = 1;
final int ENDE = 20;
int summe = 0;
int n = 0;
System.out.println("Lösung mit for-Schleife:");
for(n = START; n <= ENDE; n++) {
summe += n;
System.out.println(summe);
}
System.out.println("Lösung mit while-Schleife:");
summe = 0;
n = START;
while(n <= 20) {
summe += n;
System.out.println(summe);
n++;
}
System.out.println("Lösung mit do-while-Schleife:");
summe = 0;
n = START;
do {
summe += n;
System.out.println(summe);
n++;
} while (n <= ENDE);
}
}

View File

@@ -1,40 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int MAXIMUM = 200;
int[] zahlenreihe = new int[MAXIMUM];
// Initialisieren der Zahlenreihe.
for(int i = 0; i < MAXIMUM; i++) {
zahlenreihe[i] = i + 1;
}
// Alle Basen durchlaufen.
for(int i = 0; i <= (MAXIMUM / 2); i++) {
int basis = zahlenreihe[i];
// Alle Basen und Zahlen kleiner als 2 können ignoriert werden.
// Zahlen kleiner als 2 sind keine Primzahlen.
// Eine Base welche keine Primzahl ist wird mit 0 überschrieben ("durchgestrichen").
if(basis < 2) {
continue;
}
// Alle Zahlen der Zahlenreihe durchlaufen um für die Basis passende Zahlen zu finden.
for(int j = i + 1; j < MAXIMUM; j++) {
int zahl = zahlenreihe[j];
// Es ist keine Primzahl wenn die Zahl durch die Basis teilbar ist.
if(zahl % basis == 0) {
zahlenreihe[j] = 0;
}
}
}
// In der Zahlenreihe sind jetzt nur noch Primzahlen vorhanden.
for(int i = 0; i < MAXIMUM; i++) {
if(zahlenreihe[i] >= 2) {
System.out.println(zahlenreihe[i]);
}
}
}
}

View File

@@ -1,32 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int laufzeit = 10;
final double startwert = 100.0;
// Die verschiedenen Zinssätze definieren.
double zinssatz[] = {3.5, 4.5, 5.5};
// Es kann eine Matrix erstellt werden in welcher die Informationen für die Entwicklung gespeichert werden.
// Die Zeilen sind die Informationen der Jahre. Die Spalten sind die Entwicklung je nach Zinssatz.
double entwicklung[][] = new double[laufzeit][3];
// Für jedes Jahr muss die Entwicklung angepasst werden.
for(int i = 0; i < laufzeit; i++) {
for(int z = 0; z < zinssatz.length; z++) {
double wert = (i == 0) ? startwert : entwicklung[i-1][z];
entwicklung[i][z] = wert + (wert * (zinssatz[z] / 100.0));
}
}
// Die Parameter ausgeben damit die Grundlage der Entwicklung sichtbar ist.
System.out.printf("Laufzeit: %d Jahre - Startwert: %.2f EUR\n\n", laufzeit, startwert);
// Die Kopfleiste für die Tabelle ausgeben.
System.out.printf("Jahr\t%11.2f%%\t%11.2f%%\t%11.2f%%\n", zinssatz[0], zinssatz[1], zinssatz[2]);
// Die Informationen der Entwicklung ausgeben.
for(int i = 0; i < entwicklung.length; i++) {
System.out.printf("%d\t%8.2f EUR\t%8.2f EUR\t%8.2f EUR\n", (2023 + 1) + i, entwicklung[i][0], entwicklung[i][1], entwicklung[i][2]);
}
}
}

View File

@@ -1,73 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
final int RECHTS = 1;
final int LINKS = 0;
final int NUM_FROESCHE = 100000;
// Neuer Frosch startet bei Feld 1.
int position = 1;
int position_max = 0;
// Es gibt zwei Eimer in welchen ein Frosch landen kann.
// Eimer bei Index 0 ist der linke Eimer.
// Eimer bei Index 1 ist der rechte Eimer.
int[] eimer = {0, 0};
// Auf der Zahlengerade springt der aktuelle Frosch.
// Das Feld 0 ist bereits ein Eimer (der rechte Eimer).
// Wenn der Frosch den Index auf der linken Seite verlässt springt er in den linken Eimer.
int[] zahlengerade = new int[1000];
// Die Zahlengerade wird initialisiert. Am Anfang zeigen alle Felder nach rechts.
// 0 = Frosch springt nach links.
// 1 = Frosch springt nach rechts.
for(int i = 0; i < zahlengerade.length; i++) {
zahlengerade[i] = RECHTS;
}
// Schleife mit welcher alle Frösche durchlaufen werden.
// Es ist immer nur ein Frosch auf der Zahlengerade unterwegs.
for(int anzahl_froesche = NUM_FROESCHE; anzahl_froesche > 0; anzahl_froesche--) {
position = 1;
// Der Frosch bewegt sich so lange auf der Zahlengerade bis dieser in einen Eimer fällt.
// Es steht ein Eimer auf Index 0 und Index -1.
while(position > 0) {
// Wenn der Frosch das Feld berüht ändert sich die Richtung des Felds.
zahlengerade[position] = (zahlengerade[position] == RECHTS) ? LINKS : RECHTS;
// Je nach Richtung muss der Frosch jetzt springen.
if (zahlengerade[position] == RECHTS) {
position += 1;
// Die maximale Position soll gespeichert werden.
if (position_max < position) {
position_max = position;
}
} else {
// Der Frosch soll nach links springen.
// Er könnte jetzt in einen Eimer springen.
if (position == 1) {
// Der Frosch springt in den linken Eimer.
eimer[0]++;
} else if (position == 2) {
// Der Frosch springt in den rechten Eimer.
eimer[1]++;
}
// Der Frosch springt nach links (evtl. in einen Eimer).
position -= 2;
}
}
}
// Ausgabe der wichtigsten Informationen.
System.out.printf("Linker Eimer (Index -1): %d Frösche\n", eimer[0]);
System.out.printf("Rechter Eimer (Index 0): %d Frösche\n", eimer[1]);
System.out.printf("Maximale Position auf der Zahlengerade: %d\n", position_max);
}
}

View File

@@ -1,78 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
enum Monate {Januar, Februar, März, April, Mai, Juni, Juli, August, September, Oktober, November, Dezember};
String quartal = "";
Monate monat = Monate.Oktober;
// Ursprüngliche switch-Anweisung:
switch(monat) {
case Januar : quartal = "1. Quartal"; break;
case Februar : quartal = "1. Quartal"; break;
case März : quartal = "1. Quartal"; break;
case April : quartal = "2. Quartal"; break;
case Mai : quartal = "2. Quartal"; break;
case Juni : quartal = "2. Quartal"; break;
case Juli : quartal = "3. Quartal"; break;
case August : quartal = "3. Quartal"; break;
case September : quartal = "3. Quartal"; break;
case Oktober : quartal = "4. Quartal"; break;
case November : quartal = "4. Quartal"; break;
case Dezember : quartal = "4. Quartal"; break;
default : quartal = "";
}
System.out.println("a.) ursprüngliche switch-Anweisung: " + quartal);
// Zusammenfassung mehrerer Ausdrücke.
switch(monat) {
case Januar, Februar, März : quartal = "1. Quartal"; break;
case April, Mai, Juni : quartal = "2. Quartal"; break;
case Juli, August, September : quartal = "3. Quartal"; break;
case Oktober, November, Dezember : quartal = "4. Quartal"; break;
default : quartal = "";
}
System.out.println("b.) Zusammenfassung mehrerer Ausdrücke: " + quartal);
// Pfeilnotation
switch (monat) {
case Januar -> quartal = "1. Quartal";
case Februar -> quartal = "1. Quartal";
case März -> quartal = "1. Quartal";
case April -> quartal = "2. Quartal";
case Mai -> quartal = "2. Quartal";
case Juni -> quartal = "2. Quartal";
case Juli -> quartal = "3. Quartal";
case August -> quartal = "3. Quartal";
case September -> quartal = "3. Quartal";
case Oktober -> quartal = "4. Quartal";
case November -> quartal = "4. Quartal";
case Dezember -> quartal = "4. Quartal";
default -> quartal = "";
}
System.out.println("c.) Pfeilnotation: " + quartal);
// Zusammenfassung mehrerer Ausdrücke in Pfeilnotation.
switch(monat) {
case Januar, Februar, März -> quartal = "1. Quartal";
case April, Mai, Juni -> quartal = "2. Quartal";
case Juli, August, September -> quartal = "3. Quartal";
case Oktober, November, Dezember -> quartal = "4. Quartal";
default -> quartal = "";
}
System.out.println("d.) Zusammenfassung mehrerer Ausdrücke in Pfeilnotation: " + quartal);
// switch-Ausdruck mit Pfeilnotation
quartal = switch(monat) {
case Januar, Februar, März -> "1. Quartal";
case April, Mai, Juni -> "2. Quartal";
case Juli, August, September -> "3. Quartal";
case Oktober, November, Dezember -> "4. Quartal";
default -> "";
};
System.out.println("e.) switch-Ausdruck mit Pfeilnotation: " + quartal);
}
}

View File

@@ -1,81 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
// Es gibt zwei Möglichkeiten die Fibunacci-Folge zu erstellen.
// Dabei werden die ersten zwei Felder immer mit festen Werten definiert.
// Variante 1: F0 = 0; F1 = 1
// Variante 2: F0 = 1; F1 = 1
// Alle Werte danach werden aber immer nach der gleichen Regel erstellt.
// FN = FN-2 + FN-1
System.out.println("Iterativ:");
iterativ(45);
System.out.println("Rekursiv:");
rekursiv(0, 1, 45);
}
/**
* Ermitteln der Fibonaccizahlen mit einer iterativen Lösung.
* @param fields Die Anzahl der Felder welche mit Fibonaccizahlen gefüllt werden sollen.
*/
public static void iterativ(int fields) {
// Initialisieren der Folge. Die Größe der Folge gibt die Anzahl der Fibonaccizahlen an.
int[] fibonaccifolge = new int[fields];
// Alle Felder der Folge initialisieren.
// Die ersten zwei Zahlen müssen dabei fest definiert werden.
// Die Felder werden dabei nach Variante 1 definiert.
for(int i = 0; i < fibonaccifolge.length; i++) {
if (i == 0 || i == 1) {
fibonaccifolge[i] = i;
} else {
fibonaccifolge[i] = 0;
}
}
// Für alle Felder danach kann dann eine Formel verwendet werden.
for(int i = 2; i < fibonaccifolge.length; i++) {
fibonaccifolge[i] = fibonaccifolge[i-1] + fibonaccifolge[i-2];
}
// Ausgeben der Fibonaccifolge.
for(int i = 0; i < fibonaccifolge.length; i++) {
System.out.println(fibonaccifolge[i]);
}
}
/**
* Ermitteln der Fibonaccizahlen mit einer iterativen Lösung.
* @param a Der Wert des Felds FN-2.
* @param b Der Wert des Felds FN-1.
* @param max Die Anzahl der Fibonaccizahlen welche ermittelt werden sollen.
* @return Die ermittelte Fibonaccizahl.
*/
public static int rekursiv(int a, int b, int max) {
// Der Beginn der Fibonaccifolge muss mit bestimmten Werten initialisiert werden.
// Die Werte werden dabei mit den Werten aus Variante 1 initialisiert.
if((a == 0 && b == 0) || (a == 0 && b == 1)) {
a = 0;
b = 1;
max -= 2;
System.out.println(a);
System.out.println(b);
}
// Anzahl der Felder reduzieren.
max--;
// Ausgeben der aktuellen Fibonaccizahl.
System.out.println((a + b));
// Es werden so lange Fibonaccizahlen erstellt so lange noch Felder verfügbar sind.
if(max > 0) {
return rekursiv(b, a + b, max);
} else {
return a + b;
}
}
}

View File

@@ -1,16 +0,0 @@
class Angestellter extends Mitarbeiter {
double monatsGehalt;
public Angestellter(int personalNummer, String name, String vorname, double monatsGehalt) {
super(personalNummer, name, vorname);
this.monatsGehalt = monatsGehalt;
}
void print(){
System.out.println(getPersonalNummer());
System.out.println(getName());
System.out.println(getVorname());
System.out.println(monatsGehalt);
}
}

View File

@@ -1,16 +0,0 @@
class Arbeiter extends Mitarbeiter{
double stundenSatz;
Arbeiter(int personalNummer, String name, String vorname, double stundenSatz) {
super(personalNummer, name, vorname);
this.stundenSatz = stundenSatz;
}
void print(){
System.out.println(getPersonalNummer());
System.out.println(getName());
System.out.println(getVorname());
System.out.println(stundenSatz);
}
}

View File

@@ -1,6 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
Manager person = new Manager(1, "Wurst", "Hans", 100, 1000);
person.print();
}
}

View File

@@ -1,17 +0,0 @@
final class Facharbeiter extends Arbeiter{
String fachRichtung;
Facharbeiter(int personalNummer, String name, String vorname, double stundensatz, String fachRichtung) {
super(personalNummer, name, vorname, stundensatz);
this.fachRichtung = fachRichtung;
}
void print(){
System.out.println(getPersonalNummer());
System.out.println(getName());
System.out.println(getVorname());
System.out.println(stundenSatz);
System.out.println(fachRichtung);
}
}

View File

@@ -1,13 +0,0 @@
public class LeitenderAngestellter extends Angestellter {
double bonus;
LeitenderAngestellter(int personalNummer, String name, String vorname, double monatsGehalt, double bonus) {
super(personalNummer, name, vorname, monatsGehalt);
this.bonus = bonus;
}
void print() {
super.print();
System.out.println("Bonus: " + bonus);
}
}

View File

@@ -1,25 +0,0 @@
class Manager extends LeitenderAngestellter {
Dienstwagen dienstwagen;
Manager(int personalNummer, String name, String vorname, double monatsGehalt, double bonus){
super(personalNummer, name, vorname, monatsGehalt, bonus);
}
void print() {
super.print();
if(dienstwagen != null) {
dienstwagen.print();
}
}
}
class Dienstwagen {
String kennzeichen;
String wagentyp;
void print() {
System.out.println("Kennzeichen: " + kennzeichen);
System.out.println("Wagentyp: " + wagentyp);
}
}

View File

@@ -1,20 +0,0 @@
abstract class Mitarbeiter{
int personalNummer;
String name;
String vorname;
public Mitarbeiter(int personalNummer, String name, String vorname) {
this.personalNummer = personalNummer;
this.name = name;
this.vorname = vorname;
}
abstract void print();
void setPersonalNummer(int i){personalNummer=i;}
void setName(String s){name=s;}
void setVorname(String s){vorname=s;}
int getPersonalNummer(){return personalNummer;}
String getName(){return name;}
String getVorname(){return vorname;}
}

View File

@@ -1,35 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
Matrix matrix1 = new Matrix(3,1);
matrix1.setValue(1,1,5);
matrix1.setValue(2,1,4);
matrix1.setValue(3,1,8);
Matrix matrix2 = new Matrix(3,1);
matrix2.setValue(1,1,9);
matrix2.setValue(2,1,3);
matrix2.setValue(3,1,4);
System.out.println("Matrix 1:");
matrix1.print();
System.out.println("Matrix 2:");
matrix2.print();
System.out.println("Matrix 1 nach Multiplikation mit 5:");
matrix1.multiply(5);
matrix1.print();
System.out.println("Matrix 1 nach Addition mit Matrix 2:");
matrix1.add(matrix2);
matrix1.print();
System.out.println("Matrix 2 nach Multiplikation mit Matrix 1:");
try {
matrix2.multiply(matrix2);
matrix2.print();
} catch(MatrixException e) {
System.out.println("Fehler beim Multiplizieren zweier Matrizen: " + e.getMessage());
}
}
}

View File

@@ -1,84 +0,0 @@
public class Matrix {
private int rows = 0;
private int columns = 0;
private int[][] matrix;
Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.init();
}
private void init() {
this.matrix = new int[this.rows][this.columns];
for(int r = 0; r < this.rows; r++) {
for(int c = 0; c < this.columns; c++) {
this.setValue(r+1, c+1, 0);
}
}
}
int getValue(int row, int column) {
return this.matrix[row-1][column-1];
}
int getNumberOfColumns() {
return this.columns;
}
int getNumberOfRows() {
return this.rows;
}
void setValue(int row, int column, int value) {
this.matrix[row-1][column-1] = value;
}
void multiply(int scalar) {
for(int r = 0; r < this.rows; r++) {
for(int c = 0; c < this.columns; c++) {
this.matrix[r][c] *= scalar;
}
}
}
void multiply(Matrix matrix) {
if(isSameSize(matrix)) {
for(int r = 0; r < this.rows; r++) {
for(int c = 0; c < this.columns; c++) {
this.matrix[r][c] *= matrix.getValue(r+1, c+1);
}
}
} else {
throw new MatrixException();
}
}
boolean isSameSize(Matrix matrix) {
return this.getNumberOfColumns() == matrix.getNumberOfColumns() && this.getNumberOfRows() == matrix.getNumberOfRows();
}
void add(Matrix matrix) {
if(this.getNumberOfColumns() == matrix.getNumberOfColumns() && this.getNumberOfRows() == matrix.getNumberOfRows()) {
for(int r = 0; r < this.rows; r++) {
for(int c = 0; c < this.columns; c++) {
this.matrix[r][c] += matrix.getValue(r+1, c+1);
}
}
}
}
void print() {
for(int r = 0; r < this.rows; r++) {
for(int c = 0; c < this.columns; c++) {
System.out.printf("%5d", this.getValue(r+1, c+1));
}
System.out.print("\n");
}
}
}
class MatrixException extends RuntimeException {
}

View File

@@ -1,5 +0,0 @@
class Aufgabe {
public static void main(String[] args) {
// Code
}
}

View File

@@ -0,0 +1,12 @@
package VL03.Aufgabe01;
/**
* Vorlesung 3 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
System.out.println("Hallo Welt");
}
}

View File

@@ -1,16 +1,29 @@
class Aufgabe {
package VL03.Aufgabe02;
/**
* Vorlesung 3 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
int a1 = 2;
int a2 = 4;
int a3 = 6;
System.out.printf("a1: %d, a2: %d, a3: %d\n\n", a1, a2, a3);
System.out.println("a.) Arithmetisches Mittel: " + ((a1 + a2 + a3) / 3));
System.out.println("b.) Ist " + a1 + " < " + a2 + " < " + a3 + "? " + (a1 < a2 && a2 < a3 ? "Ja" : "Nein"));
System.out.println("c.) Ist " + a1 + " ein ganzzahliges Vielfaches von " + a2 + "? " + (a1 % a2 == 0 ? "Ja" : "Nein"));
System.out.println("c.) Ist " + a1 + " ein Vielfaches von " + a2 + "? " + (a1 % a2 == 0 ? "Ja" : "Nein"));
System.out.println("d.) " + a3 + " invertiert: " + (~a3));
a3 = Integer.MAX_VALUE;
System.out.println("e.) Größter positiver Integer-Wert: " + (a3));
System.out.println("f.) Größter postiver Integer-Wert + 1: " + (a3 + 1));
a2 = 0xFFFFFFFF;
a2 = ~(a3 << a2);
System.out.println("g.) a2 (0xFFFFFFFF) nach a3 umwandeln: " + a2);
}
}

View File

@@ -0,0 +1,30 @@
package VL03.Aufgabe03;
/**
* Vorlesung 3 / Aufgabe 3
*
* @author Sebastian Brosch
*/
public class Aufgabe03 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
System.out.printf("a: %d, b: %d, c: %d\n", a, b, c);
System.out.println("Werte entsprechen dem Satz des Pythagoras: " + (check(a, b, c) ? "Ja" : "Nein"));
}
/**
* Method to check three numbers against the pythagorean theorem.
*
* @param a The first number (a in formula).
* @param b The second number (b in formula).
* @param c The result of the theorem (c in formula).
* @return The state whether the three numbers are matching the pythagorean
* theorem.
*/
public static boolean check(int a, int b, int c) {
return (a * a + b * b == c * c) ? true : false;
}
}

View File

@@ -0,0 +1,22 @@
package VL03.Aufgabe04;
/**
* Vorlesung 3 / Aufgabe 4
*
* @author Sebastian Brosch
*/
public class Aufgabe04 {
public static void main(String[] args) {
int i = 5;
int j = 3;
boolean b = false;
System.out.println("a.) " + (!((i < j) && b)));
System.out.println("b.) " + (i / j));
System.out.println("c.) " + (float) (i / j));
System.out.println("d.) " + ((float) i / j));
System.out.println("e.) " + ((float) i / (float) j));
System.out.println("f.) " + (((i++ == 5) || (--i == 5))));
System.out.println("g.) " + (((i++ == 5) | (--i == 5))));
}
}

View File

@@ -0,0 +1,26 @@
package VL04.Aufgabe01;
/**
* Vorlesung 4 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
final int MAXIMUM = 25;
System.out.printf("\nGerade Zahlen zwischen 1 und %d:\n", MAXIMUM);
for (int i = 1; i <= MAXIMUM; i++) {
if (i % 2 == 0)
System.out.printf("%d\n", i);
}
System.out.printf("\nUngerade Zahlen zwischen 1 und %d:\n", MAXIMUM);
for (int j = 1; j <= MAXIMUM; j++) {
if (j % 2 == 1)
System.out.printf("%d\n", j);
}
}
}

View File

@@ -0,0 +1,43 @@
package VL04.Aufgabe02;
/**
* Vorlesung 4 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
final int START = 1;
final int END = 20;
int sum = 0;
// create sum using for loop.
for (int i = START; i <= END; i++) {
sum += i;
}
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (for)", START, END, sum);
// create sum using while loop.
int j = START;
sum = 0;
while (j <= END) {
sum += j;
j++;
}
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (while)", START, END, sum);
// create sum using do while loop.
int k = START;
sum = 0;
do {
sum += k;
k++;
} while (k <= END);
System.out.printf("\nSumme aller Zahlen zwischen %d und %d: %d (do while)", START, END, sum);
}
}

View File

@@ -0,0 +1,40 @@
package VL04.Aufgabe03;
/**
* Vorlesung 4 / Aufgabe 3
*
* @author Sebastian Brosch
*/
public class Aufgabe03 {
public static void main(String[] args) {
final int MAXIMUM = 50;
int[] numbers = new int[MAXIMUM];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}
for (int i = 0; i <= (numbers.length / 2); i++) {
int base = numbers[i];
if (base < 2) {
numbers[i] = 0;
continue;
}
for (int j = i + 1; j < numbers.length; j++) {
int number = numbers[j];
if (number % base == 0) {
numbers[j] = 0;
}
}
}
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > 0) {
System.out.println(numbers[i]);
}
}
}
}

View File

@@ -0,0 +1,37 @@
package VL04.Aufgabe04;
/**
* Vorlesung 4 / Aufgabe 4
*
* @author Sebastian Brosch
*/
public class Aufgabe04 {
public static void main(String[] args) {
final int laufzeit = 4;
final double startwert = 100.0;
final double[] zinssaetze = new double[] { 2.0, 2.5, 3.0 };
double entwicklung[][] = new double[laufzeit][zinssaetze.length];
for (int i = 0; i < laufzeit; i++) {
for (int z = 0; z < zinssaetze.length; z++) {
double wert = (i == 0) ? startwert : entwicklung[i - 1][z];
entwicklung[i][z] = wert + (wert * (zinssaetze[z] / 100.0));
}
}
System.out.printf("Laufzeit: %d Jahre - Startwert: %.2f EUR\n\n", laufzeit, startwert);
System.out.printf("Jahr");
for (int z = 0; z < zinssaetze.length; z++) {
System.out.printf("\t%11.2f%%", zinssaetze[z]);
}
for (int i = 0; i < entwicklung.length; i++) {
System.out.printf("\n%d", (2023 + 1) + i);
for (int z = 0; z < zinssaetze.length; z++) {
System.out.printf("\t%8.2f EUR", entwicklung[i][z]);
}
}
}
}

View File

@@ -0,0 +1,54 @@
package VL04.Aufgabe05;
/**
* Vorlesung 4 / Aufgabe 5
*
* @author Sebastian Brosch
*/
public class Aufgabe05 {
public static void main(String[] args) {
final int DIR_LEFT = 0;
final int DIR_RIGHT = 1;
final int NUM_FROGS = 100;
int[] numbersrow = new int[1000];
int[] bucket = new int[] { 0, 0 };
int position = 1;
int position_max = position;
for (int i = 0; i < numbersrow.length; i++) {
numbersrow[i] = DIR_RIGHT;
}
for (int f = NUM_FROGS; f > 0; f--) {
position = 1;
while (position > 0) {
numbersrow[position] = (numbersrow[position] == DIR_RIGHT) ? DIR_LEFT : DIR_RIGHT;
if (numbersrow[position] == DIR_RIGHT) {
position++;
if (position_max < position) {
position_max = position;
}
} else {
switch (position) {
case 1:
bucket[0]++;
break;
case 2:
bucket[1]++;
break;
}
position -= 2;
}
}
}
System.out.printf("Linker Eimer: %d Frösche\n", bucket[0]);
System.out.printf("Rechter Eimer: %d Frösche\n", bucket[1]);
System.out.printf("Maximale Position auf der Zahlengerade: %d\n", position_max);
}
}

View File

@@ -0,0 +1,129 @@
package VL04.Aufgabe06;
/**
* Vorlesung 4 / Aufgabe 6
*
* @author Sebastian Brosch
*/
public class Aufgabe06 {
public static void main(String[] args) {
enum Months {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
String quarter = "";
Months month = Months.October;
System.out.printf("Monat: %s\n\n", month);
switch (month) {
case January:
quarter = "1. Quartal";
break;
case February:
quarter = "1. Quartal";
break;
case March:
quarter = "1. Quartal";
break;
case April:
quarter = "2. Quartal";
break;
case May:
quarter = "2. Quartal";
break;
case June:
quarter = "2. Quartal";
break;
case July:
quarter = "3. Quartal";
break;
case August:
quarter = "3. Quartal";
break;
case September:
quarter = "3. Quartal";
break;
case October:
quarter = "4. Quartal";
break;
case November:
quarter = "4. Quartal";
break;
case December:
quarter = "4. Quartal";
break;
default:
quarter = "";
}
System.out.printf("a.) ursprüngliche switch-Anweisung: %s\n", quarter);
switch (month) {
case January, February, March:
quarter = "1. Quartal";
break;
case April, May, June:
quarter = "2. Quartal";
break;
case July, August, September:
quarter = "3. Quartal";
break;
case October, November, December:
quarter = "4. Quartal";
break;
default:
quarter = "";
}
System.out.printf("b.) Zusammenfassung mehrerer Ausdrücke: %s\n", quarter);
switch (month) {
case January -> quarter = "1. Quartal";
case February -> quarter = "1. Quartal";
case March -> quarter = "1. Quartal";
case April -> quarter = "2. Quartal";
case May -> quarter = "2. Quartal";
case June -> quarter = "2. Quartal";
case July -> quarter = "3. Quartal";
case August -> quarter = "3. Quartal";
case September -> quarter = "3. Quartal";
case October -> quarter = "4. Quartal";
case November -> quarter = "4. Quartal";
case December -> quarter = "4. Quartal";
default -> quarter = "";
}
System.out.printf("c.) Pfeilnotation: %s\n", quarter);
switch (month) {
case January, February, March -> quarter = "1. Quartal";
case April, May, June -> quarter = "2. Quartal";
case July, August, September -> quarter = "3. Quartal";
case October, November, December -> quarter = "4. Quartal";
default -> quarter = "";
}
System.out.printf("d.) Zusammenfassung mehrerer Ausdrücke in Pfeilnotation: %s\n", quarter);
quarter = switch (month) {
case January, February, March -> "1. Quartal";
case April, May, June -> "2. Quartal";
case July, August, September -> "3. Quartal";
case October, November, December -> "4. Quartal";
default -> "";
};
System.out.printf("e.) switch-Ausdruck mit Pfeilnotation: %s\n", quarter);
}
}

View File

@@ -0,0 +1,57 @@
package VL05.Aufgabe01;
/**
* Vorlesung 5 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
final int MAX_NUMBER = 45;
// get all fibonacci numbers using a iterative solution.
for (int i = 0; i < MAX_NUMBER; i++) {
System.out.printf("Fibonacci Nr. %d: %d\n", (i + 1), iterativeFibonacci(i));
}
// get all fibonacci numbers using a recursive solution.
for (int j = 0; j < MAX_NUMBER; j++) {
System.out.printf("Fibonacci Nr. %d: %d\n", (j + 1), recursiveFibonacci(j));
}
}
/**
* Method to get a specific fibonacci number using a iterative solution.
*
* @param nth The number of the fibonacci number.
* @return The fibonacci number.
*/
private static int iterativeFibonacci(int nth) {
int[] fib = new int[] { 1, 1 };
for (int i = 0; i <= nth; i++) {
if (i < fib.length)
continue;
int result = fib[0] + fib[1];
fib[0] = fib[1];
fib[1] = result;
}
return fib[1];
}
/**
* Method to get a specific fibonacci number using a recursive number.
*
* @param nth The number of the fibonacci number.
* @return The fibonacci number.
*/
private static int recursiveFibonacci(int nth) {
if (nth == 0 || nth == 1) {
return 1;
} else {
return recursiveFibonacci(nth - 2) + recursiveFibonacci(nth - 1);
}
}
}

View File

@@ -0,0 +1,128 @@
package VL05.Aufgabe02;
/**
* Vorlesung 5 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
final int DAMEN = 8;
int chessboard[][] = new int[DAMEN][DAMEN];
queens(chessboard, 1);
}
/**
* Method to place a queen on the chessboard.
*
* @param chessboard The chessboard to place the queen.
* @param q The number of the queen to be placed.
*/
private static void queens(int[][] chessboard, int q) {
if (q > chessboard.length) {
print(chessboard);
return;
} else {
for (int i = 0; i < chessboard.length; i++) {
if (isQueenPossible(chessboard, q - 1, i)) {
chessboard[q - 1][i] = 1;
queens(chessboard, q + 1);
chessboard[q - 1][i] = 0;
}
}
}
}
/**
* Method to check whether a queen can be placed on the field.
*
* @param chessboard The chessboard to place the queen.
* @param row The row to place the queen.
* @param column The column to place the queen.
* @return The state whether the queen can be placed on the field.
*/
private static boolean isQueenPossible(int[][] chessboard, int row, int column) {
int currentRow = 0;
int currentColumn = 0;
// check whether the queen can be placed in current row.
for (int c = 0; c < chessboard[row].length; c++) {
if (chessboard[row][c] == 1)
return false;
}
// check whether the queen can be placed in current column.
for (int r = 0; r < chessboard.length; r++) {
if (chessboard[r][column] == 1)
return false;
}
// check whether the queen can be placed diagonal (top left).
currentRow = row;
currentColumn = column;
while (currentColumn >= 0 && currentRow >= 0) {
if (chessboard[currentRow][currentColumn] == 1)
return false;
currentRow--;
currentColumn--;
}
// check whether the queen can be placed diagonal (bottom right).
currentRow = row;
currentColumn = column;
while (currentColumn < chessboard[row].length && currentRow < chessboard.length) {
if (chessboard[currentRow][currentColumn] == 1)
return false;
currentRow++;
currentColumn++;
}
// check whether the queen can be placed diagonal (bottom left).
currentRow = row;
currentColumn = column;
while (currentColumn >= 0 && currentRow < chessboard.length) {
if (chessboard[currentRow][currentColumn] == 1)
return false;
currentRow++;
currentColumn--;
}
// check whether the queen can be placed diagonal (top right).
currentRow = row;
currentColumn = column;
while (currentColumn < chessboard[row].length && currentRow >= 0) {
if (chessboard[currentRow][currentColumn] == 1)
return false;
currentRow--;
currentColumn++;
}
return true;
}
/**
* Method to print a chessboard.
*
* @param chessboard The chessboard to be printed.
*/
private static void print(int[][] chessboard) {
System.out.println("\nChessboard:\n");
for (int r = 0; r < chessboard.length; r++) {
for (int c = 0; c < chessboard[r].length; c++) {
System.out.printf("%d", chessboard[r][c]);
}
System.out.printf("\n");
}
}
}

View File

@@ -0,0 +1,22 @@
package VL05.Aufgabe03;
/**
* Vorlesung 5 / Aufgabe 3
*
* @author Sebastian Brosch
*/
public class Aufgabe03 {
public static void main(String[] args) {
loop(1);
}
/**
* Method to create a recursive infinite loop.
*
* @param counter The counter to measure the depth of the recursion.
*/
private static void loop(int counter) {
System.out.println(counter);
loop(++counter);
}
}

6
VL05/Aufgabe03/run.ps1 Normal file
View File

@@ -0,0 +1,6 @@
Set-Location ../../
javac ./VL05/Aufgabe03/Aufgabe03.java
java VL05.Aufgabe03.Aufgabe03 > VL05/Aufgabe03/output/output.txt
Set-Location ./VL05/Aufgabe03/

View File

@@ -0,0 +1,15 @@
package VL06.Aufgabe01;
class Angestellter extends Mitarbeiter {
protected double monatsGehalt;
Angestellter(int personalNummer, String name, String vorname, double monatsGehalt) {
super(personalNummer, name, vorname);
this.monatsGehalt = monatsGehalt;
}
void print() {
System.out.printf("\n%s, %s (%d):\n", this.getName(), this.getVorname(), this.getPersonalNummer());
System.out.printf(" Monatsgehalt: %.2f Euro\n", this.monatsGehalt);
}
}

View File

@@ -0,0 +1,15 @@
package VL06.Aufgabe01;
class Arbeiter extends Mitarbeiter {
protected double stundenSatz;
Arbeiter(int personalNummer, String name, String vorname, double stundenSatz) {
super(personalNummer, name, vorname);
this.stundenSatz = stundenSatz;
}
void print() {
System.out.printf("\n%s, %s (%d):\n", this.getName(), this.getVorname(), this.getPersonalNummer());
System.out.printf(" Stundensatz: %.2f Euro\n", this.stundenSatz);
}
}

View File

@@ -0,0 +1,17 @@
package VL06.Aufgabe01;
/**
* Vorlesung 6 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
Arbeiter frankMeier = new Arbeiter(4711, "Meier", "Frank", 14.67);
frankMeier.print();
Facharbeiter steffiMueller = new Facharbeiter(4712, "Müller", "Steffi", 18.33, "Chemie");
steffiMueller.print();
Manager karlHeinzKaiser = new Manager(4713, "Kaiser", "Karl-Heinz", 3000.00, 400.00, "Mazda Cabrio", "S-ZZ 999");
karlHeinzKaiser.print();
}
}

View File

@@ -0,0 +1,17 @@
package VL06.Aufgabe01;
public class Dienstwagen {
private String wagentyp;
private String kennzeichen;
Dienstwagen(String wagentyp, String kennzeichen) {
this.wagentyp = wagentyp;
this.kennzeichen = kennzeichen;
}
public void print() {
System.out.println("Dienstwagen:");
System.out.printf(" Wagentyp: %s\n", this.wagentyp);
System.out.printf(" Kennzeichen: %s\n", this.kennzeichen);
}
}

View File

@@ -0,0 +1,16 @@
package VL06.Aufgabe01;
final class Facharbeiter extends Arbeiter {
protected String fachRichtung;
Facharbeiter(int personalNummer, String name, String vorname, double stundenSatz, String fachRichtung) {
super(personalNummer, name, vorname, stundenSatz);
this.fachRichtung = fachRichtung;
}
void print() {
System.out.printf("\n%s, %s (%d):\n", this.getName(), this.getVorname(), this.getPersonalNummer());
System.out.printf(" Stundensatz: %.2f Euro\n", this.stundenSatz);
System.out.printf(" Fachrichtung: %s\n", this.fachRichtung);
}
}

View File

@@ -0,0 +1,16 @@
package VL06.Aufgabe01;
public class LeitenderAngestellter extends Angestellter {
protected double bonus;
LeitenderAngestellter(int personalNummer, String name, String vorname, double monatsGehalt, double bonus) {
super(personalNummer, name, vorname, monatsGehalt);
this.bonus = bonus;
}
void print() {
System.out.printf("\n%s, %s (%d):\n", this.getName(), this.getVorname(), this.getPersonalNummer());
System.out.printf(" Monatsgehalt: %.2f Euro\n", this.monatsGehalt);
System.out.printf(" Bonus: %.2f Euro\n", this.bonus);
}
}

View File

@@ -0,0 +1,17 @@
package VL06.Aufgabe01;
public class Manager extends LeitenderAngestellter {
private Dienstwagen dienstwagen;
Manager(int personalNummer, String name, String vorname, double monatsGehalt, double bonus, String wagentyp, String kennzeichen) {
super(personalNummer, name, vorname, monatsGehalt, bonus);
this.dienstwagen = new Dienstwagen(wagentyp, kennzeichen);
}
void print() {
System.out.printf("\n%s, %s (%d):\n", this.getName(), this.getVorname(), this.getPersonalNummer());
System.out.printf(" Monatsgehalt: %.2f Euro\n", this.monatsGehalt);
System.out.printf(" Bonus: %.2f Euro\n", this.bonus);
this.dienstwagen.print();
}
}

View File

@@ -0,0 +1,39 @@
package VL06.Aufgabe01;
abstract class Mitarbeiter {
protected int personalNummer;
protected String name;
protected String vorname;
Mitarbeiter(int personalNummer, String name, String vorname) {
this.personalNummer = personalNummer;
this.name = name;
this.vorname = vorname;
}
abstract void print();
public void setPersonalNummer(int personalNummer) {
this.personalNummer = personalNummer;
}
public void setName(String name) {
this.name = name;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public int getPersonalNummer() {
return this.personalNummer;
}
public String getName() {
return this.name;
}
public String getVorname() {
return this.vorname;
}
}

View File

@@ -0,0 +1,40 @@
package VL06.Aufgabe02;
/**
* Vorlesung 6 / Aufgabe 2
*
* @author Sebastian Brosch
*/
class Aufgabe02 {
public static void main(String[] args) {
Matrix matrix1 = new Matrix(3, 2);
matrix1.setValue(0, 0, 5);
matrix1.setValue(1, 0, 4);
matrix1.setValue(2, 0, 8);
matrix1.setValue(0, 1, 7);
matrix1.setValue(1, 1, 3);
matrix1.setValue(2, 1, 9);
Matrix matrix2 = new Matrix(3, 2);
matrix2.setValue(0, 0, 9);
matrix2.setValue(1, 0, 3);
matrix2.setValue(2, 0, 4);
matrix2.setValue(0, 1, 8);
matrix2.setValue(1, 1, 6);
matrix2.setValue(2, 1, 3);
System.out.println("\nMatrix 1:");
matrix1.print();
System.out.println("\nMatrix 2:");
matrix2.print();
System.out.println("\nMatrix 1 nach Multiplikation mit 5:");
matrix1.multiply(5);
matrix1.print();
System.out.println("\nMatrix 1 nach Addition mit Matrix 2:");
matrix1.add(matrix2);
matrix1.print();
}
}

115
VL06/Aufgabe02/Matrix.java Normal file
View File

@@ -0,0 +1,115 @@
package VL06.Aufgabe02;
/**
* A class to represent a Matrix.
*/
public class Matrix {
private int rows = 0;
private int columns = 0;
private int[][] matrix;
Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.matrix = new int[this.rows][this.columns];
// initialize all fields with zero.
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] = 0;
}
}
}
/**
* Method to get the number of columns.
*
* @return The number of columns.
*/
int getColumns() {
return this.columns;
}
/**
* Method to get the number of rows.
*
* @return The number of rows.
*/
int getRows() {
return this.rows;
}
/**
* Method to get the value of a specific field.
*
* @param row The row of the value.
* @param column The column of the value.
* @return The value of the given row and column.
*/
int getValue(int row, int column) {
return this.matrix[row][column];
}
/**
* Method to set the value of a specific field.
*
* @param row The row to set the value.
* @param column The column to set the value.
* @param value The value to be set in the field.
*/
void setValue(int row, int column, int value) {
this.matrix[row][column] = value;
}
/**
* Methode to get the state whether the given Matrix is same size.
*
* @param matrix The Matrix to check for same size.
* @return The state whether the given Matrix is same size.
*/
boolean isSameSize(Matrix matrix) {
return (this.rows == matrix.getRows()) && (this.columns == matrix.getColumns());
}
/**
* Method to add a Matrix to the current Matrix.
*
* @param matrix The Matrix to add to the current Matrix.
*/
void add(Matrix matrix) {
if (!this.isSameSize(matrix)) {
return;
}
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] += matrix.getValue(rowIndex, columnIndex);
}
}
}
/**
* Method to multiply a scalar value to the Matrix.
*
* @param scalar The scalar value to multiply the Matrix.
*/
void multiply(int scalar) {
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] *= scalar;
}
}
}
/**
* Method to print the Matrix.
*/
void print() {
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
System.out.printf("%5d", this.getValue(rowIndex, columnIndex));
}
System.out.print("\n");
}
}
}

View File

@@ -0,0 +1,38 @@
package VL08.Aufgabe03;
/**
* Vorlesung 8 / Aufgabe 3
*
* @author Sebastian Brosch
*/
public class Aufgabe03 {
public static void main(String[] args) {
Matrix matrixA = new Matrix(2, 3);
matrixA.setValue(0, 0, 3);
matrixA.setValue(0, 1, 2);
matrixA.setValue(0, 2, 1);
matrixA.setValue(1, 0, 1);
matrixA.setValue(1, 1, 0);
matrixA.setValue(1, 2, 2);
Matrix matrixB = new Matrix(3, 2);
matrixB.setValue(0, 0, 1);
matrixB.setValue(0, 1, 2);
matrixB.setValue(1, 0, 0);
matrixB.setValue(1, 1, 1);
matrixB.setValue(2, 0, 4);
matrixB.setValue(2, 1, 0);
System.out.println("------------------");
matrixA.print();
System.out.println("------------------");
matrixB.print();
System.out.println("------------------");
matrixA.multiply(matrixB);
matrixA.print();
System.out.println("------------------");
}
}

154
VL08/Aufgabe03/Matrix.java Normal file
View File

@@ -0,0 +1,154 @@
package VL08.Aufgabe03;
/**
* A class to represent a Matrix.
*/
public class Matrix {
private int rows = 0;
private int columns = 0;
private int[][] matrix;
Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.matrix = new int[this.rows][this.columns];
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] = 0;
}
}
}
/**
* Method to get the number of columns.
*
* @return The number of columns.
*/
int getColumns() {
return this.columns;
}
/**
* Method to get the number of rows.
*
* @return The number of rows.
*/
int getRows() {
return this.rows;
}
/**
* Method to get the value of a specific field.
*
* @param row The row of the value.
* @param column The column of the value.
* @return The value of the given row and column.
*/
int getValue(int row, int column) {
return this.matrix[row][column];
}
/**
* Method to set the value of a specific field.
*
* @param row The row to set the value.
* @param column The column to set the value.
* @param value The value to be set in the field.
*/
void setValue(int row, int column, int value) {
this.matrix[row][column] = value;
}
/**
* Methode to get the state whether the given Matrix is same size.
*
* @param matrix The Matrix to check for same size.
* @return The state whether the given Matrix is same size.
*/
boolean isSameSize(Matrix matrix) {
return (this.rows == matrix.getRows()) && (this.columns == matrix.getColumns());
}
/**
* Method to add a Matrix to the current Matrix.
*
* @param matrix The Matrix to add to the current Matrix.
*/
void add(Matrix matrix) {
if (!this.isSameSize(matrix)) {
return;
}
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] += matrix.getValue(rowIndex, columnIndex);
}
}
}
/**
* Method to multiply a scalar value to the Matrix.
*
* @param scalar The scalar value to multiply the Matrix.
*/
void multiply(int scalar) {
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
this.matrix[rowIndex][columnIndex] *= scalar;
}
}
}
/**
* Method to multiply a Matrix to the Matrix.
*
* @param matrix The Matrix to multiply with this Matrix.
* @return State whether the multiplication of the Matrix was successful.
*/
boolean multiply(Matrix matrix) {
// multiplication is not possible if there are no rows or no columns.
if (this.getColumns() == 0 || this.getRows() == 0) {
return false;
}
// multiplication is only possible if the number of rows of Matrix A
// is equals to the number of columns of Matrix B.
if (this.getColumns() != matrix.getRows()) {
return false;
}
int[][] tempMatrix = new int[this.matrix.length][this.matrix.length];
for (int rowIndex = 0; rowIndex < this.getRows(); rowIndex++) {
for (int columnIndex = 0; columnIndex < matrix.getColumns(); columnIndex++) {
tempMatrix[rowIndex][columnIndex] = 0;
for (int fieldIndex = 0; fieldIndex < matrix.getRows(); fieldIndex++) {
tempMatrix[rowIndex][columnIndex] += this.getValue(rowIndex, fieldIndex)
* matrix.getValue(fieldIndex, columnIndex);
}
}
}
this.matrix = tempMatrix;
this.columns = tempMatrix.length;
this.rows = tempMatrix.length;
return true;
}
/**
* Method to print the Matrix.
*/
void print() {
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
System.out.printf("%5d", this.getValue(rowIndex, columnIndex));
}
System.out.print("\n");
}
}
}

View File

@@ -0,0 +1,30 @@
package VL08.Aufgabe04;
import VL08.Aufgabe04.PersonenDH.Person;
import VL08.Aufgabe04.PersonenDH.Student;
import VL08.Aufgabe04.PersonenDH.Studiengangsleiter;
import VL08.Aufgabe04.PersonenDH.Dozent;
import VL08.Aufgabe04.PersonenDH.Mitarbeiter;
import java.util.ArrayList;
/**
* Vorlesung 8 / Aufgabe 4
*
* @author Sebastian Brosch
*/
public class Aufgabe04 {
public static void main(String[] args) {
ArrayList<Person> personen = new ArrayList<Person>();
personen.add(new Person("Petra Mustermann", 37));
personen.add(new Student("Max Mustermann", 30, "1234567"));
personen.add(new Dozent("Eva Mustermann", 50, "Informatik"));
personen.add(new Mitarbeiter("Tim Mustermann", 35, "Programmieren"));
personen.add(new Studiengangsleiter("Kevin Mustermann", 40, "BWL", "BWL 1"));
for (Person person : personen) {
person.print();
System.out.println("-------------------------");
}
}
}

View File

@@ -0,0 +1,15 @@
package VL08.Aufgabe04.PersonenDH;
public class Dozent extends Person {
private String specialization;
public Dozent(String name, int age, String specialization) {
super(name, age);
this.specialization = specialization;
}
public void print() {
super.print();
System.out.printf("Fachrichtung: %s\n", specialization);
}
}

View File

@@ -0,0 +1,15 @@
package VL08.Aufgabe04.PersonenDH;
public class Mitarbeiter extends Person {
private String activity;
public Mitarbeiter(String name, int age, String activity) {
super(name, age);
this.activity = activity;
}
public void print() {
super.print();
System.out.printf("Tätigkeit: %s\n", this.activity);
}
}

View File

@@ -0,0 +1,16 @@
package VL08.Aufgabe04.PersonenDH;
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void print() {
System.out.printf("Name: %s\n", this.name);
System.out.printf("Alter: %d\n", this.age);
}
}

View File

@@ -0,0 +1,15 @@
package VL08.Aufgabe04.PersonenDH;
public class Student extends Person {
private String studentNumber;
public Student(String name, int age, String studentNumber) {
super(name, age);
this.studentNumber = studentNumber;
}
public void print() {
super.print();
System.out.printf("Matrikel-Nummer: %s\n", this.studentNumber);
}
}

View File

@@ -0,0 +1,15 @@
package VL08.Aufgabe04.PersonenDH;
final public class Studiengangsleiter extends Dozent {
private String course;
public Studiengangsleiter(String name, int age, String specialization, String course) {
super(name, age, specialization);
this.course = course;
}
public void print() {
super.print();
System.out.printf("Kurs: %s\n", this.course);
}
}

View File

@@ -5,29 +5,29 @@ import java.io.FileWriter;
import java.util.Random;
/**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 1
* Vorlesung 12 / Aufgabe 1
*
* @author Sebastian Brosch
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe01
*/
public class Aufgabe01 {
public static void main(String[] args) {
final String COLUMN_SEPARATOR = "#";
final int START_KUNDENNUMMER = 100000;
int cntDataSets = 0;
int numberOfDatasets = 0;
do {
try {
System.out.print("Anzahl der Datensätze: ");
cntDataSets = Integer.parseInt(System.console().readLine());
numberOfDatasets = Integer.parseInt(System.console().readLine());
// only positive numbers are valid.
if(cntDataSets <= 0) {
if (numberOfDatasets <= 0) {
System.out.println("Bitte geben Sie eine gültige Anzahl ein (min. 1)!");
}
} catch(NumberFormatException e) {
} catch (NumberFormatException e) {
System.out.println("Es wurde keine gültige Anzahl Datensätze eingegeben!");
}
} while(cntDataSets <= 0);
} while (numberOfDatasets <= 0);
StringFeld namen = new StringFeld(getPathFromPackage() + "/input/Nachnamen.TXT");
StringFeld strassen = new StringFeld(getPathFromPackage() + "/input/Strassen.TXT");
@@ -41,7 +41,7 @@ public class Aufgabe01 {
final File fileOutputSeparator = new File(folderOutput + "output-separator.txt");
final File parentFolder = fileOutputFixedWidth.getParentFile();
if(null != parentFolder) {
if (null != parentFolder) {
parentFolder.mkdirs();
}
@@ -49,7 +49,7 @@ public class Aufgabe01 {
FileWriter outputFixedWidth = new FileWriter(fileOutputFixedWidth);
FileWriter outputSeparator = new FileWriter(fileOutputSeparator);
for(int i = 0; i < cntDataSets; i++) {
for (int i = 0; i < numberOfDatasets; i++) {
int kundennummer = START_KUNDENNUMMER + i;
int hausnummer = getRandomNumber(1, 99999);
int umsatz = getRandomNumber(500, 2000);
@@ -77,23 +77,27 @@ public class Aufgabe01 {
outputFixedWidth.close();
outputSeparator.close();
} catch(Throwable e) {;}
} catch (Throwable e) {
;
}
System.out.println("Die Testdaten (" + cntDataSets + " Zeilen) wurden erzeugt.");
System.out.println("Die Testdaten (" + numberOfDatasets + " Zeilen) wurden erzeugt.");
}
/**
* Method to determine a random number from a certain range.
*
* @param start The first number of the range.
* @param end The last number of the range.
* @param end The last number of the range.
* @return A random number from a certain range.
*/
private static int getRandomNumber(int start, int end) {
return (new Random()).nextInt(end-start) + start;
return (new Random()).nextInt(end - start) + start;
}
/**
* Method to get the relative path based on the package name.
*
* @return The relative path of the package based on the package name.
*/
private static String getPathFromPackage() {

View File

@@ -8,6 +8,7 @@ import java.util.List;
/**
* A field to store some information.
*
* @author Sebastian Brosch
*/
class StringFeld {
@@ -23,13 +24,14 @@ class StringFeld {
List<String> lines = getFileLines(filePath);
this.values = new String[lines.size()];
for(int i = 0; i < lines.size(); i++) {
for (int i = 0; i < lines.size(); i++) {
this.values[i] = lines.get(i).split(separator)[index];
}
}
/**
* Method to get a value.
*
* @param index The index of the value.
* @return The value of the given index.
*/
@@ -40,14 +42,15 @@ class StringFeld {
/**
* Method to get a value.
* @param index The index of the value.
*
* @param index The index of the value.
* @param maxLength The max length of the value.
* @return The value of the given index.
*/
public String get(int index, int maxLength) {
index = index % this.values.length;
if(this.values[index].length() > maxLength) {
if (this.values[index].length() > maxLength) {
return this.values[index].substring(0, maxLength);
} else {
return this.values[index];
@@ -56,6 +59,7 @@ class StringFeld {
/**
* Method to get all the lines of a file.
*
* @param filePath The path to the file to get the lines from.
* @return A list with all lines of the file.
*/

View File

@@ -1,5 +0,0 @@
100005Meier Frank Hauptstr. 9374272160Horb 728
100006Kaiser Steffi Dorfstr. 1743970179Stuttgart 1914
100007Hellmann Uschi Rosenweg 1318270191Stuttgart 593
100008Fuchs Carola Nelkenweg 3436270192Stuttgart 891
100009Hampel Susanne Stuttgarter Str. 2115270193Stuttgart 926

View File

@@ -1,5 +0,0 @@
100005#Meier#Frank#Hauptstr.#93742#72160#Horb#728
100006#Kaiser#Steffi#Dorfstr.#17439#70179#Stuttgart#1914
100007#Hellmann#Uschi#Rosenweg#13182#70191#Stuttgart#593
100008#Fuchs#Carola#Nelkenweg#34362#70192#Stuttgart#891
100009#Hampel#Susanne#Stuttgarter Str.#21152#70193#Stuttgart#926

View File

@@ -7,42 +7,46 @@ import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Vorlesung 12 - Ein- und Ausgabe - Aufgabe 2
* Vorlesung 12 / Aufgabe 2
*
* @author Sebastian Brosch
* @see https://gitea.hb.dhbw-stuttgart.de/sebastianbrosch/VL-Programmieren/src/branch/main/VL12/Aufgabe02
*/
public class Aufgabe02 {
public static void main(String[] args) {
try {
int words = 0;
int numbers = 0;
int numberOfWords = 0;
int numberOfNumbers = 0;
StringReader stringReader = new StringReader(getInputString(getPathFromPackage() + "/input/input.txt"));
StreamTokenizer streamTokenizer = new StreamTokenizer(stringReader);
while(streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch(streamTokenizer.ttype) {
while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
switch (streamTokenizer.ttype) {
case StreamTokenizer.TT_WORD:
words++;
numberOfWords++;
System.out.printf("ZEICHENKETTE:\t%s\n", streamTokenizer.sval);
break;
case StreamTokenizer.TT_NUMBER:
numbers++;
numberOfNumbers++;
System.out.printf("ZAHL:\t\t%.0f\n", streamTokenizer.nval);
break;
}
}
System.out.printf("Gelesene Zahlen: %d\n", numbers);
System.out.printf("Gelesene Zeichenketten: %d\n", words);
System.out.printf("Insgesamt gelesene Token: %d\n", (numbers + words));
} catch (Throwable e) {;}
System.out.printf("Gelesene Zahlen: %d\n", numberOfNumbers);
System.out.printf("Gelesene Zeichenketten: %d\n", numberOfWords);
System.out.printf("Insgesamt gelesene Token: %d\n", (numberOfNumbers + numberOfWords));
} catch (Throwable e) {
;
}
}
/**
* Method to get the content of a file as a string.
*
* @param inputPath The path of the file.
* @return The content of the file or an empty string if the file is not available.
* @return The content of the file or an empty string if the file is not
* available.
*/
private static String getInputString(String inputPath) {
try {
@@ -55,6 +59,7 @@ public class Aufgabe02 {
/**
* Method to get the relative path based on the package name.
*
* @return The relative path of the package based on the package name.
*/
private static String getPathFromPackage() {

View File

@@ -0,0 +1,79 @@
package VL13.Aufgabe01;
import java.util.Stack;
/**
* Vorlesung 13 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
// Integer Stack
System.out.printf("\n%s:\n", "Integer Stack");
IntegerStack integerStack = new IntegerStack();
integerStack.push(7);
integerStack.push(3);
integerStack.push(8);
integerStack.push(2);
System.out.println(integerStack.pop());
System.out.println(integerStack.pop());
integerStack.push(7);
System.out.println(integerStack.pop());
System.out.println(integerStack.pop());
// Generic Stack (Integer)
System.out.printf("\n%s:\n", "Generic Stack (Integer)");
GenericStack<Integer> gIntegerStack = new GenericStack<Integer>();
gIntegerStack.push(7);
gIntegerStack.push(3);
gIntegerStack.push(8);
gIntegerStack.push(2);
System.out.println(gIntegerStack.pop());
System.out.println(gIntegerStack.pop());
gIntegerStack.push(7);
System.out.println(gIntegerStack.pop());
System.out.println(gIntegerStack.pop());
// Generic Stack (String)
System.out.printf("\n%s:\n", "Generic Stack (String)");
GenericStack<String> gStringStack = new GenericStack<String>();
gStringStack.push("Hallo");
gStringStack.push("Welt");
gStringStack.push("!");
System.out.println(gStringStack.pop());
System.out.println(gStringStack.pop());
System.out.println(gStringStack.pop());
// Stack (Integer)
System.out.printf("\n%s:\n", "Stack (Integer)");
Stack<Integer> nIntegerStack = new Stack<Integer>();
nIntegerStack.push(7);
nIntegerStack.push(3);
nIntegerStack.push(8);
nIntegerStack.push(2);
System.out.println(nIntegerStack.pop());
System.out.println(nIntegerStack.pop());
nIntegerStack.push(7);
System.out.println(nIntegerStack.pop());
System.out.println(nIntegerStack.pop());
// Stack (String)
System.out.printf("\n%s:\n", "Stack (String)");
Stack<String> stringStack = new Stack<String>();
stringStack.push("Hallo");
stringStack.push("Welt");
stringStack.push("!");
System.out.println(stringStack.pop());
System.out.println(stringStack.pop());
System.out.println(stringStack.pop());
}
}

View File

@@ -0,0 +1,67 @@
package VL13.Aufgabe01;
/**
* A generic stack.
*/
public class GenericStack<T> {
StackItem<T> start;
/**
* A stack item to store a single item on the stack.
*/
private class StackItem<E> {
E value;
StackItem<E> next;
StackItem<E> prev;
StackItem(E value) {
this.value = value;
}
}
/**
* Method to push a value to the stack.
*
* @param value The value to store on the stack.
*/
public void push(T value) {
if (start == null) {
start = new StackItem<T>(value);
} else {
StackItem<T> item = start;
// get the last item of the stack.
while (item.next != null) {
item = item.next;
}
// create a new stack item to store the value.
item.next = new StackItem<T>(value);
item.next.prev = item;
}
}
/**
* Method to pop a value from the stack.
*
* @return The value from stack.
*/
public T pop() {
StackItem<T> item = start;
// get the last item of the stack.
while (item.next != null) {
item = item.next;
}
// remove last item from stack.
if (item.prev == null) {
start = null;
} else {
item.prev.next = null;
}
// return the value of the last item of the stack.
return item.value;
}
}

View File

@@ -0,0 +1,64 @@
package VL13.Aufgabe01;
/**
* A integer stack.
*/
public class IntegerStack {
StackItem start;
private class StackItem {
int value;
StackItem next;
StackItem prev;
StackItem(int value) {
this.value = value;
}
}
/**
* Method to push a value to the stack.
*
* @param value The value to store on the stack.
*/
public void push(int value) {
if (start == null) {
start = new StackItem(value);
} else {
StackItem item = start;
// get the last item of the stack.
while (item.next != null) {
item = item.next;
}
// create a new stack item to store the value.
item.next = new StackItem(value);
item.next.prev = item;
}
}
/**
* Method to pop a value from the stack.
*
* @return The value from stack.
*/
public int pop() {
StackItem item = start;
// get the last item of the stack.
while (item.next != null) {
item = item.next;
}
// remove last item from stack.
if (item.prev == null && item == start) {
start = null;
} else {
item.prev.next = null;
}
// return the value of the last item of the stack.
return item.value;
}
}

View File

@@ -0,0 +1,28 @@
package VL13.Aufgabe02;
/**
* Vorlesung 13 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) {
MyQueue<Integer> integerQueue = new MyQueue<Integer>();
// fill the queue with some values.
System.out.printf("\n%s:\n", "Aufbau der Queue");
for (int i = 0; i < 150; i++) {
integerQueue.add(i);
System.out.printf("Elemente: %d - Interne Größe: %d\n", integerQueue.size(), integerQueue.internalSize());
}
// clear the queue to check the resizing of the internal storage.
System.out.printf("\n%s:\n", "Abbau der Queue");
while (integerQueue.size() > 0) {
integerQueue.get();
System.out.printf("Elemente: %d - Interne Größe: %d\n", integerQueue.size(), integerQueue.internalSize());
}
}
}

View File

@@ -0,0 +1,64 @@
package VL13.Aufgabe02;
public class MyQueue<T> {
private Object[] myArray;
private int numberOfElements;
private int maxSize;
public MyQueue() {
this.numberOfElements = 0;
this.maxSize = 100;
myArray = new Object[this.maxSize];
}
public void add(T t) {
this.myArray[this.numberOfElements] = t;
this.numberOfElements++;
this.adjustInternalArray();
}
@SuppressWarnings("unchecked")
public T get() {
T retValue = (T) this.myArray[0];
for (int i = 0; i < this.numberOfElements; i++) {
this.myArray[i] = this.myArray[i + 1];
}
this.numberOfElements--;
this.adjustInternalArray();
return retValue;
}
public int size() {
return this.numberOfElements;
}
public int internalSize() {
return this.maxSize;
}
private void adjustInternalArray() {
final int MIN_SIZE = 20;
while (this.numberOfElements <= Math.ceil(this.maxSize * 0.2) && this.maxSize > MIN_SIZE) {
this.maxSize = Math.max(this.maxSize / 2, 20);
Object[] myOldArray = this.myArray;
this.myArray = new Object[this.maxSize];
for (int i = 0; i < this.myArray.length; i++) {
this.myArray[i] = myOldArray[i];
}
}
while (this.numberOfElements >= Math.ceil(this.maxSize * 0.8)) {
this.maxSize = this.maxSize * 2;
Object[] myOldArray = this.myArray;
this.myArray = new Object[this.maxSize];
for (int i = 0; i < myOldArray.length; i++) {
this.myArray[i] = myOldArray[i];
}
}
}
}

View File

@@ -0,0 +1,135 @@
package VL13.Aufgabe03;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
/**
* Vorlesung 13 / Aufgabe 3
*
* @author Sebastian Brosch
*/
public class Aufgabe03 {
public static void main(String[] args) {
final int MAX_RUNS = 10;
int[] sizes = new int[] { 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000,
20000000, 50000000 };
for (int size : sizes) {
long sumArrayList = 0;
long sumLinkedList = 0;
ArrayList<String> values = getValues(size);
for (int i = 0; i < MAX_RUNS; i++) {
sumArrayList += runTestArrayList(values);
sumLinkedList += runTestLinkedList(values);
}
System.out.printf("\nMittelwert (Zeichenketten: %,d):\n", size);
System.out.printf(" ArrayList: %d Millisekunden\n", (sumArrayList / MAX_RUNS));
System.out.printf(" LinkedList: %d Millisekunden\n", (sumLinkedList / MAX_RUNS));
}
System.out.println("\nAlle Tests wurden beendet.");
}
/**
* Method to run a test using a ArrayList.
*
* @param values The values to fill in ArrayList.
* @return The elapsed time of the test.
*/
private static long runTestArrayList(ArrayList<String> values) {
long timeStart = System.currentTimeMillis();
ArrayList<String> testArrayList = new ArrayList<String>();
for (String value : values) {
testArrayList.add(value);
}
return System.currentTimeMillis() - timeStart;
}
/**
* Method to run a test using LinkedList.
*
* @param values The values to fill in LinkedList.
* @return The elapsed time of the test.
*/
private static long runTestLinkedList(ArrayList<String> values) {
long timeStart = System.currentTimeMillis();
LinkedList<String> testLinkedList = new LinkedList<String>();
for (String value : values) {
testLinkedList.add(value);
}
return System.currentTimeMillis() - timeStart;
}
/**
* Method to determine a random number from a certain range.
*
* @param start The first number of the range.
* @param end The last number of the range.
* @return A random number from a certain range.
*/
private static int getRandomNumber(int start, int end) {
return (new Random()).nextInt((end - start + 1)) + start;
}
/**
* Method to generate a random string.
*
* @param min The minimum length of the random string.
* @param max The maximum length of the random string.
* @return A random string.
*/
private static String getRandomString(int min, int max) {
final String NUMBERS = "0123456789";
final String UCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final String LCHARS = UCHARS.toLowerCase();
String random = "";
int length = getRandomNumber(min, max);
for (int i = 0; i < length; i++) {
String chars = "";
switch (getRandomNumber(1, 3)) {
case 1:
chars = NUMBERS;
break;
case 2:
chars = UCHARS;
break;
case 3:
chars = LCHARS;
break;
}
int charIndex = getRandomNumber(0, chars.length() - 1);
random += chars.substring(charIndex, charIndex + 1);
}
return random;
}
/**
* Method to get a list with random strings.
*
* @param numberOfStrings The amount of random strings to generate.
* @return A list with random strings.
*/
private static ArrayList<String> getValues(int numberOfStrings) {
final int MIN_LENGTH = 10;
final int MAX_LENGTH = 16;
ArrayList<String> values = new ArrayList<String>();
for (int i = 0; i < numberOfStrings; i++) {
values.add(getRandomString(MIN_LENGTH, MAX_LENGTH));
}
return values;
}
}

View File

@@ -0,0 +1,18 @@
package VL14.Aufgabe01;
/**
* Vorlesung 14 / Aufgabe 1
*
* @author Sebastian Brosch
*/
class Aufgabe01 {
public static void main(String[] args) {
Counter counterUp = new Counter(1, 100);
Counter counterDown = new Counter(-10);
Thread threadCounterUp = new Thread(counterUp);
Thread threadCounterDown = new Thread(counterDown);
threadCounterUp.start();
threadCounterDown.start();
}
}

View File

@@ -0,0 +1,52 @@
package VL14.Aufgabe01;
/**
* Class representing a counter.
*
* @author Sebastian Brosch
*/
public class Counter implements Runnable {
static int counter;
// some information of the counter.
private int number;
/**
* Create a new counter with initializing the start value.
*
* @param number The number to increment or decrement the counter.
* @param start The start value of the counter.
*/
public Counter(int number, int start) {
this(number);
counter = start;
}
/**
* Create a new counter without initializing the start value.
*
* @param number The number to increment or decrement the counter.
*/
public Counter(int number) {
this.number = number;
}
/**
* Method to run some code in a Thread.
*/
public void run() {
while (counter > 0 && counter < 1000) {
this.count();
}
}
/**
* Method to increment or decrement the counter.
*/
private void count() {
synchronized (getClass()) {
counter += this.number;
System.out.printf("%3d [%s: %3d]\n", counter, Thread.currentThread().getName(), this.number);
}
}
}

View File

@@ -0,0 +1,38 @@
package VL14.Aufgabe02;
import java.util.Random;
/**
* Vorlesung 14 / Aufgabe 2
*
* @author Sebastian Brosch
*/
public class Aufgabe02 {
public static void main(String[] args) throws Exception {
final int NUMBER_OF_USERS = 2;
final int NUMBER_OF_PRINTERS = 2;
// create some printers.
for (int p = 0; p < NUMBER_OF_PRINTERS; p++) {
Thread threadPrinter = new Thread(new Printer());
threadPrinter.start();
}
// create some users.
for (int u = 0; u < NUMBER_OF_USERS; u++) {
Thread threadUser = new Thread(new User(getRandomNumber(1, 10) * 1000));
threadUser.start();
}
}
/**
* Method to determine a random number from a certain range.
*
* @param start The first number of the range.
* @param end The last number of the range.
* @return A random number from a certain range.
*/
private static int getRandomNumber(int start, int end) {
return (new Random()).nextInt((end - start + 1)) + start;
}
}

View File

@@ -0,0 +1,19 @@
package VL14.Aufgabe02;
/**
* Class which represents a printer.
*
* @author Sebastian Brosch
*/
public class Printer implements Runnable {
public void run() {
while (true) {
synchronized (PrinterQueue.queue) {
if (PrinterQueue.queue.size() > 0) {
System.out.println("Printer " + Thread.currentThread().threadId() + " prints: " + PrinterQueue.queue.getFirst());
PrinterQueue.queue.removeFirst();
}
}
}
}
}

View File

@@ -0,0 +1,14 @@
package VL14.Aufgabe02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Class which represents a printer queue.
*
* @author Sebastian Brosch
*/
public class PrinterQueue {
static List<Object> queue = Collections.synchronizedList(new ArrayList<>());
}

36
VL14/Aufgabe02/User.java Normal file
View File

@@ -0,0 +1,36 @@
package VL14.Aufgabe02;
/**
* Class which represents a user.
*
* @author Sebastian Brosch
*/
public class User implements Runnable {
private int number = 0;
private long wait = 0;
/**
* Constructor to initialize a User.
*/
public User() {
this.wait = 1000;
}
/**
* Constructor to initialize a User.
*
* @param wait The time the user is waiting for creating new print jobs.
*/
public User(int wait) {
this.wait = wait;
}
public void run() {
while (this.wait > 0) {
int data = this.number++;
PrinterQueue.queue.add(data);
System.out.println("User " + Thread.currentThread().threadId() + " added " + data);
this.wait--;
}
}
}

View File

@@ -0,0 +1,18 @@
package VL15.Aufgabe01;
import javax.swing.JFrame;
/**
* Vorlesung 15 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
Screenshot screenshot = new Screenshot("Screenshot");
screenshot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screenshot.setSize(690, 390);
screenshot.setResizable(false);
screenshot.setVisible(true);
}
}

View File

@@ -0,0 +1,238 @@
package VL15.Aufgabe01;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import java.awt.event.WindowListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Screenshot extends JFrame {
Screenshot(String title) {
super(title);
JTabbedPane tcMain = new JTabbedPane();
JPanel pnlVerbindungssuche = new JPanel();
pnlVerbindungssuche.setLayout(null);
JPanel pnlErweiterteSuche = new JPanel();
tcMain.addTab("Verbindungssuche", pnlVerbindungssuche);
tcMain.addTab("Erweiterte Suche", pnlErweiterteSuche);
JTextArea txtLanguageMenu = new JTextArea();
txtLanguageMenu.setText("deutsch | english | francais | italiano");
txtLanguageMenu.setBounds(480, 0, 200, 20);
this.getContentPane().add(txtLanguageMenu);
insertHeading("Start & Ziel", pnlVerbindungssuche, 0);
JLabel lblStartZielVon = new JLabel();
lblStartZielVon.setText("Von:");
lblStartZielVon.setBounds(0, 25, 100, 25);
pnlVerbindungssuche.add(lblStartZielVon);
JLabel lblStartZielNach = new JLabel();
lblStartZielNach.setText("Nach:");
lblStartZielNach.setBounds(0, 50, 100, 25);
pnlVerbindungssuche.add(lblStartZielNach);
JLabel lblStartZielBahnhofHaltestelleVon = new JLabel();
lblStartZielBahnhofHaltestelleVon.setText("Bahnhof/Haltestelle");
lblStartZielBahnhofHaltestelleVon.setBounds(100, 25, 150, 25);
lblStartZielBahnhofHaltestelleVon.setFont(lblStartZielBahnhofHaltestelleVon.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblStartZielBahnhofHaltestelleVon);
JLabel lblStartZielBahnhofHaltestelleNach = new JLabel();
lblStartZielBahnhofHaltestelleNach.setText("Bahnhof/Haltestelle");
lblStartZielBahnhofHaltestelleNach.setBounds(100, 50, 150, 25);
lblStartZielBahnhofHaltestelleNach.setFont(lblStartZielBahnhofHaltestelleNach.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblStartZielBahnhofHaltestelleNach);
JTextField txtStartZielVon = new JTextField();
txtStartZielVon.setBounds(250, 27, 300, 20);
txtStartZielVon.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtStartZielVon);
JTextField txtStartZielNach = new JTextField();
txtStartZielNach.setBounds(250, 52, 300, 20);
txtStartZielNach.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pnlVerbindungssuche.add(txtStartZielNach);
pnlVerbindungssuche.add(getInfoIcon(555, 27));
JButton btnStartZielUeber = new JButton();
btnStartZielUeber.setText("Über");
btnStartZielUeber.setBounds(555, 52, 65, 20);
pnlVerbindungssuche.add(btnStartZielUeber);
insertHeading("Reisedatum und -zeit", pnlVerbindungssuche, 75);
JLabel lblReisedatumHinfahrt = new JLabel();
lblReisedatumHinfahrt.setText("Hinfahrt:");
lblReisedatumHinfahrt.setBounds(0, 100, 100, 25);
pnlVerbindungssuche.add(lblReisedatumHinfahrt);
JLabel lblReisedatumHinfahrtDatum = new JLabel();
lblReisedatumHinfahrtDatum.setText("Datum:");
lblReisedatumHinfahrtDatum.setBounds(100, 100, 75, 25);
lblReisedatumHinfahrtDatum.setFont(lblReisedatumHinfahrtDatum.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumHinfahrtDatum);
JLabel lblReisedatumHinfahrtUhrzeit = new JLabel();
lblReisedatumHinfahrtUhrzeit.setText("Uhrzeit:");
lblReisedatumHinfahrtUhrzeit.setBounds(100, 125, 50, 25);
lblReisedatumHinfahrtUhrzeit.setFont(lblReisedatumHinfahrtUhrzeit.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumHinfahrtUhrzeit);
JTextField txtReisedatumHinfahrtDatum = new JTextField();
txtReisedatumHinfahrtDatum.setBounds(150, 102, 100, 20);
txtReisedatumHinfahrtDatum.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumHinfahrtDatum);
JTextField txtReisedatumHinfahrtUhrzeit = new JTextField();
txtReisedatumHinfahrtUhrzeit.setBounds(150, 127, 50, 20);
txtReisedatumHinfahrtUhrzeit.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumHinfahrtUhrzeit);
String uhrzeitTypes[] = { "Ankunft", "Abfahrt" };
JComboBox<String> cmbReisedatumHinfahrtUhrzeitType = new JComboBox<String>(uhrzeitTypes);
cmbReisedatumHinfahrtUhrzeitType.setBounds(205, 127, 100, 20);
pnlVerbindungssuche.add(cmbReisedatumHinfahrtUhrzeitType);
JSeparator sepReisedatum = new JSeparator(SwingConstants.VERTICAL);
sepReisedatum.setBounds(315, 100, 1, 50);
sepReisedatum.setBackground(Color.LIGHT_GRAY);
pnlVerbindungssuche.add(sepReisedatum);
JLabel lblReisedatumRueckfahrt = new JLabel();
lblReisedatumRueckfahrt.setText("Rückfahrt:");
lblReisedatumRueckfahrt.setBounds(325, 100, 100, 25);
pnlVerbindungssuche.add(lblReisedatumRueckfahrt);
JLabel lblReisedatumRueckfahrtDatum = new JLabel();
lblReisedatumRueckfahrtDatum.setText("Datum:");
lblReisedatumRueckfahrtDatum.setBounds(425, 100, 75, 25);
lblReisedatumRueckfahrtDatum.setFont(lblReisedatumRueckfahrtDatum.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumRueckfahrtDatum);
JLabel lblReisedatumRueckfahrtUhrzeit = new JLabel();
lblReisedatumRueckfahrtUhrzeit.setText("Uhrzeit:");
lblReisedatumRueckfahrtUhrzeit.setBounds(425, 125, 50, 25);
lblReisedatumRueckfahrtUhrzeit.setFont(lblReisedatumRueckfahrtUhrzeit.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumRueckfahrtUhrzeit);
JTextField txtReisedatumRueckfahrtDatum = new JTextField();
txtReisedatumRueckfahrtDatum.setBounds(475, 102, 100, 20);
txtReisedatumRueckfahrtDatum.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumRueckfahrtDatum);
JTextField txtReisedatumRueckfahrtUhrzeit = new JTextField();
txtReisedatumRueckfahrtUhrzeit.setBounds(475, 127, 50, 20);
txtReisedatumRueckfahrtUhrzeit.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumRueckfahrtUhrzeit);
JComboBox<String> cmbReisedatumRueckfahrtUhrzeitType = new JComboBox<String>(uhrzeitTypes);
cmbReisedatumRueckfahrtUhrzeitType.setBounds(530, 127, 100, 20);
pnlVerbindungssuche.add(cmbReisedatumRueckfahrtUhrzeitType);
insertHeading("Angaben zur Preisberechnung", pnlVerbindungssuche, 150);
JLabel lblPreisberechnungReisende = new JLabel();
lblPreisberechnungReisende.setText("Reisende:");
lblPreisberechnungReisende.setBounds(0, 175, 100, 25);
pnlVerbindungssuche.add(lblPreisberechnungReisende);
String personType[] = { "1 Erwachsener", "2 Erwachsene" };
JComboBox<String> cmbPreisberechnungPersonType = new JComboBox<String>(personType);
cmbPreisberechnungPersonType.setBounds(100, 177, 205, 20);
pnlVerbindungssuche.add(cmbPreisberechnungPersonType);
JButton btnPreisberechnungAdd = new JButton();
btnPreisberechnungAdd.setText("Personen hinzufügen");
btnPreisberechnungAdd.setBounds(100, 202, 160, 20);
pnlVerbindungssuche.add(btnPreisberechnungAdd);
pnlVerbindungssuche.add(getInfoIcon(265, 202));
String rabattType[] = { "Keine Ermäßigung", "Bahn-Card 25", "Bahn-Card 50" };
JComboBox<String> cmbPreisberechnungRabattType = new JComboBox<String>(rabattType);
cmbPreisberechnungRabattType.setBounds(315, 177, 210, 20);
pnlVerbindungssuche.add(cmbPreisberechnungRabattType);
JButton btnPreisberechnungAuslandspreise = new JButton();
btnPreisberechnungAuslandspreise.setText("Auslandspreise");
btnPreisberechnungAuslandspreise.setBounds(315, 202, 125, 20);
pnlVerbindungssuche.add(btnPreisberechnungAuslandspreise);
pnlVerbindungssuche.add(getInfoIcon(445, 202));
String klasseType[] = { "1. Klasse", "2. Klasse" };
JComboBox<String> cmbPreisberechnungKlasse = new JComboBox<String>(klasseType);
cmbPreisberechnungKlasse.setBounds(530, 177, 100, 20);
pnlVerbindungssuche.add(cmbPreisberechnungKlasse);
insertHeading("Angaben zur Verbindung", pnlVerbindungssuche, 225);
JLabel lblVerbindungVerkehrsmittel = new JLabel();
lblVerbindungVerkehrsmittel.setText("Verkehrsmittel:");
lblVerbindungVerkehrsmittel.setBounds(0, 250, 100, 25);
pnlVerbindungssuche.add(lblVerbindungVerkehrsmittel);
String searchType[] = { "Standardsuche", "Erweiterte Suche" };
JComboBox<String> cmbVerbindungSucheType = new JComboBox<String>(searchType);
cmbVerbindungSucheType.setBounds(100, 252, 205, 20);
pnlVerbindungssuche.add(cmbVerbindungSucheType);
JButton btnVerbindungAdvanced = new JButton();
btnVerbindungAdvanced.setText("Erweitert");
btnVerbindungAdvanced.setBounds(315, 252, 100, 20);
pnlVerbindungssuche.add(btnVerbindungAdvanced);
pnlVerbindungssuche.add(getInfoIcon(420, 252));
JCheckBox chkVerbindungSchnelleVerbindung = new JCheckBox();
chkVerbindungSchnelleVerbindung.setText("schnelle Verbindungen bevorzugen");
chkVerbindungSchnelleVerbindung.setBounds(100, 277, 235, 20);
pnlVerbindungssuche.add(chkVerbindungSchnelleVerbindung);
pnlVerbindungssuche.add(getInfoIcon(335, 277));
JCheckBox chkVerbindungFahrrad = new JCheckBox();
chkVerbindungFahrrad.setText("Fahrradmitnahme");
chkVerbindungFahrrad.setBounds(370, 277, 150, 20);
pnlVerbindungssuche.add(chkVerbindungFahrrad);
JSeparator sepControls = new JSeparator(SwingConstants.HORIZONTAL);
sepControls.setBounds(0, 300, 670, 1);
sepControls.setBackground(Color.LIGHT_GRAY);
pnlVerbindungssuche.add(sepControls);
JButton btnVerbindungSuchen = new JButton();
btnVerbindungSuchen.setText("Verbindung suchen");
btnVerbindungSuchen.setBounds(0, 305, 150, 20);
pnlVerbindungssuche.add(btnVerbindungSuchen);
JButton btnVerbindungNeueAnfrage = new JButton();
btnVerbindungNeueAnfrage.setText("Neue Anfrage");
btnVerbindungNeueAnfrage.setBounds(155, 305, 125, 20);
pnlVerbindungssuche.add(btnVerbindungNeueAnfrage);
JButton btnMeinAnfrageprofil = new JButton();
btnMeinAnfrageprofil.setText("Mein Anfrageprofil");
btnMeinAnfrageprofil.setBounds(520, 305, 150, 20);
pnlVerbindungssuche.add(btnMeinAnfrageprofil);
this.getContentPane().add(tcMain);
WindowListener closeListener = new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
};
this.addWindowListener(closeListener);
}
private void insertHeading(String title, JPanel panel, int top) {
JLabel lblStartZiel = new JLabel();
lblStartZiel.setText(title);
lblStartZiel.setOpaque(true);
lblStartZiel.setBackground(Color.decode("#ceccfe"));
lblStartZiel.setForeground(Color.decode("#1b0897"));
lblStartZiel.setBounds(0, top, 670, 25);
panel.add(lblStartZiel);
}
/**
* Get a JLabel with displaying the info icon.
*
* @param left The position on the x-axis.
* @param top The position on the y-axis.
* @return The label with info icon.
*/
private JLabel getInfoIcon(int left, int top) {
ImageIcon icon = (ImageIcon) UIManager.get("OptionPane.informationIcon");
Image imgFit = icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
JLabel lblInfoIcon = new JLabel(new ImageIcon(imgFit));
lblInfoIcon.setBounds(left, top, 20, 20);
return lblInfoIcon;
}
}

View File

@@ -0,0 +1,18 @@
package VL16.Aufgabe01;
import javax.swing.JFrame;
/**
* Vorlesung 16 / Aufgabe 1
*
* @author Sebastian Brosch
*/
public class Aufgabe01 {
public static void main(String[] args) {
Screenshot screenshot = new Screenshot("Screenshot mit Zusammenfassung");
screenshot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screenshot.setSize(690, 390);
screenshot.setResizable(false);
screenshot.setVisible(true);
}
}

View File

@@ -0,0 +1,281 @@
package VL16.Aufgabe01;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import java.awt.event.WindowListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Screenshot extends JFrame implements ActionListener {
// Controls (used in events).
JButton btnVerbindungSuchen = new JButton("Verbindung suchen");
JTextField txtStartZielVon = new JTextField();
JTextField txtStartZielNach = new JTextField();
JTextField txtReisedatumHinfahrtDatum = new JTextField();
JTextField txtReisedatumHinfahrtUhrzeit = new JTextField();
String uhrzeitTypes[] = { "Ankunft", "Abfahrt" };
JComboBox<String> cmbReisedatumHinfahrtUhrzeitType = new JComboBox<String>(uhrzeitTypes);
JTextField txtReisedatumRueckfahrtDatum = new JTextField();
JTextField txtReisedatumRueckfahrtUhrzeit = new JTextField();
JComboBox<String> cmbReisedatumRueckfahrtUhrzeitType = new JComboBox<String>(uhrzeitTypes);
String personType[] = { "1 Erwachsener", "2 Erwachsene" };
JComboBox<String> cmbPreisberechnungPersonType = new JComboBox<String>(personType);
String rabattType[] = { "Keine Ermäßigung", "Bahn-Card 25", "Bahn-Card 50" };
JComboBox<String> cmbPreisberechnungRabattType = new JComboBox<String>(rabattType);
String klasseType[] = { "1. Klasse", "2. Klasse" };
JComboBox<String> cmbPreisberechnungKlasse = new JComboBox<String>(klasseType);
String searchType[] = { "Standardsuche", "Erweiterte Suche" };
JComboBox<String> cmbVerbindungSucheType = new JComboBox<String>(searchType);
JCheckBox chkVerbindungSchnelleVerbindung = new JCheckBox();
JCheckBox chkVerbindungFahrrad = new JCheckBox();
Screenshot(String title) {
super(title);
JTabbedPane tcMain = new JTabbedPane();
JPanel pnlVerbindungssuche = new JPanel();
pnlVerbindungssuche.setLayout(null);
JPanel pnlErweiterteSuche = new JPanel();
tcMain.addTab("Verbindungssuche", pnlVerbindungssuche);
tcMain.addTab("Erweiterte Suche", pnlErweiterteSuche);
JTextArea txtLanguageMenu = new JTextArea();
txtLanguageMenu.setText("deutsch | english | francais | italiano");
txtLanguageMenu.setBounds(480, 0, 200, 20);
this.getContentPane().add(txtLanguageMenu);
insertHeading("Start & Ziel", pnlVerbindungssuche, 0);
JLabel lblStartZielVon = new JLabel();
lblStartZielVon.setText("Von:");
lblStartZielVon.setBounds(0, 25, 100, 25);
pnlVerbindungssuche.add(lblStartZielVon);
JLabel lblStartZielNach = new JLabel();
lblStartZielNach.setText("Nach:");
lblStartZielNach.setBounds(0, 50, 100, 25);
pnlVerbindungssuche.add(lblStartZielNach);
JLabel lblStartZielBahnhofHaltestelleVon = new JLabel();
lblStartZielBahnhofHaltestelleVon.setText("Bahnhof/Haltestelle");
lblStartZielBahnhofHaltestelleVon.setBounds(100, 25, 150, 25);
lblStartZielBahnhofHaltestelleVon.setFont(lblStartZielBahnhofHaltestelleVon.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblStartZielBahnhofHaltestelleVon);
JLabel lblStartZielBahnhofHaltestelleNach = new JLabel();
lblStartZielBahnhofHaltestelleNach.setText("Bahnhof/Haltestelle");
lblStartZielBahnhofHaltestelleNach.setBounds(100, 50, 150, 25);
lblStartZielBahnhofHaltestelleNach.setFont(lblStartZielBahnhofHaltestelleNach.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblStartZielBahnhofHaltestelleNach);
txtStartZielVon.setBounds(250, 27, 300, 20);
txtStartZielVon.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtStartZielVon);
txtStartZielNach.setBounds(250, 52, 300, 20);
txtStartZielNach.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pnlVerbindungssuche.add(txtStartZielNach);
pnlVerbindungssuche.add(getInfoIcon(555, 27));
JButton btnStartZielUeber = new JButton();
btnStartZielUeber.setText("Über");
btnStartZielUeber.setBounds(555, 52, 65, 20);
pnlVerbindungssuche.add(btnStartZielUeber);
insertHeading("Reisedatum und -zeit", pnlVerbindungssuche, 75);
JLabel lblReisedatumHinfahrt = new JLabel();
lblReisedatumHinfahrt.setText("Hinfahrt:");
lblReisedatumHinfahrt.setBounds(0, 100, 100, 25);
pnlVerbindungssuche.add(lblReisedatumHinfahrt);
JLabel lblReisedatumHinfahrtDatum = new JLabel();
lblReisedatumHinfahrtDatum.setText("Datum:");
lblReisedatumHinfahrtDatum.setBounds(100, 100, 75, 25);
lblReisedatumHinfahrtDatum.setFont(lblReisedatumHinfahrtDatum.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumHinfahrtDatum);
JLabel lblReisedatumHinfahrtUhrzeit = new JLabel();
lblReisedatumHinfahrtUhrzeit.setText("Uhrzeit:");
lblReisedatumHinfahrtUhrzeit.setBounds(100, 125, 50, 25);
lblReisedatumHinfahrtUhrzeit.setFont(lblReisedatumHinfahrtUhrzeit.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumHinfahrtUhrzeit);
txtReisedatumHinfahrtDatum.setBounds(150, 102, 100, 20);
txtReisedatumHinfahrtDatum.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumHinfahrtDatum);
txtReisedatumHinfahrtUhrzeit.setBounds(150, 127, 50, 20);
txtReisedatumHinfahrtUhrzeit.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumHinfahrtUhrzeit);
cmbReisedatumHinfahrtUhrzeitType.setBounds(205, 127, 100, 20);
pnlVerbindungssuche.add(cmbReisedatumHinfahrtUhrzeitType);
JSeparator sepReisedatum = new JSeparator(SwingConstants.VERTICAL);
sepReisedatum.setBounds(315, 100, 1, 50);
sepReisedatum.setBackground(Color.LIGHT_GRAY);
pnlVerbindungssuche.add(sepReisedatum);
JLabel lblReisedatumRueckfahrt = new JLabel();
lblReisedatumRueckfahrt.setText("Rückfahrt:");
lblReisedatumRueckfahrt.setBounds(325, 100, 100, 25);
pnlVerbindungssuche.add(lblReisedatumRueckfahrt);
JLabel lblReisedatumRueckfahrtDatum = new JLabel();
lblReisedatumRueckfahrtDatum.setText("Datum:");
lblReisedatumRueckfahrtDatum.setBounds(425, 100, 75, 25);
lblReisedatumRueckfahrtDatum.setFont(lblReisedatumRueckfahrtDatum.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumRueckfahrtDatum);
JLabel lblReisedatumRueckfahrtUhrzeit = new JLabel();
lblReisedatumRueckfahrtUhrzeit.setText("Uhrzeit:");
lblReisedatumRueckfahrtUhrzeit.setBounds(425, 125, 50, 25);
lblReisedatumRueckfahrtUhrzeit.setFont(lblReisedatumRueckfahrtUhrzeit.getFont().deriveFont(Font.PLAIN));
pnlVerbindungssuche.add(lblReisedatumRueckfahrtUhrzeit);
txtReisedatumRueckfahrtDatum.setBounds(475, 102, 100, 20);
txtReisedatumRueckfahrtDatum.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumRueckfahrtDatum);
txtReisedatumRueckfahrtUhrzeit.setBounds(475, 127, 50, 20);
txtReisedatumRueckfahrtUhrzeit.setBorder(BorderFactory.createLineBorder(Color.black));
pnlVerbindungssuche.add(txtReisedatumRueckfahrtUhrzeit);
cmbReisedatumRueckfahrtUhrzeitType.setBounds(530, 127, 100, 20);
pnlVerbindungssuche.add(cmbReisedatumRueckfahrtUhrzeitType);
insertHeading("Angaben zur Preisberechnung", pnlVerbindungssuche, 150);
JLabel lblPreisberechnungReisende = new JLabel();
lblPreisberechnungReisende.setText("Reisende:");
lblPreisberechnungReisende.setBounds(0, 175, 100, 25);
pnlVerbindungssuche.add(lblPreisberechnungReisende);
cmbPreisberechnungPersonType.setBounds(100, 177, 205, 20);
pnlVerbindungssuche.add(cmbPreisberechnungPersonType);
JButton btnPreisberechnungAdd = new JButton();
btnPreisberechnungAdd.setText("Personen hinzufügen");
btnPreisberechnungAdd.setBounds(100, 202, 160, 20);
pnlVerbindungssuche.add(btnPreisberechnungAdd);
pnlVerbindungssuche.add(getInfoIcon(265, 202));
cmbPreisberechnungRabattType.setBounds(315, 177, 210, 20);
pnlVerbindungssuche.add(cmbPreisberechnungRabattType);
JButton btnPreisberechnungAuslandspreise = new JButton();
btnPreisberechnungAuslandspreise.setText("Auslandspreise");
btnPreisberechnungAuslandspreise.setBounds(315, 202, 125, 20);
pnlVerbindungssuche.add(btnPreisberechnungAuslandspreise);
pnlVerbindungssuche.add(getInfoIcon(445, 202));
cmbPreisberechnungKlasse.setBounds(530, 177, 100, 20);
pnlVerbindungssuche.add(cmbPreisberechnungKlasse);
insertHeading("Angaben zur Verbindung", pnlVerbindungssuche, 225);
JLabel lblVerbindungVerkehrsmittel = new JLabel();
lblVerbindungVerkehrsmittel.setText("Verkehrsmittel:");
lblVerbindungVerkehrsmittel.setBounds(0, 250, 100, 25);
pnlVerbindungssuche.add(lblVerbindungVerkehrsmittel);
cmbVerbindungSucheType.setBounds(100, 252, 205, 20);
pnlVerbindungssuche.add(cmbVerbindungSucheType);
JButton btnVerbindungAdvanced = new JButton();
btnVerbindungAdvanced.setText("Erweitert");
btnVerbindungAdvanced.setBounds(315, 252, 100, 20);
pnlVerbindungssuche.add(btnVerbindungAdvanced);
pnlVerbindungssuche.add(getInfoIcon(420, 252));
chkVerbindungSchnelleVerbindung.setText("schnelle Verbindungen bevorzugen");
chkVerbindungSchnelleVerbindung.setBounds(100, 277, 235, 20);
pnlVerbindungssuche.add(chkVerbindungSchnelleVerbindung);
pnlVerbindungssuche.add(getInfoIcon(335, 277));
chkVerbindungFahrrad.setText("Fahrradmitnahme");
chkVerbindungFahrrad.setBounds(370, 277, 150, 20);
pnlVerbindungssuche.add(chkVerbindungFahrrad);
JSeparator sepControls = new JSeparator(SwingConstants.HORIZONTAL);
sepControls.setBounds(0, 300, 670, 1);
sepControls.setBackground(Color.LIGHT_GRAY);
pnlVerbindungssuche.add(sepControls);
btnVerbindungSuchen.addActionListener(this);
btnVerbindungSuchen.setBounds(0, 305, 150, 20);
pnlVerbindungssuche.add(btnVerbindungSuchen);
JButton btnVerbindungNeueAnfrage = new JButton();
btnVerbindungNeueAnfrage.setText("Neue Anfrage");
btnVerbindungNeueAnfrage.setBounds(155, 305, 125, 20);
pnlVerbindungssuche.add(btnVerbindungNeueAnfrage);
JButton btnMeinAnfrageprofil = new JButton();
btnMeinAnfrageprofil.setText("Mein Anfrageprofil");
btnMeinAnfrageprofil.setBounds(520, 305, 150, 20);
pnlVerbindungssuche.add(btnMeinAnfrageprofil);
this.getContentPane().add(tcMain);
WindowListener closeListener = new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
};
this.addWindowListener(closeListener);
}
/**
* Method to insert a heading to the JFrame.
*
* @param title Title of the heading.
* @param panel The parent panel of the heading.
* @param top The absolute top position of the heading.
*/
private void insertHeading(String title, JPanel panel, int top) {
JLabel lblStartZiel = new JLabel();
lblStartZiel.setText(title);
lblStartZiel.setOpaque(true);
lblStartZiel.setBackground(Color.decode("#ceccfe"));
lblStartZiel.setForeground(Color.decode("#1b0897"));
lblStartZiel.setBounds(0, top, 670, 25);
panel.add(lblStartZiel);
}
/**
* Get a JLabel with displaying the info icon.
*
* @param left The position on the x-axis.
* @param top The position on the y-axis.
* @return The label with info icon.
*/
private JLabel getInfoIcon(int left, int top) {
ImageIcon icon = (ImageIcon) UIManager.get("OptionPane.informationIcon");
Image imgFit = icon.getImage().getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
JLabel lblInfoIcon = new JLabel(new ImageIcon(imgFit));
lblInfoIcon.setBounds(left, top, 20, 20);
return lblInfoIcon;
}
/**
* Method to react to events of the JFrame.
*
* @param event The event to react.
*/
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == btnVerbindungSuchen) {
String infoVerbindung = "";
infoVerbindung += String.format("Verbindung von %s nach %s.\n", txtStartZielVon.getText(), txtStartZielNach.getText());
infoVerbindung += String.format("Hinfahrt: %s am %s um %s Uhr.\n", cmbReisedatumHinfahrtUhrzeitType.getSelectedItem(), txtReisedatumHinfahrtDatum.getText(), txtReisedatumHinfahrtUhrzeit.getText());
infoVerbindung += String.format("Rückfahrt: %s am %s um %s Uhr.\n", cmbReisedatumRueckfahrtUhrzeitType.getSelectedItem(), txtReisedatumRueckfahrtDatum.getText(), txtReisedatumRueckfahrtUhrzeit.getText());
infoVerbindung += String.format("Reisende: %s (%s)\n", cmbPreisberechnungPersonType.getSelectedItem(), cmbPreisberechnungKlasse.getSelectedItem());
infoVerbindung += String.format("Ermäßigung: %s\n", cmbPreisberechnungRabattType.getSelectedItem());
if (chkVerbindungSchnelleVerbindung.isSelected()) {
if (chkVerbindungFahrrad.isSelected()) {
infoVerbindung += "Es soll die schnellste Verbindung mit Fahrradmitnahme gesucht werden.\n";
} else {
infoVerbindung += "Es soll die schnellste Verbindung gesucht werden.\n";
}
} else if (chkVerbindungFahrrad.isSelected()) {
infoVerbindung += "Es sollen nur Verbindungen mit Fahrradmitnahme angezeigt werden.\n";
}
JOptionPane.showMessageDialog(this, infoVerbindung, cmbVerbindungSucheType.getSelectedItem().toString(), JOptionPane.INFORMATION_MESSAGE);
}
}
}