Hello all,

This is a very simplistic simulation but i'm hoping to show bacterial growth using this simple piece of code.

public class Bacteria
{
    boolean living;
    Bacteria x;
    int population;
   public void spawn()
   {
       living = true;
       try{
                    Thread.sleep(2000);
                }
            catch (InterruptedException e )
                {}
       Fission();
       try{
                    Thread.sleep(100000);
                }
            catch (InterruptedException e )
                {}
       Die();
    }
   private void Fission()
   {
       x = new Bacteria();
       x.spawn();
    }
   private void Die(){living = false;}
   private boolean Check(){return this.living;}
   private boolean ping(){return  x.Check();}
   
}

The idea is that if i initialize one object of the Bacteria class and run the spawn() module then it will just continue to "reproduce". The class that is going to run this code needs to be able to keep count of the population of bacteria. I want to represent the population graphically using dashes to represent individual bacteria. My problem is that i cannot see a way to keep count. I was thinking of using something like the ping() module which basically just checks the "next" bacteria and returns a boolean. My problem is i cannot refer to all of the objects within the initial Bacteria object in any sort of efficient manner.

Does anyone have any ideas? I know this could probably be achieved without objects... But I just think this a very elegant design (aside from not being functional).

Thanks for lookin,
Khodeir

For simple cases like this you can just declare some kind of collection (eg ArrayList) as a static variable in the class, then, in the constructor, add the new instance to the collection.

Yeah I'm thinking something like this:

public class Bacteria
{
    boolean living;
    int population;
    static Bacteria [] x;
   public void spawn()
   {
       living = true;
       try{
                    Thread.sleep(2000);
                }
            catch (InterruptedException e )
                {}
       Fission();
       try{
                    Thread.sleep(100000);
                }
            catch (InterruptedException e )
                {}
       Die();
    }
   private void Fission()
   {
       if(x!=null)
       x = expand(x);
       else x = new Bacteria[1];
       x[x.length-1] = new Bacteria();
       x[x.length-1].spawn();
    }
   private void Die(){living = false;}
   private boolean Check(){return this.living;}
   public void all()
   {
       population = 0;
       for(int count = 0; count<x.length; count++)
       if(x[count].Check())
       population++;
    }
   private Bacteria[] expand(Bacteria[] array) {
    Bacteria[] temp = new Bacteria[array.length+1];
    System.arraycopy(array, 0, temp, 0, array.length);
    return temp;
}
}

and then use another class to run it like:

public class Medium
{
public static void main()
{
   Bacteria x = new Bacteria();
   x.spawn();
   try{
                    Thread.sleep(2001);
                }
            catch (InterruptedException e )
                {}
   
   for(int count = 0; count<500; count++){
       try{
                    Thread.sleep(2001);
                }
            catch (InterruptedException e )
                {}
       x.all();
       System.out.println('-' * x.population);
    }
}
}

Is this what you meant? This should work, correct?

Edited 6 Years Ago by Khodz: n/a

Oh actually.. I've just realized the fatal flaw in my program. Once the objects have been initialized, nothing runs because once the first 2 seconds are over, it creates a new object which then waits 2 seconds to create a new object which goes on and on and on. I doubt i can get this program to multi-task... Any ideas?

Maybe spawn should run in a new Thread each time? (Sorry, I don't have time right now to give this any serious design-type thought)

Maybe spawn should run in a new Thread each time? (Sorry, I don't have time right now to give this any serious design-type thought)

Yea that's a good idea. The problem still remains though, that if i want each bacteria to "reproduce" by itself, then i need to have each Bacteria object initialize its own Bacteria offspring. And if that's the case, there's no way of referring to all the different objects from the outside class. (or is there?)

Alternatively, I could have them all initialized by the class itself and just keep an array of all of them, but that defeats the purpose of the simulation (to show the population as the individual bacteria multiply and populate the medium) because then they dont grow exponentially.

What i could do, is count the number of threads. This should be possible, although i don't remember the code. Would the thread terminate when it reaches then end of the code? If so, this is the best solution. If not, could i refer to variables within these different threads?

I wonder if there's a way to have all the objects pass a "count" back to the original class.I can't think of any other solutions.

This article has been dead for over six months. Start a new discussion instead.