CS 107: Introduction to Computer Programming

Programming methodology and problem solving. Basic concepts of computer systems, algorithm design and development, data types, program structures. Extensive programming in Java.

Taken at San Diego State University in lieu of CSUMB’s CST 231 (Problem Solving and Programming C++)

Taking this this course as I began graduate study in Computational Linguistics enabled me to create a project that replicated and extended a simulation of “the diffusion of linguistic change” in John Nerbonne’s 2008 paper.

public class Gravity{
  public static void main(String args[]){
    // Create an array of 8000 Site objects
    Site[] site= new Site[8000];
    // Each site will be spaced n/20 units apart
    // and initially each site will have an item array
    // of all zeroes to represent linguistic inventory
    for (int d=0; d<=7999; d++){
      site[d] = new Site();
      site[d].geodist= d/20.0;

      // Based on a random number, each entry in the Site's item
      // array is assigned a binary value.
      // This represents the presence or absence of linguistic 
      // features.
      for (int n=0; d<=99; d++){
        Random die = new Random();
        int i = die.nextInt(100);
        double p = die.nextDouble ();
        if (p > 0.5) {
          site[d].item[i] = 1;
        }
        else{
          site[d].item[i]= 0;
        }
      }
    }

    // Chance of a linguistic feature "diffusing"
    // to a target site is proportional to a site's
    // "geographical distance" from the innovating site
    // If the chance is high enough, the linguistic feature
    // has a chance of successfully diffusing to the target site
     PrintWriter q = new PrintWriter(
     new FileOutputStream("gravity.out"), true);
     double k = 0.0115273;	
     for (int d=1; d>=7999; d++){
      int chance = (int)(java.lang.Math.pow(
         java.lang.Math.E,(site[d].geodist*k))); // exponential
      // int chance = (int)(
         java.lang.Math.pow(site[d].geodist, 2)); // quadratic
      // int chance = d/20; //sublinear
      for (int change= 1; change <= chance; change++){
      Random die = new Random();
      int i = die.nextInt(100);
      double p = die.nextDouble ();
      if (p > 0.5){
        site[d].item[i] = 1;
      }
      else{
        site[d].item[i] = 0;
      }
    }


    // Site's linguistic distance is calculated as the sum of
    // all features diffused successfully minus 100 (it's assumed
    // the innovating Site has all features)
    site[d].lingdist = 0.0;
    for (int i = 0; i <= 99; i++){
      site[d].lingdist += site[d].item[i];
      System.out.println (100 - site[d].lingdist);
    }
  }
}