PoD.lava Fridge Proble attributes protected LinkedL1stcPerson, grocery ne - new
ID: 3915958 • Letter: P
Question
PoD.lava Fridge Proble attributes protected LinkedL1stcPerson, grocery ne - new LinkecL1st:Fersons(); protected Stackestring> topShelt-new Stack middlesnelf : new stack?String>(); protected StackeString» bottomshelf new Stackestring>); Problem of the Day Problem of the day 3: Murphy's Law Why is it tnat whenever you need?n ingredient from the fridge it is 1wvav: the back? Toda? you wil model this real-life application of Murphy's Law by finishing off the cess Fridge. Justas n reel fridge, you wil heve to remove many ingredients betore you cen get to the one you wen. We will elway have to gr?b from the front 'until we reacn what we desire. Iwe wil model this using stacks public Fridge) PLEASE START WORK HERE 20 s(Stringll croceries, Stack shelf) method, constructor and attr outes have been glven to you @method getingredient (String ingredient) 38 ? @parer, ingredient (String) ; ingredient to outsin eparam shelf (StackExplanation / Answer
// PLEASE START WORK HERE // =============================================== /** * @method addGroceries(String[] groceries, Stack shelf) * * @param groceries * String[]) : groceries to add to shelf * @param shelf * (Stack) : shelf to add the groceries to * @returns void * Add provided groceries to front of the specified shelf */ public void addGroceries(String[] groceries, Stack shelf) { for(String g: groceries) { shelf.push(g); } } /** * @method getIngredient(String ingredient) * * @param ingredient * (String) : ingredient to obtain * @param shelf * (Stack) : shelf that the ingredient is (definitely) on. * @returns void * You need a new ingredient, but it is (possibly) not at the front of * the shelf. * Take out whatever ingredients are necessary to remove what you need * and then * return the removed items back in the reverse order that you took * them out. */ public void getIngredient(String ingredient, Stack shelf) { LinkedList removedIngredients = new LinkedList(); while(!shelf.peek().equals(ingredient)) { // remove the ingredient from front of shelf and add it to the removedIngeredient list at back removedIngredients.add(shelf.pop()); } // remove intended ingredient shelf.pop(); // now put back the ingredients from removedIngeredient list's front while(!removedIngredients.isEmpty()) { shelf.push(removedIngredients.remove(0)); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.