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
15 changes: 13 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 @@ -48,6 +48,7 @@ public void pressClearKey() {
screen = "0";
latestOperation = "";
latestValue = 0.0;

}

/**
Expand All @@ -72,15 +73,18 @@ 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;

var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
case "%" -> Double.parseDouble(screen) / 100;
case "1/x" -> 1 / Double.parseDouble(screen);
case "%" -> (latestValue / 100) * Double.parseDouble(screen);

default -> throw new IllegalArgumentException();
};
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);

}
Expand Down Expand Up @@ -127,5 +131,12 @@ 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.equals("Infinity")) screen = "Error";
if(screen.startsWith("-")) ;

}




}
129 changes: 128 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 @@ -26,6 +26,7 @@ void testPositiveAddition() {
assertEquals(expected, actual);
}


@Test
@DisplayName("should display result after getting the square root of two")
void testSquareRoot() {
Expand All @@ -40,6 +41,132 @@ void testSquareRoot() {
assertEquals(expected, actual);
}

//TODO hier weitere Tests erstellen

@Test
@DisplayName("should display result after dividing two numbers")
void testDivide() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDigitKey(0);
calc.pressBinaryOperationKey("/");
calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}


//failed
@Test
@DisplayName("should display result after dividing a number with zero")
void testDivideWithZero() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}


@Test
@DisplayName("should display result after adding two comma numbers")
void testAddingTwoCommaNumbers() {
Calculator calc = new Calculator();

calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("+");
calc.pressDigitKey(2);
calc.pressDotKey();
calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}


//failed
/*@Test
@DisplayName("should display result after multiplying two negative numbers")
void testAddingTwoNegativeNumbers() {
Calculator calc = new Calculator();

calc.pressNegativeKey();
calc.pressDigitKey(5);
calc.pressBinaryOperationKey("x");
calc.pressNegativeKey();
calc.pressDigitKey(5);
calc.pressEqualsKey();

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

assertEquals(expected, actual);
}*/

@Test
@DisplayName("should clear display after pressing clear")
void clearDisplay() {
Calculator calc = new Calculator();

calc.pressClearKey();

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

assertEquals(expected, actual);
}


/* @Test
@DisplayName("%")
void testPercentage() {
Calculator calc = new Calculator();


calc.pressDigitKey(5);
calc.pressDigitKey(0);
calc.pressUnaryOperationKey("%");
calc.pressDigitKey(7);
calc.pressDigitKey(0);

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

assertEquals(expected, actual);
}*/

@Test
@DisplayName("should display square root ")
void testSqrt() {
Calculator calc = new Calculator();


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

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

assertEquals(expected, actual);
}

}