Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Simeon Christoffersen
test2021
Commits
af77acf3
Commit
af77acf3
authored
Sep 09, 2021
by
Simeon Christoffersen
Browse files
initial commit
parent
53d5ca90
Changes
3
Hide whitespace changes
Inline
Side-by-side
javafx-template/src/main/java/app/AppController.java
View file @
af77acf3
...
...
@@ -74,14 +74,14 @@ public class AppController {
updateOperandsView
();
}
private
void
appendToOperand
(
String
s
)
{
// TODO
private
void
appendToOperand
(
String
s
tring
)
{
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 o
perand
setO
perand
(
""
);
}
@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
javafx-template/src/main/java/app/Calc.java
View file @
af77acf3
...
...
@@ -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
javafx-template/src/main/resources/app/App.fxml
View file @
af77acf3
...
...
@@ -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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment