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
8 changes: 4 additions & 4 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void pressBinaryOperationKey(String operation) {
* @param operation "√" für Quadratwurzel, "%" für Prozent, "1/x" für Inversion
*/
public void pressUnaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;
//latestValue = Double.parseDouble(screen);
//latestOperation = operation;
var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
Expand All @@ -83,7 +83,7 @@ public void pressUnaryOperationKey(String operation) {
screen = Double.toString(result);
if(screen.equals("NaN")) screen = "Error";
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);

if(screen.equals("Infinity")) screen = "Error";
}

/**
Expand Down Expand Up @@ -130,4 +130,4 @@ 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);
}
}
}
47 changes: 46 additions & 1 deletion 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,50 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen
}

// Aufgabe1
@Test
@DisplayName("should display result after minus two positive multi-digit numbers")
void testPositiveMinus() {
Calculator calc = new Calculator();
calc.pressDigitKey(3);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(1);
calc.pressDigitKey(0);
calc.pressEqualsKey();

String expected = "20";
String actual = calc.readScreen();

assertEquals(expected, actual);
}

//Aufgabe2 roter Test
@Test
@DisplayName("should display Error when trying to calculate 1/0")
void testInvertofZero(){
Calculator calculator = new Calculator();
calculator.pressDigitKey(0);
calculator.pressUnaryOperationKey("1/x");
String expected = "Error";
String actual = calculator.readScreen();
assertEquals(expected, actual);

}

// zweiter roter Test.
@Test
@DisplayName("schould display result when calculate Binary plus Unary ")
void testResultCalculateOfBinaryplusUnary(){
Calculator calculator = new Calculator();
calculator.pressDigitKey(5);
calculator.pressBinaryOperationKey("+");
calculator.pressDigitKey(4);
calculator.pressUnaryOperationKey("√");
calculator.pressEqualsKey();
String expected = "7";
String actual = calculator.readScreen();
assertEquals(expected, actual);
}
}