Java - Programmierung - Prozedurale Programmierung 1 // 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; byte b = 127 short s = 32767 int i = 2147483647 long l = 9223372036854775807 long l0 = 0 char c = j float f = 3.40282e+038 double d = 1.79769e+308 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);
Java - Programmierung - Prozedurale Programmierung 2 // 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 mit Zeichenketten (13 Zeichen) Arbeit mit Umlauten (8 Zeichen)
// Alternativen Java - Programmierung - Prozedurale Programmierung 3 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 58 33 ungleich 58 z >= v Verarbeitung
// Iteration Java - Programmierung - Prozedurale Programmierung 4 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 ); abcdefghijklm zyxwvutsrqpon 1000 / 47 = 21.2766 1000 / 20 = 50
Java - Programmierung - Prozedurale Programmierung 5 // Ausnahme import 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 (""); ; 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 = 50 100 / 1 = 100 java.lang.arithmeticexception: / by zero 100 / -1 = -100 100 / -2 = -50 a!!! FEHLER!!! java.lang.error: Ziffer java.lang.error: Ziffer
Java - Programmierung - Prozedurale Programmierung 6 // Feld public 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 3 0 2 0 2 MODULOSUMME 0 2 0 2 1 3 1 3 2 0 2 0 3 1 3 1