Decoupling Class from Wolf and Sheep
<p>We have been asked to decouple our class, Simulator, so that it no longer has any sheep or wolf knowledge but instead only deals with Animals as a List. We've been told to modify the constructors of the Simulator class so that it uses an AnimalFactory object as the parameter. Any help would be greatly appreciated. I'm looking for any guidance or help and not direct code. Thanks in advance.</p>

<p>This is the code we have been given for AnimalFactory to begin with.</p>

<pre><code>    import java.awt.Color;


     public class AnimalFactory
     {

    public AnimalFactory()
    {

    }

    public void produce(SimulatorView view) 
    {
        // Associate colors with the animal classes.
        view.setColor(Sheep.class, Color.green);
        view.setColor(Wolf.class, Color.red);
    }
}
</code></pre>

<p>This is the Simulator class and we are supposed to move the populate method.</p>

<pre><code> import java.util.Random;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.awt.Color;


public class Simulator
 {
// Constants representing configuration information for the simulation.
// The default width for the grid.
private static final int DEFAULT_WIDTH = 70;
// The default depth of the grid.
private static final int DEFAULT_DEPTH = 40;

// List of animals in the field.
private List&lt;Animal&gt; animals;
// The current state of the field.
private Field field;
// The current step of the simulation.
private int step;
// A graphical view of the simulation.
private SimulatorView view;
// A factory for creating animals - unused as yet.
private AnimalFactory factory;

/**
 * Construct a simulation field with default size.
 */
public Simulator()
{
    this(DEFAULT_DEPTH, DEFAULT_WIDTH);
}

/**
 * Create a simulation field with the given size.
 * @param depth Depth of the field. Must be greater than zero.
 * @param width Width of the field. Must be greater than zero.
 */
public Simulator(int depth, int width)
{
    if(width &lt;= 0 || depth &lt;= 0) {
        System.out.println("The dimensions must be greater than zero.");
        System.out.println("Using default values.");
        depth = DEFAULT_DEPTH;
        width = DEFAULT_WIDTH;
    }

    animals = new ArrayList&lt;Animal&gt;();
    field = new Field(depth, width);

    // Create a view of the state of each location in the field.
    view = new SimulatorView(depth, width);
    factory = new AnimalFactory();

    // Setup a valid starting point.
    reset();
}

/**
 * Run the simulation from its current state for a reasonably long period,
 * e.g. 500 steps.
 */
public void runLongSimulation()
{
    simulate(500);
}

/**
 * Run the simulation from its current state for the given number of steps.
 * Stop before the given number of steps if it ceases to be viable.
 * @param numSteps The number of steps to run for.
 */
public void simulate(int numSteps)
{
    for(int step = 1; step &lt;= numSteps &amp;&amp; view.isViable(field); step++) {
        simulateOneStep();
    }
}

/**
 * Run the simulation from its current state for a single step.
 * Iterate over the whole field updating the state of each
 * fox and rabbit.
 */
public void simulateOneStep()
{
    step++;

    // Provide space for newborn animals.
   List&lt;Animal&gt; newAnimals = new ArrayList&lt;Animal&gt;();        
    // Let all rabbits act.
    for(Iterator&lt;Animal&gt; it = animals.iterator(); it.hasNext(); ) {
        Animal animal = it.next();
        animal.act(newAnimals);
        if(! animal.isAlive()) {
            it.remove();
        }
    }

    // Add the newly born foxes and rabbits to the main lists.
    animals.addAll(newAnimals);

    view.showStatus(step, field);

    // Pause for 200 milliseconds
    try {
        Thread.sleep(200);
    } catch(InterruptedException e) {
    }
}

/**
 * Reset the simulation to a starting position.
 */
public void reset()
{
    step = 0;
    animals.clear();
    populate();
    factory.produce(view);

    // Show the starting state in the view.
    view.showStatus(step, field);
}

/**
 * Randomly populate the field with woolfs and a sheep.
 */
private void populate()
{
    field.clear();
    Sheep sheep = new Sheep(field,field.freeRandomLocation());
    animals.add(sheep);
    for(int no = 1; no &lt;= 3; no++) {
        Wolf wolf = new Wolf(field,field.freeRandomLocation(),sheep);
        animals.add(wolf);
    }
}
 }
</code></pre>
