added ws202425 courses

This commit is contained in:
2024-11-14 13:11:04 +01:00
parent 800282c2e8
commit beb31897b1
461 changed files with 20368 additions and 0 deletions
@@ -0,0 +1,20 @@
// Sonderfall abfangen
if (x == 0)
// der ggT ist y
if (y == 0)
// der ggT ist x
// Endlosschleife vermeiden
if (x < 0)
x = -x;
if (y < 0)
y = -y;
// Algorithmus von Euklid
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
// der ggT ist x
@@ -0,0 +1,55 @@
/** Static.java
*
* Eine Klasse, die Effekte mit Klassenvariablen demonstriert.
*/
class Static {
int n;
static int snum;
void increase() {
n++;
snum++;
}
void output() {
System.out.println("n = " + n + ", snum = " +snum);
}
static void set(int n) {
snum = n;
}
public static void main(String[] args) {
Static ex1 = new Static();
Static ex2 = new Static();
ex1.output();
ex2.output();
System.out.println();
ex1.increase();
ex1.output();
ex2.output();
set(5); // Aufruf statische Methode; alternativ: Static.set(5);
Static ex3 = new Static();
System.out.println();
ex1.output();
ex2.output();
ex3.output();
}
}
// Static
@@ -0,0 +1,32 @@
// UseRational.java.1-3 (Version .1-3)
import java.util.Scanner;
class UseRational {
public static void main(String[] args) {
// Definitionen
Scanner scanner = new Scanner(System.in);
Rational r;
// Dialog
System.out.println();
System.out.print("Geben Sie den Zaehler ein: ");
int a = scanner.nextInt();
System.out.print("Geben Sie den Nenner ein: ");
int b = scanner.nextInt();
System.out.println();
// Rational-Objekt erzeugen
r = new Rational(a,b);
// Methodenaufrufe
System.out.println(r.toString()); // Bruch ausgeben
System.out.println();
}
}
// UseRational
@@ -0,0 +1,20 @@
// Sonderfall abfangen
if (x == 0)
// der ggT ist y
if (y == 0)
// der ggT ist x
// Endlosschleife vermeiden
if (x < 0)
x = -x;
if (y < 0)
y = -y;
// Algorithmus von Euklid
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
// der ggT ist x
Binary file not shown.
@@ -0,0 +1,32 @@
public class Gcd{
public static int gcd(int num, int denom){
int x = num;
int y = denom;
// Sonderfall abfangen
if (x == 0)
return y;
if (y == 0)
return x;
// Endlosschleife vermeiden
if (x < 0)
x = -x;
if (y < 0)
y = -y;
// Algorithmus von Euklid
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
}
@@ -0,0 +1,94 @@
public class Rational{
private int num, denom;
Rational(){
this.num = 0;
this.denom = 1;
}
Rational(int num){
this.num = num;
this.denom = 1;
}
Rational(int num, int denom){
this.num = num;
this.denom = denom;
}
/*
int gcd(){
int x = this.num;
int y = this.denom;
// Sonderfall abfangen
if (x == 0)
return y;
if (y == 0)
return x;
// Endlosschleife vermeiden
if (x < 0)
x = -x;
if (y < 0)
y = -y;
// Algorithmus von Euklid
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
return x;
}
*/
void reduce(){
//int gcd = gcd();
Gcd gcd_object = new Gcd();
int gcd = gcd_object.gcd(this.num, this.denom);
this.num = this.num / gcd;
this.denom = this.denom /gcd;
}
void extend(int fac){
this.num *= fac;
this.denom *= fac;
}
public String toString(){
String str = this.num + "/" + this.denom;
return str;
}
String toStringReduced(){
reduce();
String str = this.num + "/" + this.denom;
return str;
}
Rational mult(Rational rat){
Rational mult_rat = new Rational();
mult_rat.num = this.num * rat.num;
mult_rat.denom = this.denom * rat.denom;
mult_rat.reduce();
return mult_rat;
}
}
@@ -0,0 +1,14 @@
public class SimpleOutputDemo{
public static void main(String[] args){
for (int i = args.length-1; i>=0; i--){
System.out.print(args[i]+ " ");
}
System.out.println();
}
}
@@ -0,0 +1,53 @@
/** Static.java
*
* Eine Klasse, die Effekte mit Klassenvariablen demonstriert.
*/
class Static {
int n;
static int snum;
void increase() {
n++;
snum++;
}
void output() {
System.out.println("n = " + n + ", snum = " +snum);
}
static void set(int n) {
snum = n;
}
public static void main(String[] args) {
Static ex1 = new Static();
Static ex2 = new Static();
ex1.output();
ex2.output();
System.out.println();
ex1.increase();
ex1.output();
ex2.output();
set(5); // Aufruf statische Methode; alternativ: Static.set(5);
Static ex3 = new Static();
System.out.println();
ex1.output();
ex2.output();
ex3.output();
}
}
// Static
@@ -0,0 +1,40 @@
// UseRational.java.1-3 (Version .1-3)
import java.util.Scanner;
class UseRational {
public static void main(String[] args) {
// Definitionen
Scanner scanner = new Scanner(System.in);
Rational r;
// Dialog
System.out.println();
System.out.print("Geben Sie den Zaehler ein: ");
int a = scanner.nextInt();
System.out.print("Geben Sie den Nenner ein: ");
int b = scanner.nextInt();
System.out.println();
// Rational-Objekt erzeugen
r = new Rational(a,b);
// Methodenaufrufe
System.out.println(r.toString()); // Bruch ausgeben
System.out.println(r.toStringReduced());
System.out.println();
// testen mult rat
Rational r1 = new Rational(4,1);
Rational r2 =r.mult(r1);
System.out.println(r2.toString());
}
}
// UseRational
Binary file not shown.