Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -62,6 +77,10 @@ public void pressClearKey() {
public void pressBinaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;

//Teilaufgabe 3.2
isNewInput = true;

}

/**
Expand All @@ -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);
Expand All @@ -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;
}

/**
Expand Down
63 changes: 63 additions & 0 deletions app/src/test/java/htw/berlin/prog2/ha1/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,68 @@ 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);
}

//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);
}
}