JMU
Look-up Arrays
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
An Example: Highway Exit Numbers
An Example: Highway Exit Numbers (cont.)
images/ExitNumbers.svg
The Pattern
  1. Create an array in which the indexes correspond to the key that will be used to perform the look-up, and the elements correspond to the value that is to be determined.
  2. Create a method that validates the key and returns the appropriate element of the array for any valid key (and an error status for any invalid key).
An Example: Highway Exit Numbers (cont.)
javaexamples/programmingpatterns/LookupArrays.java (Fragment: newExitNumberFor)
    private static final int[] NEW_NUMBERS = {-1, 1, 15, 16, 28, 35};

    /**
     * Get the new exit number that corresponds to an old exit number.
     * Old exit numbers were consecutive. New exit numbers are based
     * on mileage markers.
     *
     * @param  oldExitNumber   The old exit number
     * @return The new exit number (or -1 if there was no such old exit number)
     */
    public static int newExitNumberFor(int oldExitNumber) {
        if ((oldExitNumber < 1) || (oldExitNumber > 5)) {
            return -1;
        } else {
            return NEW_NUMBERS[oldExitNumber];
        }
    }
        
An Example: Highway Exit Names (cont.)
javaexamples/programmingpatterns/LookupArrays.java (Fragment: exitNameFor)
    private static final String[] NAMES = {"", "Willow Ave.",
        "Broad St.", "Downtown", "North End", "Lake Dr."};

    /**
     * Get the name for an old exit number.
     *
     * @param  oldExitNumber   The old exit number
     * @return The name of the exit (or "")
     */
    public static String exitNameFor(int oldExitNumber) {
        if ((oldExitNumber < 1) || (oldExitNumber > 5)) {
            return "";
        } else {
            return NAMES[oldExitNumber];
        }
    }
        
Learning from the Previous Example
From a Key to an Index
From a Key to an Index (cont.)
From a Key to an Index (cont.)
Pass/Fail Grading
javaexamples/programmingpatterns/LookupArrays.java (Fragment: passFail)
    private static final char[] STATUS = {'F', 'P'};

    /**
     * Determine if a grade is passing or failing.
     *
     * @param grade  The grade
     * @return 'F' or 'P'
     */
    public static char passFail(double grade) {
        int index;
        
        index = (int) (grade / 60.0);
        return STATUS[index];
    }
        
From a Key to an Index (cont.)
An Example: Population (cont.)
javaexamples/programmingpatterns/LookupArrays.java (Fragment: population)
    private static final double[] POP = {
        3.9, 5.2, 7.2, 9.6, 12.9, 17.1, 23.1, 31.4, 38.6, 49.4, 63.0, 76.2,
        92.2, 106.0, 123.2, 132.2, 151.3, 179.3, 203.2, 226.5, 248.7, 281.4, 
        308.7};
    
    /**
     * Get the US population for the previous census year.
     *
     * @param year  The year of interest
     * @return      The population for the previous census year (or -1)
     */
    public static double population(int year) {
        int index;
        
        if ((year < 1790) || (year >= 2020)) {
            return -1.0;
        } else {
            index = (year - 1790) / 10;
            return POP[index];
        }
    }