Java decoupling class and moving method
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/5171832/decoupling-class-from-wolf-and-sheep">Decoupling Class from Wolf and Sheep</a>  </p>
</blockquote>



<p>Hey there. I'm fairly new to java so apologies for the noobish understanding :) I'll try and explain the problem as well as I possibly can. I have a project that I've been trying to edit. The java project involves a simulation of wolves and sheeps as they coexist and the sheep are eaten. I have various classes but the main ones I'm focusing on are Simulator and AnimalFactory. There is a wolf and sheep class but they aren't particularly important with the problem. The simulator class uses sheep and wolf data in one of the methods. I'm trying to move that method to another class, called AnimalFactory, so that the Simulator only relies on the animals list and not individual wolves and sheep. I've also been trying to modify the constructors in the Simulation class and to make them take an AnimalFacotry object as paramater but am having problems. When I move the populate method into AnimalFactory and try to compile the Simulator class, I get the following error: cannot find symbol - method populate. I assume this is because the method is in a different class but when i reference the Simulator.populate method, it says that it can't be referenced from a static object. Any help to get the method working in the AnimalFactory class and for changing the Simulator constructors, so that it takes the populate method into account would be greatly appreciated. Here is the code for the AnimalFactory class so far. Below that will be the Simulator code</p>

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


  public class AnimalFactory
 {
/**
 * Constructor for objects of class AnimalFactory
 */
public AnimalFactory()
{
}


    /**
 * Randomly populate the field with woolfs and a sheep.
 */
public 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);
    }
}


/**
 * Populate the simulation with animals and connect to view.
 * @param view The visualisation of the simulation.
 */
public void produce(SimulatorView view) 
{
    // Associate colors with the animal classes.
    view.setColor(Sheep.class, Color.green);
    view.setColor(Wolf.class, Color.red);
}
}





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();
    AnimalFactory.populate();
    factory.produce(view);

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