public class FoodItem { private String name; private double fat; private double carbs; private double protein; // TODO: Define default constructor // TODO: Define second constructor with parameters to initialize private fields (name, fat, carbs, protein) public String getName() { return name; } public double getFat() { return fat; } public double getCarbs() { return carbs; } public double getProtein() { return protein; } public double getCalories(double numServings) { // Calorie formula double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings; return calories; } public void printInfo() { System.out.println("Nutritional information per serving of " + name + ":"); System.out.printf(" Fat: %.2f g\n", fat); System.out.printf(" Carbohydrates: %.2f g\n", carbs); System.out.printf(" Protein: %.2f g\n", protein); } } import java.util.Scanner; public class NutritionalInfo { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); FoodItem foodItem; String itemName = scnr.next(); if(itemName.equals("Water") || itemName.equals("water")) { foodItem = new FoodItem(); foodItem.printInfo(); System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0, foodItem.getCalories(1.0)); } else { double amountFat = scnr.nextDouble(); double amountCarbs = scnr.nextDouble(); double amountProtein = scnr.nextDouble(); foodItem = new FoodItem(itemName, amountFat, amountCarbs, amountProtein); double numServings = scnr.nextDouble(); foodItem.printInfo(); System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0, foodItem.getCalories(1.0)); System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings, foodItem.getCalories(numServings)); } } }
public class FoodItem {
private String name;
private double fat;
private double carbs;
private double protein;
// TODO: Define default constructor
// TODO: Define second constructor with parameters to initialize private fields (name, fat, carbs, protein)
public String getName() {
return name;
}
public double getFat() {
return fat;
}
public double getCarbs() {
return carbs;
}
public double getProtein() {
return protein;
}
public double getCalories(double numServings) {
// Calorie formula
double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
return calories;
}
public void printInfo() {
System.out.println("Nutritional information per serving of " + name + ":");
System.out.printf(" Fat: %.2f g\n", fat);
System.out.printf(" Carbohydrates: %.2f g\n", carbs);
System.out.printf(" Protein: %.2f g\n", protein);
}
}
import java.util.Scanner;
public class NutritionalInfo {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
FoodItem foodItem;
String itemName = scnr.next();
if(itemName.equals("Water") || itemName.equals("water")) {
foodItem = new FoodItem();
foodItem.printInfo();
System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0,
foodItem.getCalories(1.0));
}
else {
double amountFat = scnr.nextDouble();
double amountCarbs = scnr.nextDouble();
double amountProtein = scnr.nextDouble();
foodItem = new FoodItem(itemName, amountFat, amountCarbs, amountProtein);
double numServings = scnr.nextDouble();
foodItem.printInfo();
System.out.printf("Number of calories for %.2f serving(s): %.2f\n", 1.0,
foodItem.getCalories(1.0));
System.out.printf("Number of calories for %.2f serving(s): %.2f\n", numServings,
foodItem.getCalories(numServings));
}
}
}
1public class FoodItem {
2 private String name;
3 private double fat;
4 private double carbs;
5 private double protein;
6 // defining default constructor
7 public FoodItem() {
8 // assinging name to water as specified
9 this.name = "Water";
10 // assigning all other values to 0.0 as specified
11 this.fat = 0.0;
12 this.carbs = 0.0;
13 this.protein = 0.0;
14 }
15
16 // defining constructor to initialize all private fields
17 public FoodItem(String name, double fat, double carbs, double protein) {
18 // initializing all the values to values passed
19 this.name = name;
20 this.fat = fat;
21 this.carbs = carbs;
22 this.protein = protein;
23 }
24 public String getName() {
25 return name;
26 }
27 public double getFat() {
28 return fat;
29 }
30 public double getCarbs() {
31 return carbs;
32 }
33 public double getProtein() {
34 return protein;
35 }
36 public double getCalories(double numServings) {
37 // Calorie formula
38 double calories = ((fat * 9) + (carbs * 4) + (protein * 4)) * numServings;
39 return calories;
40 }
41 public void printInfo() {
42 System.out.println("Nutritional information per serving of " + name + ":");
43 System.out.printf(" Fat: %.2f g\n", fat);
44 System.out.printf(" Carbohydrates: %.2f g\n", carbs);
45 System.out.printf(" Protein: %.2f g\n", protein);
46 }
47}
Step by step
Solved in 3 steps with 4 images