From c5622323046065bbaa6cea71808eeb42f36fc6db Mon Sep 17 00:00:00 2001 From: thuy Date: Mon, 18 Oct 2021 18:16:03 +0200 Subject: [PATCH 1/8] do task 1 --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 18 ++++++++++++++++++ 1 file changed, 18 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 addc5f26..64e13ee2 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -41,5 +41,23 @@ void testSquareRoot() { } //TODO hier weitere Tests erstellen + + @Test + @DisplayName("should display result after multiply two positive multi-digit numbers") + void testPositiveMultiplikation(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressBinaryOperationKey("x"); + calc.pressDigitKey(1); + calc.pressDigitKey(0); + calc.pressDigitKey(0); + calc.pressEqualsKey(); + + String expected = "200"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } } From 329ae2001f9c09f7e1110b2c2c9d6839da705200 Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 24 Oct 2021 18:41:52 +0200 Subject: [PATCH 2/8] =?UTF-8?q?1.=20Commit=20f=C3=BCr=20neuer=20gr=C3=BCne?= =?UTF-8?q?r=20Test(bitte=20ignorieren=20Sie=20den=20anderen=20Commit)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/htw/berlin/prog2/ha1/CalculatorTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 64e13ee2..c37f5ade 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -43,21 +43,20 @@ void testSquareRoot() { //TODO hier weitere Tests erstellen @Test - @DisplayName("should display result after multiply two positive multi-digit numbers") - void testPositiveMultiplikation(){ + @DisplayName("should display result after adding three positive single-digit numbers") + void testReadScreen(){ Calculator calc = new Calculator(); - calc.pressDigitKey(2); - calc.pressBinaryOperationKey("x"); + calc.pressDigitKey(5); + calc.pressDotKey(); calc.pressDigitKey(1); - calc.pressDigitKey(0); - calc.pressDigitKey(0); - calc.pressEqualsKey(); - String expected = "200"; + String expected = "5.1"; String actual = calc.readScreen(); assertEquals(expected, actual); } + + } From e18e733f211bb010e9c003a78f98c90330780d51 Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 24 Oct 2021 18:46:29 +0200 Subject: [PATCH 3/8] =?UTF-8?q?2.=20Commit=20f=C3=BCr=20neuer=20roter=20Te?= =?UTF-8?q?st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 18 ++++++++++++++++++ 1 file changed, 18 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 c37f5ade..458e192a 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -58,5 +58,23 @@ void testReadScreen(){ } + @Test + @DisplayName("should display result after adding three positive single-digit numbers") + void testAdditionThree(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(1); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(4); + calc.pressEqualsKey(); + + String expected = "10"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + } From 01a05301f1492cede59427e6001485ab86c8de8a Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 24 Oct 2021 18:53:16 +0200 Subject: [PATCH 4/8] =?UTF-8?q?3.=20Commit=20f=C3=BCr=20den=20Fix=20zu=20d?= =?UTF-8?q?iesem=20Test?= 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 | 9 +++++++++ 1 file changed, 9 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 aaef8862..8db5c923 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -14,6 +14,7 @@ public class Calculator { private String latestOperation = ""; + /** * @return den aktuellen Bildschirminhalt als String */ @@ -34,6 +35,7 @@ public void pressDigitKey(int digit) { if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; screen = screen + digit; + } /** @@ -60,8 +62,14 @@ public void pressClearKey() { * @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division */ public void pressBinaryOperationKey(String operation) { + + if (!(latestOperation.equals(""))) { + pressEqualsKey(); + } + latestValue = Double.parseDouble(screen); latestOperation = operation; + } /** @@ -128,4 +136,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); } + } From d28956e9705d43d46dd50f12ef3f85019071815f Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 24 Oct 2021 19:32:09 +0200 Subject: [PATCH 5/8] =?UTF-8?q?4.=20Commit=20f=C3=BCr=20weiteren=20neuen?= =?UTF-8?q?=20roter=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) 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 458e192a..5bf0bc16 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -43,8 +43,8 @@ void testSquareRoot() { //TODO hier weitere Tests erstellen @Test - @DisplayName("should display result after adding three positive single-digit numbers") - void testReadScreen(){ + @DisplayName("should display result after adding a dot") + void testDot(){ Calculator calc = new Calculator(); calc.pressDigitKey(5); @@ -76,5 +76,21 @@ void testAdditionThree(){ assertEquals(expected, actual); } + @Test + @DisplayName("should display text after getting the square root of minus two") + void testNegaitveSquareRoot(){ + Calculator calc = new Calculator(); + + calc.pressDigitKey(2); + calc.pressNegativeKey(); + calc.pressUnaryOperationKey("√"); + + String expected = "-2"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } + + } From 7b5f87913cbfbca6af3c115297eccf13845199dc Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 24 Oct 2021 19:45:24 +0200 Subject: [PATCH 6/8] =?UTF-8?q?5.=20Commit=20f=C3=BCr=20Fix=20zu=20diesem?= =?UTF-8?q?=20=20Test?= 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 | 5 +++++ app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) 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 8db5c923..140e4f86 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -80,6 +80,11 @@ public void pressBinaryOperationKey(String operation) { * @param operation "√" für Quadratwurzel, "%" für Prozent, "1/x" für Inversion */ public void pressUnaryOperationKey(String operation) { + + if (Double.parseDouble(screen) < 0) { + throw new IllegalArgumentException("Wurzel aus negativen Zahlen kann man nicht ziehen!"); + } + latestValue = Double.parseDouble(screen); latestOperation = operation; var result = switch(operation) { 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 5bf0bc16..25de636f 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; @DisplayName("Retro calculator") class CalculatorTest { @@ -83,12 +84,8 @@ void testNegaitveSquareRoot(){ calc.pressDigitKey(2); calc.pressNegativeKey(); - calc.pressUnaryOperationKey("√"); - - String expected = "-2"; - String actual = calc.readScreen(); - assertEquals(expected, actual); + assertThrows(IllegalArgumentException.class, () -> {calc.pressUnaryOperationKey("√");} ); } From 85832e83d189c78fd2e0fe7dd4ed0a2f92ebc030 Mon Sep 17 00:00:00 2001 From: thuy Date: Tue, 26 Oct 2021 18:22:23 +0200 Subject: [PATCH 7/8] 6. Commit: Nachreichen - "Error" mit Exception --- app/src/main/java/htw/berlin/prog2/ha1/Calculator.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 140e4f86..a3157be6 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -69,7 +69,6 @@ public void pressBinaryOperationKey(String operation) { latestValue = Double.parseDouble(screen); latestOperation = operation; - } /** @@ -82,11 +81,12 @@ public void pressBinaryOperationKey(String operation) { public void pressUnaryOperationKey(String operation) { if (Double.parseDouble(screen) < 0) { - throw new IllegalArgumentException("Wurzel aus negativen Zahlen kann man nicht ziehen!"); + throw new IllegalArgumentException("Error"); } latestValue = Double.parseDouble(screen); latestOperation = operation; + var result = switch(operation) { case "√" -> Math.sqrt(Double.parseDouble(screen)); case "%" -> Double.parseDouble(screen) / 100; @@ -130,6 +130,11 @@ public void pressNegativeKey() { * und das Ergebnis direkt angezeigt. */ public void pressEqualsKey() { + + /*if(latestOperation == "/"){ + screen = "Error"; + }*/ + var result = switch(latestOperation) { case "+" -> latestValue + Double.parseDouble(screen); case "-" -> latestValue - Double.parseDouble(screen); From 675c009511138ac7e8ec5e385f4adcd2d055ba9c Mon Sep 17 00:00:00 2001 From: thuy Date: Sun, 31 Oct 2021 16:56:13 +0100 Subject: [PATCH 8/8] 7. Commit: Nachreichen - "Error" mit Exception --- .../java/htw/berlin/prog2/ha1/Calculator.java | 26 ++++++++++++------- .../htw/berlin/prog2/ha1/CalculatorTest.java | 8 +++--- 2 files changed, 21 insertions(+), 13 deletions(-) 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 a3157be6..2a1526f5 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -19,9 +19,16 @@ public class Calculator { * @return den aktuellen Bildschirminhalt als String */ public String readScreen() { + + if(screen.equals("NaN")){ + screen = "Error"; + } + return screen; } + + /** * Empfängt den Wert einer gedrückten Zifferntaste. Da man nur eine Taste auf einmal * drücken kann muss der Wert positiv und einstellig sein und zwischen 0 und 9 liegen. @@ -80,10 +87,6 @@ public void pressBinaryOperationKey(String operation) { */ public void pressUnaryOperationKey(String operation) { - if (Double.parseDouble(screen) < 0) { - throw new IllegalArgumentException("Error"); - } - latestValue = Double.parseDouble(screen); latestOperation = operation; @@ -93,9 +96,9 @@ public void pressUnaryOperationKey(String operation) { case "1/x" -> 1 / Double.parseDouble(screen); default -> throw new IllegalArgumentException(); }; + screen = Double.toString(result); if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10); - } /** @@ -117,7 +120,14 @@ public void pressDotKey() { * entfernt und der Inhalt fortan als positiv interpretiert. */ public void pressNegativeKey() { - screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; + //screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; + + if(screen.startsWith("-")){ + screen = screen.substring(1); + + } else{ + screen = "-" + screen; + } } /** @@ -131,10 +141,6 @@ public void pressNegativeKey() { */ public void pressEqualsKey() { - /*if(latestOperation == "/"){ - screen = "Error"; - }*/ - var result = switch(latestOperation) { case "+" -> latestValue + Double.parseDouble(screen); case "-" -> latestValue - Double.parseDouble(screen); 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 25de636f..273dc471 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -84,10 +84,12 @@ void testNegaitveSquareRoot(){ calc.pressDigitKey(2); calc.pressNegativeKey(); + calc.pressUnaryOperationKey("√"); - assertThrows(IllegalArgumentException.class, () -> {calc.pressUnaryOperationKey("√");} ); - } - + String expected = "Error"; + String actual = calc.readScreen(); + assertEquals(expected, actual); + } }