teaching machines

CS 145 Lecture 18 – Searching arrays

November 11, 2011 by . Filed under cs145, fall 2011, lectures.

Agenda

Code

States.java

package lecture;

import java.util.Scanner;

public class States {
  private static String[] states = {
                                    "Alabama",
                                    "Alaska",
                                    "Arizona",
                                    "Arkansas",
                                    "California",
                                    "Colorado",
                                    "Connecticut",
                                    "Delaware",
                                    "Florida",
                                    "Georgia",
                                    "Hawaii",
                                    "Idaho",
                                    "Illinois",
                                    "Indiana",
                                    "Iowa",
                                    "Kansas",
                                    "Kentucky",
                                    "Louisiana",
                                    "Maine",
                                    "Maryland",
                                    "Massachusetts",
                                    "Michigan",
                                    "Minnesota",
                                    "Mississippi",
                                    "Missouri",
                                    "Montana",
                                    "Nebraska",
                                    "Nevada",
                                    "New Hampshire",
                                    "New Jersey",
                                    "New Mexico",
                                    "New York",
                                    "North Carolina",
                                    "North Dakota",
                                    "Ohio",
                                    "Oklahoma",
                                    "Oregon",
                                    "Pennsylvania",
                                    "Rhode Island",
                                    "South Carolina",
                                    "South Dakota",
                                    "Tennessee",
                                    "Texas",
                                    "Utah",
                                    "Vermont",
                                    "Virginia",
                                    "Washington",
                                    "West Virginia",
                                    "Wisconsin",
                                    "Wyoming",
  };

  private static String[] capitals = {
                                      "Montgomery",
                                      "Juneau",
                                      "Phoenix",
                                      "Little Rock",
                                      "Sacramento",
                                      "Denver",
                                      "Hartford",
                                      "Dover",
                                      "Tallahassee",
                                      "Atlanta",
                                      "Honolulu",
                                      "Boise",
                                      "Springfield",
                                      "Indianapolis",
                                      "Des Moines",
                                      "Topeka",
                                      "Frankfort",
                                      "Baton Rouge",
                                      "Augusta",
                                      "Annapolis",
                                      "Boston",
                                      "Lansing",
                                      "St. Paul",
                                      "Jackson",
                                      "Jefferson City",
                                      "Helena",
                                      "Lincoln",
                                      "Carson City",
                                      "Concord",
                                      "Trenton",
                                      "Santa Fe",
                                      "Albany",
                                      "Raleigh",
                                      "Bismarck",
                                      "Columbus",
                                      "Oklahoma City",
                                      "Salem",
                                      "Harrisburg",
                                      "Providence",
                                      "Columbia",
                                      "Pierre",
                                      "Nashville",
                                      "Austin",
                                      "Salt Lake City",
                                      "Montpelier",
                                      "Richmond",
                                      "Olympia",
                                      "Charleston",
                                      "Madison",
                                      "Cheyenne"
  };

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("> ");
    while (in.hasNextLine()) {
      String state = in.nextLine();
      String capital = getCapital(state);
      System.out.println(capital.toUpperCase());
      System.out.print("> ");
    }
  }

  /**
   * Given a state, get its capital.
   * @param State for which we're to find the capital
   * @return Capital
   */
  public static String getCapital(String state) {
    // //find location of state in state list
    // for each state
    //   check if user-inputted state is this state
    //   if so, store/remember index
    // use the location to index into capitals list
    // give back that capital
    
    for (int i = 0; i < states.length; ++i) {
      if (state.equals(states[i])) {
        return capitals[i];
      }
    }
    
    return null;
  }
}

TODO

Haiku

new means distant, cold
too good for the stack, for us
in a heap, all right