A good answer might be:

Let us use integer values for input and use double precision for the calculation and the result. (It would be OK to use integers for everything.)

Application part of the Code

The application part of the new program will look much like the previous example, except that now there are two values input from the user. This will not much affect the application, but will later on affect the GUI.

public class percentFat
{

  int calories ;          // input from the user: total calories per serving
  int fatGrams ;          // input from the user: grams of fat per serving
  double percent ;        // result: percent of calories from fat
    
  percentFat()            // constructor  
  {  
    . . . . .
  }

  // The application
  public void calcPercent( )  
  {
    percent = ( (fatGrams * 9.0) / calories ) * 100.0 ;
  }


  // GUI code
  . . . . .

  public static void main ( String[] args )
  {
    percentFat fatApp  = new percentFat() ;
    
    . . . . .
  }
}

As with fahrConvert you could add a few lines of code to make this a non-GUI application (sometimes called a ASCII terminal application.)

QUESTION 9:

Would the calculation


    percent = ( (fatGrams * 9) / calories ) * 100 ;
have worked as well?

Click Here after you have answered the question