From b4ba006d3f821e3a0d5378a1732f73c93782be89 Mon Sep 17 00:00:00 2001 From: Kai Sellmann <2210892@stud.hs-mannheim.de> Date: Thu, 27 Apr 2023 10:46:35 +0200 Subject: [PATCH] update printf/suche/rechner --- .../auffrischung/printf/DoubleFormatter.java | 45 +++++++++++++++---- .../auffrischung/suchemax/GroessteZahl.java | 13 +++++- .../taschenrechner/Taschenrechner.java | 24 ++++++++-- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/sources/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java b/sources/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java index fc95fdf..60acb59 100644 --- a/sources/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java +++ b/sources/src/main/java/pr2/auffrischung/printf/DoubleFormatter.java @@ -1,18 +1,47 @@ package pr2.auffrischung.printf; +import java.text.DecimalFormat; +import java.text.NumberFormat; + public class DoubleFormatter { public static void printDouble(double d) { - // TODO: Wert ausgeben + String format = "0.000"; + NumberFormat formatter = new DecimalFormat(format); + String newD = formatter.format(d); + System.out.print("Mit Formatting: " + newD); + System.out.println(); } public static void main(String[] args) { - printDouble(1.0); - printDouble(10.1); - printDouble(2.01); - printDouble(2.001); - printDouble(2.0001); - printDouble(2.0004); - printDouble(2.0005); + double dVal1 = 1.0; + printDouble(dVal1); + System.out.printf("Mit printf: %.3f", dVal1); + System.out.println(); + double dVal2 = 10.1; + printDouble(dVal2); + System.out.printf("Mit printf: %.3f", dVal2); + System.out.println(); + double dVal3 = 2.01; + printDouble(dVal3); + System.out.printf("Mit printf: %.3f", dVal3); + System.out.println(); + double dVal4 = 2.001; + printDouble(dVal4); + System.out.printf("Mit printf: %.3f", dVal4); + System.out.println(); + double dVal5 = 2.0001; + printDouble(dVal5); + System.out.printf("Mit printf: %.3f", dVal5); + System.out.println(); + double dVal6 = 2.0004; + printDouble(dVal6); + System.out.printf("Mit printf: %.3f", dVal6); + System.out.println(); + double dVal7 = 2.0005; + printDouble(dVal7); + System.out.printf("Mit printf: %.3f", dVal7); + System.out.println(); + } } diff --git a/sources/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java b/sources/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java index 05954cd..54b8e75 100644 --- a/sources/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java +++ b/sources/src/main/java/pr2/auffrischung/suchemax/GroessteZahl.java @@ -5,8 +5,17 @@ public class GroessteZahl { public int sucheMax(int[] zahlen) { int max = 0; - // TODO: Methode implementieren - + for (int i = 0; i < zahlen.length; i++) { + if (max > zahlen[i]) { + max = zahlen[i]; + } + } + for (int i = 0; i < zahlen.length; i++) { + if (max < zahlen[i]) { + max = zahlen[i]; + } + + } return max; } diff --git a/sources/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java b/sources/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java index d0c6463..7f7de9e 100644 --- a/sources/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java +++ b/sources/src/main/java/pr2/auffrischung/taschenrechner/Taschenrechner.java @@ -3,8 +3,26 @@ package pr2.auffrischung.taschenrechner; public class Taschenrechner { public double rechne(double o1, char op, double o2) { - // TODO: Implementieren - return 0.0; + + switch (op) { + case '+': + return o1 + o2; + case '-': + return o1 - o2; + case '*': + return o1 * o2; + case '/': + if (o2 == 0) { + return o2; + } else { + return o1 / 02; + } + case '^': + return Math.pow(o1, o2); + default: + return 0.0; + } + } public static void main(String[] args) { @@ -13,6 +31,6 @@ public class Taschenrechner { System.out.println(t.rechne(1, '-', 2)); System.out.println(t.rechne(2, '*', 2)); System.out.println(t.rechne(4, '/', 2)); - System.out.println(t.rechne(2, '^', 32)); + System.out.println(t.rechne(2, '^', 3)); } } -- 2.43.0