percent = ( (fatGrams * 9) / calories ) * 100 ;
No. This calculation is done with integer arithmetic.
It is likely that (fatGrams * 9)
calories
,
so the division would produce zero.
Here are the variables and the graphical component each corresponds to:
GUI Component | GUI Method | |
---|---|---|
int calories | JTextField | getText() |
int fatGrams | JTextField | getText() |
double percent | JTextField | setText() |
Each 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 both values are
available.
With the previous program,
the input JTextField
had a listener registered for it.
When the user typed something and hit enter
,
the listener was "fired" and ran the application.
(Design Question: ) Will this method work with the current program?