Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hausaufgabe1 #123

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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
32 changes: 31 additions & 1 deletion app/src/main/java/htw/berlin/prog2/ha1/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package htw.berlin.prog2.ha1;

import org.w3c.dom.ls.LSOutput;

/**
* Eine Klasse, die das Verhalten des Online Taschenrechners imitiert, welcher auf
* https://www.online-calculator.com/ aufgerufen werden kann (ohne die Memory-Funktionen)
Expand All @@ -14,6 +16,9 @@ public class Calculator {

private String latestOperation = "";

private boolean wasCleared = false; // Neues Feld von mir hinzugefügt s0582356


/**
* @return den aktuellen Bildschirminhalt als String
*/
Expand Down Expand Up @@ -45,9 +50,19 @@ public void pressDigitKey(int digit) {
* im Ursprungszustand ist.
*/
public void pressClearKey() {
screen = "0";
/* screen = "0";
latestOperation = "";
latestValue = 0.0;
*/
if (wasCleared) {
screen = "0";
latestOperation = "";
latestValue = 0.0;
wasCleared = false;
} else {
screen = "0";
wasCleared = true;
}
}

/**
Expand All @@ -60,8 +75,16 @@ public void pressClearKey() {
* @param operation "+" für Addition, "-" für Substraktion, "x" für Multiplikation, "/" für Division
*/
public void pressBinaryOperationKey(String operation) {
/* latestValue = Double.parseDouble(screen);
latestOperation = operation;
*/
// umgeändert gefixt
if (!latestOperation.isEmpty()) {
pressEqualsKey(); // Führe die vorherige Operation aus, wenn sie existiert
}
latestValue = Double.parseDouble(screen);
latestOperation = operation;

}

/**
Expand All @@ -73,6 +96,11 @@ public void pressBinaryOperationKey(String operation) {
*/
public void pressUnaryOperationKey(String operation) {
latestValue = Double.parseDouble(screen);
// Überprüfen Sie, ob der aktuelle Wert 0 ist und die Inversion ausgewählt wurde
if(latestValue == 0.0 && operation.equals("1/x")) {
screen = "Error";
return; // Beenden Sie die Methode frühzeitig, da es einen Fehler gibt
}
latestOperation = operation;
var result = switch(operation) {
case "√" -> Math.sqrt(Double.parseDouble(screen));
Expand All @@ -81,6 +109,7 @@ public void pressUnaryOperationKey(String operation) {
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 Down Expand Up @@ -130,4 +159,5 @@ 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);
}

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


//------------------------------------------------------------------------------------
//TODO hier weitere Tests erstellen
// meine Arbeit s0582356
@Test
@DisplayName("Zwei positive Zahlen werden voneinander subtrahiert!")
void testPositiveSubtraktion() {
Calculator calc = new Calculator();

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

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

assertEquals(expected, actual);
}
// meine Arbeit s0582356 Inversion von 0 nicht möglich
@Test
@DisplayName("Die Inversion von 0 nicht möglich")
public void testInverseOfZero() {
// Neues Calculator-Objekt erstellen
Calculator calculator = new Calculator();

// Die Zahl "0" eingeben
calculator.pressDigitKey(0);

// Versuch, die Inversion zu berechnen
calculator.pressUnaryOperationKey("1/x");

// Das Ergebnis sollte "Error" sein, da die Inversion von 0 nicht definiert ist
assertEquals("Error", calculator.readScreen());
}
// meine Arbeit s0582356
@Test
public void testSingleClearPress() {
Calculator calculator = new Calculator();
calculator.pressDigitKey(5);
calculator.pressBinaryOperationKey("+");
calculator.pressDigitKey(3);
calculator.pressClearKey(); // Sollte nur den Bildschirm zurücksetzen
calculator.pressDigitKey(2);
calculator.pressEqualsKey(); // Das erwartete Ergebnis ist 5 + 2 = 7

assertEquals("7", calculator.readScreen());
}

// meine Arbeit s0582356

@Test
public void testConsecutiveBinaryOperations() {
Calculator calculator = new Calculator();
calculator.pressDigitKey(5);
calculator.pressBinaryOperationKey("+");
calculator.pressDigitKey(3);
calculator.pressBinaryOperationKey("-");
calculator.pressDigitKey(2);
calculator.pressEqualsKey();
assertEquals("6", calculator.readScreen()); // Erwarte 5 + 3 - 2 = 6
}



}