11
Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programm ierungstechnik - Institut für Informatik Verteilte Software - Java - Prozedurale Programmierung 1 Datenbezug Ablaufsteuerun g Objektbezug boolean byte char (const) double false final float int long short static transient true void break case catch continue default do else finally for (goto) if return switch synchronized throw throws try while abstract class extends final implements import instanceof interface native new null package private protected public super this volatile Schlüsse l- worte

Verteilte Software - Java - Prozedurale Programmierung 1

Embed Size (px)

DESCRIPTION

Verteilte Software - Java - Prozedurale Programmierung 1. Schlüssel- worte. Verteilte Software - Java - Prozedurale Programmierung 2. // elementare Datentypen public class el_dt { public static void main(String args []) { byte b = 127; short s = 32767; - PowerPoint PPT Presentation

Citation preview

Page 1: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 1

Datenbezug Ablaufsteuerung Objektbezug

booleanbytechar(const)doublefalsefinalfloatint longshortstatictransienttruevoid

breakcasecatchcontinuedefaultdoelsefinallyfor (goto)ifreturnswitchsynchronizedthrowthrowstrywhile

abstractclassextendsfinalimplementsimportinstanceofinterfacenativenewnullpackageprivateprotectedpublicsuperthisvolatile

Schlüssel-worte

Page 2: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 2

// elementare Datentypen

public class el_dt{

public static void main(String args []){

byte b = 127;short s = 32767;int i = 2147483647;long l = 9223372036854775807L, l0;float f = 3.40282347e+38f;double d = 1.79769313486231570e+308;char c = 'j';boolean bool = true;

l0 = 0;System.out.println ("byte\tb = " + b);System.out.println ("short\ts = " + s);System.out.println ("int\ti = " + i);System.out.println ("long\tl = " + l);System.out.println ("long\tl0 = " + l0);System.out.println ("char\tc = " + c);System.out.println ("float\tf = " + f);System.out.println ("double\td = " + d);System.out.println ("boolean\tbool = " + bool);

}}

byte b = 127short s = 32767int i = 2147483647long l = 9223372036854775807long l0 = 0char c = jfloat f = 3.40282e+038double d = 1.79769e+308boolean bool = true

Page 3: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 3

// Zeichenketten und Modifizierer

public class zk_mod{static String static_text = "Zeichenketten";

public static void main(String args []){String s1 = "Arbeit ";String s2;

s2 = "mit ";System.out.println (s1 + s2);

System.out.println (static_text + " (" + static_text.length() + " Zeichen)" );

static_text = "Umlauten";

System.out.println (s1 + s2); System.out.println (static_text +

" (" + static_text.length() + " Zeichen)" );

}}

Arbeit mitZeichenketten (13 Zeichen)Arbeit mitUmlauten (8 Zeichen)

Page 4: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 4

1 () [] . lv++ lv-- links nach rechts

2 ! ~ -unär +unär ++lv --lv rechts nach links

3 new (type)  

4 * / % links nach rechts

5 + - links nach rechts

6 << >> >>> links nach rechts

7 < <= > >= instanceof links nach rechts

8 == != links nach rechts

9 & links nach rechts

10 ^ links nach rechts

11 | links nach rechts

12 && links nach rechts

13 || links nach rechts

14 ?: rechts nach links

15 = += -= *= /= %= ^= &= |= <<= >>= >>>=

rechts nach links

Page 5: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 5

Alternativen

bedingte Verarbeitungif ( Bedingung ) Anweisung

einfache Alternative if ( Bedingung ) Anweisung_1 else Anweisung_2

mehrfache Alternative switch (Ausdruck){ case const_ausdruck_1 : Anweisungen case const_ausdruck_2 : Anweisungen : case const_ausdruck_n : Anweisungen default : Anweisungen}

Page 6: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 6

// Alternativen

public class alt{

public static void main(String args []){

short i1 = 33;int i2 = 58;char antwort;boolean b = false;

if (i1 == i2) System.out.println (i1 + " gleich " + i2);else System.out.println (i1 + " ungleich " + i2);if (i1 != i2 && !b) System.out.println (i1 + " ungleich " + i2);antwort = 'v';b = 'z' >= antwort;if (b) System.out.println ('z' + " >= " + antwort);switch (antwort){ case 'e': System.out.println ("Eingabe"); break; case 'v': System.out.println ("Verarbeitung"); break; case 'a': System.out.println ("Ausgabe"); break; default: System.out.println ("Fehler");}

}}

33 ungleich 5833 ungleich 58z >= vVerarbeitung

Page 7: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 7

Iteration

Abweisschleife while ( Bedingung ) Anweisung

Zählschleife for ( Initialisierung; Bedingung; Ausdruck ) Anweisung

Nichtabweisschleife do Anweisung while (Bedingung);

Steuerung von Schleifen continue break continue labelbreak label

Page 8: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 8

// Iteration

public class iter { public static void main(String args[]) { int i;

double zahl = 0;char c;String text;

c = 'a';text = "";while (c <= 'm'){ text += c; c++;}System.out.println (text);for (c = 'z', text = ""; c >= 'n'; c--) text += c;System.out.println (text);i = 100;do{ i = (i - 6) / 2; if (i == 0) continue; if (i % 6 == 1) break;

zahl = 1000 / i; System.out.println (1000 + " / " + i + " = " + zahl);} while ( zahl > 0 );

}}

abcdefghijklmzyxwvutsrqpon1000 / 47 = 21.27661000 / 20 = 50

Page 9: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 9

Ausnahmebehandlung

Deklaration [modifier] typ name ( [Parameterliste] )

[throws Typnamenliste]{ Vereinbarungen und Anweisungen }

Ausnahmeobjekt auswerfen throw objekt

try-catch-Anweisung try { Anweisungen }catch ( Ausnahme oa1 ) {Anweisungen bei Ausnahme oa1}catch ( Ausnahme oa2 ) {Anweisungen bei Ausnahme oa2}:catch ( Ausnahme oan ) {Anweisungen bei Ausnahme oan}[finally { Anweisungen als abschließende Maßnahmen }]

Page 10: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 10

// Ausnahmeimport java.lang.Error;public class ausnahme { static Error s = new Error ("Ziffer"); public static void main(String args[]) { int i;

for (i = 2; i > -3; i--) try { System.out.println (100 + " / " + i + " = " + 100/i); } catch (ArithmeticException e) { System.out.println (e); } finally { System.out.println ("fertig"); }; try { alfa_zeichen('a'); alfa_zeichen('4'); alfa_zeichen('b'); } catch (Error x) { System.out.println

("!!! FEHLER !!! " + x); } alfa_zeichen('4'); } static void alfa_zeichen (char z) throws Error { if (z >= '0' && z <= '9') throw s; System.out.println (z); }}

100 / 2 = 50fertig100 / 1 = 100fertigjava.lang.ArithmeticException: / by zerofertig100 / -1 = -100fertig100 / -2 = -50fertiga!!! FEHLER !!! java.lang.Error: Zifferjava.lang.Error: Ziffer

Page 11: Verteilte Software  -  Java  - Prozedurale Programmierung  1

Prof. Dr.-Ing. habil. B. Steinbach - Informatik / Softwaretechnologie und Programmierungstechnik - Institut für Informatik

Verteilte Software - Java - Prozedurale Programmierung 11

// Feldpublic class feld{ final static int MAX = 4; public static void main(String args[]) { int i, j, x [], y [], matrix [] []; x = new int[MAX]; y = new int[MAX]; matrix = new int [MAX][MAX];

for (i = 0; i < MAX; i++) { x[i] = i; y[i] = (i * 2) % MAX;} printvec(x); printvec(y); for (i = 0; i < matrix.length; i++) for (j = 0; j < matrix[i].length; j++) matrix[i][j] = (x[i] + y[j]) % MAX; System.out.println ("MODULOSUMME"); for (i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) System.out.print (matrix[i][j] + " "); System.out.println (); } } static void printvec (int[] z) { for (int i = 0; i < z.length; i++) System.out.print (z[i] + " "); System.out.println (); }}

0 1 2 30 2 0 2MODULOSUMME0 2 0 21 3 1 32 0 2 03 1 3 1