New Example: Fat Calculator

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:

"rats"

Sometimes the first few lines of a stack trace are useful for debugging.

New Example: Fat Calculator

Fat Calculator GUI

Let us develop another GUI application. Health experts recommend that what you eat have less than 30% of its calories from fat. Our new application calculates the percentage of calories that come from fat, given the number of grams of fat and the number of calories per serving of a food. Most packaged foods display these values on their label. Fat contains 9 calories per gram.

  1. Input:
    • Calories per serving.
    • Grams of fat per serving.
  2. Calculation:
    • percent = ( (fat * 9) / calories ) * 100
  3. Output:
    • percent of fat from calories

QUESTION 9:

What data type should be used for the variables and for the calculation?