Answer:

Use integer values for input and use double precision for calculation. (It would be OK to use integers for everything, although you would need to be careful.)

Application part of the Code

The application part looks much like the previous example, except that now there are two values input from the user. This adds complication to the event listener.

public class PercentFat extends JFrame implements ActionListener
{

  int calories ;   // input: total calories
  int fatGrams ;   // input:  grams of fat
  double percent ; // result: percent of calories from fat
    
  public PercentFat()  // constructor  
  {  
    . . . . .
  }
   
  // Application method
  public void calcPercent()  
  {
    percent = ( (fatGrams * 9.0) / calories ) * 100.0 ;
  }
   
   
  // GUI code
  . . . . .
   
  public static void main ( String[] args )
  {
    PercentFat fatApp  = new PercentFat() ;
    
    . . . . .
  }
}  

QUESTION 9:

Is the following calculation accurate?

percent = ( (fatGrams * 9) / calories ) * 100 ;