Here's another program I want to share with you guys, which is simply a program that multiplies the number of grams of fat in food by nine, which gives off how many calories you earn upon eating a serving of that snack or provision. You need to enter the number of calories in the actual serving in that calculator and the number of grams of fat, and what will do is it will calculate the percentage and determine whether the food has a high amount of fat, at least 30%, or not.
This time, there are no notes taken on how this works.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class FatGramCalculator
{
public static void main(String[] args)
{
String inputString;
float totalCalories,
fatGrams,
caloriesFromFat,
ratio,
percent;
DecimalFormat formatter = new DecimalFormat("##");
inputString =
JOptionPane.showInputDialog("How many calories are in the food item you want to study?");
totalCalories = Float.parseFloat(inputString);
inputString =
JOptionPane.showInputDialog("How much fat is in the food item, in grams?");
fatGrams = Float.parseFloat(inputString);
caloriesFromFat = fatGrams * 9;
ratio = caloriesFromFat / totalCalories;
percent = ratio * 100;
if (percent > 100)
JOptionPane.showMessageDialog(null, "Invalid entry.");
if (percent <= 100)
JOptionPane.showMessageDialog(null, "Number of calories in food item: " + totalCalories +
"\nNumber of grams of fat in food item: " + fatGrams +
"\n\nPercent of calories of fat to total calories: " +
(formatter.format(percent)) + "%");
if (percent < 30)
JOptionPane.showMessageDialog(null, "The food item is low in fat.");
System.exit(0);
}
}
Subscribe to:
Post Comments (Atom)
Google+ Followers
Popular Posts by Gregory
-
Hello, people. How have you been? Sorry if I haven't posted a Blogger blog in a long time! Anyway, I want to present to you a very tou...
-
Hello, everyone. This is the first blog of a whole series of blogs I'm writing and publishing rapidly because what's going on is, ...
-
Hello , everyone. This is the last blog I will write that contains code I wrote myself but from one of my three JAVA courses that is...
-
Rock Paper Scissors Lizard Spock (JAVA Program) It's time for another JAVA program to exhibit for new programmers; this time, with...
-
Hello, guys. Here's another JAVA Program I want to show off; this is another one I did more than a year ago. A description is provided...
-
JAVA Code for the Rock Paper Scissors Program Hello, people. How are you doing? I want to show off the code for the game of Rock Paper...
-
Here's another program I want to share with you guys, which is simply a program that multiplies the number of grams of fat in food by ni...
-
Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different si...
-
Here's a nother JAVA Program I want to share with you guys , which is a demonst ratio n of the Lottery class I w as as ked to design fro...
-
NOTE: Because this post is very long, I've highlighted the most relevant parts in yellow. That's basically the most important detai...
