0
public class FoodItem {

 private String name;
 private int calories;
 private int fatGrams;
 private int caloriesFromFat;

 public FoodItem(String n, int cal, int fat) {
    name = n;
    calories = cal;
    fatGrams = fat;  
 }

 public void setName(String n) {
    name = n;
 }

 public void setCalories(int cal) {
    calories = cal; 
 }

 public void setFatGrams(int fat) {
    fatGrams = fat;  
 }

 public String getName() {
    return name;
 }

 public int getCalories() {
    return calories;
 }

 public int getFatGrams() {
    return fatGrams; 
 }

 public int getCaloriesFromFat() {
    return (fatGrams * 9.0); 
 }

 public int getPercentFat() {
    return (caloriesFromFat / calories);
 }

 // Ignore for now
 public String toString() {
    return name + calories + fatGrams;
 }
}

My instructions are to "Include a method named caloriesFromFat which returns an integer with the number of calories that fat contributes to the total calories (fat grams * 9.0).", but when I try to compile I get the above error. I have no clue what I have wrong.

Kristofer Ard
  • 133
  • 1
  • 1
  • 5

1 Answers1

1

You've used a double literal 9.0 in your multiplication in getCaloriesFromFat. Because of this, fatGrams is promoted to a double for the multiplication, and the product is a double. However, you have that method returning an int.

You can use an int literal (9, no decimal point) or you can have getCaloriesFromFat return a double instead of an int. Since you're just multiplying by 9, I would use the int literal 9.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Also, my driver calls for the percent of fat "System.out.printf("The percent of fat is %.1f%%\n", fries.percentFat());" but when I run the driver it just returns 0.0%. Do you have any idea what is causing this? – Kristofer Ard Dec 05 '15 at 02:08
  • [Division of integers in Java](https://stackoverflow.com/questions/7220681/division-of-integers-in-java). – rgettman Dec 07 '15 at 17:07