Your Recipe class should include the following items: 1.Instance variables: reci
ID: 3876696 • Letter: Y
Question
Your Recipe class should include the following items:
1.Instance variables: recipeName, servings, recipeIngredients, and totalRecipeCalories
2.Accessors and mutators for the instance variables
3.Constructors
4. printRecipe() method
5 createNewRecipe() method to build a recipe from user input
6.Pseudocode for the custom method selected from the list in Stepping Stone Lab Five
Your Recipe_Test.java file containing a main() method that:
1. Uses a constructor to create a new recipe
2.Accesses the printRecipe() method to print the formatted recipe
3.Invokes the createNewRecipe() method to accept user inpu
Specifically, the following critical elements of the final project are addressed:
I. Data Types: Your Recipe class should properly employ each of the following data types that meet the scenario’s requirements where necessary:
A. Utilize appropriate numerical and string data types to represent values for variables and attributes in your program.
B. Populate a list or array that allows the management of a set of values as a single unit in your program.
II. Algorithms and Control Structure: Your Recipe class should properly employ each of the following control structures as required or defined by the scenario where necessary:
A. Utilize expressions or statements that carry out appropriate actions or that make appropriate changes to your program’s state as represented in your program’s variables.
B. Employ the appropriate conditional control structures that enable choosing between options in your program.
C. Utilize iterative control structures that repeat actions as needed to achieve the program’s goal.
III. Methods: Your Recipe class should properly employ each of the following aspects of method definition as determined by the scenario’s requirements where necessary:
A. Use formal parameters that provide local variables in a function’s definition.
B. Use actual parameters that send data as arguments in function calls.
C. Create both value-returning and void functions to be parts of expressions or stand-alone statements in your program.
D. Invoke methods that access the services provided by an object.
E. Describe a user-defined method that provides custom services for an object.
F. Create unit tests that ensure validity of the methods.
IV. Classes: Construct classes for your program that include the following as required by the scenario where necessary:
A. Include attributes that allow for encapsulation and information hiding in your program.
B. Include appropriate methods that provide an object’s behaviors.
V. Documentation: Utilize inline comments directed toward software engineers for the ongoing maintenance of your program that explain the decisions you made in the construction of the classes in your program.
Stepping Stone five Recipe
import java.util.ArrayList;
/**
* Ingredient class :
* items that will be stored in each recipe
*
*/
public class Ingredient {
//variables
private String ingredientName;
private double ingredientAmount;
private String unitMeasurement;
private double totalCalories;
public Ingredient() {
}
//constructor
public Ingredient(String ingredientName, double ingredientAmount, String unitMeasurement, double totalCalories) {
super();
this.ingredientName = ingredientName;
this.ingredientAmount = ingredientAmount;
this.unitMeasurement = unitMeasurement;
this.totalCalories = totalCalories;
}
//Seter and getter
public String getIngredientName() {
return ingredientName;
}
public void setIngredientName(String ingredientName) {
this.ingredientName = ingredientName;
}
public double getIngredientAmount() {
return ingredientAmount;
}
public void setIngredientAmount(double ingredientAmount) {
this.ingredientAmount = ingredientAmount;
}
public String getUnitMeasurement() {
return unitMeasurement;
}
public void setUnitMeasurement(String unitMeasurement) {
this.unitMeasurement = unitMeasurement;
}
public double getTotalCalories() {
return totalCalories;
}
public void setTotalCalories(double totalCalories) {
this.totalCalories = totalCalories;
}
//print item detail
public void printItemDetails() {
System.out.println("ingredientName " + this.ingredientName);
System.out.println("ingredientAmount " + this.ingredientAmount);
System.out.println("totalCalories " + this.totalCalories);
System.out.println("unitMeasurement " + this.unitMeasurement);
}
}
import java.util.ArrayList;
/**
* Recipe class :
* create one recipe
*
*/
public class Recipe {
//variables
private ArrayList<Ingredient> recipeIngredients;
private ArrayList<String> instructions;
private String recipeName;
private int servings;
private double totalRecipeCalories;
//constructor
public Recipe(String recipeName,int servings) {
this.recipeName = recipeName;
this.recipeIngredients = new ArrayList<>();
this.instructions = new ArrayList<>();
this.servings = servings;
this.totalRecipeCalories = 0;
}
//get IngredientList
public ArrayList<Ingredient> getIngredientList() {
return recipeIngredients;
}
//set IngredientList
public void setIngredientList(ArrayList<Ingredient> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
/**
* @return the recipeName
*/
public String getRecipeName() {
return recipeName;
}
/**
* @param recipeName the recipeName to set
*/
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
/**
* @return the servings
*/
public int getServings() {
return servings;
}
/**
* @param servings the servings to set
*/
public void setServings(int servings) {
this.servings = servings;
}
/**
* @return the totalRecipeCalories
*/
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
/**
* @param totalRecipeCalories the totalRecipeCalories to set
*/
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
// add Ingredient in recipe
public boolean addIngredient(Ingredient ingredient){
this.totalRecipeCalories = this.totalRecipeCalories + ingredient.getTotalCalories();
return this.recipeIngredients.add(ingredient);
}
// remove Ingredient in recipe
public boolean removeIngredient(Ingredient ingredient){
if (this.recipeIngredients.remove(ingredient)){
this.totalRecipeCalories = this.totalRecipeCalories - ingredient.getTotalCalories();
return true;
}
else
return false;
}
// remove all Ingredient in recipe
public boolean removeAllIngredient(){
return this.recipeIngredients.removeAll(recipeIngredients);
}
// print Ingredients in recipe
public void printRecipe(){
int singleServingCalories = (int) (totalRecipeCalories/servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for(Ingredient ingredient: recipeIngredients) {
ingredient.printItemDetails();
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
//create new recipe
public static Recipe createNewRecipe(String recipeName,int servings){
return new Recipe(recipeName,servings);
}
//insert instruction
public boolean insertInstruction(String instruction){
return this.instructions.add(instruction);
}
public void printInstruction(){
instructions.toString();
}
}
mport java.util.ArrayList;
/**
* SteppingStone5_Recipe_Test class :
* collection of recipe
*
*/
public class SteppingStone5_Recipe_Test {
private ArrayList<Recipe> recipes;
public SteppingStone5_Recipe_Test() {
this.recipes = new ArrayList<>();
}
//add a Recipe in collection
public void addItem(Recipe recipe){
recipes.add(recipe);
}
//delete a Recipe in collection
public void deleteItem(Recipe recipe){
recipes.remove(recipe);
}
//print all recipes
public void printAllRecipies(){
int i = 0;
for(Recipe rec: recipes){
i++;
System.out.println("Recipe " + i);
rec.printRecipe();
}
}
public static void main(String[] args) {
//create some demo recipes
Recipe recipe1 = Recipe.createNewRecipe("Pizza", 4);
Ingredient ingredient1 = new Ingredient("Anchovies",20,"gram",300);
recipe1.addIngredient(ingredient1);
Recipe recipe2 = Recipe.createNewRecipe("Ramen", 2);
Ingredient ingredient2 = new Ingredient("Noodles",50,"gram",200);
recipe2.addIngredient(ingredient2);
// add in box
SteppingStone5_Recipe_Test box = new SteppingStone5_Recipe_Test();
box.addItem(recipe1);
box.addItem(recipe2);
box.printAllRecipies();
}
}
Stepping Stone Recipe Test
public class SteppingStone5_RecipeTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create two recipe objects first
SteppingStone5_Recipe myFirstRecipe = new SteppingStone5_Recipe();
ArrayList<Ingredient> recipeIngredients = new ArrayList();
ArrayList<Ingredient> recipeIngredientsTwo = new ArrayList();
String ingredientName = "Anchovies";
Ingredient tempIngredient = new Ingredient().addNewIngredient(ingredientName);
recipeIngredients.add(tempIngredient);
SteppingStone5_Recipe mySecondRecipe = new SteppingStone5_Recipe("Pizza", 2, recipeIngredients, 300);
// Initialize first recipe
String ingredientNameTwo = "Noodles";
Ingredient tempIngredientTwo = new Ingredient().addNewIngredient(ingredientNameTwo);
recipeIngredientsTwo.add(tempIngredientTwo);
myFirstRecipe.setRecipeName("Ramen");
myFirstRecipe.setServings(2);
myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);
myFirstRecipe.setTotalRecipeCalories(150);
// Print details of both recipes
myFirstRecipe.printRecipe();
mySecondRecipe.printRecipe();
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
public class SteppingStone5_Recipe {
private String recipeName;
private double totalRecipeCalories;
private ArrayList<String> recipeIngredients;
private int servings;
public SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = 0;
this.recipeIngredients = new ArrayList<>();
this.totalRecipeCalories = 0;
}
public SteppingStone5_Recipe(String recipeName, int servings,
ArrayList recipeIngredients, double totalRecipeCalories) {
this.recipeName = recipeName;
this.servings = servings;
this.recipeIngredients = recipeIngredients;
this.totalRecipeCalories = totalRecipeCalories;
}
public String getRecipeName() {
return recipeName;
}
public void setRecipeName(String recipeName) {
this.recipeName = recipeName;
}
public double getTotalRecipeCalories() {
return totalRecipeCalories;
}
public void setTotalRecipeCalories(double totalRecipeCalories) {
this.totalRecipeCalories = totalRecipeCalories;
}
public ArrayList getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
public int getServings() {
return servings;
}
public void setServings(int servings) {
this.servings = servings;
}
public void printRecipe() {
int singleServingCalories = (int) (totalRecipeCalories / servings);
System.out.println("Recipe: " + recipeName);
System.out.println("Serves: " + servings);
System.out.println("Ingredients:");
for (String ingredient : recipeIngredients) {
System.out.println(ingredient);
}
System.out.println("Each serving has " + singleServingCalories + " Calories.");
}
public static void main(String[] args) {
SteppingStone5_Recipe recipe1 = createNewRecipe();
recipe1.printRecipe();
}
public static SteppingStone5_Recipe createNewRecipe() {
double totalRecipeCalories = 0;
ArrayList<String> recipeIngredients = new ArrayList();
boolean addMoreIngredients = true;
Scanner scnr = new Scanner(System.in);
System.out.println("Please enter the recipe name: ");
String recipeName = scnr.nextLine();
System.out.println("Please enter the number of servings: ");
int servings = scnr.nextInt();
do {
System.out.println("Please enter the ingredient name"
+ " or type end if you are finished entering ingredients: ");
String ingredientName = scnr.next();
if (ingredientName.toLowerCase().equals("end")) {
addMoreIngredients = false;
} else {
recipeIngredients.add(ingredientName);
System.out.println("Please enter the ingredient amount: ");
float ingredientAmount = scnr.nextFloat();
System.out.println("Please enter the ingredient Calories: ");
int ingredientCalories = scnr.nextInt();
totalRecipeCalories += (ingredientCalories * ingredientAmount);
}
} while (addMoreIngredients);
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
scnr.close();
return recipe1;
}
}
I made 2 minor changes to the code. Every thing else is good acording to the specs. And please use params while declaring ArrayList as ArrayList<String> if the list is intended to store string data
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.