Question: When I run the following code I get chicken as an ingredient under my mac and cheese recipe and also get macaroni as an ingredient

When I run the following code I get chicken as an ingredient under my mac and cheese recipe and also get macaroni as an ingredient for my fried chicken recipe.  I need help in reversing this order, what am I not seeing here?  Thanks!

 

package SteppingStones;

 

import java.util.ArrayList;

 

public class RecipeTest {

/**
 * @param args the command line arguments --- in case you use
 */
public static void main(String[] args) {

 

/* Creating first recipe object for Recipe class */
 Recipe myFirstRecipe = new Recipe();

 

/* Creating array lists for first recipe ingredients */
 ArrayList recipeIngredientsOne = new ArrayList<>();

 

 /* Ingeredients that will be added */
 String ingredientName = "Macaroni";

 

 /* Adding ingredients for first recipe */
 recipeIngredientsOne.add(ingredientName);

 

/* Creating second recipe object for Recipe class with parameters */
 Recipe mySecondRecipe = new Recipe("Fried Chicken", 2, recipeIngredientsOne, 700);

 

/* Creating array lists for second recipe ingredients */
 ArrayList recipeIngredientsTwo = new ArrayList<>();

 

/* Ingeredients that will be added */
 String ingredientName2 = "Chicken";

 

/* Adding ingredients for second recipe */
 recipeIngredientsTwo.add(ingredientName2);

 

 /* Calling mutators with the first recipe object */
 myFirstRecipe.setRecipeName("Mac and Cheese");
 myFirstRecipe.setServings(3);
 myFirstRecipe.setRecipeIngredients(recipeIngredientsTwo);
 myFirstRecipe.setTotalRecipeCalories(400);

 

// Print details of both recipes
 myFirstRecipe.printRecipe();
 mySecondRecipe.printRecipe();
}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Answer In your code you are mistakenly setting the same recipeIngredients list for both recipes This ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!