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