Answer:
int or double would be fine.
In order to show some more methods, the code below will use doubles.
Application part of the Code
The application part looks much like the previous example, except that now there are two values input from text fields. The event listener will keep them separate.
public class PercentFat extends JFrame implements ActionListener
{
double calories ; // input: total calories
double fatGrams ; // input: grams of fat
double percent ; // result: percent of calories from fat
public PercentFat() // constructor
{
. . . . .
}
// Application method
public void calcPercent()
{
percent = ( (fatGrams * 9) / calories ) * 100 ;
}
// GUI code
. . . . .
public static void main ( String[] args )
{
PercentFat fatApp = new PercentFat() ;
. . . . .
}
}
QUESTION 10:
Is the following calculation correct?
percent = fatGrams * 9 / calories * 100 ;