Skip to content
Snippets Groups Projects
Commit 9827268a authored by laith rustom's avatar laith rustom
Browse files

final commit : Laith and Marwa

parent d550996f
No related branches found
No related tags found
No related merge requests found
Showing
with 225 additions and 22 deletions
a) Warum kann der Taschenrechner ohne Fehler gestartet werden, obwohl die verwendeten Methoden gelöscht wurden?
Der Taschenrechner kann ohne Fehler gestartet werden, weil die Standardimplementierungen der Methoden toString,
equals und hashCode aus der Klasse Object noch verfügbar sind. Jede Klasse in Java erbt diese Methoden automatisch von Object,
also sind sie auch dann noch vorhanden, wenn sie nicht explizit in der Klasse Value definiert sind.
b) Welche Auswirkungen hat das Löschen der toString-Methode auf die Funktion des Taschenrechners?
Das Löschen der toString-Methode führt dazu, dass die Standardimplementierung von toString aus der Klasse Object verwendet wird.
Diese Standardimplementierung gibt einen String zurück, der aus dem Klassennamen, dem "@"-Zeichen und dem Hashcode des Objekts in Hexadezimal besteht.
Wenn der Taschenrechner das Ergebnis anzeigt, wird er also etwas wie Value@5f1dc517 anzeigen, anstatt des tatsächlichen numerischen Wertes.
c) Welche Auswirkungen hat das Löschen der equals- und hashCode-Methoden auf die Funktion des Taschenrechners?
Das Löschen der equals- und hashCode-Methoden führt dazu,
dass die Standardimplementierungen aus der Klasse Object verwendet werden.
Die Standard-Equals-Methode prüft, ob zwei Referenzen auf dasselbe Objekt zeigen (Referenzgleichheit),
und die Standard-hashCode-Methode gibt eine eindeutige Ganzzahl für jedes Objekt zurück. Das bedeutet,
dass die Funktion des Taschenrechners zur Überprüfung, ob ein Ergebnis schon einmal aufgetreten ist (in der Ergebnisspeicherung),
nicht korrekt funktioniert, da gleiche Werte nicht korrekt erkannt werden, wenn sie verschiedene Objekte mit demselben Wert sind.
d) Wie verhält sich der Taschenrechner, wenn Sie eine equals-Methode implementieren, die immer true zurückgibt?
Wenn die equals-Methode immer true zurückgibt, betrachtet der Taschenrechner alle Value-Objekte als gleich,
unabhängig von ihren tatsächlichen Werten. Das führt dazu, dass die Ergebnisspeicherung falsch funktioniert und anzeigt,
dass jedes neue Ergebnis schon einmal aufgetreten ist. Die Anzeige wird zeigen, dass das Ergebnis in vorherigen Schritten aufgetreten ist,
selbst wenn es ein neues Ergebnis ist.
e) Was passiert, wenn Ihre toString-Methode null zurückgibt?
Wenn die toString-Methode null zurückgibt, haben die Label-Komponenten in der GUI, die das Ergebnis anzeigen,
ein Problem, weil sie einen nicht-null String zum Anzeigen erwarten. Dies wird wahrscheinlich zu einer NullPointerException führen,
wenn das resultLabel versucht, seinen Text auf null zu aktualisieren.
\ No newline at end of file
package de.upb.se.profcalculator;
public class Divide {
private Value leftValue;
private Value rightValue;
public Divide(Value leftValue, Value rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public Value evaluate() {
int result = leftValue.getValue() / rightValue.getValue();
return new Value(result);
}
public String returnExpressionString() {
return leftValue.toString() + " / " + rightValue.toString();
}
public int evaluateExpression() {
if (rightValue.getValue() == 0) {
throw new ArithmeticException("Division by zero");
}
return leftValue.getValue() / rightValue.getValue();
}
public String computeEquationRepresentation() {
return returnExpressionString() + " = " + evaluateExpression();
}
}
package de.upb.se.profcalculator;
public class Multiply {
private Value leftValue;
private Value rightValue;
public Multiply(Value leftValue, Value rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public Value evaluate() {
int result = leftValue.getValue() * rightValue.getValue();
return new Value(result);
}
public String returnExpressionString() {
return leftValue.toString() + " * " + rightValue.toString();
}
public int evaluateExpression() {
return leftValue.getValue() * rightValue.getValue();
}
public String computeEquationRepresentation() {
return returnExpressionString() + " = " + evaluateExpression();
}
}
package de.upb.se.profcalculator;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
......@@ -14,12 +17,18 @@ import javafx.stage.Stage;
public class ProfCalculator extends Application implements EventHandler<ActionEvent> {
private final static Value defaultValue = new Value(0);
private Add addition = new Add(defaultValue, defaultValue);
private final static Value defaultValue = new Value();
private Value currentValue = defaultValue;
private Label errorLabel = new Label("");
private TextField inputField = new TextField("");
private Button addButton = new Button("+");
private Label resultLabel = new Label(addition.computeEquationRepresentation());
private Button subButton = new Button("-");
private Button mulButton = new Button("*");
private Button divButton = new Button("/");
private Label resultLabel = new Label(defaultValue.toString());
private Button resetButton = new Button("Reset");
private List<Value> resultsMemory = new ArrayList<>();
private Label occurrenceLabel = new Label("");
@Override
public void start(Stage stage) throws Exception {
......@@ -27,27 +36,69 @@ public class ProfCalculator extends Application implements EventHandler<ActionEv
errorLabel.setTextFill(Color.web("#AA0000"));
VBox layout = new VBox(10);
layout.getChildren().addAll(errorLabel, inputField, addButton, resultLabel);
layout.getChildren().addAll(errorLabel, inputField, addButton, subButton, mulButton, divButton, resultLabel, occurrenceLabel, resetButton);
layout.setPadding(new Insets(20, 80, 20, 80));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
addButton.setOnAction(this);
subButton.setOnAction(this);
mulButton.setOnAction(this);
divButton.setOnAction(this);
resetButton.setOnAction(this::handleResetButtonClick);
}
@Override
public void handle(ActionEvent event) {
try {
int newValue = Integer.parseInt(inputField.getText());
int oldResult = addition.evaluateExpression();
addition = new Add(new Value(oldResult), new Value(newValue));
resultLabel.setText(addition.computeEquationRepresentation());
String input = inputField.getText();
Value newValue = Value.parseValue(input);
if (event.getSource() == addButton) {
currentValue = new Add(currentValue, newValue).evaluate();
} if (event.getSource() == subButton) {
currentValue = new Subtract(currentValue, newValue).evaluate();
} if (event.getSource() == mulButton) {
currentValue = new Multiply(currentValue, newValue).evaluate();
} if (event.getSource() == divButton) {
currentValue = new Divide(currentValue, newValue).evaluate();
}
resultsMemory.add(currentValue);
updateOccurrenceLabel();
resultLabel.setText(currentValue.toString());
inputField.setText("");
errorLabel.setText("");
inputField.requestFocus();
} catch (NumberFormatException e) {
errorLabel.setText("\"" + inputField.getText() + "\" is not a valid integer");
} catch (IllegalArgumentException e) {
errorLabel.setText(e.getMessage());
} catch (ArithmeticException e) {
errorLabel.setText("Error: Division by zero");
}
}
private void handleReset() {
currentValue = defaultValue;
resultsMemory.clear();
occurrenceLabel.setText("");
resultLabel.setText(defaultValue.toString());
inputField.setText("");
errorLabel.setText("");
}
private void handleResetButtonClick(ActionEvent event) {
handleReset();
}
private void updateOccurrenceLabel() {
if (resultsMemory.contains(currentValue)) {
int stepsAgo = resultsMemory.size() - resultsMemory.indexOf(currentValue) - 1;
occurrenceLabel.setText("Last occurred " + stepsAgo + " steps ago");
} else {
occurrenceLabel.setText("New result");
}
}
......
package de.upb.se.profcalculator;
public class Subtract {
private Value leftValue;
private Value rightValue;
public Subtract(Value leftValue, Value rightValue) {
this.leftValue = leftValue;
this.rightValue = rightValue;
}
public Value evaluate() {
int result = leftValue.getValue() - rightValue.getValue();
return new Value(result);
}
public String returnExpressionString() {
return leftValue.toString() + " - " + rightValue.toString();
}
public int evaluateExpression() {
return leftValue.getValue() - rightValue.getValue();
}
public String computeEquationRepresentation() {
return returnExpressionString() + " = " + evaluateExpression();
}
}
......@@ -3,6 +3,10 @@ package de.upb.se.profcalculator;
public class Value {
private Integer value;
public Value() {
this.value = 0;
}
public Value(int value) {
this.value = value;
}
......@@ -15,4 +19,27 @@ public class Value {
return value.intValue();
}
public static Value parseValue(String s) {
try {
int parsedValue = Integer.parseInt(s);
return new Value(parsedValue);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Ungültige Eingabe: " + s);
}
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
Value Va = (Value) obj;
return this.value.equals(Va.value);
}
public int hashCode() {
return value.hashCode();
}
}
......@@ -9,6 +9,11 @@ public class Add {
this.rightValue = rightValue;
}
public Value evaluate() {
int result = leftValue.getValue() + rightValue.getValue();
return new Value(result);
}
public String returnExpressionString() {
return leftValue.toString() + " + " + rightValue.toString();
}
......
/module-info.class
/META-INF/
/de/
#Generated by Maven Integration for Eclipse
#Sat May 11 23:58:36 CEST 2024
m2e.projectLocation=C\:\\Users\\Asus\\eclipse-workspace\\ProfCalculator\\ProfCalculator
#Mon May 27 19:54:54 CEST 2024
m2e.projectLocation=C\:\\Users\\Laith\\eclipse-workspace\\ProfCalculator\\ProfCalculator
m2e.projectName=ProfCalculator
groupId=de.upb.se
artifactId=ProfCalculator
......
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment