Übung 3.a.1.1:
1 | Math.pow(7,5); |
Übung 3.a.1.2:
1 | Math.pow(7,(5+3)); |
Übung 3.a.1.3:
1 | Math.pow(7,5+Math.pow(9,4)); |
Übung 3.a.1.4:
1 | Math.pow((2 + Math.cos(Math.PI/2)),2) + 3 / Math.sin(0.32); |
Übung 3.a.1.5: Allen aufgefallen, dass diese Nummer übersprungen wurde? :D
Übung 3.a.1.6:
1 | Math.pow(Math.E, x) + Math.sin(x) + Math.sqrt(x); |
Übung 3.a.1.7:
1 | Math.sin(x); |
Übung 3.a.1.8:
1 | Math.pow(Math.E,Math.sin(Math.sqrt(x))); |
Übung 3.a.1.9:
1 | Math.max(x,y) + Math.max(a,b); |
Übung 3.a.1.10:
1 | Math.max(a, Math.max(b,c)); |
Übung 3.a.2.1:
39 40 41 | public static int maxVon(int i1, int i2) { return i1 >= i2 ? i1 : i2; // oder Math.max(i1,i2); } |
Übung 3.a.2.2:
43 44 45 | public static double hoch3Von(double basis) { return basis * basis * basis; // oder Math.pow(basis, 3); } |
Übung 3.a.2.3:
47 48 49 | public static boolean istGroesser5(int zahl) { return zahl > 5; } |
Übung 3.a.2.4:
51 52 53 | public static boolean istGroesser5(double zahl) { return Double.compare(zahl, 5) > 0; } |
Übung 3.b.1:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public static void test3a() { float x = 1; float y = 1; float a = 1; float b = 1; float c = 1; cout.println("3.a.1.1: 7^5 = " + Math.pow(7, 5)); cout.println("3.a.1.2: 7^(5+3) = " + Math.pow(7, (5 + 3))); cout.println("3.a.1.3: 7^(5+9^4) = " + Math.pow(7, 5 + Math.pow(9, 4))); cout.println("3.a.1.4: (2+cos(PI/2))^2 + 3 / sin(0.32) = " + Math.pow((2 + Math.cos(Math.PI / 2)), 2) + 3 / Math.sin(0.32)); cout.println("3.a.1.6: e^x + sin(x) + sqrt(x) = " + Math.pow(Math.E, x) + Math.sin(x) + Math.sqrt(x)); cout.println("3.a.1.7: sin(x) = " + Math.sin(x)); cout.println("3.a.1.8: e^(sin(sqrt(x)) = " + Math.pow(Math.E, Math.sin(Math.sqrt(x)))); cout.println("3.a.1.9: max(x,y) + max(a,b) = " + Math.max(x, y) + Math.max(a, b)); cout.println("3.a.1.10: max(a,b,c) = " + Math.max(a, Math.max(b, c))); } |
Übung 3.b.2:
31 32 33 34 35 36 37 | public static void test3b() { cout.println("maxVon(3,5) = " + maxVon(3, 5)); cout.println("hoch3Von(2) = " + hoch3Von(2)); cout.println("istGroesser5(10) = " + istGroesser5(10)); cout.println("istGroesser5(5) = " + istGroesser5(5)); cout.println("istGleich5(5.0) = " + istGleich5(5.0)); } |
Übung 3.c.1:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Square root approximation. * * @param x - a value * @return Returns the positive square root of a double value. */ public static double sqrt(int x) { if (x == 0) return 0; if (x < 0) return Double.NaN; double root = x; double a = root; do { a = root; root = (a + x / a) / 2; } while (a - root > 0.0000001); return root; } |
Übung 3.c.2:
7 8 9 10 11 12 13 14 15 16 17 18 | static PrintWriter cout = new PrintWriter(System.out, true); public static void main(String[] args) { for (int i = 0; i < 16; ++i) { cout.println("[Org] Quadratwurzel von " + i + " ist annähernd " + Math.sqrt(i)); cout.println("Quadratwurzel von " + i + " ist annähernd " + sqrt(i)); } cout.println(maxIntValue()); cout.println(Integer.MAX_VALUE); } |
Übung 3.d:
38 39 40 | public static int maxIntValue() { return (int) (Math.pow(2, 31) - 1); } |
Wenn euch das eben schon verwirrt hat, es geht noch besser mit der inversen Wurzel.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /** * Fast approximation of 1.0 / sqrt(x) after John Carmack (Quake 3) * <p> * See <a href="http://www.beyond3d.com/content/articles/8/">http://www.beyond3d.com/content/articles/8/</a> * * @param x Positive value to estimate inverse of square root of * @return Approximately 1.0 / sqrt(x) */ public static double invSqrt(double x) { double xhalf = 0.5 * x; long i = Double.doubleToRawLongBits(x); i = 0x5FE6EB50C7B537AAL - (i >> 1); // original: 0x5f3759df; x = Double.longBitsToDouble(i); x = x * (1.5 - xhalf * x * x); return x; } |
Aufgabe 3b
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class TestSchachbrett { public static final String FILEPATH = "assets/files/"; public static final String FILENAME = "schachbrett.txt"; public static void main(String[] args) { // naively creating folder if not exist File f = new File(FILEPATH); f.mkdirs(); printSchachbrett(FILEPATH + FILENAME, false); } public static void printSchachbrett(String filename, boolean isReverse) { // wrap PrintWriter around DirtyFileWriter PrintWriter out = new PrintWriter(new DirtyFileWriter(filename), true); if (isReverse) { Schachbrett.printSchachbrett(out); } else { Schachbrett.printSchachbrettReverse(out); } out.close(); } } |
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class Hypothenuse { public static void main(String [] args) { Hypothenuse.testHypothenuse(7, 2, new PrintWriter(System.out, true)); } public static double hypothenuse(double k1, double k2) { return Math.sqrt(k1 * k1 + k2 * k2); // also Math.hypot(k1,k2); } public static void printHypothenuse(double k1, double k2, int width, int precision, PrintWriter out) { out.printf("%" + width + "." + precision + "f", hypothenuse(k1, 2)); } public static void testHypothenuse(int width, int precision, PrintWriter out) { // cols title for (int i = 0; i <= 10; ++i) { out.printf("%" + width + "d", i); } // line break out.println(); // first cols for (int y = 1; y <= 10; ++y) { // row title out.printf("%" + width + "d", y); // then rows for (int x = 1; x <= 10; ++x) { printHypothenuse(x, y, width, precision, out); } // line break each 10 values out.println(); } } } |
Aufgabe 3c
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public class Aufgabe03 { public static final String FILEPATH = "assets/files/"; public static final String FILENAME_INT_NUMBERS = "IntNumberFile.txt"; public static final String FILENAME_MIXED_NUMBERS = "MixedNumberFile.txt"; public static void main(String[] args) { // naively creating folder if not exist File f = new File(FILEPATH); f.mkdirs(); writeIntNumberFile(FILEPATH + FILENAME_INT_NUMBERS, 10000); writeMixedNumberFile(FILEPATH + FILENAME_MIXED_NUMBERS, 10000, 9); } public static void writeIntNumberFile(String filename, int numberCount) { PrintWriter fout = new PrintWriter(new DirtyFileWriter(filename)); for (int i = 1; i <= numberCount; i++) { fout.printf("%7d ", (int) (Math.random() * numberCount)); // line break every 10 numbers if (i % 10 == 0) { fout.println(); } } fout.close(); } public static void writeMixedNumberFile(String filename, int numberCount, int width) { PrintWriter fout = new PrintWriter(new DirtyFileWriter(filename)); for (int i = 1; i <= numberCount; i++) { // 50 int : 50 double numbers if (Math.random() < 0.5) { fout.printf("%" + width + ".2f ", (Math.random() * numberCount)); } else { fout.printf("%" + width + "d ", (int) (Math.random() * numberCount)); } // line break each 10 numbers if (i % 10 == 0) { fout.println(); } } fout.close(); } } |
Leave a Reply