Working in an object oriented App course and have "starter code" with inline instructions on what to complete (SteppingStone5_Recipe.java).
Working in an object oriented App course and have “starter code” with inline instructions on what to complete (SteppingStone5_Recipe.java). I have completed the code to the best of ability. When I run the tester code (SteppingStone5_RecipeTest.java) I am getting errors with my code and I can’t figure out why and I am too embarrassed to ask my instructor for more help.
It’s important to note I am not allowed to edit the SteppingStone5_RecipeTest file. That needs to remain as is and that is what is confusing me because the error message points to an error in the SteppingStone5_RecipeTest file.
The error I am initially receiving is:
run:
Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – Erroneous ctor sym type: SteppingStones.SteppingStone5_Recipe.<init>
at SteppingStones.SteppingStone5_RecipeTest.main(SteppingStone5_RecipeTest.java:13)
C:Usersr.mahoney1_snhuAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
/* SteppingStone_Recipe.java */ package SteppingStones; import java.util.Scanner; import java.util.ArrayList; import java.util.List; /** * * ---------------- */ 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 * */ private int servings; private List<Ingredient> recipeIngredients; private double totalRecipeCalories; public List<Recipe> listofRecipes = new ArrayList<Recipe>(); //Add mutators and accessors for the class variable. public String getRecipeName() { return recipeName; } public void setRecipeName(String recipeName) { this.recipeName = recipeName; } public int getServings() { return servings; } public void setServings(int servings) { this.servings = servings; } public List<Ingredient> getRecipeIngredients() { return recipeIngredients; } public void setRecipeIngredients(List<Ingredient> recipeIngredients) { this.recipeIngredients = recipeIngredients; } public double getTotalRecipeCalories() { return totalRecipeCalories; } public void setTotalRecipeCalories(double totalRecipeCalories) { this.totalRecipeCalories = totalRecipeCalories; } //End mutators and accessors public SteppingStone5_Recipe() { this.recipeName = ""; this.servings = 0; //<--- assignment value with appropriate data type this.recipeIngredients = new ArrayList <Ingredient>(); //<-- assignment value for empty ArrayList this.totalRecipeCalories = 0; } public SteppingStone5_Recipe(String recipeName, int servings, ArrayList<Ingredient> 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 * */ int singleServingCalories = 0; singleServingCalories = (int) (this.totalRecipeCalories / this.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 */ System.out.println("Recipe: " + this.recipeName); System.out.println("Serves: " + this.servings); System.out.println("Ingredients: "); for (Ingredient ingrdients : recipeIngredients) { System.out.println("Ingredient name: " + ingrdients.getNameOfIngredient()); System.out.println("Ingredient Amount: " + ingrdients.getIngredientAmount()); System.out.println("Each serving has " + singleServingCalories + " Calories with measurement: " + ingrdients.unitMeasurement); } } public static SteppingStone5_Recipe createNewRecipe() { double totalRecipeCalories = 0.0; ArrayList <Ingredient> 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 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 { thisIngredient.setNameOfIngredient(ingredientName); //adding ingredient to 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 * */ totalRecipeCalories = totalRecipeCalories + (ingredientCalories * ingredientAmount); recipeIngredients.add(thisIngredient); } } while (addMoreIngredients) ; SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe(recipeName, servings, recipeIngredients, totalRecipeCalories); return recipe1; } }
package SteppingStones; import java.util.ArrayList; public class SteppingStone5_RecipeTest { public static void main(String[] args) { // Create first recipe using constructor and setter methods ArrayList<String> recipeIngredients = new ArrayList<>(); recipeIngredients.add("Peanut butter"); recipeIngredients.add("Jelly"); recipeIngredients.add("Bread"); SteppingStone5_Recipe recipe1 = new SteppingStone5_Recipe( "Peanut butter & jelly sandwich", 2, recipeIngredients, 300); System.out.println("RECIPE 1"); recipe1.printRecipe(); // Now change some of the values using mutator methods recipe1.setRecipeName("Turkey sandwich"); recipe1.setServings(5); recipe1.setTotalRecipeCalories(500); System.out.println(); // blank line System.out.println("RECIPE 1 (Modified)"); // printRecipe() will show these values have changed but // recipeIngredients was not modified recipe1.printRecipe(); System.out.println(); // blank line System.out.println("RECIPE 2"); // Create second recipe using our static createNewRecipe() method // This is similar to a well-known design pattern called Factory SteppingStone5_Recipe recipe2 = SteppingStone5_Recipe.createNewRecipe(); // No need to set hard-coded values here as createNewRecipe() will // prompt the user for ingredient info recipe2.printRecipe(); } }