As part of NTNU's increased focus on security, all users are now forced to enable and use Two-factor authentication.

Commit af77acf3 authored by Simeon Christoffersen's avatar Simeon Christoffersen
Browse files

initial commit

parent 53d5ca90
......@@ -74,14 +74,14 @@ public class AppController {
updateOperandsView();
}
private void appendToOperand(String s) {
// TODO
private void appendToOperand(String string) {
setOperand(getOperandString() + string);
}
@FXML
void handleDigit(ActionEvent ae) {
if (ae.getSource() instanceof Labeled l) {
// TODO append button label to operand
appendToOperand(l.getText());
}
}
......@@ -89,45 +89,71 @@ public class AppController {
void handlePoint() {
var operandString = getOperandString();
if (operandString.contains(".")) {
// TODO remove characters after point
String s = operandString.substring(0, operandString.indexOf(".")) + ".";
setOperand(s);
} else {
// TODO append point
appendToOperand(".");
}
}
@FXML
void handleClear() {
// TODO clear operand
setOperand("");
}
@FXML
void handleSwap() {
// TODO clear operand
calc.swap();
updateOperandsView();
}
private void performOperation(UnaryOperator<Double> op) {
// TODO
calc.performOperation(op);
updateOperandsView();
}
private void performOperation(boolean swap, BinaryOperator<Double> op) {
if (hasOperand()) {
// TODO push operand first
handleEnter();
}
if (swap) {
calc.swap();
calc.performOperation(op);
updateOperandsView();
} else {
calc.performOperation(op);
updateOperandsView();
}
// TODO perform operation, but swap first if needed
}
@FXML
void handleOpAdd() {
// TODO
performOperation(false, (a,b) -> a + b);
}
@FXML
void handleOpSub() {
// TODO
performOperation(false, (a,b) -> a - b);
}
@FXML
void handleOpMult() {
// TODO
performOperation(false, (a,b) -> a * b);
}
@FXML
void handleOpDiv() {
performOperation(false, (a,b) -> a / b);
}
@FXML
void handleOpSqrt() {
performOperation(a -> Math.sqrt(a));
}
@FXML
void handlePi() {
calc.pushOperand(Math.PI);
updateOperandsView();
}
}
}
\ No newline at end of file
......@@ -73,8 +73,15 @@ public class Calc {
* @throws IllegalStateException if the operand stack is empty
*/
public double performOperation(UnaryOperator<Double> op) throws IllegalStateException {
// TODO
return 0.0;
if (getOperandCount() == 0) {
throw new IllegalStateException();
}
double n1 = op.apply(peekOperand());
popOperand();
pushOperand(n1);
return n1;
}
/**
......@@ -102,7 +109,12 @@ public class Calc {
* @throws IllegalStateException if the operand count is less than two
*/
public void swap() {
// TODO
if (getOperandCount() < 2) {
throw new IllegalStateException();
}
double n1 = popOperand();
operandStack.add(getOperandCount() - 1, n1);
}
/**
......@@ -111,6 +123,10 @@ public class Calc {
* @throws IllegalStateException if the operand stack is empty
*/
public void dup() {
// TODO
if (getOperandCount() == 0) {
throw new IllegalStateException();
}
popOperand();
}
}
\ No newline at end of file
......@@ -55,10 +55,10 @@
GridPane.rowIndex="6" GridPane.columnIndex="2"/>
<!-- TODO -->
<Button text="/"
<Button text="/" onAction="#handleOpDiv"
GridPane.rowIndex="6" GridPane.columnIndex="3"/>
<Button text="√"
<Button text="√" onAction="#handleOpSqrt"
GridPane.rowIndex="7" GridPane.columnIndex="0"/>
<Button text="π"
<Button text="π" onAction="#handlePi"
GridPane.rowIndex="7" GridPane.columnIndex="1"/>
</GridPane>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment