Skip to main content



      Home
Home » Newcomers » Newcomers » Eclipse project help(Fat Gram Calculator )
Eclipse project help [message #1702529] Wed, 22 July 2015 19:14 Go to next message
Eclipse UserFriend
I could use some help on this please!

Fat Gram calculator

Design a program that asks for the number of fat grams and calories in a food item. Validate the input as follows:

- Make sure the number of fat grams and calories are not less than 0.
- According to nutritional formulats, the number of calories cannot exceed fat grams * 9. Make sure the number of calories entered is not greater than fat grams * 9.

Once correct data has been entered, the program should calculate and display the percentage of calories that come from fat. Use the following formula: Percentage of calories from fat = (Fat grams * 9) / calories.

Some nutritionists classify a food as "low fat" if less than 30 percent of its calories come from fat. If the results of this formula are less than 0.3, the program should display a message indicating the food is low in fat.

Seems I am having issues with the while statement or I have this all wrong. I would truly appreciate some suggestions, troubleshooting steps, and resolution to this problem. Please see file attached.




Re: Eclipse project help [message #1702531 is a reply to message #1702529] Wed, 22 July 2015 19:19 Go to previous messageGo to next message
Eclipse UserFriend
This forum is to help you get Eclipse running. You'll need your fellow students, javaranch.com, or stackoverflow.com for purely Java questions. And I'd suggest attaching source rather than a compiled class at that time.
Re: Eclipse project help [message #1702532 is a reply to message #1702529] Wed, 22 July 2015 19:22 Go to previous messageGo to next message
Eclipse UserFriend
import java.util.Scanner;

public class PercentCalFat3 {

public static void main(String[] args)
{
//Create scanner for keyboard input
Scanner keyboard = new Scanner(System.in);

//Local variables
String doAnother;

do
{
//Calculate and display item fat grams and calories
showPercentage();

//Do this again?
System.out.print("Do you have another item? (Enter y for yes): ");
doAnother = keyboard.next();
}while (doAnother.equals("y") || doAnother.equals("Y"));
}

//The Percentage module gets an items % of calories from fat
//from the user and diplsys its percentage.
public static void showPercentage()
{

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner (System.in);

//Local Variables
double fatGrams, calories, percent;


//Ask user to enter amount of fat grams.
System.out.print("Enter the amount of fat grams: ");
fatGrams = keyboard.nextDouble();

//Validate fat grams
{while (fatGrams * 9 < 0)
{
System.out.println("fat grams cannot be less than 0");
System.out.print("Enter the correct amount of fatGrams: ");
fatGrams = keyboard.nextDouble();
}}
//Ask user to enter amount of calories.
System.out.print("Enter the amount of calories: ");
calories = keyboard.nextDouble();

//Validate calories
while (calories > 0 || calories < fatGrams * 9);
{
System.out.println("calories cannot be less than 0");
System.out.println("or greater than fat grams. ");
System.out.print("Enter the correct amount of calories: ");
calories = keyboard.nextDouble();

//Calculate the percentage of calories from fat.
percent = (fatGrams * 9) / calories;

//Display the percent.
System.out.println("The percent of calories is " + percent);
{
while (percent <.03);
//Display this food is low fat if
// less than 30 percent of calories
//come from fat.

System.out.println("This item is low fat");

{


}
}

}
}
}



Re: Eclipse project help [message #1702533 is a reply to message #1702531] Wed, 22 July 2015 19:23 Go to previous messageGo to next message
Eclipse UserFriend
thank you!
Re: Eclipse project help [message #1702785 is a reply to message #1702531] Fri, 24 July 2015 10:34 Go to previous message
Eclipse UserFriend
Nitin Dahyabhai wrote on Wed, 22 July 2015 23:19
This forum is to help you get Eclipse running. You'll need your fellow students, javaranch.com, or stackoverflow.com for purely Java questions.


You did read that, right?
Previous Topic:Linking Eclipse project to my build directory
Next Topic:CVS repository
Goto Forum:
  


Current Time: Fri May 16 17:58:43 EDT 2025

Powered by FUDForum. Page generated in 0.08382 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top