Overview: Now that you have some experience building basic, single-class applica
ID: 3903152 • Letter: O
Question
Overview: Now that you have some experience building basic, single-class applications, here is an opportunity to delve into making a full-fledged application with more than one class. This stepping stone lab is the point where we start to pull all the elements of the course together. You will start to implement a first version of the Recipe class and create a test class to put it through its paces. Be sure to review the Stepping Stone Lab Five guidelines before beginning this lab. The completed code (.java file) from Stepping Stone Lab Four is included in this module to use as a reference for comparison of your work in Module Five as well as moving forward. This code serves a number of functions: ? It allows you to review your own submitted code. ? You may utilize it as a useful foundation for Stepping Stone Lab Five as well as other assignments to come. ? Together with instructor feedback on your submitted code, you are building your own final project application. As you are developing the code in this stepping stone lab, you should consider how you can transition it to your final project. Eventually, the user input for collecting the ingredients into an ArrayList of strings will be converted to an ArrayList of Ingredient objects. Think about the variable names you are using and where in the code you are collecting the ingredient input.
SteppingStone5_Recipe -
recipeName: String
- servings: int
- recipeIngredients: ArrayList
- totalRecipeCalories: double
+ getRecipeName(): String
+ setRecipeName(String): void
+ getServings(): int
+ setServings(int): void
+ getRecipeIngredients(): ArrayList
+ setRecipeIngredients(ArrayList): void
+ getTotalRecipeCalories(): double
+ setTotalRecipeCalories(double): void
+ printRecipe(): void
+ addNewRecipe(): SteppingStone5_Recipe
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 *To test the functionality of your finished code, use the SteppingStone5_RecipeTest.java file *Replace the public static void main(String[] args) with public SteppingStone5_Recipe createNewRecipe() Guidelines for Submission: This assignment should be submitted as a Java file. Extending This Lab for Your Final Project For your final project: 1. Modify this code to develop a Recipe class: A. Change the void main method createNewRecipe() that returns a Recipe 2.
FOR FINAL SUBMISSION ONLY: Change the ArrayList type to an Ingredient object. When a user adds an ingredient to the recipe, instead of adding just the ingredient name, you will add the actual ingredient including name, amount, measurement type, and calories. For the Milestone Two submission, the recipeIngredients ArrayList can remain as a string type. 3. Adapt the printRecipe() method to print the amount and measurement type as well as the ingredient name. 4. Create a custom method in the Recipe class. Choose one of the following options: A. Print out a recipe with amounts adjusted for a different number of servings. B. Create an additional list or ArrayList that allows users to insert step-by-step recipe instructions. C. Convert ingredient amounts from English to metric (or vice versa). D. Calculate select nutritional information. E. Calculate recipe cost. F. Propose a suitable alternative to your instructor.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SteppingStones;
import java.util.Scanner;
import java.util.ArrayList;
/**
*
* @author snhu.edu
*/
public class SteppingStone5_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 SteppingStone5_Recipe() {
this.recipeName = "";
this.servings = ??? //<--- assignment value with appropriate data type
this.recipeIngredients = ???; //<-- assignment value for empty ArrayList
this.totalRecipeCalories = 0;
}
public SteppingStone5_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: <<recipeName>>
* Serves: <<servings>>
* Ingredients:
* <<Ingredient1>>
* <<Ingredient2>>
* ...
* <<Last Ingredient>>
*
* Each serving has <<singleServingCalories>> Calories.
*
* HINT --> Use a for loop to iterate through the ingredients
*/
}
public static void main(String[] args) {
??? totalRecipeCalories = ???;
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: ");
//correct data type & Scanner assignment method for servings variable
???? servings = ????
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 {
/**
* 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") ;
SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName,
servings, recipeIngredients, totalRecipeCalories);
recipe1.printRecipe();
}
}
/**
* Final Project
*
* For your Final Project:
*
* 1. Modify this code to develop a Recipe class:
* a. change the void main method createNewRecipe() that returns a Recipe
*
* 2. FOR FINAL SUBMISSION ONLY:Change the ArrayList type to an
* Ingredient object. When a user adds an ingredient to the recipe,
* instead of adding just the ingredient name, you will be adding the
* actual ingredient including name, amount, measurement type, calories.
* For the Milestone Two submission, the recipeIngredients ArrayList can
* remain as a String type.
*
* 3. Adapt the printRecipe() method to print the amount and measurement
* type as well as the ingredient name.
*
* 4. Create a custom method in the Recipe class.
* Choose one of the following options:
*
* a. print out a recipe with amounts adjusted for a different
* number of servings
*
* b. create an additional list or ArrayList that allow users to
* insert step-by-step recipe instructions
*
* c. conversion of ingredient amounts from
* English to metric (or vice versa)
*
* d. calculate select nutritional information
*
* e. calculate recipe cost
*
* f. propose a suitable alternative to your instructor
*
//SteppingStone5_recipeTester.java
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SteppingStones;
/**
*
* @author snhu.edu
*/
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
As per chegg policy, I am solving just part A of the project where I need to complete the given code. I have also added a class called Ingredient.java.
Please post saperate question to add ArrayList, of Ingredient in place of Strings i.e. requirement under Final Submission part.
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
SteppingStone5_Recipe.java
/**
*
*
*
* @author
*
*/
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<String> 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<String> getRecipeIngredients() {
return recipeIngredients;
}
public void setRecipeIngredients(ArrayList<String> 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;
}
}
Ingredient.java
public class Ingredient {
private String name;
private double amount;
private String unitOfMeasure;
private int calories;
private Scanner sc = new Scanner(System.in);
public Ingredient() {
System.out.println("Please enter the name of the ingredient: ");
name = sc.nextLine();
System.out.println("Please enter the number of cups of " + name + " we'll need: ");
boolean valid = false;
while (!valid) {
try {
amount = Double.parseDouble(sc.nextLine());
valid = true;
} catch (Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
System.out.println("Please enter the unit of measure: ");
unitOfMeasure = sc.nextLine();
System.out.println("Please enter the number of calories per cup: ");
valid = false;
while (!valid) {
try {
calories = Integer.parseInt(sc.nextLine());
valid = true;
} catch (Exception e) {
System.out.println("Invalid value. Try again: ");
}
}
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public int getCalories() {
return calories;
}
public String getUnitOfMeasure() {
return unitOfMeasure;
}
@Override
public String toString() {
return "Ingredient [name=" + name + ", amount=" + amount + ", unitOfMeasure=" + unitOfMeasure + ", calories="
+ calories + "]";
}
public static void main(String args[]) {
Ingredient i = new Ingredient();
System.out.println(" " + i);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.