Computer Science 210
Data Structures

Fall 2016, Siena College

SpellsArray BlueJ Project

Click here to download a BlueJ project for SpellsArray.


SpellsArray Source Code

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


SpellsArray.java

/*
 * Example SpellsArray: a version of the Spells program that
 * uses an array instead of an ArrayList
 *
 * Jim Teresco, The College of Saint Rose, CSC 523, Summer 2014
 *
 * $Id: SpellsArray.java 2377 2014-06-10 03:12:01Z terescoj $
 */

import structure5.*;
import java.util.Scanner;

public class SpellsArray {

    public static void main(String args[]) {
        // 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[10];
       
        spells[0] = new Association<String,String>("Reparo","Fixes damaged object");
        spells[1] = new Association<String,String>("Evanesco","Makes something vanish");
        spells[2] = new Association<String,String>("Expelliarmus","Disarm opponent");
        spells[3] = new Association<String,String>("Accio","Summon object");
        spells[4] = new Association<String,String>("Alohomora","Open a locked door");
        spells[5] = new Association<String,String>("Lumos","Illuminate wand");
        spells[6] = new Association<String,String>("Crucio","Inflict great pain");
        spells[7] = new Association<String,String>("Engorgio","Cause target to swell");
        spells[8] = new Association<String,String>("Immobulus","Stop an object's motion");
        spells[9] = new Association<String,String>("Incendio","Start a fire");

        // 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!");
            }
        }
    }
}