From 5a5391e4f28903fe8fdb258eda3e7233dca0cf95 Mon Sep 17 00:00:00 2001 From: Altan Date: Sun, 22 Oct 2023 17:35:42 +0200 Subject: [PATCH 1/7] Erster erfolgreicher Test. Subtraktion von 2 Zahlen --- .../java/htw/berlin/prog2/ha1/Calculator.java | 3 +++ .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 84c04f21..cdbce094 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -1,5 +1,7 @@ package htw.berlin.prog2.ha1; +import org.w3c.dom.ls.LSOutput; + /** * Eine Klasse, die das Verhalten des Online Taschenrechners imitiert, welcher auf * https://www.online-calculator.com/ aufgerufen werden kann (ohne die Memory-Funktionen) @@ -130,4 +132,5 @@ public void pressEqualsKey() { if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); } + } diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index ddff0daf..04a0b30f 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -90,5 +90,22 @@ void testMultipleDecimalDots() { //TODO hier weitere Tests erstellen + @Test + @DisplayName("Zwei positive Zahlen werden voneinander subtrahiert!") + void testPositiveSubtraktion() { + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(2); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "0"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } } From 3cdbeb2c36e918038b1a96afee17378b6d923bea Mon Sep 17 00:00:00 2001 From: Altan Date: Sun, 22 Oct 2023 21:02:10 +0200 Subject: [PATCH 2/7] =?UTF-8?q?Zweiter=20Test=20der=20fehlschl=C3=A4gt,=20?= =?UTF-8?q?weil=20die=20Inversion=20mit=200=20nicht=20inbegriffen=20ist.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index 04a0b30f..5c6cd24e 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -90,6 +90,7 @@ void testMultipleDecimalDots() { //TODO hier weitere Tests erstellen + // meine Arbeit s0582356 @Test @DisplayName("Zwei positive Zahlen werden voneinander subtrahiert!") void testPositiveSubtraktion() { @@ -107,5 +108,21 @@ void testPositiveSubtraktion() { assertEquals(expected, actual); } + // s0582356 Inversion von 0 nicht möglich + @Test + public void testInverseOfZero() { + // Neues Calculator-Objekt erstellen + Calculator calculator = new Calculator(); + + // Die Zahl "0" eingeben + calculator.pressDigitKey(0); + + // Versuch, die Inversion zu berechnen + calculator.pressUnaryOperationKey("1/x"); + + // Das Ergebnis sollte "Error" sein, da die Inversion von 0 nicht definiert ist + assertEquals("Error", calculator.readScreen()); + } + } From 620def4a8162eebb6a8992b5a8ed92fc5035b97a Mon Sep 17 00:00:00 2001 From: Altan Date: Sun, 22 Oct 2023 21:18:23 +0200 Subject: [PATCH 3/7] =?UTF-8?q?Zweiter=20Test=20der=20jetzt=20gr=C3=BCn=20?= =?UTF-8?q?ist!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/htw/berlin/prog2/ha1/Calculator.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index cdbce094..6fa2ea9f 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -75,6 +75,11 @@ public void pressBinaryOperationKey(String operation) { */ public void pressUnaryOperationKey(String operation) { latestValue = Double.parseDouble(screen); + // Überprüfen Sie, ob der aktuelle Wert 0 ist und die Inversion ausgewählt wurde + if(latestValue == 0.0 && operation.equals("1/x")) { + screen = "Error"; + return; // Beenden Sie die Methode frühzeitig, da es einen Fehler gibt + } latestOperation = operation; var result = switch(operation) { case "√" -> Math.sqrt(Double.parseDouble(screen)); @@ -83,6 +88,7 @@ public void pressUnaryOperationKey(String operation) { default -> throw new IllegalArgumentException(); }; screen = Double.toString(result); + if(screen.equals("NaN")) screen = "Error"; if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); From 5e4255e72513106a8b9607d3ca69e09b75d90de9 Mon Sep 17 00:00:00 2001 From: Altan Date: Mon, 23 Oct 2023 19:33:03 +0200 Subject: [PATCH 4/7] Dritter Test testSinglePress() ist fehlgeschlagen! --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index 5c6cd24e..c6d5791a 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -108,8 +108,9 @@ void testPositiveSubtraktion() { assertEquals(expected, actual); } - // s0582356 Inversion von 0 nicht möglich + // meine Arbeit s0582356 Inversion von 0 nicht möglich @Test + @DisplayName("Die Inversion von 0 nicht möglich") public void testInverseOfZero() { // Neues Calculator-Objekt erstellen Calculator calculator = new Calculator(); @@ -123,6 +124,20 @@ public void testInverseOfZero() { // Das Ergebnis sollte "Error" sein, da die Inversion von 0 nicht definiert ist assertEquals("Error", calculator.readScreen()); } + // meine Arbeit s0582356 + @Test + public void testSingleClearPress() { + Calculator calculator = new Calculator(); + calculator.pressDigitKey(5); + calculator.pressBinaryOperationKey("+"); + calculator.pressDigitKey(3); + calculator.pressClearKey(); // Sollte nur den Bildschirm zurücksetzen + calculator.pressDigitKey(2); + calculator.pressEqualsKey(); // Das erwartete Ergebnis ist 5 + 2 = 7 + + assertEquals("7", calculator.readScreen()); + } + } From 7cd58022808f8fd6cde355dff6a7ec4e84183641 Mon Sep 17 00:00:00 2001 From: Altan Date: Mon, 23 Oct 2023 19:37:28 +0200 Subject: [PATCH 5/7] =?UTF-8?q?Dritter=20Test=20testSinglePress()=20ist=20?= =?UTF-8?q?somit=20gr=C3=BCn=20und=20Test=20ist=20bestanden!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/htw/berlin/prog2/ha1/Calculator.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 6fa2ea9f..790182ca 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -16,6 +16,9 @@ public class Calculator { private String latestOperation = ""; + private boolean wasCleared = false; // Neues Feld von mir hinzugefügt s0582356 + + /** * @return den aktuellen Bildschirminhalt als String */ @@ -47,9 +50,19 @@ public void pressDigitKey(int digit) { * im Ursprungszustand ist. */ public void pressClearKey() { - screen = "0"; + /* screen = "0"; latestOperation = ""; latestValue = 0.0; + */ + if (wasCleared) { + screen = "0"; + latestOperation = ""; + latestValue = 0.0; + wasCleared = false; + } else { + screen = "0"; + wasCleared = true; + } } /** From 4d54dea6b0b585b6f5f6bf541b4b416bb833d85e Mon Sep 17 00:00:00 2001 From: Altan Date: Tue, 24 Oct 2023 13:43:47 +0200 Subject: [PATCH 6/7] =?UTF-8?q?Extra=20Test=20mit=20zwei=20aufeinanderfolg?= =?UTF-8?q?enden=20bin=C3=A4ren=20Operanden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java index c6d5791a..f63f9d33 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -88,7 +88,7 @@ void testMultipleDecimalDots() { assertEquals(expected, actual); } - + //------------------------------------------------------------------------------------ //TODO hier weitere Tests erstellen // meine Arbeit s0582356 @Test @@ -138,6 +138,21 @@ public void testSingleClearPress() { assertEquals("7", calculator.readScreen()); } + // meine Arbeit s0582356 + + @Test + public void testConsecutiveBinaryOperations() { + Calculator calculator = new Calculator(); + calculator.pressDigitKey(5); + calculator.pressBinaryOperationKey("+"); + calculator.pressDigitKey(3); + calculator.pressBinaryOperationKey("-"); + calculator.pressDigitKey(2); + calculator.pressEqualsKey(); + assertEquals("6", calculator.readScreen()); // Erwarte 5 + 3 - 2 = 6 + } + + } From a6b0c3cdfcd0cda23d434c10d3102e557fd30d5f Mon Sep 17 00:00:00 2001 From: Altan Date: Tue, 24 Oct 2023 13:46:32 +0200 Subject: [PATCH 7/7] =?UTF-8?q?Extra=20Test=20mit=20zwei=20aufeinanderfolg?= =?UTF-8?q?enden=20bin=C3=A4ren=20Operanden,=20dass=20jetzt=20gr=C3=BCn=20?= =?UTF-8?q?durchl=C3=A4uft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/htw/berlin/prog2/ha1/Calculator.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java index 790182ca..e81f3e5b 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -75,8 +75,16 @@ public void pressClearKey() { * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ public void pressBinaryOperationKey(String operation) { + /* latestValue = Double.parseDouble(screen); + latestOperation = operation; + */ + // umgeändert gefixt + if (!latestOperation.isEmpty()) { + pressEqualsKey(); // Führe die vorherige Operation aus, wenn sie existiert + } latestValue = Double.parseDouble(screen); latestOperation = operation; + } /**