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
9 changes: 9 additions & 0 deletions app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void pressDigitKey(int digit) {

if(screen.equals("0") || latestValue == Double.parseDouble(screen)) screen = "";

if(latestOperation == "-" && latestValue == 0){
digit = digit * -1;
}

screen = screen + digit;
}

Expand Down Expand Up @@ -62,6 +66,8 @@ public void pressClearKey() {
public void pressBinaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
latestOperation = operation;


}

/**
Expand Down Expand Up @@ -127,5 +133,8 @@ public void pressEqualsKey() {
screen = Double.toString(result);
if(screen.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
if(screen.contains(".") && screen.length() > 11) screen = screen.substring(0, 10);
if(screen.contains("Infinity"))screen = ("Error");


}
}
65 changes: 65 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 @@ -40,6 +40,71 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

@Test
@DisplayName("should display result of two decimal numbers multiplied")
void testMultiplicationOfDecimalNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("x");
calc.pressDigitKey(6);
calc.pressDotKey();
calc.pressDigitKey(2);
calc.pressEqualsKey();

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

assertEquals(expected, actual);


}

@Test
@DisplayName("should display result of division by zero")
void testDivisionByZero() {
Calculator calc = new Calculator();

calc.pressDigitKey(8);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(0);
calc.pressEqualsKey();


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

assertEquals(expected, actual);
}

@Test
@DisplayName("should display result of two negative numbers subtracted")
void testSubtractionOfNegativeNumbers() {
Calculator calc = new Calculator();

calc.pressBinaryOperationKey("-");
calc.pressDigitKey(6);
calc.pressBinaryOperationKey("-");
calc.pressDigitKey(7);
calc.pressEqualsKey();


String expected = "-13";
String actual = calc.readScreen();

assertEquals(expected, actual);
}









//TODO hier weitere Tests erstellen
}