teaching machines

CS 145 Lecture 35 – Data Analysis

December 9, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

People often ask, “What can I do with computer science?” I usually splutter and stammer out an unconvincing answer, because the answer is pretty much anything. Your choices are many, and they widen near our nation’s larger cities. Some people fear that they will be spending all their time at a computer, but in actuality, not a lot of code gets written in a day. A lot of time is spent on design and in meetings.

I am not equipped with the relevant life experiences to tell you all the ways that you can make a career in computing. But if you love subject X, there’s a way to find or build a career in it that involves technology. Computer science feeds on problems from all manner of subjects.

That said, today let’s pretend to be a sociologist or a geographer. Let’s take a data set and investigate it with code. But let’s start with coming up with some questions:

You have the populations of each country in the world from 1960 through 2015. Investigate this data set. What are three things you want to know?

Here’s your TODO to complete before we meet again:

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

Who moves the clock’s hands?
It does, thank goodness it does
I don’t have the time

P.P.S. Here’s the code we wrote together…

Country.java

package lecture1209;

import java.util.Arrays;

public class Country implements Comparable<Country> {
  private String name;
  private int[] populations;
  
  public Country(String name, int[] populations) {
    this.name = name;
    this.populations = populations;
  }
  
  public boolean isDwindling() {
    return populations[populations.length - 1] < populations[0];
  }
  
  public double getGrowth() {
    return populations[populations.length - 1] / populations[0];
  }
  
  public boolean isEven() {
    return populations[populations.length - 1] % 2 == 0;
  }
  
  public String toString() {
    return name + " " + Arrays.toString(populations);
  }
  
  public int compareTo(Country that) {
    if (this.getGrowth() < that.getGrowth()) {
      return -1;
    } else if (this.getGrowth() > that.getGrowth()) {
      return 1;
    } else {
      return 0;
    }
  }
}

Sociographer.java

package lecture1209;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Sociographer {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("/Users/johnch/Desktop/pops.csv"));
    ArrayList<Country> countries = new ArrayList<>();

    while (in.hasNextLine()) {
      // read a line
      String line = in.nextLine();
      String[] pieces = line.split(",");
      int[] populations = new int[pieces.length - 1];
//      System.out.println(line);
      for (int i = 0; i < populations.length; ++i) {
        if (pieces[i + 1].isEmpty()) {
          populations[i] = -1;
        } else {
          populations[i] = Integer.parseInt(pieces[i + 1]);
        }
      }

      Country country = new Country(pieces[0], populations);
      countries.add(country);
    }

    in.close();
    
//    for (int i = 0; i < countries.size(); ++i) {
//      if (countries.get(i).isDwindling()) {
////        System.out.println(countries.get(i).toString());
//        System.out.println(countries.get(i));
//      }
//    }
    
    int nEvens = 0;
    for (Country country : countries) {
      if (country.isDwindling()) {
        System.out.println(country);
      }
      if (country.isEven()) {
        ++nEvens;
      }
    }
    
    System.out.println(nEvens / (double) countries.size());
    
    Collections.sort(countries);
    Collections.reverse(countries);
    System.out.println(countries.get(0));
  }
}