Skip to content
Open

ha1 #92

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
Binary file added app/src/main/java/htw/berlin/prog2/.DS_Store
Binary file not shown.
13 changes: 9 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 @@ -45,9 +45,14 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
if(screen.equals ("0")){
latestValue = 0.0;
latestOperation = "";
}
screen = "0";
latestOperation = "";
latestValue = 0.0;
}
public String giveLatestValue(){
return Double.toString(latestValue);
}

/**
Expand Down Expand Up @@ -83,7 +88,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.endsWith(".0")) screen = screen.substring(0,screen.length()-2);
}

/**
Expand Down Expand Up @@ -130,4 +135,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);
}
}
}
Binary file added app/src/test/java/htw/berlin/prog2/.DS_Store
Binary file not shown.
48 changes: 48 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,53 @@ void testMultipleDecimalDots() {


//TODO hier weitere Tests erstellen
@Test
@DisplayName("should calculate the result of multiple additions sequentially")
void testMultipleAdditions() {
Calculator calc = new Calculator();
calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressEqualsKey(); // Ergebnis: 3
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(3);
calc.pressEqualsKey(); // Ergebnis: 6
String expected = "6";
String actual = calc.readScreen();
assertEquals(expected, actual);
}



@Test
@DisplayName("should display result after getting the scare root of 4")
void testSquareRootOfFour() {
Calculator calc = new Calculator();

calc.pressDigitKey(4);
calc.pressUnaryOperationKey("√");

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

assertEquals(expected, actual);
}

@Test
@DisplayName("the latest value should not be deleted after pressing the Clear Key once")
void testCeKey() {
Calculator calc = new Calculator();

calc.pressDigitKey(1);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(1);
calc.pressClearKey();

String expected = "1.0";
String actual = calc.giveLatestValue();

assertEquals(expected, actual);
}


}