Computer Science 210
Data Structures

Fall 2017, Siena College

SpellsArrayAssociation BlueJ Project

Click here to download a BlueJ project for SpellsArrayAssociation.


SpellsArrayAssociation Source Code

The Java source code for SpellsArrayAssociation is below. Click on a file name to download it.


SpellsArrayAssociation.java

/*
 * Example SpellsArray: a version of the Spells program that
 * uses an array of Associations, which is developed in class
 *
 * Siena College, CSIS 210, Fall 2017
 */

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class SpellsArrayAssociation {

    public static void main(String args[]) throws IOException {

	final int NUM_SPELLS = 10;
        // create an array of the magic spells we know.
        // Note: due to the way Java handles arrays and generic types,
        // we have to leave off the <String,String> in the construction..
        Association<String,String> spells[] = new Association[NUM_SPELLS];
	Scanner inFile = new Scanner(new File("spells.txt"));
	for (int i = 0; i < NUM_SPELLS; i++) {
	    String spell = inFile.nextLine().trim();
	    String action = inFile.nextLine().trim();
       
	    spells[i] = new Association<String,String>(spell, action);
	    //System.out.println(spells[i]);
	}
	inFile.close();

        // we play a little game matching spells to descriptions
        // until an invalid spell is specified
        Scanner keyboard = new Scanner(System.in);
        int spellnum = 0;
        while (spellnum >= 0) {
            System.out.print("Which spell will you use? ");
            String spellName = keyboard.next();
            spellnum = -1;
            for (int spellIndex = 0; spellIndex < spells.length; spellIndex++) {
                if (spellName.equals(spells[spellIndex].getKey())) {
                    spellnum = spellIndex;
                    break;
                }
            }
            if (spellnum >= 0) {
                System.out.println(spells[spellnum].getValue());
            }
            else {
                System.out.println("Your wand doesn't know that one.  It explodes.  Bye!");
            }
        }
    }
}

Association.java


/**
 * Associate key/value pairs.  keys are immutable.
 * 
 * @author Jim Teresco and the CSIS 210 class, Fall 2017
 * Siena College on its birthday.
 *
 */
public class Association<K,V>
{
    /* instance variables to store our key and our value */
    private K key;
    private V value;
    
    /* construct a new Association with the given key/value pair */
    public Association(K k, V v) {
     
        key = k;
        value = v;
    }
    
    /* construct a new Association with the given key, no value */
    public Association(K k) {
     
        key = k;
        value = null;
    }
    
    /* set a new value */
    public void setValue(V v) {
     
        value = v;
    }
    
    /* accessors */
    public K getKey() {
     
        return key;
    }
    
    public V getValue() {
        
        return value;
    }
    
    /* convert contents to a String */
    public String toString() {
     
        return "<key: " + key + ", value: " + value + ">";
    }
    
    public boolean equals(Object o) {
     
        Association<K,V> other = (Association<K,V>) o;
        return other.key.equals(key);
    }
}