# teaching machines

## CS 145 Lecture 30 – Filter Pattern

### Agenda

• what ?s
• what does this do?
• program this
• the filter pattern

### TODO

• Read sections 7.3 through 7.5. Share two questions and two observations on a 1/4 sheet.
• No WOP this week.

### What Does This Do?

1. public static int ?(double[] numbers) {
int n = 0;
for (double number : numbers) {
if (number < 0) {
++n;
}
}
return n;
}

2. public static String[] ?(ArrayList<String> list) {
String[] items = new String[list.size()];
for (int i = 0; i < list.size(); ++i) {
items[i] = list.get(i);
}
return items;
}

3. public static int[] ?(int[] raw) {
int[] cooked = new int[raw.length];
cooked[0] = raw[0];
for (int i = 1; i < raw.length; ++i) {
cooked[i] = cooked[i - 1] + raw[i];
}
return cooked;
}

### Program This

• Write a method join that accepts an array of Strings and a String separator. It joins the elements together, delimiting them by the separator. (For what it’s worth, this method is called implode in PHP, a language often used to generate web pages.)

### Filter

keeper = new list
for each element
if element meets criteria
add element to keepers

### Code

#### StopWords.java

package lecture1112;

import java.util.ArrayList;

public class StopWords {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<>();

ArrayList<String> stopWords = new ArrayList<>();

ArrayList<String> wheat = filter(words, stopWords);
System.out.println(wheat);
}

public static ArrayList<String> filter(ArrayList<String> words,
ArrayList<String> stopWords) {
ArrayList<String> keepers = new ArrayList<>();
for (String word : words) {
if (!stopWords.contains(word)) {
}
}
return keepers;
}
}

#### Country.java

package lecture1112;

public class Country {
private String name;
private int population1980;
private int population2014;

public Country(String name,
int population1980,
int population2014) {
this.name = name;
this.population1980 = population1980;
this.population2014 = population2014;
}

public String getName() {
return name;
}

public int getPopulation1980() {
return population1980;
}

public int getPopulation2014() {
return population2014;
}

public String toString() {
return name;
}
}

#### PopFilter.java

package lecture1112;

import java.util.ArrayList;

public class PopFilter {
public static void main(String[] args) {
Country[] countries = {
new Country("Afghanistan", 13180431, 28397812),
new Country("Albania", 2671997, 2856673),
new Country("Algeria", 19475204, 37062820),
new Country("American Samoa", 32456, 55636),
new Country("Andorra", 36063, 77907),
new Country("Angola", 7637141, 19549124),
new Country("Antigua and Barbuda", 70301, 87233),
new Country("Argentina", 28120135, 40374224),
new Country("Armenia", 3096298, 2963496),
new Country("Aruba", 60096, 101597),
new Country("Australia", 14692000, 22031800),
new Country("Austria", 7549433, 8389771),
new Country("Azerbaijan", 6163990, 9054332),
new Country("Bahamas, The", 210660, 360498),
new Country("Bahrain", 359902, 1251513),
new Country("Belarus", 9643000, 9490000),
new Country("Belgium", 9859242, 10920272),
new Country("Belize", 144151, 308595),
new Country("Benin", 3718024, 9509798),
new Country("Bermuda", 54670, 65124),
new Country("Bhutan", 412745, 716939),
new Country("Bolivia", 5368901, 10156601),
new Country("Bosnia and Herzegovina", 4099903, 3845929),
new Country("Botswana", 997534, 1969341),
new Country("Brazil", 121740438, 195210154),
new Country("Brunei Darussalam", 193049, 400569),
new Country("Bulgaria", 8861535, 7395599),
new Country("Burkina Faso", 6822840, 15540284),
new Country("Burundi", 4126538, 9232753),
new Country("Cabo Verde", 301591, 487601),
new Country("Cambodia", 6699273, 14364931),
new Country("Cameroon", 8932121, 20624343),
new Country("Cayman Islands", 16164, 55509),
new Country("Central African Republic", 2274089, 4349921),
new Country("Chile", 11192384, 17150760),
new Country("China", 981235000, 1337705000),
new Country("Colombia", 26934591, 46444798),
new Country("Comoros", 313598, 683081),
new Country("Congo, Dem. Rep.", 26357407, 62191161),
new Country("Congo, Rep.", 1796412, 4111715),
new Country("Costa Rica", 2348427, 4669685),
new Country("Cote d'Ivoire", 8265549, 18976588),
new Country("Croatia", 4588000, 4417781),
new Country("Cuba", 9835085, 11281768),
new Country("Cyprus", 685406, 1103685),
new Country("Czech Republic", 10304193, 10474410),
new Country("Denmark", 5123027, 5547683),
new Country("Djibouti", 359247, 834036),
new Country("Dominica", 75313, 71167),
new Country("Dominican Republic", 5825715, 10016797),
new Country("Egypt, Arab Rep.", 44931971, 78075705),
new Country("Equatorial Guinea", 220589, 696167),
new Country("Eritrea", 2414647, 5741159),
new Country("Estonia", 1477219, 1331475),
new Country("Ethiopia", 35241209, 87095281),
new Country("Faeroe Islands", 43010, 49581),
new Country("Fiji", 635256, 860559),
new Country("Finland", 4779535, 5363352),
new Country("France", 55224670, 65023142),
new Country("French Polynesia", 151702, 268065),
new Country("Gabon", 726454, 1556222),
new Country("Gambia, The", 604371, 1680640),
new Country("Georgia", 4467700, 4452800),
new Country("Germany", 78288576, 81776930),
new Country("Ghana", 10802497, 24262901),
new Country("Greece", 9642505, 11153454),
new Country("Greenland", 50200, 56905),
new Country("Guam", 104131, 159440),
new Country("Guatemala", 7001101, 14341576),
new Country("Guinea", 4495479, 10876033),
new Country("Guinea-Bissau", 818280, 1586624),
new Country("Guyana", 776927, 786126),
new Country("Haiti", 5691941, 9896400),
new Country("Honduras", 3635862, 7621204),
new Country("Hong Kong SAR, China", 5063100, 7024200),
new Country("Hungary", 10711122, 10000023),
new Country("Iceland", 228138, 318041),
new Country("India", 698965575, 1205624648),
new Country("Indonesia", 145494452, 240676485),
new Country("Iran, Islamic Rep.", 38889520, 74462314),
new Country("Iraq", 13653358, 30962380),
new Country("Ireland", 3412800, 4560155),
new Country("Isle of Man", 65108, 83992),
new Country("Israel", 3878000, 7623600),
new Country("Italy", 56433883, 59277417),
new Country("Jamaica", 2133000, 2690824),
new Country("Japan", 116782000, 127450459),
new Country("Jordan", 2181000, 6046000),
new Country("Kazakhstan", 14518924, 16321581),
new Country("Kenya", 16267906, 40909194),
new Country("Kiribati", 54510, 97743),
new Country("Korea, Dem. Rep.", 17372172, 24500520),
new Country("Korea, Rep.", 38123775, 49410366),
new Country("Kosovo", 1521000, 1775680),
new Country("Kuwait", 1371494, 2991580),
new Country("Kyrgyz Republic", 3617400, 5447900),
new Country("Lao PDR", 3250924, 6395713),
new Country("Latvia", 2511701, 2097555),
new Country("Lebanon", 2605294, 4341092),
new Country("Lesotho", 1307403, 2008921),
new Country("Liberia", 1892529, 3957990),
new Country("Libya", 3078255, 6040612),
new Country("Liechtenstein", 25869, 36120),
new Country("Lithuania", 3413202, 3097282),
new Country("Luxembourg", 364150, 506953),
new Country("Macao SAR, China", 246236, 534626),
new Country("Macedonia, FYR", 1895727, 2102216),
new Country("Malawi", 6236824, 15013694),
new Country("Malaysia", 13833739, 28275835),
new Country("Maldives", 154316, 325694),
new Country("Mali", 6735247, 13985961),
new Country("Malta", 316645, 414508),
new Country("Marshall Islands", 30581, 52428),
new Country("Mauritania", 1534141, 3609420),
new Country("Mauritius", 966000, 1280924),
new Country("Mexico", 70353013, 117886404),
new Country("Micronesia, Fed. Sts.", 72967, 103619),
new Country("Moldova", 3397000, 3562045),
new Country("Monaco", 26746, 36845),
new Country("Mongolia", 1689621, 2712738),
new Country("Montenegro", 579088, 620078),
new Country("Morocco", 19798703, 31642360),
new Country("Mozambique", 12142014, 23967265),
new Country("Myanmar", 34474755, 51931231),
new Country("Namibia", 1012761, 2178967),
new Country("Nepal", 14384864, 26846016),
new Country("Netherlands", 14149800, 16615394),
new Country("New Caledonia", 143000, 250000),
new Country("New Zealand", 3112900, 4367800),
new Country("Nicaragua", 3250470, 5822209),
new Country("Niger", 5834248, 15893746),
new Country("Nigeria", 73698099, 159707780),
new Country("Northern Mariana Islands", 17048, 53860),
new Country("Norway", 4085620, 4889252),
new Country("Oman", 1154375, 2802768),
new Country("Pakistan", 79984297, 173149306),
new Country("Palau", 12197, 20470),
new Country("Panama", 1990062, 3678128),
new Country("Papua New Guinea", 3215483, 6858945),
new Country("Paraguay", 3198848, 6459721),
new Country("Peru", 17328542, 29262830),
new Country("Philippines", 47398432, 93444322),
new Country("Poland", 35574150, 38183683),
new Country("Portugal", 9766312, 10573100),
new Country("Puerto Rico", 3206000, 3721208),
new Country("Qatar", 223752, 1749713),
new Country("Romania", 22242653, 20246871),
new Country("Russian Federation", 139010000, 142385523),
new Country("Rwanda", 5140786, 10836732),
new Country("Samoa", 155554, 186029),
new Country("San Marino", 21397, 30861),
new Country("Sao Tome and Principe", 94953, 178228),
new Country("Saudi Arabia", 9843265, 27258387),
new Country("Senegal", 5568908, 12950564),
new Country("Seychelles", 64400, 89770),
new Country("Sierra Leone", 3180237, 5751976),
new Country("Singapore", 2413900, 5076700),
new Country("Slovak Republic", 4979815, 5391428),
new Country("Slovenia", 1901315, 2048583),
new Country("Solomon Islands", 230614, 526447),
new Country("Somalia", 6089707, 9636173),
new Country("South Africa", 27576000, 50895698),
new Country("South Sudan", 4701961, 9940929),
new Country("Spain", 37439035, 46576897),
new Country("Sri Lanka", 14747000, 20653000),
new Country("St. Kitts and Nevis", 43205, 52352),
new Country("St. Lucia", 117984, 177397),
new Country("St. Martin (French part)", 7580, 30235),
new Country("St. Vincent and the Grenadines", 100506, 109316),
new Country("Sudan", 14418063, 35652002),
new Country("Suriname", 365616, 524960),
new Country("Swaziland", 603373, 1193148),
new Country("Sweden", 8310531, 9378126),
new Country("Switzerland", 6319408, 7824909),
new Country("Syrian Arab Republic", 8956156, 21532647),
new Country("Tajikistan", 3917642, 7627326),
new Country("Tanzania", 18686808, 44973330),
new Country("Thailand", 47369137, 66402316),
new Country("Timor-Leste", 580730, 1066409),
new Country("Togo", 2720838, 6306014),
new Country("Tonga", 93007, 104098),
new Country("Trinidad and Tobago", 1085308, 1328095),
new Country("Tunisia", 6384000, 10549100),
new Country("Turkey", 43905790, 72137546),
new Country("Turkmenistan", 2861000, 5041995),
new Country("Turks and Caicos Islands", 7525, 30993),
new Country("Tuvalu", 8051, 9827),
new Country("Uganda", 12549779, 33987213),
new Country("Ukraine", 49973757, 45870700),
new Country("United Arab Emirates", 1014825, 8441537),
new Country("United Kingdom", 56314216, 62766365),
new Country("United States", 227225000, 309326295),
new Country("Uruguay", 2915735, 3371982),
new Country("Uzbekistan", 16026812, 28562400),
new Country("Vanuatu", 115634, 236299),
new Country("Venezuela, RB", 15096432, 29043283),
new Country("Vietnam", 53700000, 86932500),
new Country("Virgin Islands (U.S.)", 97000, 106267),
new Country("Yemen, Rep.", 7906977, 22763008),
new Country("Zambia", 5847241, 13216985),
new Country("Zimbabwe", 7289069, 13076978),
};

ArrayList<Country> matches = filter(countries);
System.out.println(matches);
}

private static ArrayList<Country> filter(Country[] countries) {
ArrayList<Country> keepers = new ArrayList<>();

for (Country country : countries) {
//      if (country.getPopulation1980() * 3 < country.getPopulation2014()) {
if (country.getPopulation1980() > country.getPopulation2014()) {
}