Application part of the Code

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

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 ;