Computer Science 210
Data Structures

Fall 2019, 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
 *
 * Siena College, CSIS 210, Fall 2017
 * @version Fall 2019
 */

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

public class SpellsArrayAssociation {

    /**
       Create an array of Association objects representing Harry
       Potter spells, and look up spell actions by name.

       @param args not used
       @throws IOException
    */
    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.
 *
 * @version Fall 2019
 *
 */
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.

       @param k the immutable key
       @param v the initial value
    */
    public Association(K k, V v) {
     
        key = k;
        value = v;
    }
    
    /**
       construct a new Association with the given key, but no value.
       value defaults to null.

       @param k the immutable key
    */
    public Association(K k) {
     
        key = k;
        value = null;
    }
    
    /**
       Set a new value for this Association

       @param v the new value to set
    */
    public void setValue(V v) {
     
        value = v;
    }
    
    /* accessors */
    /**
       retrieve this Association's key

       @return the key of this Association
    */
    public K getKey() {
     
        return key;
    }
    
    /**
       retrieve this Association's value

       @return the value of this Association
    */
    public V getValue() {
        
        return value;
    }
    
    /**
      Produce human-readable representation of this Association's
      key and value.

      @return a human-readable representation of this Association's
      key and value
    */
    public String toString() {
     
        return "<key: " + key + ", value: " + value + ">";
    }

    /**
       Compare this Association with another for equality, defined by the
       two having equal keys, regardless of values.

       @param o the other Association
       @return true if this Association and o are equal, false otherwise
    */
    public boolean equals(Object o) {
     
        Association<K,V> other = (Association<K,V>) o;
        return other.key.equals(key);
    }
}