Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. This question is about the Composite and Iterator patterns.(10 points) The di

ID: 3730978 • Letter: 2

Question

2. This question is about the Composite and Iterator patterns.(10 points) The diagram on the left shows the composite pattern for a wizard's bag. Assume that the Wizard has several wizard bags which he can put inside each other (like Babushka dolls). Bags can contain other bags or loose items. He can make the entire contents of a bag or a single item disappear by saying the words: Evanesco Ocsenave Magicltems Wizarditems WizardBayg a. Write java-like pseudo code to for a method called disappear() to make the WizardBag and everything in it disappear. (3 points) b. Write java-like pseudo code for an external iterator class that iterates through all of the Wizarditems (i.e the leaf items only). You will get points for showing the main methods of the class (3 points), showing any data structure(s) needed in the class (1 points), and writing high quality pseudocode inside the methods of the class (3 points)

Explanation / Answer

Answer for Question 2(a):

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

    Void disappear(){

       String removeElem = “WizardBag”;

        Iterator<String> itr = WizardBag.iterator();

        while(itr.hasNext()){

           if(removeElem.equals(itr.next())){

               disappear()  

               }

           else(){

               itr.remove();

            }

        }

The answer to Question 3(a)

The iterator decouples the iteration logic from the collection implementation.

The answer to Question 3(b)

The visitor decouples the host's structural behaviors from the extrinsic algorithms.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote