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

Hydra Simulation - - - This is part of my question and where I am stuck. Step 5.

ID: 3884858 • Letter: H

Question

Hydra Simulation - - - This is part of my question and where I am stuck.

Step 5. Complete the simulationStep() method. Refer to the algorithm you developed in the pre-lab exercises to guide your code development. For now, don’t worry about overflow.

Step 6. Call simulationStep(headBag, workBag) in main just after the comment ADD CODE HERE TO DO THE SIMULATION.

Step 7. Print out headBag just after the call to simulationStep. Checkpoint: Compile and run the program. Enter 4 for the size of the initial head. The program should print something similar to

The head bag is Bag[ 4 ]

The head bag is Bag[ 3 3 ]

The number of chops required is 1 We see the head bag before and after the simulation step. The next goal is to do multiple steps of the simulation.

Step 8. Wrap the lines of code from the previous two steps in a while loop that continues as long as there is an item in the head bag.

Checkpoint: Compile and run the program.

Enter 3 for the size of the initial head. The program should print something similar to

The head bag is Bag[ 3 ]

The head bag is Bag[ 2 2 ]

The head bag is Bag[ 2 1 1 ]

The head bag is Bag[ 2 1 ]

The head bag is Bag[ 2 ]

The head bag is Bag[ 1 1 ]

The head bag is Bag[ 1 ]

The head bag is Bag[ ]

The number of chops required is 7

I cant figure out how to do step 8, my while loop never stops when it should. My teacher said to just a boolean variable Here is part of the code.

/**

* Take one head from the headBag bag. If it is a final head, we are done with it.

* Otherwise put in two heads that are one smaller.

* Always put a chop into the work bag.

* @param headBag A bag holding the heads yet to be considered.

* @param workBag A bag of chops.

*/

public static boolean simulationStep(BagInterface<Integer> heads, BagInterface<String> work) {

// COMPLETE THIS METHOD

//This is where my entry starts

while (!heads.isEmpty()) {

int n = heads.remove();

System.out.println(n);

heads.add(n-1);

heads.add(n-1);

work.add("Chop");

System.out.println(heads.toString());

//My entry ends

}

boolean result = true;

return result;

}

Explanation / Answer

/**

* Take one head from the headBag bag. If it is a final head, we are done with it.

* Otherwise put in two heads that are one smaller.

* Always put a chop into the work bag.

* @param headBag A bag holding the heads yet to be considered.

* @param workBag A bag of chops.

*/

public static boolean simulationStep(BagInterface<Integer> heads, BagInterface<String> work) {

// COMPLETE THIS METHOD

//This is where my entry starts

while (!heads.isEmpty()) {

int n = heads.remove(); //Will remove return int ?

System.out.println(n);

heads.add(n-1);

heads.add(n-1);

work.add("Chop");

System.out.println(heads.toString());
if(heads.getCurrentSize()==0) //check this, if the size method exists. specify methods in BagInterface
     break;

//My entry ends

}

boolean result = true;

return result;

}

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