/*----------------------------------------------------------------------+ | Title: Herd.java | | | | Author: David E. Joyce | | Department of Mathematics and Computer Science | | Clark University | | Worcester, MA 01610-1477 | | U.S.A. | | http://aleph0.clarku.edu/~djoyce/home.html | | djoyce@clarku.edu | | | | Date: February, 2001. | +----------------------------------------------------------------------*/ import java.awt.Color; public class Herd { int paired; // the number of paired altruist animals int unpaired; // the number of unpaired altruist animals int selfish; // the number of selfish animals Herd () { } // default constructor Herd (int initPaired, int initUnpaired, int initSelfish) { set (initPaired, initUnpaired, initSelfish); } public void set (int initPaired, int initUnpaired, int initSelfish) { paired = initPaired; unpaired = initUnpaired; selfish = initSelfish; } public String toString() { return "["+paired+","+unpaired+","+selfish+"]"; } // see if on this day any unpaired altruists become paired public void pairing() { int i = unpaired; // i and j indicate those yet to pair int j = selfish; while (i>1) if (Math.random() < (i-1.0)/(i-1+j)) { paired +=2; unpaired -=2; i -=2; } else { --i; --j; } } public void death (int n) { // n animals of the herd die int total = paired + unpaired + selfish; for (int i=0; i