Einführung in die Programmierung
System.out.println(“Meme of the week!”);
Plan für heute
1. Useful Java stuff (Break, Continue, ?, Command line arguments, JVM, Bytecode)
2. You decide: https://forms.gle/Vna42ozvUK1184U78
a. Static
b. Loop Invariant
3. Prüfungstipps 4. Kahoot
5. Fragerunde
Some useful Java stuff
Ternary Operator ?
variable = Expression1 ? Expression2: Expression3
if(Expression1){
variable = Expression2;
}else{
variable = Expression3;
}
Ternary Operator ?
int n1 = 5, n2 = 10, res;
res = (n1 > n2) ? (n1 + n2) : (n1 - n2);
System.out.println(res); -5
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
System.out.println(max); 10
Break / Continue
● Break: Bricht den innersten Loop ab
● Continue: Geht zur nächsten Iteration des innersten Loops
Break
for (int i = 0; i < 10; i++) { if (i == 4) {
break;
}
System.out.println(i);
}
0 1 2 3
int i = 0;
while (i < 10) { if (i == 4) {
break;
}
System.out.println(i);
i++;
}
0 1 2 3 int i = 0;
do {if (i == 4) { break;
}System.out.println(i);
} while (i < 10);i++;
0 1 2 3
Break
0 0 0 0
for (int j = 0; j < 4; j++) { for (int i = 0; i < 10; i++) {
if (i == 1) { break;
}System.out.println(i);
} }
Continue
for (int i = 0; i < 10; i++) { if (i == 4) {
continue;
}
System.out.println(i);
}
0 1 2 3 5 6 7 8 9
int i = 0;
while (i < 10) { if (i == 4) {
continue;
}
System.out.println(i);
i++;
}
0 1 2 3 ...
int i = 0;
do {if (i == 4) { continue;
}System.out.println(i);
} while (i < 10);i++;
0 1 2 3 ...
Continue
0 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 0 2 3 4 5 6 7 8 9 for (int j = 0; j < 4; j++) {
for (int i = 0; i < 10; i++) { if (i == 1) {
continue;
}System.out.println(i);
} }
Command line arguments
public class Main {
public static void main(String[] args) { System.out.println("HelloWorld");
} }
Command line arguments
public class Main {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) { System.out.println(args[i]);
} }
}
Run java from command line with arguments
1. $ javac Main.java
2. $ java Main 1. $ javac Main.java Other1.java Other2.java ...
2. $ java Main args1 args2 args3 ...
How Java works
Compiled languages Interpreted languages
Compiler (gcc, clang)
Assembly
Interpreter simuliert das Programm 101010
101010 010111 101110
How Java works
Compiled languages Interpreted languages
Compiler (javac)
Java Bytecode
Java Bytecode
JVM (Java Virtual Machine) simuliert das Programm Bsp. HotSpot
public static void main(java.lang.String[]);
Code:
0: iconst_0 1: istore_1 2: iload_1 3: iconst_4
4: if_icmpge 42 7: iconst_0
Static
Static
● Static Methoden
● Static Variablen
Static Methods
public classMain{
public staticvoidmain(String[]args) { Testclass.method_static(5);
Testclass testi =newTestclass(1);
testi.normal_method(3);
} }
public classTestclass{ intb;
Testclass(intb){
this.b=b;
}
public staticvoidmethod_static(inta) { System.out.println("method_static "+a);
// System.out.println("method_static " + b);
}
publicvoidnormal_method(inta) {
System.out.println("normal_method "+a);
System.out.println("normal_method "+b);
} }
Static Variablen
public classMain{
public staticvoidmain(String[]args) { Testclass instance1 =newTestclass();
Testclass instance2 =newTestclass();
instance1.normal_method();
instance2.normal_method();
Instance1.static_int++;
instance1.normal_method();
instance2.normal_method();
Instance2.static_int++;
instance1.normal_method();
instance2.normal_method();
Testclass instance3 =newTestclass();
instance1.normal_method();
instance2.normal_method();
instance3.normal_method();
Instance3.static_int++;
Testclass.static_int++;
instance1.normal_method();
instance2.normal_method();
instance3.normal_method();
}}
public classTestclass{
staticintstatic_int;
publicvoidnormal_method() { System.out.println(static_int);
} }
Zusammenfassung
● Static Methods
○ Können aufgerufen werden ohne eine Instanz der Klasse zu erstellen.
○ Können nur auf static Attribute zugreifen.
● Static Variablen
○ Können gebraucht werden ohne eine Instanz der Klasse zu erstellen.
○ Alle Instanzen der Klasse teilen sich denselben Wert (die Variable gibt genau einmal im Speicher)
Loop Invariante
Hoare Triple
x = a;
z = 0;
while (x > 0) { z = z + b;
} x--;
Finden Sie Precondition, Postcondition und Invariante für diesen Loop, der das Produkt a * b berechnen soll.
x = a;
z = 0;
while (x > 0) { z = z + b;
x--;
}
Hoare Triple
x = a;
z = 0;
{ pre: ?? } { inv: ?? }
while (x > 0) { z = z + b;
} x--;
{ post: ?? }
Finden Sie Precondition, Postcondition und Invariante für diesen Loop, der das Produkt a * b berechnen soll.
x = a;
z = 0;
{ pre: x == a && z == 0 && a ≥ 0}
{ inv: z == a * b - x * b && x ≥ 0 } while (x > 0) { // B: x > 0
z = z + b;
} x--;
{ z == a * b - x * b && x ≥ 0 && !(x > 0) } { post: z == a * b } Nötig, damit gilt:
inv && !B ⇒ post
Prüfungstipps
● Macht einen Lernplan!
● Prüfungssammlung VIS: https://exams.vis.ethz.ch/
● PVK Skripts: https://vis.ethz.ch/de/services/pvw-scripts/
● Prüfungen oft ähnlich zum Vorjahr!!!
● 4 Stunden: 1h schriftlicher Teil, 3h Programmieren
○ 4 Stunden sind lang, nimmt etwas zu essen und trinken mit
● Vollständig gelöste Programmieraufgaben geben Bonus
○ Lieber eine Aufgabe ganz richtig, als zwei halb richtig
● Oft zu wenig Zeit -> man muss nicht alles lösen
Kahoot
https://create.kahoot.it/share/eprog-u13/123592e1-782b-4cec-93b7- 0d4d391b6a5d