Hello everyone,

I'm programming genetic processes in java for my project and I want simulate the mitosis of a human cell. A human cell contains 23 pairs of chromosome. Mitosis is basically a cell division, or reproduction, in which a cell gives rise to two genetically identical daughter cells. You can find a picture about it here (scroll a little bit down the page):

http://leilabelinda.over-blog.com/article-mitose-et-meiose-53866656.html

I think that this mitosis method would be like a java method in a class "Cell" for example. So i made a class Chromosome with it's own methods to represent a single chromosome and made a class "Cell" containing 23 pairs of chromosomes. I plan on putting the method mitosis in the class Cell and using the Cloneable interface to clone a cell. But the problem is that this method should return 2 identical cells, I think, and I don't know how to go about it. For the moment my method mitosis just returns one cell and if i want to test it, I end up testing it this way for example.

TestMitosis.java

Cell c = new Cell();
		Cell d = new Cell();
		Cell e = new Cell();
		
		try{
			d = c.mitosis();
			e = c.mitosis();
		}catch(CloneNotSupportedException e){}

Any suggestions an how to create this method? Or maybe another approach other than the one i'm using? Thanks.

You can return a Pair object which contains a pair of Cell instances. A sample Pair class implementation can be found here.

Also, cloning in Java is pretty much broken (read the experts view here) and you would be better off creating a copy constructor which does the same stuff.

class Pair<F, S> {
    
    final F first;
    
    final S second;
    
    private Pair(final F first, final S second) {
        this.first = first;
        this.second = second;
    }
    
    public static <A, B> Pair<A, B> newPair(final A first, final B second) {
        return new Pair<A, B>(first, second);
    }
    
    // getters here i.e. getFirst() and getSecond()
}

class Cell {
    
    private final String name;
    
    private Cell(final String name) {
        this.name = name;
    }
 
    // copy constructor
    public Cell(final Cell other) {
        this.name = other.name;
    }
    
    public static Cell newCell(final String name) {
        return new Cell(name);
    }
    
    public Pair<Cell, Cell> mitosis() {
        return Pair.newPair(new Cell(this), new Cell(this));
    }
    
}

Thanks for your help. I got some info on some stuffs. For returning a pair of objects, your class will work. But here is my real problem.
You know let's say an organism is born with one cell. It duplicates (mitosis) this cell into 2 cells. At the end of this first mitosis, the organism has 2 cells and not 3. Each of these 2 cells duplicate into 4 cells and so on. But with your classes I'll ended up having 3 cells (1 Cell and 1 pair of Cells) in the organism.
This is an example of my class Organism

/**
 * 
 */
package project_try;

import java.util.ArrayList;

public class Organism {
	private ArrayList<Cell> cells = new ArrayList<Cell>();

	public Organism(ArrayList<Cell> cells){
		this.cells = cells;
	}
	
	public Organism(Organism o){
		this.cells = o.cells;
	}
	
        //will this work?
	public void mitosis(Cell c) {
	       cells.add(c);
	       cells.add(new Cell(c));
	}
	/**
	 * @param cells the cells to set
	 */
	public void setCells(ArrayList<Cell> cells) {
		this.cells = cells;
	}

	/**
	 * @return the cells
	 */
	public ArrayList<Cell> getCells() {
		return cells;
	}
}

Is my mitosis method correct? Or is there another better way to do it?

How about something like...

public void mitosis()        // duplicates all the cells in this Organism...
  ArrayList<Cell> temp = new ArrayList<Cell>(cells);
  // need temp copy of cells because cells will be modified in the loop
  cells.clear();   
  // We could just add 1 copy, but removing the originals and
  // adding 2 copies is better when we get into mutations etc
  for (Cell c : temp) {      // for every cell in this Organism...
    cells.add(new Cell(c));  
    cells.add(new Cell(c));  
  }
}

Cool, thanks. That's a nice idea.

On another different note. What does this function return?

public Cells[] action() {
    return new Cells[]{this, this.clone()};
}

I don't understand line 2.
PS: Sorry for the double post

It returns an array of two C objects; one object is the object on which the 'action' method is invoked and the second one been a clone of the first one. Your mitosis method can use the same trick i.e. return return Pair.newPair(this, new Cell(this)) .

So does it mean that, if i have for example:

Cells c;
	Cells[]d = c.action();

then

d[0] = this
d[1] = this.clone()

Yeah, the first item of the array will be the object on which the method was invoked and the second item would be the newly created Cell with the same attributes as that of the original Cell object.

Oh i get it now.
Thanks everyone for the explanations and help. Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.