This course has already ended.

⌛ 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 ids fieldOp1 and fieldOp2. Set the ids by using the JavaFX element member function setId.

    • The input fields must be preceded by the label texts "First operand:" and "Second operand:". These must be implemented either as JavaFX Text or Label elements and have ids labelOp1 and labelOp2.

  • Buttons for the four basic arithmetic operations: addition, subtraction, multiplication and division.

    • These must be JavaFX Button elements and have ids btnAdd, btnSub, btnMul and btnDiv.

    • 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 id fieldRes and whose background is white. (See, for example, the documentation of the member function setBackground. You may use the color Color.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 JavaFX Text or Label element whose id is labelRes.

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.

An example calculator GUI.

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:

../../../_images/calc_add.png ../../../_images/calc_sub.png ../../../_images/calc_mul.png ../../../_images/calc_div.png

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.

Posting submission...