Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
Wie man das .zip File in Eclipse öffnet
Pascal Schärli 25.09.2020
Wie man automatische Tests mit JUnit macht
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
Code Formattieren
Autocomplete
Zeile Löschen
markierte Zeilen als Kommentar
Programm ausführen
Liste aller Shortcuts anzeigen
Pascal Schärli 25.09.2020
Code Formattieren
Autocomplete
Zeile Löschen
markierte Zeilen als Kommentar
Programm ausführen
Liste aller Shortcuts anzeigen
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
System.out.println("signum(3) = " + Signum.signum(3));
@Test public void positive2() {
Assert.assertEquals(1, Signum.signum(10));
}
Pascal Schärli 25.09.2020
Zeichnen Sie den Graphen, der aus allen möglichen Zuständen und Umschüttungen besteht.
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
(Russische Bauernmultiplikation / Russian Peasant Multiplication)
Pascal Schärli 25.09.2020
(Russische Bauernmultiplikation / Russian Peasant Multiplication)
a | b | f(a,b) |
---|---|---|
f(5,98) = f(10,49)
f(10,49) = f(20,24) + 10
f(20,24) = f(40,12)
f(40,12) = f(80,6)
f(80,6) = f(160,3)
f(160,3) = f(320,1) + 160
f(320,1) = 320
f(5,98) = 320 + 160 + 10 = 490
5
10
20
40
80
160
320
98
49
24
12
6
3
1
Pascal Schärli 25.09.2020
Induktionsverankerung: \(b = 1 \)
Induktionsschritt:
Gegeben, dass \(f(a,b) = a \cdot b \) für \(b \in [1 \dots n-1] \).
Zeige daraus, dass \(f(a,n) = a \cdot n\).
\(n\) gerade:
\(f(a,n) = f(2a,\frac{n}{2})\)
\(=2a \cdot \frac{n}{2}\)
\(=a \cdot n \)
\(n\) ungerade:
\(f(a,n) = f(2a,\frac{n-1}{2}) + a\)
\(=2a \cdot \frac{n-1}{2} + a\)
\(=a \cdot (n-1) + a \)
\(=a \cdot n \)
Pascal Schärli 25.09.2020
static int summe(int x) {
if (x == 0) return 0;
return summe(x-1) + x;
}
summe(5) = summe(4) + 5
summe(4) = summe(3) + 4
summe(3) = summe(2) + 3
summe(2) = summe(1) + 2
summe(1) = summe(0) + 1
summe(0) = 0
= 10 + 5 = 15
= 6 + 4 = 10
= 3 + 3 = 6
= 1 + 2 = 3
= 0 + 1 = 1
= 0
-> \(summe(x)\) ruft sich selbst \(x\) mal rekursiv auf
Beispiel
1
2
3
4
5
Pascal Schärli 25.09.2020
public static void pleaseDontPassZero(int i) throws IllegalArgumentException {
if(i == 0){
throw new IllegalArgumentException("I told you so!");
}
}
Pascal Schärli 25.09.2020
Achtung, diese Funktion könnte einen Fehler werfen
"Werfe" einen neuen Fehler
public static void foo1() throws IllegalArgumentException {
pleaseDontPassZero(1);
}
public static void foo2() {
try{
foo1();
}
catch(IllegalArgumentException e){
System.out.println("Something went wrong.");
}
}
Fehler müssen weitergeleitet oder gefangen werden:
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020
(Russische Bauernmultiplikation / Russian Peasant Multiplication)
static int f(int a, int b){
System.out.println(a + " " + b);
if (b == 0)
return 0;
else if (b%2 == 0)
return f(a+a, b/2);
else
return a + f(a+a, (b-1)/2);
}
static int f(int a, int b){
System.out.println(a + " " + b);
if (b == 1)
return a;
else if (b%2 == 0)
return f(a+a, b/2);
else
return a + f(a+a, (b-1)/2);
}
Pascal Schärli 25.09.2020
static boolean gerade(int x) {
if (x == 0) return true;
return !gerade(x-1);
}
static int verdopple(int x) {
if ( x == 0 ) return 0;
return 2 + verdopple(x-1);
}
static int halbiere(int x) {
if (x == 0) return 0;
if (x == 1) return 0;
return 1 + halbiere(x-2);
}
static int f(int a, int b) {
if (b == 0) return 0;
if (gerade(b))
return f(verdopple(a), halbiere(b));
else
return a + f(verdopple(a), halbiere(b));
}
Pascal Schärli 25.09.2020
Implementation der Altägyptische Multiplikation gegeben, nur positive Zahlen erlaubt!
Nicht erlaubte Eingaben müssen einen Fehler (Exception) auslösenÂ
Teilaufgabe b)
Findet den Fehler in der Funktion f
Pascal Schärli 25.09.2020
private static int f(int a, int b) {
if (b==0) return a;
if (b%2 == 0) return f(2*a, b/2);
else return a + f(2*a, b/2);
}
c) - Java Doc
/**
* Beschreibung
*
* @param a Beschreibung Variable a
* @param b Beschreibung Variable b
* @return Beschreibung Rückgabewert
* @throws Beschreibung Exceptions
*/
Pascal Schärli 25.09.2020
Pascal Schärli 25.09.2020