Hello, I created this code for the assignment. I also have a recipetest class. D
ID: 3874354 • Letter: H
Question
Hello, I created this code for the assignment. I also have a recipetest class. Do I enter my code into the recipetest? Don't know why I need that.
This is the code:
import java.util.Scanner;
import java.util.ArrayList;
/**
*
* @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) {
double totalRecipeCalories = 0;
ArrayList<String> recipeIngredients = new ArrayList<String>();
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);
recipe1.printRecipe();
scnr.close();
}
}
This is the recipetest:
//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 SteppingStone5_Recipe createNewRecipe() {
// 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);
This is the instruction for the assinment:
*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()
When I run the code it works but why do I need the test recipe?
Explanation / Answer
hi, You do not need ant recipetest class to run your code.In JavaSE public static void main(String[] args) is only method which call by JVM. If you made only SteppingStone5_Recipe class and provide main method in it and intatiate the SteppingStone5_Recipe class object in it and also call the method of SteppingStone5_Recipe class its also work well. But it is a good coding practice to make a class and another class to test it.
AS: You made SteppingStone5_Recipe claas menu driven which run with out recipe test class.You can also make like this without menu drawn which also can run code without recipe test.
package test;
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<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 myFirstRecipe = new SteppingStone5_Recipe();
ArrayList<String> recipeIngredients = new ArrayList<String>();
ArrayList<String> recipeIngredientsTwo = new ArrayList<String>();
String ingredientName = "Anchovies";
recipeIngredients.add(ingredientName);
SteppingStone5_Recipe mySecondRecipe = new SteppingStone5_Recipe("Pizza", 2, recipeIngredients, 300);
// Initialize first recipe
String ingredientNameTwo = "Noodles";
recipeIngredientsTwo.add(ingredientNameTwo);
myFirstRecipe.setRecipeName("Ramen");
myFirstRecipe.setServings(2);
myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);
myFirstRecipe.setTotalRecipeCalories(150);
System.out.println("first reciepie is" );
myFirstRecipe.printRecipe();
System.out.println("Second reciepie is");
mySecondRecipe.printRecipe();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.