percent = ( (fatGrams * 9) / calories ) * 100 ;
No. This calculation is done with integer arithmetic,
then the result is
double when it is assigned to
This is not as accurate as performing the calculation with
Here are the variables and the graphical component each corresponds to:
|GUI Component||GUI Method|
|int calories|| || |
|int fatGrams|| || |
|double percent|| || |
JTextField will have a
JLabel and the whole GUI
will have a
JLabel at the top to act as a heading.
All this is much as in the previous example.
There is something different about this GUI, though.
The user inputs two values.
The calculation should only be carried out when two values are
With the previous program,
JTextField had a listener registered for it.
When the user typed something and hit
the listener was "fired" and in turn ran the application.