Eclipse Community Forums Forum Search:

Home » Newcomers » Newcomers » Eclipse project help(Fat Gram Calculator )
Eclipse project help Wed, 22 July 2015 23:14
 Matt KroegerMessages: 3Registered: July 2015 Junior Member
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 23:19
 Nitin DahyabhaiMessages: 3962Registered: July 2009 Senior Member
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.

Nitin Dahyabhai
Eclipse Web Tools Platform
Re: Eclipse project help [message #1702532 is a reply to message #1702529] Wed, 22 July 2015 23:22
 Matt KroegerMessages: 3Registered: July 2015 Junior Member
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 23:23
 Matt KroegerMessages: 3Registered: July 2015 Junior Member
thank you!
Re: Eclipse project help [message #1702785 is a reply to message #1702531] Fri, 24 July 2015 14:34
 Eric RizzoMessages: 3071Registered: July 2009 Senior Member
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.