Question: Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using
Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:
The instance variables for the class (recipeName, serving size, and recipeIngredients)
The methods (the accessors/mutators, the constructors, and the extra print details method) for the class
A custom method to print the recipe out to the console
Here is the code:
package recipe.collection;
import java.util.ArrayList; import java.util.Scanner;
/** * * @author kenhi */ public class Recipe { private String recipeName; /** * Add three variables: * * 1. a variable 'servings' to store how many people the recipe will feed; * * 2. an ArrayList variable 'recipeIngredients' to store the text for the * names (recipeName) each recipe ingredient added * * 3. a variable totalRecipeCalories * */ /** * Add mutators and accessors for the class variable. * */ public Recipe() { this.recipeName = ""; this.servings = ??? //<--- assignment value with appropriate data type this.recipeIngredients = ???; //<-- assignment value for empty ArrayList this.totalRecipeCalories = 0; } public Recipe(String recipeName, ??? servings, ArrayList???> recipeIngredients, double totalRecipeCalories) //<-- use appropriate data type for the ArrayList and the servings arguments { this.recipeName = recipeName; this.servings = servings; this.recipeIngredients = recipeIngredients this.totalRecipeCalories = totalRecipeCalories; } public void printRecipe() { /** * Declare an int variable singleServingCalories. * Assign singleServingCalories to * the totalRecipeCalories divided by the servings * */
/** * Print the following recipe information: * Recipe: <
/** * Add the ingredient name to recipeIngredients * */ System.out.println("Please enter the ingredient amount: "); float ingredientAmount = scnr.nextFloat(); System.out.println("Please enter the ingredient Calories: "); int ingredientCalories = scnr.nextInt(); /** * Add the total Calories from this ingredient * (ingredientCalories * ingredientAmount) * to the totalRecipeCalories * */ } } while (!reply.equals("n") ; Recipe recipe1 = new Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories); recipe1.printRecipe(); } }
Stepping stone 4 lab code
package recipe.collection;
import java.util.ArrayList; import java.util.Scanner;
/** * * @author kenhi */ public class Loops { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine(); do { System.out.println("Would you like to enter an ingredient: (y or n)"); String reply = scnr.next().toLowerCase(); //checking whether user entered y or n if y is entered then if(reply.equalsIgnoreCase("y")) { //asking user for ingredient name System.out.println("Enter ingredient name"); //storing into ingredient newIngredient=scnr.next(); //adding ingrdient to ist ingredientList.add(newIngredient); } else { //if n is entered then the do loop is exited addMoreIngredients=false; } } while (addMoreIngredients); for (int i = 0; i < ingredientList.size(); i++) { //getting the ingredient from list and storing in ingredient String ingredient = (String) ingredientList.get(i); //printing the ingredient System.out.println(ingredient); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
