From 4128042528a111dc4908d568e96a0d477f67fb08 Mon Sep 17 00:00:00 2001 From: Karen Date: Thu, 14 Nov 2024 06:30:56 +0100 Subject: [PATCH 1/3] Teilaufgabe 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Grüner Test -> basic subtraction --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 16 ++++++++++++++++ 1 file changed, 16 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 ddff0da..cdfd95e 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,21 @@ void testMultipleDecimalDots() { //TODO hier weitere Tests erstellen + + //Teilaufgabe 1 + @Test + @DisplayName("Should display result after subtracting 2 single digit numbers") + public void testBasicSubtraction(){ + Calculator calc = new Calculator(); + calc.pressDigitKey(9); + calc.pressBinaryOperationKey("-"); + calc.pressDigitKey(3); + calc.pressEqualsKey(); + + String expected = "6"; + String actual=calc.readScreen(); + + assertEquals(expected, actual); + } } From eb1569d5aa8431510d79986412b5838ab4fb37c9 Mon Sep 17 00:00:00 2001 From: Karen Date: Thu, 14 Nov 2024 06:39:16 +0100 Subject: [PATCH 2/3] Teilaufgabe 2.1, 2.2, 2.3 Rote Tests : 2.1 -> should display error when calculating inverse of 0 2.2 ->Should correctly handle new input after binary operations 2.3 -> Should correctly handle negative operands in binary operations --- .../htw/berlin/prog2/ha1/CalculatorTest.java | 47 +++++++++++++++++++ 1 file changed, 47 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 cdfd95e..52fb02b 100644 --- a/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java +++ b/app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java @@ -106,5 +106,52 @@ public void testBasicSubtraction(){ assertEquals(expected, actual); } + + //Teilaufgabe 2.1 + @Test + @DisplayName("Should display error when calculating inverse of 0") + public void testInverseZero(){ + Calculator calc=new Calculator(); + calc.pressDigitKey(0); + calc.pressUnaryOperationKey("1/x"); + + String actual = calc.readScreen(); + + String expected="Error"; + + assertEquals(expected,actual); + } + //Teilaufgabe 2.2 + @Test + @DisplayName("Should correctly handle new input after binary operations") + void testWeirdFunctinalityOfPressDigitKey(){ + Calculator calc=new Calculator(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressDigitKey(5); + calc.pressDigitKey(3); + + String expected="53"; + String actual=calc.readScreen(); + + assertEquals(expected,actual); + } + + //Teilaufgabe 2.3 + @Test + @DisplayName("Should correctly handle negative operands in binary operations") + public void testAdditionOfnegatives(){ + Calculator calc = new Calculator(); + calc.pressDigitKey(5); + calc.pressBinaryOperationKey("+"); + calc.pressNegativeKey(); + calc.pressDigitKey(3); + calc.pressEqualsKey(); + + String expected="2"; + String actual = calc.readScreen(); + + assertEquals(expected, actual); + } } From b4f1f10ce09a8304576ea77046791ecf98042bf0 Mon Sep 17 00:00:00 2001 From: Karen Date: Thu, 14 Nov 2024 06:59:11 +0100 Subject: [PATCH 3/3] Teilaufgabe 3.1, 3.2, 3.3 Bug fixes zu Teilaufgabe 2.1, 2.2, 2.3 3.1 -> Bug Fix: Handling inversion of 0 3.2 -> Bug fix: Handling New Input After Binary Operations 3.3 -> Bug fix: Handling Negative Operands in Binary Operations --- .../java/htw/berlin/prog2/ha1/Calculator.java | 60 ++++++++++++++++++- 1 file changed, 58 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 84c04f2..d081801 100644 --- a/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java +++ b/app/src/main/java/htw/berlin/prog2/ha1/Calculator.java @@ -13,6 +13,7 @@ public class Calculator { private double latestValue; private String latestOperation = ""; + private boolean isNewInput = false; //Teilaufgabe 3.2 /** * @return den aktuellen Bildschirminhalt als String @@ -31,7 +32,21 @@ public String readScreen() { public void pressDigitKey(int digit) { if(digit > 9 || digit < 0) throw new IllegalArgumentException(); - if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; + // Teilaufgabe 3.2 + /** + * Bug fix: Handling New Input After Binary Operations + * - Introduced a boolean flag isNewInput to determine when to clear the screen + * for a new number after a binary operation. + * - This change ensures that when a user presses a binary operation key, the next + * digit entry starts a new number on the screen instead of appending to the previous one. + */ + + + if (isNewInput || screen.equals("0")) { + screen = ""; + isNewInput = false; // Reset flag after starting new input + } + //if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = ""; screen = screen + digit; } @@ -62,6 +77,10 @@ public void pressClearKey() { public void pressBinaryOperationKey(String operation) { latestValue = Double.parseDouble(screen); latestOperation = operation; + + //Teilaufgabe 3.2 + isNewInput = true; + } /** @@ -74,12 +93,29 @@ public void pressBinaryOperationKey(String operation) { public void pressUnaryOperationKey(String operation) { latestValue = Double.parseDouble(screen); latestOperation = operation; + + //Teilaufgabe 3.1 + /** + * Bug Fix: Handling inversion of 0 + * If the inverse operation (1/x) is performed with zero as the input, + * the screen will display "Error" instead of "Infinity" to indicate + * that division by zero is undefined. This prevents "Infinity" from being + * displayed and ensures a clear error message for invalid operations. + */ + + if (Double.parseDouble(screen) == 0 || operation.equals("1/x")){ + screen = "Error"; + return; + } + var result = switch(operation) { case "√" -> Math.sqrt(Double.parseDouble(screen)); case "%" -> Double.parseDouble(screen) / 100; case "1/x" -> 1 / Double.parseDouble(screen); 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); @@ -105,7 +141,27 @@ public void pressDotKey() { * entfernt und der Inhalt fortan als positiv interpretiert. */ public void pressNegativeKey() { - screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; + + //Teilaufgabe 3.3 + /** + * Bug fix: Handling Negative Operands in Binary Operations + * - When called after a binary operation (e.g., "+"), this method clears the screen and + * starts a new input sequence with a negative sign. + * - Sets the screen to "-" if it is the start of a new input, allowing the next digit entry + * to represent a negative number. + * - Resets isNewInput to false after setting the negative sign, ensuring that further + * digit entries will append to the current screen display. + */ + if (screen.equals("0") || isNewInput) { + screen = "-"; // Start with the negative sign for the next input + isNewInput = false; // Reset to allow input continuation + } else if (screen.startsWith("-")) { + screen = screen.substring(1); // Remove the negative sign if it's already present + } else { + screen = "-" + screen; // Add the negative sign if not present + } + + //screen = screen.startsWith("-") ? screen.substring(1) : "-" + screen; } /**