fh.h42.de - What´s the question?
Navigation: [START] [00-ws] [prog] [p-java001.htm]
[print]  [e-mail]  [main]

Java - Arbeiten mit Datentypen

Lösung zu Übung 10, Aufgabe 2:

Aufgabentext: Schreiben Sie ein Java-Programm, das die zwei Integerzahlen 2000000000 und 2100000000 addiert und das Ergebnis ausgibt:
/* AddNumbers
   $DATE (13.12.2000)
   $AUTH Lars Ehrhardt
   $DESC Demonstrates overflow while adding two numbers,
   so beware the types you use!
 */

class AddNumbers {
    public static void main (String[] arguments) {
        int i1, i2;
        i1 = 2000000000;
        i2 = 2100000000;

        long l1, l2;
        l1 = 2000000000;
        l2 = 2100000000;

        System.out.println("Integer-Addition of " + i1 + " and " + i2 + " is: " + (i1 + i2));
        System.out.println("Long-Addition of    " + l1 + " and " + l2 + " is: " + (l1 + l2));
    }
}


Lösung zu Übung 10, Aufgabe 3:

Aufgabentext: Schreiben Sie ein Java-Programm, das für die Float-Werte x = 2304f, y = -4096f und z = 4096.001953125f die Berechnungen x*(y+z) und x*y + x*z ausführt:
/* FloatCalc
   $DATE (13.12.2000)
   $AUTH Lars Ehrhardt
   $DESC Demonstrates calculation with float
 */

class FloatCalc {
    public static void main(String arguments[]){
        float x,y,z;
        x = 2304f;
        y = -4096f;
        z = 4096.001953125f;

        System.out.println("Calculating with floatnumbers...");
        System.out.println("The following equations should give the same output:");
        System.out.println("x * (y + z):   " + (x*(y+z)));
        System.out.println("x * y + x * z: " + (x*y+x*z));
        System.out.println("... but they do not!");

        System.out.print  ("y + z is: " + (y + z));
        System.out.println(", x * (y + z) is: " + (x*(y+z)));
        System.out.print  ("x * y is: " + (x * y));
        System.out.print  (", x * z is: " + (x*z));
        System.out.println(", x * y + x * z is: " + (x*y+x*z));
    }
}

(Keine Garantie auf Richtigkeit!)
zurück zur Übersicht
News
12.10.: Seiten zum WS03 aktualisiert
02.09: Seite zum WLAN-Projekt aktualisiert
10.04: Seiten zum SS03 aktualisiert
12.08: Eigene Vorlage für Seminararbeiten der FH hinzugefügt
14.07: Informationen zum 5. Semester hinzugefügt.
14.07: Druckfunktion für die Seiten überarbeitet: Auflistung der Hyperlinks über Fußnoten realisiert.
04.06: Links für IT-Sec neu strukturiert
27.04: Projekte des SS02 hinzugefügt
07.04: Lehrveranstaltungen des SS02 hinzugefügt
07.04: Dokumentation zum X-Terminals Projekt
Navigation: [START] [00-ws] [prog] [p-java001.htm]
[print]  [e-mail]  [main]