- COMP.CS.140
- 12. Graphical User Interfaces
- 12.3 ⌛ JavaFX calculator
⌛ JavaFX calculator¶
Place the main class of your implementation into the file
round12\calc\src\main\java\fi\tuni\prog3\calc\Calculator.java
. As implied by the directory
structure, the task is organised as a Maven project and the implementation must belong to
the package fi.tuni.prog3.calc
. There are no material for this task in the remote directory.
This task concerns implementing a pocket calculator with the JavaFX GUI library. The calculator must have the following parts:
Input fields for the first and second operand.
The input fields must be JavaFX
TextField
elements that have idsfieldOp1
andfieldOp2
. Set the ids by using the JavaFX element member functionsetId
.The input fields must be preceded by the label texts
"First operand:"
and"Second operand:"
. These must be implemented either as JavaFXText
orLabel
elements and have idslabelOp1
andlabelOp2
.
Buttons for the four basic arithmetic operations: addition, subtraction, multiplication and division.
These must be JavaFX
Button
elements and have idsbtnAdd
,btnSub
,btnMul
andbtnDiv
.The buttons must have texts
"Add"
,"Subtract"
,"Multiply"
and"Divide"
.
A result field.
The result field must be a JavaFX
Label
element that has the idfieldRes
and whose background is white. (See, for example, the documentation of the member functionsetBackground
. You may use the colorColor.WHITE
.) The result field is initially empty: It contains the empty string""
.The result field must be preceded by the label text
"Result:"
implemented either as JavaFXText
orLabel
element whose id islabelRes
.
You have to specify the above-mentioned element ids in order to allow the grader to use unit tests in testing your GUI implementation.
Below is an example image of a calculator GUI. Note that this is an example: You do not have to implement an identical GUI. Just make sure that your GUI contains the required parts in some form and layout.
If a user sets values into the operator input fields and presses one of the buttons, the result of
applying the the corresponding operation to the two operands should appear in the result field.
The operation will not affect the contents of the input fields: They will hold their existing
values until the user explicitly modifies them. The program must use double
variables for
handling the input values and the result. Below are examples of all four operations:
The main class of your implementation must be fi.tuni.prog3.calc.Calculator
, that is, the class
Calculator
belonging to the package fi.tuni.prog3.calc
. By a main class we mean the
class that contains your implementation of the function start
that initializes the JavaFX GUI.
Your implementation can contain also other source files as long as also they are located in the
src\main\java\fi\tuni\prog3\calc
directory.
About testing¶
Due to using a GUI, this task does not have example input or output files. However, it should be fairly simple to test the calculator on your own, because the calculator has a quite limited functionality and the operations are simple to compute. Be sure to check that there are no typos, when setting the element ids and label texts.
A+ presents the exercise submission form here.