• Keine Ergebnisse gefunden

Einführung in die Programmierung. System.out.println( Meme of the week! );

N/A
N/A
Protected

Academic year: 2022

Aktie "Einführung in die Programmierung. System.out.println( Meme of the week! );"

Copied!
26
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Einführung in die Programmierung

System.out.println(“Meme of the week!”);

(2)

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

(3)

Some useful Java stuff

(4)

Ternary Operator ?

variable = Expression1 ? Expression2: Expression3

if(Expression1){

variable = Expression2;

}else{

variable = Expression3;

}

(5)

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

(6)

Break / Continue

● Break: Bricht den innersten Loop ab

● Continue: Geht zur nächsten Iteration des innersten Loops

(7)

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

(8)

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);

} }

(9)

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 ...

(10)

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);

} }

(11)

Command line arguments

public class Main {

public static void main(String[] args) { System.out.println("HelloWorld");

} }

(12)

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]);

} }

}

(13)

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 ...

(14)

How Java works

Compiled languages Interpreted languages

Compiler (gcc, clang)

Assembly

Interpreter simuliert das Programm 101010

101010 010111 101110

(15)

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

(16)

Static

(17)

Static

● Static Methoden

● Static Variablen

(18)

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);

} }

(19)

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);

} }

(20)

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)

(21)

Loop Invariante

(22)

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.

(23)

x = a;

z = 0;

while (x > 0) { z = z + b;

x--;

}

(24)

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

(25)

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

(26)

Kahoot

https://create.kahoot.it/share/eprog-u13/123592e1-782b-4cec-93b7- 0d4d391b6a5d

Referenzen

ÄHNLICHE DOKUMENTE

Fühlst du dich sicher im Stoff? Dann kannst du bei unserem Tutor den Kapiteltest machen. Wenn du ihn bestehst, darfst du das nächste Kapitel oder das Additum bearbeiten. + Additum

This information maps each memory location of the VM to a type at each program point, identifies the instructions that make up subroutines, indicates the variables over which

The data on the second file is replaced with the data from the first file. The second file is given the same structure, data type, record size, and number of records as the first

(These operators work independently of SUBMIT.) For fast execution of a one line, one parameter command file or a command file without parameters, the period (.)

aload_0 // Parameter0 (this) auf Stack aconst_null // null − Referenz auf den Stack putfield field:Lfield; // Schreibe Wert (null). // auf Feld field:Lfield; von Objekt(this) return

• Method Area: enthält für jede Klasse Konstantentabelle sowie Bytecode für Me- thoden / Konstruktoren.. • Operandenstack: zur Auswertung von (arithmetischen, logischen,

• Method Area: enthält für jede Klasse Konstantentabelle sowie Byte- code für Methoden / Konstruktoren.. • Operandenstack: zur Auswertung von (arithmetischen, logischen,

• Method Area: enthält für jede Klasse Konstan- tentabelle sowie Bytecode für Methoden / Kon- struktoren.. • Operandenstack: zur Auswertung von (arithme- tischen, logischen,