Mittwoch lesson

This commit is contained in:
sageTheDM 2024-12-19 07:51:04 +01:00
parent e0efeb82ef
commit 8901dea11b
61 changed files with 663 additions and 0 deletions

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,3 @@
public abstract class Animal {
public abstract void makeSound();
}

View file

@ -0,0 +1,11 @@
public class Camel extends Animal implements Climbing {
@Override
public void makeSound() {
System.out.println("Desert Camel grumbles!");
}
@Override
public void climb() {
System.out.println("Desert Camel climbs a dune.");
}
}

View file

@ -0,0 +1,3 @@
public interface Climbing {
void climb();
}

View file

@ -0,0 +1,16 @@
public class Eagle extends Animal implements Flying, Swimming {
@Override
public void makeSound() {
System.out.println("Eagle screams!");
}
@Override
public void fly() {
System.out.println("Eagle flies high in the sky.");
}
@Override
public void swim() {
System.out.println("Eagle fishes in the water (swims...).");
}
}

View file

@ -0,0 +1,4 @@
public interface Flying {
void fly();
}

View file

@ -0,0 +1,16 @@
public class Frog extends Animal implements Swimming, Climbing {
@Override
public void makeSound() {
System.out.println("Green Frog croaks!");
}
@Override
public void swim() {
System.out.println("Green Frog swims in the pond.");
}
@Override
public void climb() {
System.out.println("Green Frog climbs a tree.");
}
}

View file

@ -0,0 +1,3 @@
public interface Swimming {
void swim();
}

View file

@ -0,0 +1,17 @@
public class ZooTest {
public static void main(String[] args) {
Animal eagle = new Eagle();
eagle.makeSound();
((Eagle) eagle).fly();
((Eagle) eagle).swim();
Animal frog = new Frog();
frog.makeSound();
((Frog) frog).swim();
((Frog) frog).climb();
Animal camel = new Camel();
camel.makeSound();
((Camel) camel).climb();
}
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

View file

@ -0,0 +1,32 @@
public class App {
public static void main(String[] args) throws Exception {
String text = null;
int zahler = 24;
int nenner = 0;
try {
System.out.println(text.length());
System.out.println(zahler / nenner);
} catch (ArithmeticException e1) {
System.out.println("fehler: " + e1.getMessage());
} catch (Exception e) {
System.out.println("fehler: " + e.getMessage());
} finally {
System.out.println("ABORT");
Thread.sleep(1000);
clear();
}
}
public static void clear() {
try {
if (System.getProperty("os.name").contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
new ProcessBuilder("clear").inheritIO().start().waitFor();
}
} catch (Exception e) {
e.getMessage();
}
}
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,34 @@
public class Anteil extends Bruch {
private static Bruch verteilt = new Bruch(0, 1);
// Standardkonstruktor: Anteil = 0
public Anteil() {
super(0, 1);
}
// Konstruktor mit Zähler und Nenner
public Anteil(int z, int n) {
super(z, n);
Bruch neuerAnteil = new Bruch(z, n);
verteilt = verteilt.addiere(neuerAnteil);
if (verteilt.dezimalwert() > 1) {
System.out.println("Fehler: Der Gesamtwert der verteilten Anteile übersteigt 1.");
verteilt = verteilt.subtrahiere(neuerAnteil); // Rollback des Fehlers
setZaehler(0); // Setzt Anteil zurück
setNenner(1);
}
}
// Methode, um den verteilten Anteil als double zu erhalten
public static double getVerteilt() {
return verteilt.dezimalwert();
}
// Methode, um den verbleibenden Anteil als Bruch zu erhalten
public static Bruch getRest() {
Bruch eins = new Bruch(1, 1);
return eins.subtrahiere(verteilt);
}
}

View file

@ -0,0 +1,57 @@
public class AnteilTest {
public static void main(String[] args) {
// Bruch-Test
System.out.println("*** Aufgabe Bruch ergänzen");
Bruch bruch1 = new Bruch(1, 2);
Bruch bruch2 = new Bruch(3, 4);
Bruch bruch3 = bruch1.addiere(bruch2);
bruch3.gekuerztausgeben();
System.out.println("");
System.out.println(bruch3.dezimalwert());
Bruch bruch4 = bruch1.subtrahiere(bruch2);
bruch4.gekuerztausgeben();
System.out.println("");
System.out.println(bruch4.dezimalwert());
// Anteil-Test
System.out.println("");
System.out.println("*** Aufgabe Anteil");
int vermoegen = 200000;
Anteil anteil1 = new Anteil(1, 4);
Anteil anteil2 = new Anteil(1, 2);
System.out.println("Anteil anteil1: " + anteil1.bruchToString());
System.out.println("Betrag von anteil1: " + vermoegen * anteil1.dezimalwert());
System.out.println("Anteil anteil2: " + anteil2.bruchToString());
System.out.println("Betrag von anteil2: " + vermoegen * anteil2.dezimalwert());
System.out.println("");
// System.out.println("Verteilt: " + Anteil.verteilt.bruchToString());
System.out.println("Rest: " + anteil1.getRest().bruchToString());
System.out.println("Restbetrag: " + vermoegen * anteil1.getRest().dezimalwert());
}
}

View file

@ -0,0 +1,101 @@
public class Bruch {
private int zaehler;
private int nenner;
Bruch() {
zaehler = 0;
nenner = 1;
}
Bruch(int x) {
zaehler = x;
nenner = 1;
}
Bruch(int x, int y) {
zaehler = x;
nenner = y;
}
void setZaehler(int z) {
zaehler = z;
}
void setNenner(int n) {
nenner = n;
}
int getZaehler() {
return zaehler;
}
int getNenner() {
return nenner;
}
String bruchToString() {
return zaehler + "/" + nenner;
}
void ausgeben() {
System.out.println(zaehler + "/" + nenner);
}
void kuerzen() {
int m, n, r;
m = Math.abs(zaehler);
n = Math.abs(nenner);
r = m % n;
while (r > 0) {
m = n;
n = r;
r = m % n;
}
zaehler /= n;
nenner /= n;
}
void gekuerztausgeben() {
kuerzen();
ausgeben();
}
Bruch multipliziere(Bruch m) {
var z = zaehler * m.zaehler;
var n = nenner * m.nenner;
return new Bruch(z, n);
}
boolean equals(Bruch x) {
var a = new Bruch(this.zaehler, this.nenner);
var b = new Bruch(x.zaehler, x.nenner);
a.kuerzen();
b.kuerzen();
return (a.zaehler == b.zaehler) && (a.nenner == b.nenner);
}
// Neue Methode: Addition
Bruch addiere(Bruch b) {
int neuerZaehler = this.zaehler * b.nenner + b.zaehler * this.nenner;
int neuerNenner = this.nenner * b.nenner;
return new Bruch(neuerZaehler, neuerNenner);
}
// Neue Methode: Subtraktion
Bruch subtrahiere(Bruch b) {
int neuerZaehler = this.zaehler * b.nenner - b.zaehler * this.nenner;
int neuerNenner = this.nenner * b.nenner;
return new Bruch(neuerZaehler, neuerNenner);
}
// Neue Methode: Dezimalwert
double dezimalwert() {
if (nenner == 0) {
System.out.println("Error Divided by 0");
return 0;
} else {
return (double) zaehler / nenner;
}
}
}

View file

@ -0,0 +1,78 @@
import java.util.ArrayList;
public class BruchTest {
public static void main(String[] args) {
ArrayList<Bruch> brueche = new ArrayList<>();
// Test 1: Standardkonstruktor
brueche.add(new Bruch());
System.out.println("Test 1: " + brueche.get(0).bruchToString()); // Erwartet: 0/1
// Test 2: Konstruktor mit einem Parameter
brueche.add(new Bruch(3));
System.out.println("Test 2: " + brueche.get(1).bruchToString()); // Erwartet: 3/1
// Test 3: Konstruktor mit zwei Parametern (positive Werte)
brueche.add(new Bruch(4, 8));
System.out.println("Test 3: " + brueche.get(2).bruchToString()); // Erwartet: 4/8
brueche.get(2).gekuerztausgeben(); // Erwartet: 1/2
// Test 4: Konstruktor mit zwei Parametern (negative Werte)
brueche.add(new Bruch(-4, 8));
System.out.println("Test 4: " + brueche.get(3).bruchToString()); // Erwartet: -4/8
brueche.get(3).gekuerztausgeben(); // Erwartet: -1/2
// Test 5: Setzen von Zähler und Nenner (normal)
brueche.get(2).setZaehler(6);
brueche.get(2).setNenner(9);
System.out.println("Test 5: " + brueche.get(2).bruchToString()); // Erwartet: 6/9
brueche.get(2).gekuerztausgeben(); // Erwartet: 2/3
// Test 6: Setzen von Zähler und Nenner (mit Null)
brueche.get(2).setZaehler(0);
brueche.get(2).setNenner(9);
System.out.println("Test 6: " + brueche.get(2).bruchToString()); // Erwartet: 0/9
// Test 7: Setzen von Zähler und Nenner (Division durch Null)
brueche.get(2).setNenner(0);
System.out.println("Test 7: " + brueche.get(2).bruchToString());
// Test 8: Multiplikation
Bruch bruch4 = new Bruch(2, 3);
Bruch produkt = brueche.get(2).multipliziere(bruch4);
System.out.println("Test 8: " + produkt.bruchToString()); // Erwartet: 0/1
// Test 9: Gleichheit
Bruch bruch5 = new Bruch(2, 3);
Bruch bruch6 = new Bruch(4, 6);
System.out.println("Test 9: " + bruch5.equals(bruch6)); // Erwartet: true
// Test 10: Ungleichheit
Bruch bruch7 = new Bruch(1, 2);
System.out.println("Test 10: " + bruch5.equals(bruch7)); // Erwartet: false
// Test 11: Addition (positive und negative Werte)
Bruch bruch8 = new Bruch(1, 4);
Bruch summe = bruch5.addiere(bruch8);
System.out.println("Test 11: " + summe.bruchToString()); // Erwartet: 11/12
// Test 12: Subtraktion (negatives Ergebnis)
Bruch bruch9 = new Bruch(3, 4);
Bruch differenz = bruch5.subtrahiere(bruch9);
System.out.println("Test 12: " + differenz.bruchToString()); // Erwartet: -1/12
// Test 13: Dezimalwert
double dezimalwert = bruch5.dezimalwert();
System.out.println("Test 13: " + dezimalwert); // Erwartet: 0.6666666666666666 (2/3)
// Test 14: Dezimalwert eines anderen Bruchs
Bruch bruch10 = new Bruch(3, 4);
double dezimalwert2 = bruch10.dezimalwert();
System.out.println("Test 14: " + dezimalwert2); // Erwartet: 0.75
// Test 16: Dezimalwert eines anderen Bruchs (null)
Bruch bruch11 = new Bruch(3, 0);
double dezimalwert3 = bruch11.dezimalwert();
System.out.println("Test 15: " + dezimalwert3); // Erwartet: 0
}
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,6 @@
public class App {
public static void main(String[] args) throws Exception {
Hans hans = new Hans();
hans.walk();
}
}

View file

@ -0,0 +1,8 @@
public class Hans implements IWalk, ISleep {
@Override
public void walk() {
System.out.println("Walk");
}
}

View file

@ -0,0 +1,3 @@
public interface ISleep {
void walk();
}

View file

@ -0,0 +1,3 @@
public interface IWalk {
void walk();
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

View file

@ -0,0 +1,24 @@
public class App {
public static void main(String[] args) throws Exception {
Payment creditCard = new CreditCardPayment("1234-5678-9012-3456", "Max Mustermann");
Payment paypal = new PayPalPayment("max@example.com");
Payment bankTransfer = new BankTransferPayment("DE89 3704 0044 0532 0130 00");
processPayment(creditCard, 49.99);
processPayment(paypal, 19.99);
processPayment(bankTransfer, 99.99);
}
public static void processPayment(Payment payment, double amount) {
payment.pay(amount);
}
}

View file

@ -0,0 +1,12 @@
public class BankTransferPayment extends Payment implements IPay {
String securityNumber;
public BankTransferPayment(String securityNumber) {
this.securityNumber = securityNumber;
}
@Override
public void pay(double amount) {
System.out.println("Bezahlung von " + amount + " CHF mit Banktransfer " + securityNumber);
}
}

View file

@ -0,0 +1,14 @@
public class CreditCardPayment extends Payment implements IPay {
String number;
String name;
public CreditCardPayment(String number, String name) {
this.number = number;
this.name = name;
}
@Override
public void pay(double amount) {
System.out.println("Bezahlung von " + amount + " CHF mit Kreditkarte " + number + " von " + name);
}
}

View file

@ -0,0 +1,3 @@
public interface IPay {
public void pay(double amount);
}

View file

@ -0,0 +1,12 @@
public class PayPalPayment extends Payment implements IPay {
String mail;
public PayPalPayment(String mail) {
this.mail = mail;
}
@Override
public void pay(double amount) {
System.out.println("Bezahlung von " + amount + " CHF mit Kreditkarte " + mail);
}
}

View file

@ -0,0 +1,3 @@
public abstract class Payment implements IPay {
public abstract void pay(double amount);
}

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

Binary file not shown.

View file

@ -0,0 +1,28 @@
import java.io.*;
public class App {
public static void main(String[] args) {
try {
readFile("test.txt");
} catch (FileNotFoundException e) {
System.err.println("Error: The file was not found. Please check the file path.");
} catch (IOException e) {
System.err.println("Error: An I/O error occurred while reading the file.");
} catch (Exception e) {
System.err.println("An unexpected error occurred: " + e.getMessage());
}
}
private static void readFile(String fileName) throws IOException {
FileReader f = new FileReader(fileName);
try {
int c;
while ((c = f.read()) != -1) {
System.out.print((char) c);
}
} finally {
f.close();
}
}
}

View file

@ -0,0 +1,22 @@
🎉 Super! 🎉
Du hast es geschafft.
Herzliche Gratulation!
🌟 STEINER 🌟
Ein Hauch von Magie in der Luft!
✨ Die Sterne funkeln, während die Gedanken fliegen. ✨
🔮 Zufällige Gedanken:
Kaffeetassen tanzen im Regen.
Ein fliegender Teppich voller Bücher.
Die Farben des Windes singen Lieder.
🌈 Lass die Kreativität sprudeln!
Was wäre, wenn die Fische die Wolken fangen könnten?
Ein Abenteuer im Land der Träume wartet auf dich!