Question: My code has error it says FoodItem.java:61: error: class, interface, or enum expected Class - NutritionalInfo.java^ ^ 1 error Q: Given main(), complete theFoodItemclass (in

My code has error it says FoodItem.java:61: error: class, interface, or enum expected

Class - NutritionalInfo.java^

^

1 error

Q: Given main(), complete theFoodItemclass (in file FoodItem.java) with constructors to initialize each food item. The default constructor should initialize the name to "None" and all other fields to 0.0. The second constructor should have four parameters (food name, grams of fat, grams of carbohydrates, and grams of protein) and should assign each private field with the appropriate parameter value.

Ex: If the input is:

M&M's 10.0 34.0 2.0 1.0 

where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 1.0 is the number of servings, the output is:

Nutritional information per serving of None: Fat: 0.00 g Carbohydrates: 0.00 g Protein: 0.00 g Number of calories for 1.00 serving(s): 0.00 Nutritional information per serving of M&M's: Fat: 10.00 g Carbohydrates: 34.00 g Protein: 2.00 g Number of calories for 1.00 serving(s): 234.00 

The first FoodItem above is initialized using the default constructor.

CODE:

class FoodItem{

private String name;

private double fat;

private double carbs;

private double protien;

//default constructor

FoodItem(){

//initializeing value name to None

name = "None";

//fat to 0.0

fat = 0.0;

//carbs to 0.0

carbs = 0.0;

//protien to 0.0

protien = 0.0;

}

//parameterized constructor name fat carbs protien as parameter

FoodItem(String name, double fat, double carbs,double protien){

//initialize name

this.name = name;

//initialize fat

this.fat = fat;

//initialize carbs

this.carbs = carbs;

//initialize protien

this.protien = protien;

}

public String getName(){

return name;

}

public double getFat(){

return fat;

}

public double getCarbs(){

return carbs;

}

public double getProtien(){

return protien;

}

public double getCalories(double numberServings){

double calories = ((fat*9)+(carbs*4)+(protien*4))*numberServings;

return calories;

}

public void printInfo(){

System.out.println("Nutritional Info per serving of "+name+" : ");

System.out.printf(" Fat: %.2f g ", fat);

System.out.printf(" Carbohydrates: %.2f g ", carbs);

System.out.printf(" Protien: %.2f g ", protien);

}

}

Class - NutritionalInfo.java

import java.util.*;

public class NutritionalInfo

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

FoodItem foodItem1 = new FoodItem();

String itemName = sc.next();

double amountFat = sc.nextDouble();

double amountCarbs = sc.nextDouble();

double amountProtien = sc.nextDouble();

FoodItem foodItem2 = new FoodItem(itemName,amountFat,amountCarbs,amountProtien);

double numServings = sc.nextDouble();

foodItem1.printInfo();

System.out.printf("Number of calories for %.2f serving(s): %.2f ",numServings,foodItem1.getCalories(numServings));

System.out.println(" ");

foodItem2.printInfo();

System.out.printf("Number of calories for %.2f serving(s): %.2f ",numServings,foodItem2.getCalories(numServings));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!