# ICM, Genetic Crossings

For this week’s intro to computational media assignment, we were to re-factor some of our old code into a more object-oriented programming style.  It turns out my code from past weeks didn’t really make use of anything that could be turned into objects or modules, though I ended up using some Strings and some imported objects.  So I decided to write some new code focusing on the classes, constructors, and objects, so I could get some good practice with the concept.  OOP is extremely important to understand and I’ve had trouble learning it in the past when I’ve tried reading books on Java programming.

So my code this week is for genetic crossings in Galapag.us.  Genetic crossings, Punnett squares, remember those from high school?  You’d take two parents, map out the dominant vs. recessive genes, and predict what the offspring would look like.  And Galapag.us is my reputation ecosystem for calculating numbers using all the data each of us has generated in our lives, and pumping out formulas for different realms of our lives (like work, school, religion, reading, etc.).

I made a Person class so I could instantiate multiple people with different values for their “characteristics” like strength, intelligence, education, religiosity, etc.  Two people were instantiated with random values, and the other two were Gisele Bundchen and Albert Einstein.  I had each person randomly placed on the palette with each characteristic (18 in all, most on a scale from low to high, 1 to 10) represented by a circle filled in with a randomly generated color.  The circles are randomly placed around each person’s main body circle (full black).

I then wanted to make a sex() function that returned a new Person object.  So I had Albert and Gisele mate with each other.  This was more of a proof of concept than a fully-fleshed-out (giggedy) function, since all it does is average the values of Albert’s and Gisele’s “appearance” characteristic.  As you may expect, Gisele has a high attractiveness and Albert doesn’t (I didn’t have time to code in sexiness growing over time and relativity (heyohh) to intellect or charm).  The result of sex() generates a new object, person3, which is then displayed via draw() and person3.display().

I did manage to create a Hashtable object to store the characteristics and their values.  I still hate strict typecasting and the inability to create variable variables or arrays with string keys.  I am partial to PHP which is made for non-coders and also made for the web — I’m not used to having to be so strict about everything. :(

I also managed to figure out how to reference variables within specific objects (e.g. this.trait.get(“appearance”)) and then convert it to a string, then to a float, then to an int.  It shouldn’t be that hard though.  But I guess that’s Java-style and I’m not really using it in a real-worldy kind of way for this app.

Obviously the design needs work.  Sometimes the people overlap each other, and the sizes of the circles don’t really reveal much of anything interesting.  It would have been nice to create mini-relationships between the different people, such as people in the same family sharing similar characteristics.  Or I could have randomized the sex() function based on appearance, intellect, compatibility (both single?), etc.

This seems like a good place to start for Galapag.us though, a building block for having peoples’ “characteristics” interact with each other so I can create formulas which compare the two or predict offspring or whatnot.

YAY FOR OOP! <3

Demo, code, and links below the jump.

Here’s the code (and the .pde file):

```// Ben Turner
// NYU-ITP, Intro to Computational Media

// Exercise with objects, classes, constructors

// need these functions for all the vars and walking thru them
import java.util.Enumeration;
import java.util.Hashtable;

// init the Person objects
Person person1;
Person person2;
Person albertEinstein;
Person giseleBundchen;
int numPeople = 4;

// total of 18 characteristics per person right now
int numCharacteristics = 18;
int[][] colorArray = new int[numCharacteristics];
int[] characteristicsArray = new int[numCharacteristics];

void setup() {
size(600, 400);
smooth();
frameRate(1);

/* reference:
* float _strength, float _intelligence, float _wisdom,
* float _charisma, float _stamina, float _wit, float _humor,
* float _pWeight, float _pHeight, float _education, float _age,
* float _creativity, float _responsibility, float _discipline,
* float _honesty, float _religiosity, float _entrepreneurialism,
* float _appearance, String _namePerson
*/

person1 = new Person(random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), random(0, 10), random(0, 300), random(0, 100), random(0, 10),
random(0, 120), random(0, 10), random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), "person1");
person2 = new Person(random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), random(0, 10), random(0, 300), random(0, 100), random(0, 10),
random(0, 120), random(0, 10), random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), "person2");
albertEinstein = new Person(6, 10, 10, 7, 6, 8, 6, 180, 67, 10, 80, 10, 10, 9, 8, 5, 2, 1, "Albert Einstein");
giseleBundchen = new Person(2, 2, 1, 8, 5, 2, 2, 130, 70, 4, 30, 2, 6, 7, 5, 2, 2, 10, "Gisele Bundchen");

// generates random color standard for all the characteristics
for (int i=0;i<numCharacteristics;i++) {
for (int j=0;j<3;j++) {
colorArray[i][j] = int(random(255));
}
}
}

void draw() {
background(255);
person1.display();
person2.display();
albertEinstein.display();
giseleBundchen.display();
Person person3 = sex(giseleBundchen, albertEinstein);
person3.display();
int i = 0;
int j = 30;

// loads circle diameters from hashtable, iterates thru all characteristics
for (Enumeration e = person1.trait.keys() ; e.hasMoreElements() ; ) {
fill(colorArray[i], colorArray[i], colorArray[i], 100);
rect(405, j, 5, 5);
fill(colorArray[i], colorArray[i], colorArray[i]);
text(e.nextElement().toString(), 415, j+7);
i++;
j = j + 15;
}

noLoop();
}

Person sex(Person comp1, Person comp2) {
Person newPerson;
newPerson = new Person(random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), random(0, 10), random(0, 300), random(0, 100), random(0, 10),
random(0, 120), random(0, 10), random(0, 10), random(0, 10), random(0, 10), random(0, 10),
random(0, 10), random(0, 10), "person3");
int _appearance;
Object diameterCirc1 = comp1.trait.get("appearance");
Object diameterCirc2 = comp2.trait.get("appearance");
float _diameter1 = Float.parseFloat(diameterCirc1.toString());
float _diameter2 = Float.parseFloat(diameterCirc2.toString());
_appearance = round(_diameter1 + _diameter2)*2;
newPerson.trait.put("appearance", _appearance);
numPeople++;
return newPerson;
}

class Person {
Hashtable trait = new Hashtable();
int xPos = int(random(50, 350));
int yPos = int(random(50, 350));
String namePerson;

// constructor
Person(float _strength, float _intelligence, float _wisdom,
float _charisma, float _stamina, float _wit, float _humor,
float _pWeight, float _pHeight, float _education, float _age,
float _creativity, float _responsibility, float _discipline,
float _honesty, float _religiosity, float _entrepreneurialism,
float _appearance, String _namePerson) {
trait.put("strength", _strength);
trait.put("intelligence", _intelligence);
trait.put("wisdom", _wisdom);
trait.put("charisma", _charisma);
trait.put("stamina", _stamina);
trait.put("wit", _wit);
trait.put("humor", _humor);
_pWeight = round(_pWeight / 20);
trait.put("pWeight", _pWeight);
_pHeight = round(_pHeight / 10);
trait.put("pHeight", _pHeight);
trait.put("education", _education);
trait.put("age", _age);
trait.put("creativity", _creativity);
trait.put("responsibility", _responsibility);
trait.put("discipline", _discipline);
trait.put("honesty", _honesty);
trait.put("religiosity", _religiosity);
trait.put("entrepreneurialism", _entrepreneurialism);
trait.put("appearance", _appearance);
namePerson = _namePerson;
} // end Person method

void display() {
int i = 0;

// loads circle diameters from hashtable, iterates thru all characteristics
for (Enumeration e = trait.keys() ; e.hasMoreElements() ; ) {
fill(colorArray[i], colorArray[i], colorArray[i], 100);
Object diameterCirc = trait.get(e.nextElement());
float _diameter = Float.parseFloat(diameterCirc.toString());
int diam = round(_diameter)*2;
ellipse(xPos+int(random(-40, 40)), yPos+int(random(-40, 40)), diam, diam);
i++;
}

// displays main circle for each Person
fill(0);
ellipse(xPos, yPos, 20, 20); // overall rating, in middle
text(this.namePerson, xPos+15, yPos+15);
} // display for Person
} // end Person class```